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 | |
16 | namespace Poco |
17 | { |
18 | namespace Net |
19 | { |
20 | class HTTPServerResponse; |
21 | } |
22 | } |
23 | |
24 | |
25 | namespace DB |
26 | { |
27 | constexpr int HTTP_TOO_MANY_REQUESTS = 429; |
28 | |
29 | class SingleEndpointHTTPSessionPool : public PoolBase<Poco::Net::HTTPClientSession> |
30 | { |
31 | private: |
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 | |
39 | public: |
40 | SingleEndpointHTTPSessionPool(const std::string & host_, UInt16 port_, bool https_, size_t max_pool_size_); |
41 | }; |
42 | using PooledHTTPSessionPtr = SingleEndpointHTTPSessionPool::Entry; |
43 | using HTTPSessionPtr = std::shared_ptr<Poco::Net::HTTPClientSession>; |
44 | |
45 | void (Poco::Net::HTTPServerResponse & response, unsigned keep_alive_timeout); |
46 | |
47 | /// Create session object to perform requests and set required parameters. |
48 | HTTPSessionPtr makeHTTPSession(const Poco::URI & uri, const ConnectionTimeouts & timeouts); |
49 | |
50 | /// As previous method creates session, but tooks it from pool |
51 | PooledHTTPSessionPtr makePooledHTTPSession(const Poco::URI & uri, const ConnectionTimeouts & timeouts, size_t per_endpoint_pool_size); |
52 | |
53 | bool 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 | */ |
60 | std::istream * receiveResponse( |
61 | Poco::Net::HTTPClientSession & session, const Poco::Net::HTTPRequest & request, Poco::Net::HTTPResponse & response, bool allow_redirects); |
62 | void assertResponseIsOk(const Poco::Net::HTTPRequest & request, Poco::Net::HTTPResponse & response, std::istream & istr, const bool allow_redirects = false); |
63 | } |
64 | |