1#pragma once
2
3#include <iostream>
4#include <memory>
5#include <mutex>
6
7#include <Poco/Net/HTTPClientSession.h>
8#include <Poco/Net/HTTPRequest.h>
9#include <Poco/Net/HTTPResponse.h>
10#include <Poco/URI.h>
11#include <Common/PoolBase.h>
12#include <Poco/URIStreamFactory.h>
13
14#include <IO/ConnectionTimeouts.h>
15
16namespace Poco
17{
18namespace Net
19{
20 class HTTPServerResponse;
21}
22}
23
24
25namespace DB
26{
27constexpr int HTTP_TOO_MANY_REQUESTS = 429;
28
29class SingleEndpointHTTPSessionPool : public PoolBase<Poco::Net::HTTPClientSession>
30{
31private:
32 const std::string host;
33 const UInt16 port;
34 const bool https;
35 using Base = PoolBase<Poco::Net::HTTPClientSession>;
36
37 ObjectPtr allocObject() override;
38
39public:
40 SingleEndpointHTTPSessionPool(const std::string & host_, UInt16 port_, bool https_, size_t max_pool_size_);
41};
42using PooledHTTPSessionPtr = SingleEndpointHTTPSessionPool::Entry;
43using HTTPSessionPtr = std::shared_ptr<Poco::Net::HTTPClientSession>;
44
45void setResponseDefaultHeaders(Poco::Net::HTTPServerResponse & response, unsigned keep_alive_timeout);
46
47/// Create session object to perform requests and set required parameters.
48HTTPSessionPtr makeHTTPSession(const Poco::URI & uri, const ConnectionTimeouts & timeouts);
49
50/// As previous method creates session, but tooks it from pool
51PooledHTTPSessionPtr makePooledHTTPSession(const Poco::URI & uri, const ConnectionTimeouts & timeouts, size_t per_endpoint_pool_size);
52
53bool isRedirect(const Poco::Net::HTTPResponse::HTTPStatus status);
54
55/** Used to receive response (response headers and possibly body)
56 * after sending data (request headers and possibly body).
57 * Throws exception in case of non HTTP_OK (200) response code.
58 * Returned istream lives in 'session' object.
59 */
60std::istream * receiveResponse(
61 Poco::Net::HTTPClientSession & session, const Poco::Net::HTTPRequest & request, Poco::Net::HTTPResponse & response, bool allow_redirects);
62void assertResponseIsOk(const Poco::Net::HTTPRequest & request, Poco::Net::HTTPResponse & response, std::istream & istr, const bool allow_redirects = false);
63}
64