2016-12-30 20:52:56 +00:00
|
|
|
#pragma once
|
|
|
|
|
2018-06-14 21:20:39 +00:00
|
|
|
#include <iostream>
|
2018-11-16 13:15:17 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <mutex>
|
2018-06-14 21:20:39 +00:00
|
|
|
|
|
|
|
#include <Poco/Net/HTTPClientSession.h>
|
|
|
|
#include <Poco/Net/HTTPRequest.h>
|
|
|
|
#include <Poco/Net/HTTPResponse.h>
|
|
|
|
#include <Poco/URI.h>
|
2018-11-16 13:15:17 +00:00
|
|
|
#include <Common/PoolBase.h>
|
2019-09-11 16:39:30 +00:00
|
|
|
#include <Poco/URIStreamFactory.h>
|
2018-06-14 21:20:39 +00:00
|
|
|
|
|
|
|
#include <IO/ConnectionTimeouts.h>
|
2017-01-30 05:13:58 +00:00
|
|
|
|
|
|
|
namespace Poco
|
|
|
|
{
|
2018-11-16 13:15:17 +00:00
|
|
|
namespace Net
|
|
|
|
{
|
|
|
|
class HTTPServerResponse;
|
|
|
|
}
|
2017-01-30 05:13:58 +00:00
|
|
|
}
|
|
|
|
|
2016-12-30 20:52:56 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2018-11-16 13:15:17 +00:00
|
|
|
constexpr int HTTP_TOO_MANY_REQUESTS = 429;
|
2016-12-30 20:52:56 +00:00
|
|
|
|
2018-11-16 13:15:17 +00:00
|
|
|
class SingleEndpointHTTPSessionPool : public PoolBase<Poco::Net::HTTPClientSession>
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
const std::string host;
|
|
|
|
const UInt16 port;
|
|
|
|
const bool https;
|
|
|
|
using Base = PoolBase<Poco::Net::HTTPClientSession>;
|
2018-06-14 21:20:39 +00:00
|
|
|
|
2018-11-16 13:15:17 +00:00
|
|
|
ObjectPtr allocObject() override;
|
2016-12-30 20:52:56 +00:00
|
|
|
|
2018-11-16 13:15:17 +00:00
|
|
|
public:
|
|
|
|
SingleEndpointHTTPSessionPool(const std::string & host_, UInt16 port_, bool https_, size_t max_pool_size_);
|
|
|
|
};
|
|
|
|
using PooledHTTPSessionPtr = SingleEndpointHTTPSessionPool::Entry;
|
2018-11-20 13:15:44 +00:00
|
|
|
using HTTPSessionPtr = std::shared_ptr<Poco::Net::HTTPClientSession>;
|
2018-11-16 13:15:17 +00:00
|
|
|
|
|
|
|
void setResponseDefaultHeaders(Poco::Net::HTTPServerResponse & response, unsigned keep_alive_timeout);
|
2017-02-27 21:07:57 +00:00
|
|
|
|
2018-06-16 05:54:06 +00:00
|
|
|
/// Create session object to perform requests and set required parameters.
|
2020-05-28 16:11:27 +00:00
|
|
|
HTTPSessionPtr makeHTTPSession(const Poco::URI & uri, const ConnectionTimeouts & timeouts, bool resolve_host = true);
|
2018-06-16 05:54:06 +00:00
|
|
|
|
2018-11-16 13:15:17 +00:00
|
|
|
/// As previous method creates session, but tooks it from pool
|
|
|
|
PooledHTTPSessionPtr makePooledHTTPSession(const Poco::URI & uri, const ConnectionTimeouts & timeouts, size_t per_endpoint_pool_size);
|
2018-06-16 05:54:06 +00:00
|
|
|
|
2019-09-16 17:28:41 +00:00
|
|
|
bool isRedirect(const Poco::Net::HTTPResponse::HTTPStatus status);
|
|
|
|
|
2018-06-16 05:54:06 +00:00
|
|
|
/** Used to receive response (response headers and possibly body)
|
|
|
|
* after sending data (request headers and possibly body).
|
|
|
|
* Throws exception in case of non HTTP_OK (200) response code.
|
|
|
|
* Returned istream lives in 'session' object.
|
|
|
|
*/
|
2018-11-16 13:15:17 +00:00
|
|
|
std::istream * receiveResponse(
|
2019-09-16 17:28:41 +00:00
|
|
|
Poco::Net::HTTPClientSession & session, const Poco::Net::HTTPRequest & request, Poco::Net::HTTPResponse & response, bool allow_redirects);
|
2019-09-23 08:53:09 +00:00
|
|
|
void assertResponseIsOk(const Poco::Net::HTTPRequest & request, Poco::Net::HTTPResponse & response, std::istream & istr, const bool allow_redirects = false);
|
2016-12-30 20:52:56 +00:00
|
|
|
}
|