1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
#include "http_client.h"
#include <library/cpp/string_utils/url/url.h>
#include <library/cpp/uri/http_url.h>
#include <util/stream/output.h>
#include <util/string/cast.h>
#include <util/string/join.h>
#include <util/string/split.h>
TKeepAliveHttpClient::TKeepAliveHttpClient(const TString& host,
ui32 port,
TDuration socketTimeout,
TDuration connectTimeout)
: Host(CutHttpPrefix(host))
, Port(port)
, SocketTimeout(socketTimeout)
, ConnectTimeout(connectTimeout)
, IsHttps(host.StartsWith("https"))
, IsClosingRequired(false)
, HttpsVerification(TVerifyCert{Host})
, IfResponseRequired([](const THttpInput&) { return true; })
{
}
TKeepAliveHttpClient::THttpCode TKeepAliveHttpClient::DoGet(const TStringBuf relativeUrl,
IOutputStream* output,
const THeaders& headers,
THttpHeaders* outHeaders) {
return DoRequest(TStringBuf("GET"),
relativeUrl,
{},
output,
headers,
outHeaders);
}
TKeepAliveHttpClient::THttpCode TKeepAliveHttpClient::DoPost(const TStringBuf relativeUrl,
const TStringBuf body,
IOutputStream* output,
const THeaders& headers,
THttpHeaders* outHeaders) {
return DoRequest(TStringBuf("POST"),
relativeUrl,
body,
output,
headers,
outHeaders);
}
TKeepAliveHttpClient::THttpCode TKeepAliveHttpClient::DoRequest(const TStringBuf method,
const TStringBuf relativeUrl,
const TStringBuf body,
IOutputStream* output,
const THeaders& inHeaders,
THttpHeaders* outHeaders) {
const TString contentLength = IntToString<10, size_t>(body.size());
return DoRequestReliable(FormRequest(method, relativeUrl, body, inHeaders, contentLength), output, outHeaders);
}
TKeepAliveHttpClient::THttpCode TKeepAliveHttpClient::DoRequestRaw(const TStringBuf raw,
IOutputStream* output,
THttpHeaders* outHeaders) {
return DoRequestReliable(raw, output, outHeaders);
}
void TKeepAliveHttpClient::DisableVerificationForHttps() {
HttpsVerification.Clear();
Connection.Reset();
}
void TKeepAliveHttpClient::SetClientCertificate(const TOpenSslClientIO::TOptions::TClientCert& options) {
ClientCertificate = options;
}
void TKeepAliveHttpClient::ResetConnection() {
Connection.Reset();
}
TVector<IOutputStream::TPart> TKeepAliveHttpClient::FormRequest(TStringBuf method,
const TStringBuf relativeUrl,
TStringBuf body,
const TKeepAliveHttpClient::THeaders& headers,
TStringBuf contentLength) const {
TVector<IOutputStream::TPart> parts;
parts.reserve(16 + 4 * headers.size());
parts.push_back(method);
parts.push_back(TStringBuf(" "));
parts.push_back(relativeUrl);
parts.push_back(TStringBuf(" HTTP/1.1"));
parts.push_back(IOutputStream::TPart::CrLf());
parts.push_back(TStringBuf("Host: "));
parts.push_back(TStringBuf(Host));
parts.push_back(IOutputStream::TPart::CrLf());
parts.push_back(TStringBuf("Content-Length: "));
parts.push_back(contentLength);
parts.push_back(IOutputStream::TPart::CrLf());
for (const auto& entry : headers) {
parts.push_back(IOutputStream::TPart(entry.first));
parts.push_back(IOutputStream::TPart(TStringBuf(": ")));
parts.push_back(IOutputStream::TPart(entry.second));
parts.push_back(IOutputStream::TPart::CrLf());
}
parts.push_back(IOutputStream::TPart::CrLf());
if (body) {
parts.push_back(IOutputStream::TPart(body));
}
return parts;
}
TKeepAliveHttpClient::THttpCode TKeepAliveHttpClient::ReadAndTransferHttp(THttpInput& input,
IOutputStream* output,
THttpHeaders* outHeaders) const {
TKeepAliveHttpClient::THttpCode statusCode;
try {
statusCode = ParseHttpRetCode(input.FirstLine());
} catch (TFromStringException& e) {
TString rest = input.ReadAll();
ythrow THttpRequestException() << "Failed parse status code in response of " << Host << ": " << e.what() << " (" << input.FirstLine() << ")"
<< "\nFull http response:\n"
<< rest;
}
auto canContainBody = [](auto statusCode) {
return statusCode != HTTP_NOT_MODIFIED && statusCode != HTTP_NO_CONTENT;
};
if (output && canContainBody(statusCode) && IfResponseRequired(input)) {
TransferData(&input, output);
}
if (outHeaders) {
*outHeaders = input.Headers();
}
return statusCode;
}
THttpInput* TKeepAliveHttpClient::GetHttpInput() {
return Connection ? Connection->GetHttpInput() : nullptr;
}
bool TKeepAliveHttpClient::CreateNewConnectionIfNeeded() {
if (IsClosingRequired || (Connection && !Connection->IsOk())) {
Connection.Reset();
}
if (!Connection) {
Connection = MakeHolder<NPrivate::THttpConnection>(Host,
Port,
SocketTimeout,
ConnectTimeout,
IsHttps,
ClientCertificate,
HttpsVerification);
IsClosingRequired = false;
return true;
}
return false;
}
THttpRequestException::THttpRequestException(int statusCode)
: StatusCode(statusCode)
{
}
int THttpRequestException::GetStatusCode() const {
return StatusCode;
}
TSimpleHttpClient::TSimpleHttpClient(const TOptions& options)
: Host(options.Host())
, Port(options.Port())
, SocketTimeout(options.SocketTimeout())
, ConnectTimeout(options.ConnectTimeout())
{
}
TSimpleHttpClient::TSimpleHttpClient(const TString& host, ui32 port, TDuration socketTimeout, TDuration connectTimeout)
: Host(host)
, Port(port)
, SocketTimeout(socketTimeout)
, ConnectTimeout(connectTimeout)
{
}
void TSimpleHttpClient::EnableVerificationForHttps() {
HttpsVerification = true;
}
void TSimpleHttpClient::DoGet(const TStringBuf relativeUrl, IOutputStream* output, const THeaders& headers) const {
TKeepAliveHttpClient cl = CreateClient();
TKeepAliveHttpClient::THttpCode code = cl.DoGet(relativeUrl, output, headers);
Y_ENSURE(cl.GetHttpInput());
ProcessResponse(relativeUrl, *cl.GetHttpInput(), output, code);
}
void TSimpleHttpClient::DoPost(const TStringBuf relativeUrl, TStringBuf body, IOutputStream* output, const THashMap<TString, TString>& headers) const {
TKeepAliveHttpClient cl = CreateClient();
TKeepAliveHttpClient::THttpCode code = cl.DoPost(relativeUrl, body, output, headers);
Y_ENSURE(cl.GetHttpInput());
ProcessResponse(relativeUrl, *cl.GetHttpInput(), output, code);
}
void TSimpleHttpClient::DoPostRaw(const TStringBuf relativeUrl, const TStringBuf rawRequest, IOutputStream* output) const {
TKeepAliveHttpClient cl = CreateClient();
TKeepAliveHttpClient::THttpCode code = cl.DoRequestRaw(rawRequest, output);
Y_ENSURE(cl.GetHttpInput());
ProcessResponse(relativeUrl, *cl.GetHttpInput(), output, code);
}
namespace NPrivate {
THttpConnection::THttpConnection(const TString& host,
ui32 port,
TDuration sockTimeout,
TDuration connTimeout,
bool isHttps,
const TMaybe<TOpenSslClientIO::TOptions::TClientCert>& clientCert,
const TMaybe<TOpenSslClientIO::TOptions::TVerifyCert>& verifyCert)
: Addr(Resolve(host, port))
, Socket(Connect(Addr, sockTimeout, connTimeout, host, port))
, SocketIn(Socket)
, SocketOut(Socket)
{
if (isHttps) {
TOpenSslClientIO::TOptions opts;
if (clientCert) {
opts.ClientCert_ = clientCert;
}
if (verifyCert) {
opts.VerifyCert_ = verifyCert;
}
Ssl = MakeHolder<TOpenSslClientIO>(&SocketIn, &SocketOut, opts);
HttpOut = MakeHolder<THttpOutput>(Ssl.Get());
} else {
HttpOut = MakeHolder<THttpOutput>(&SocketOut);
}
HttpOut->EnableKeepAlive(true);
}
TNetworkAddress THttpConnection::Resolve(const TString& host, ui32 port) {
try {
return TNetworkAddress(host, port);
} catch (const yexception& e) {
ythrow THttpRequestException() << "Resolve of " << host << ": " << e.what();
}
}
TSocket THttpConnection::Connect(TNetworkAddress& addr,
TDuration sockTimeout,
TDuration connTimeout,
const TString& host,
ui32 port) {
try {
TSocket socket(addr, connTimeout);
TDuration socketTimeout = Max(sockTimeout, TDuration::MilliSeconds(1)); // timeout less than 1ms will be interpreted as 0 in SetSocketTimeout() call below and will result in infinite wait
ui32 seconds = socketTimeout.Seconds();
ui32 milliSeconds = (socketTimeout - TDuration::Seconds(seconds)).MilliSeconds();
socket.SetSocketTimeout(seconds, milliSeconds);
return socket;
} catch (const yexception& e) {
ythrow THttpRequestException() << "Connect to " << host << ':' << port << " failed: " << e.what();
}
}
}
void TSimpleHttpClient::ProcessResponse(const TStringBuf relativeUrl, THttpInput& input, IOutputStream*, const unsigned statusCode) const {
if (!(statusCode >= 200 && statusCode < 300)) {
TString rest = input.ReadAll();
ythrow THttpRequestException(statusCode) << "Got " << statusCode << " at " << Host << relativeUrl << "\nFull http response:\n"
<< rest;
}
}
TSimpleHttpClient::~TSimpleHttpClient() {
}
TKeepAliveHttpClient TSimpleHttpClient::CreateClient() const {
TKeepAliveHttpClient cl(Host, Port, SocketTimeout, ConnectTimeout);
if (!HttpsVerification) {
cl.DisableVerificationForHttps();
}
PrepareClient(cl);
return cl;
}
void TSimpleHttpClient::PrepareClient(TKeepAliveHttpClient&) const {
}
TRedirectableHttpClient::TRedirectableHttpClient(const TString& host, ui32 port, TDuration socketTimeout, TDuration connectTimeout)
: TSimpleHttpClient(host, port, socketTimeout, connectTimeout)
{
}
void TRedirectableHttpClient::PrepareClient(TKeepAliveHttpClient& cl) const {
cl.IfResponseRequired = [](const THttpInput& input) {
return !input.Headers().HasHeader("Location");
};
}
void TRedirectableHttpClient::ProcessResponse(const TStringBuf relativeUrl, THttpInput& input, IOutputStream* output, const unsigned statusCode) const {
for (auto i = input.Headers().Begin(), e = input.Headers().End(); i != e; ++i) {
if (0 == TString::compare(i->Name(), TStringBuf("Location"))) {
TVector<TString> request_url_parts, request_body_parts;
size_t splitted_index = 0;
for (auto& iter : StringSplitter(i->Value()).Split('/')) {
if (splitted_index < 3) {
request_url_parts.push_back(TString(iter.Token()));
} else {
request_body_parts.push_back(TString(iter.Token()));
}
++splitted_index;
}
TString url = JoinSeq("/", request_url_parts);
ui16 port = 443;
THttpURL u;
if (THttpURL::ParsedOK == u.Parse(url)) {
const char* p = u.Get(THttpURL::FieldPort);
if (p) {
port = FromString<ui16>(p);
url = u.PrintS(THttpURL::FlagScheme | THttpURL::FlagHost);
}
}
TRedirectableHttpClient cl(url, port, TDuration::Seconds(60), TDuration::Seconds(60));
if (HttpsVerification) {
cl.EnableVerificationForHttps();
}
cl.DoGet(TString("/") + JoinSeq("/", request_body_parts), output);
return;
}
}
if (!(statusCode >= 200 && statusCode < 300)) {
TString rest = input.ReadAll();
ythrow THttpRequestException(statusCode) << "Got " << statusCode << " at " << Host << relativeUrl << "\nFull http response:\n"
<< rest;
}
TransferData(&input, output);
}
|