From 00a568c01680fd9918f6af032bf5a81e9860821b Mon Sep 17 00:00:00 2001 From: keita Date: Wed, 10 Mar 2021 22:34:20 +0900 Subject: [PATCH] =?UTF-8?q?HttpGet=E3=81=AE=E5=AE=9F=E8=A3=85=E3=81=AB?= =?UTF-8?q?=E5=90=91=E3=81=91=E3=81=A6HttpBase=E3=81=AE=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cocoatweet/api/interface/httpBase.h | 25 ++++++++++++++++++++++++ src/cocoatweet/api/interface/httpPost.cc | 14 ++++++------- src/cocoatweet/api/interface/httpPost.h | 8 ++------ 3 files changed, 34 insertions(+), 13 deletions(-) create mode 100644 src/cocoatweet/api/interface/httpBase.h diff --git a/src/cocoatweet/api/interface/httpBase.h b/src/cocoatweet/api/interface/httpBase.h new file mode 100644 index 0000000..b52ce83 --- /dev/null +++ b/src/cocoatweet/api/interface/httpBase.h @@ -0,0 +1,25 @@ +#ifndef COCOATWEET_API_INTERFACE_HTTPBASE_H_ +#define COCOATWEET_API_INTERFACE_HTTPBASE_H_ + +#include +#include "cocoatweet/oauth/oauth.h" + +namespace CocoaTweet::API::Interface { +class HttpBase { +public: +protected: + std::weak_ptr oauth_; + std::map bodyParam_; + std::string url_; + std::string contentType_; + virtual void process(std::weak_ptr _oauth, + std::function _callback) = 0; + static size_t curlCallback_(char* _ptr, size_t _size, size_t _nmemb, std::string* _stream){ + int realsize = _size * _nmemb; + _stream->append(_ptr, realsize); + return realsize; + } +}; +} // namespace CocoaTweet::API::Interface + +#endif diff --git a/src/cocoatweet/api/interface/httpPost.cc b/src/cocoatweet/api/interface/httpPost.cc index 618d0c4..7a1d208 100644 --- a/src/cocoatweet/api/interface/httpPost.cc +++ b/src/cocoatweet/api/interface/httpPost.cc @@ -9,13 +9,11 @@ extern "C" { #include } -namespace CocoaTweet::API::Interface { -size_t HttpPost::curlCallback_(char* _ptr, size_t _size, size_t _nmemb, std::string* _stream) { - int realsize = _size * _nmemb; - _stream->append(_ptr, realsize); - return realsize; -} +#ifndef NDEBUG +#include +#endif +namespace CocoaTweet::API::Interface { void HttpPost::process(std::weak_ptr _oauth, std::function _callback) { // エンドポイントへのパラメータにOAuthパラメータを付加して署名作成 @@ -38,7 +36,7 @@ void HttpPost::process(std::weak_ptr _oauth, { std::vector tmp; for (const auto& [key, value] : bodyParam_) { - tmp.push_back(key + "=" + value); + tmp.push_back(key + "=" + CocoaTweet::Util::urlEncode(value)); } requestBody = CocoaTweet::Util::join(tmp, "&"); } @@ -69,12 +67,14 @@ void HttpPost::process(std::weak_ptr _oauth, curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlCallback_); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (std::string*)&rcv); #ifndef NDEBUG +std::cout << "requestBody : " << requestBody << std::endl; curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); #endif // Headerを保持するcurl_slist*を初期化 struct curl_slist* headers = NULL; // Authorizationをヘッダに追加 headers = curl_slist_append(headers, oauthHeader.c_str()); + headers = curl_slist_append(headers, ("Content-Type: " + contentType_).c_str()); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); res = curl_easy_perform(curl); curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &responseCode); diff --git a/src/cocoatweet/api/interface/httpPost.h b/src/cocoatweet/api/interface/httpPost.h index 5eeeb40..d38fcd3 100644 --- a/src/cocoatweet/api/interface/httpPost.h +++ b/src/cocoatweet/api/interface/httpPost.h @@ -3,18 +3,14 @@ #include #include "cocoatweet/oauth/oauth.h" +#include namespace CocoaTweet::API::Interface { -class HttpPost { +class HttpPost : public HttpBase{ public: protected: - std::weak_ptr oauth_; - std::map bodyParam_; - std::string url_; - std::string contentType_; void process(std::weak_ptr _oauth, std::function _callback); - static size_t curlCallback_(char* _ptr, size_t _size, size_t _nmemb, std::string* _stream); }; } // namespace CocoaTweet::API::Interface