From 39ade6df4f0f30d49483598b496f725cf75450eb Mon Sep 17 00:00:00 2001 From: Chunting Gu Date: Mon, 10 Aug 2020 09:48:30 +0800 Subject: [PATCH] add Accept() for Accept header to request builder --- CMakeLists.txt | 2 +- examples/client_basics.cc | 4 ++-- webcc/client_session.cc | 10 ++++++++-- webcc/client_session.h | 7 +++++-- webcc/request_builder.cc | 11 +++++++++++ webcc/request_builder.h | 22 +++++++++++++++------- 6 files changed, 42 insertions(+), 14 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 71dad1c..4dfea5b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.1.0) +cmake_minimum_required(VERSION 3.11) project(webcc) diff --git a/examples/client_basics.cc b/examples/client_basics.cc index 79a4f58..4ab480f 100644 --- a/examples/client_basics.cc +++ b/examples/client_basics.cc @@ -8,14 +8,14 @@ int main() { WEBCC_LOG_INIT("", webcc::LOG_CONSOLE); webcc::ClientSession session; + session.Accept("application/json"); webcc::ResponsePtr r; try { r = session.Send(webcc::RequestBuilder{}. Get("http://httpbin.org/get"). - Query("name", "Adam Gu", /*encode*/true). - Header("Accept", "application/json").Date() + Query("name", "Adam Gu", /*encode*/true).Date() ()); assert(r->status() == webcc::Status::kOK); diff --git a/webcc/client_session.cc b/webcc/client_session.cc index e56b688..febb2f8 100644 --- a/webcc/client_session.cc +++ b/webcc/client_session.cc @@ -13,6 +13,12 @@ ClientSession::ClientSession(int timeout, bool ssl_verify, InitHeaders(); } +void ClientSession::Accept(const std::string& content_types) { + if (!content_types.empty()) { + headers_.Set(headers::kAccept, content_types); + } +} + void ClientSession::Auth(const std::string& type, const std::string& credentials) { headers_.Set(headers::kAuthorization, type + " " + credentials); @@ -74,14 +80,14 @@ void ClientSession::InitHeaders() { // Simply put, "deflate" is not recommended for HTTP 1.1 encoding. // (https://www.zlib.net/zlib_faq.html#faq39) + headers_.Set(kAccept, "*/*"); + #if WEBCC_ENABLE_GZIP headers_.Set(kAcceptEncoding, "gzip, deflate"); #else headers_.Set(kAcceptEncoding, "identity"); #endif // WEBCC_ENABLE_GZIP - headers_.Set(kAccept, "*/*"); - headers_.Set(kConnection, "Keep-Alive"); } diff --git a/webcc/client_session.h b/webcc/client_session.h index c0dfea5..04bfcd0 100644 --- a/webcc/client_session.h +++ b/webcc/client_session.h @@ -46,6 +46,9 @@ public: charset_ = charset; } + // Set content types to accept. + void Accept(const std::string& content_types); + // Set authorization. void Auth(const std::string& type, const std::string& credentials); @@ -69,11 +72,11 @@ private: ResponsePtr DoSend(RequestPtr request, bool stream); private: - // Default media type for `Content-Type` header. + // The media (or MIME) type of `Content-Type` header. // E.g., "application/json". std::string media_type_; - // Default charset for `Content-Type` header. + // The charset of `Content-Type` header. // E.g., "utf-8". std::string charset_; diff --git a/webcc/request_builder.cc b/webcc/request_builder.cc index 2231fea..8ce4c7a 100644 --- a/webcc/request_builder.cc +++ b/webcc/request_builder.cc @@ -22,6 +22,10 @@ RequestPtr RequestBuilder::operator()() { request->SetHeader(std::move(headers_[i - 1]), std::move(headers_[i])); } + if (!accept_.empty()) { + request->SetHeader(headers::kAccept, std::move(accept_)); + } + // If no Keep-Alive, explicitly set `Connection` to "Close". if (!keep_alive_) { request->SetHeader(headers::kConnection, "Close"); @@ -78,6 +82,13 @@ RequestBuilder& RequestBuilder::FormData(const std::string& name, return Form(FormPart::New(name, std::move(data), media_type)); } +RequestBuilder& RequestBuilder::Header(const std::string& key, + const std::string& value) { + headers_.push_back(key); + headers_.push_back(value); + return *this; +} + RequestBuilder& RequestBuilder::Auth(const std::string& type, const std::string& credentials) { headers_.push_back(headers::kAuthorization); diff --git a/webcc/request_builder.h b/webcc/request_builder.h index 32a8cdc..5d3c777 100644 --- a/webcc/request_builder.h +++ b/webcc/request_builder.h @@ -117,6 +117,12 @@ public: return *this; } + // Set content types to accept. + RequestBuilder& Accept(const std::string& content_types) { + accept_ = content_types; + return *this; + } + RequestBuilder& Body(const std::string& data) { body_.reset(new StringBody{ data, false }); return *this; @@ -148,11 +154,7 @@ public: RequestBuilder& FormData(const std::string& name, std::string&& data, const std::string& media_type = ""); - RequestBuilder& Header(const std::string& key, const std::string& value) { - headers_.push_back(key); - headers_.push_back(value); - return *this; - } + RequestBuilder& Header(const std::string& key, const std::string& value); RequestBuilder& KeepAlive(bool keep_alive = true) { keep_alive_ = keep_alive; @@ -185,12 +187,18 @@ private: // Request body. BodyPtr body_; - // Media type of the body (e.g., "application/json"). + // The media (or MIME) type of `Content-Type` header. + // E.g., "application/json". std::string media_type_; - // Character set of the body (e.g., "utf-8"). + // The charset of `Content-Type` header. + // E.g., "utf-8". std::string charset_; + // Accept content types (could be comma separated multiple types). + // E.g., "application/json", "text/html, application/xhtml+xml". + std::string accept_; + // Files to upload for a POST request. std::vector form_parts_;