Add timeout setting HttpClientSession.

master
Chunting Gu 6 years ago
parent 53fb2a92c9
commit 3566b1d98b

@ -92,6 +92,8 @@ HttpResponsePtr HttpClientSession::Request(HttpRequestArgs&& args) {
LOG_VERB("Reuse an existing connection."); LOG_VERB("Reuse an existing connection.");
} }
client->set_timeout(timeout_);
if (!client->Request(request, !reuse)) { if (!client->Request(request, !reuse)) {
throw Exception(client->error(), client->timed_out()); throw Exception(client->error(), client->timed_out());
} }

@ -27,10 +27,6 @@ public:
charset_ = charset; charset_ = charset;
} }
void AddHeader(const std::string& key, const std::string& value) {
headers_.Add(key, value);
}
void set_ssl_verify(bool ssl_verify) { void set_ssl_verify(bool ssl_verify) {
ssl_verify_.emplace(ssl_verify); ssl_verify_.emplace(ssl_verify);
} }
@ -39,6 +35,16 @@ public:
buffer_size_ = buffer_size; buffer_size_ = buffer_size;
} }
void set_timeout(int timeout) {
if (timeout > 0) {
timeout_ = timeout;
}
}
void AddHeader(const std::string& key, const std::string& value) {
headers_.Add(key, value);
}
HttpResponsePtr Request(HttpRequestArgs&& args); HttpResponsePtr Request(HttpRequestArgs&& args);
HttpResponsePtr Get(const std::string& url, HttpResponsePtr Get(const std::string& url,
@ -68,16 +74,19 @@ private:
// E.g., "utf-8". // E.g., "utf-8".
std::string charset_; std::string charset_;
// Headers for each request sent from this session. // Additional headers for each request.
HttpHeaderDict headers_; HttpHeaderDict headers_;
// Verify the certificate of the peer or not. // Verify the certificate of the peer or not.
boost::optional<bool> ssl_verify_; boost::optional<bool> ssl_verify_;
// The bytes of the buffer for reading response. // The size of the buffer for reading response.
// 0 means default value will be used. // 0 means default value will be used.
std::size_t buffer_size_ = 0; std::size_t buffer_size_ = 0;
// Timeout in seconds for receiving response.
int timeout_ = 0;
// Connection pool for keep-alive. // Connection pool for keep-alive.
HttpClientPool pool_; HttpClientPool pool_;
}; };

Loading…
Cancel
Save