forked from tdlight-team/tdlight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.cpp
505 lines (455 loc) · 14 KB
/
http.cpp
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
//
// Copyright Aliaksei Levin ([email protected]), Arseny Smirnov ([email protected]) 2014-2021
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "data.h"
#if TD_DARWIN_WATCH_OS
#include "td/net/DarwinHttp.h"
#endif
#include "td/net/HttpChunkedByteFlow.h"
#include "td/net/HttpHeaderCreator.h"
#include "td/net/HttpQuery.h"
#include "td/net/HttpReader.h"
#include "td/utils/AesCtrByteFlow.h"
#include "td/utils/algorithm.h"
#include "td/utils/base64.h"
#include "td/utils/buffer.h"
#include "td/utils/BufferedFd.h"
#include "td/utils/ByteFlow.h"
#include "td/utils/common.h"
#include "td/utils/crypto.h"
#include "td/utils/format.h"
#include "td/utils/Gzip.h"
#include "td/utils/GzipByteFlow.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"
#include "td/utils/port/detail/PollableFd.h"
#include "td/utils/port/FileFd.h"
#include "td/utils/port/path.h"
#include "td/utils/port/PollFlags.h"
#include "td/utils/port/thread_local.h"
#include "td/utils/Random.h"
#include "td/utils/Slice.h"
#include "td/utils/SliceBuilder.h"
#include "td/utils/Status.h"
#include "td/utils/tests.h"
#include "td/utils/UInt.h"
#include <algorithm>
#include <limits>
#include <condition_variable>
#include <mutex>
using namespace td;
static string make_chunked(const string &str) {
auto v = rand_split(str);
string res;
for (auto &s : v) {
res += PSTRING() << format::as_hex_dump(static_cast<int32>(s.size()));
res += "\r\n";
res += s;
res += "\r\n";
}
res += "0\r\n\r\n";
return res;
}
static string gen_http_content() {
int t = Random::fast(0, 2);
int len;
if (t == 0) {
len = Random::fast(1, 10);
} else if (t == 1) {
len = Random::fast(100, 200);
} else {
len = Random::fast(1000, 20000);
}
return rand_string(std::numeric_limits<char>::min(), std::numeric_limits<char>::max(), len);
}
static string make_http_query(string content, bool is_json, bool is_chunked, bool is_gzip, double gzip_k = 5,
string zip_override = string()) {
HttpHeaderCreator hc;
hc.init_post("/");
hc.add_header("jfkdlsahhjk", rand_string('a', 'z', Random::fast(1, 2000)));
if (is_json) {
hc.add_header("content-type", "application/json");
}
if (is_gzip) {
BufferSlice zip;
if (zip_override.empty()) {
zip = gzencode(content, gzip_k);
} else {
zip = BufferSlice(zip_override);
}
if (!zip.empty()) {
hc.add_header("content-encoding", "gzip");
content = zip.as_slice().str();
}
}
if (is_chunked) {
hc.add_header("transfer-encoding", "chunked");
content = make_chunked(content);
} else {
hc.set_content_size(content.size());
}
string res;
auto r_header = hc.finish();
CHECK(r_header.is_ok());
res += r_header.ok().str();
res += content;
return res;
}
static string rand_http_query(string content) {
bool is_chunked = Random::fast_bool();
bool is_gzip = Random::fast_bool();
return make_http_query(std::move(content), false, is_chunked, is_gzip);
}
static string join(const std::vector<string> &v) {
string res;
for (auto &s : v) {
res += s;
}
return res;
}
TEST(Http, stack_overflow) {
ChainBufferWriter writer;
BufferSlice slice(string(256, 'A'));
for (int i = 0; i < 1000000; i++) {
ChainBufferWriter tmp_writer;
writer.append(slice.clone());
}
{
auto reader = writer.extract_reader();
reader.sync_with_writer();
}
}
TEST(Http, reader) {
#if TD_ANDROID || TD_TIZEN
return;
#endif
clear_thread_locals();
SET_VERBOSITY_LEVEL(VERBOSITY_NAME(ERROR));
auto start_mem = BufferAllocator::get_buffer_mem();
auto start_size = BufferAllocator::get_buffer_slice_size();
{
BufferSlice a("test test");
BufferSlice b = std::move(a);
#if TD_CLANG
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunknown-pragmas"
#pragma clang diagnostic ignored "-Wunknown-warning-option"
#pragma clang diagnostic ignored "-Wself-move"
#endif
a = std::move(a);
b = std::move(b);
#if TD_CLANG
#pragma clang diagnostic pop
#endif
a = std::move(b);
BufferSlice c = a.from_slice(a);
CHECK(c.size() == a.size());
}
clear_thread_locals();
ASSERT_EQ(start_mem, BufferAllocator::get_buffer_mem());
ASSERT_EQ(start_size, BufferAllocator::get_buffer_slice_size());
for (int i = 0; i < 20; i++) {
td::ChainBufferWriter input_writer;
auto input = input_writer.extract_reader();
HttpReader reader;
int max_post_size = 10000;
reader.init(&input, max_post_size, 0);
std::vector<string> contents(100);
std::generate(contents.begin(), contents.end(), gen_http_content);
auto v = td::transform(contents, rand_http_query);
auto vec_str = rand_split(join(v));
HttpQuery q;
std::vector<string> res;
for (auto &str : vec_str) {
input_writer.append(str);
input.sync_with_writer();
while (true) {
auto r_state = reader.read_next(&q);
LOG_IF(ERROR, r_state.is_error()) << r_state.error() << tag("ok", res.size());
ASSERT_TRUE(r_state.is_ok());
auto state = r_state.ok();
if (state == 0) {
if (q.files_.empty()) {
ASSERT_TRUE(td::narrow_cast<int>(q.content_.size()) <= max_post_size);
auto expected = contents[res.size()];
ASSERT_EQ(expected, q.content_.str());
res.push_back(q.content_.str());
} else {
auto r_fd = FileFd::open(q.files_[0].temp_file_name, FileFd::Read);
ASSERT_TRUE(r_fd.is_ok());
auto fd = r_fd.move_as_ok();
string content(td::narrow_cast<size_t>(q.files_[0].size), '\0');
auto r_size = fd.read(MutableSlice(content));
ASSERT_TRUE(r_size.is_ok());
ASSERT_TRUE(r_size.ok() == content.size());
ASSERT_TRUE(td::narrow_cast<int>(content.size()) > max_post_size);
ASSERT_EQ(contents[res.size()], content);
res.push_back(content);
fd.close();
}
} else {
break;
}
}
}
ASSERT_EQ(contents.size(), res.size());
ASSERT_EQ(contents, res);
}
clear_thread_locals();
ASSERT_EQ(start_mem, BufferAllocator::get_buffer_mem());
ASSERT_EQ(start_size, BufferAllocator::get_buffer_slice_size());
}
TEST(Http, gzip_bomb) {
#if TD_ANDROID || TD_TIZEN || TD_EMSCRIPTEN // the test should be disabled on low-memory systems
return;
#endif
auto gzip_bomb_str =
gzdecode(gzdecode(base64url_decode(Slice(gzip_bomb, gzip_bomb_size)).ok()).as_slice()).as_slice().str();
auto query = make_http_query("", false, false, true, 0.01, gzip_bomb_str);
auto parts = rand_split(query);
td::ChainBufferWriter input_writer;
auto input = input_writer.extract_reader();
HttpReader reader;
HttpQuery q;
reader.init(&input, 100000000, 0);
for (auto &part : parts) {
input_writer.append(part);
input.sync_with_writer();
auto r_state = reader.read_next(&q);
if (r_state.is_error()) {
LOG(INFO) << r_state.error();
return;
}
ASSERT_TRUE(r_state.ok() != 0);
}
}
TEST(Http, gzip) {
auto gzip_str = gzdecode(base64url_decode(Slice(gzip, gzip_size)).ok()).as_slice().str();
td::ChainBufferWriter input_writer;
auto input = input_writer.extract_reader();
HttpReader reader;
reader.init(&input, 0, 0);
auto query = make_http_query("", true, false, true, 0.01, gzip_str);
input_writer.append(query);
input.sync_with_writer();
HttpQuery q;
auto r_state = reader.read_next(&q);
ASSERT_TRUE(r_state.is_error());
ASSERT_EQ(413, r_state.error().code());
}
TEST(Http, aes_ctr_encode_decode_flow) {
auto str = rand_string('a', 'z', 1000000);
auto parts = rand_split(str);
td::ChainBufferWriter input_writer;
auto input = input_writer.extract_reader();
ByteFlowSource source(&input);
UInt256 key;
UInt128 iv;
Random::secure_bytes(key.raw, sizeof(key));
Random::secure_bytes(iv.raw, sizeof(iv));
AesCtrByteFlow aes_encode;
aes_encode.init(key, iv);
AesCtrByteFlow aes_decode;
aes_decode.init(key, iv);
ByteFlowSink sink;
source >> aes_encode >> aes_decode >> sink;
ASSERT_TRUE(!sink.is_ready());
for (auto &part : parts) {
input_writer.append(part);
source.wakeup();
}
ASSERT_TRUE(!sink.is_ready());
source.close_input(Status::OK());
ASSERT_TRUE(sink.is_ready());
LOG_IF(ERROR, sink.status().is_error()) << sink.status();
ASSERT_TRUE(sink.status().is_ok());
ASSERT_EQ(str, sink.result()->move_as_buffer_slice().as_slice().str());
}
TEST(Http, aes_file_encryption) {
auto str = rand_string('a', 'z', 1000000);
CSlice name = "test_encryption";
unlink(name).ignore();
UInt256 key;
UInt128 iv;
Random::secure_bytes(key.raw, sizeof(key));
Random::secure_bytes(iv.raw, sizeof(iv));
{
BufferedFdBase<FileFd> fd(FileFd::open(name, FileFd::Write | FileFd::Create).move_as_ok());
auto parts = rand_split(str);
ChainBufferWriter output_writer;
auto output_reader = output_writer.extract_reader();
ByteFlowSource source(&output_reader);
AesCtrByteFlow aes_encode;
aes_encode.init(key, iv);
ByteFlowSink sink;
source >> aes_encode >> sink;
fd.set_output_reader(sink.get_output());
for (auto &part : parts) {
output_writer.append(part);
source.wakeup();
fd.flush_write().ensure();
}
fd.close();
}
{
BufferedFdBase<FileFd> fd(FileFd::open(name, FileFd::Read).move_as_ok());
ChainBufferWriter input_writer;
auto input_reader = input_writer.extract_reader();
ByteFlowSource source(&input_reader);
AesCtrByteFlow aes_encode;
aes_encode.init(key, iv);
ByteFlowSink sink;
source >> aes_encode >> sink;
fd.set_input_writer(&input_writer);
fd.get_poll_info().add_flags(PollFlags::Read());
while (can_read_local(fd)) {
fd.flush_read(4096).ensure();
source.wakeup();
}
fd.close();
source.close_input(Status::OK());
ASSERT_TRUE(sink.is_ready());
LOG_IF(ERROR, sink.status().is_error()) << sink.status();
ASSERT_TRUE(sink.status().is_ok());
auto result = sink.result()->move_as_buffer_slice().as_slice().str();
ASSERT_EQ(str, result);
}
}
TEST(Http, chunked_flow) {
auto str = rand_string('a', 'z', 100);
auto parts = rand_split(make_chunked(str));
td::ChainBufferWriter input_writer;
auto input = input_writer.extract_reader();
ByteFlowSource source(&input);
HttpChunkedByteFlow chunked_flow;
ByteFlowSink sink;
source >> chunked_flow >> sink;
for (auto &part : parts) {
input_writer.append(part);
source.wakeup();
}
source.close_input(Status::OK());
ASSERT_TRUE(sink.is_ready());
LOG_IF(ERROR, sink.status().is_error()) << sink.status();
ASSERT_TRUE(sink.status().is_ok());
auto res = sink.result()->move_as_buffer_slice().as_slice().str();
ASSERT_EQ(str.size(), res.size());
ASSERT_EQ(str, res);
}
TEST(Http, chunked_flow_error) {
auto str = rand_string('a', 'z', 100000);
for (int d = 1; d < 100; d += 10) {
auto new_str = make_chunked(str);
new_str.resize(str.size() - d);
auto parts = rand_split(new_str);
td::ChainBufferWriter input_writer;
auto input = input_writer.extract_reader();
ByteFlowSource source(&input);
HttpChunkedByteFlow chunked_flow;
ByteFlowSink sink;
source >> chunked_flow >> sink;
for (auto &part : parts) {
input_writer.append(part);
source.wakeup();
}
ASSERT_TRUE(!sink.is_ready());
source.close_input(Status::OK());
ASSERT_TRUE(sink.is_ready());
ASSERT_TRUE(!sink.status().is_ok());
}
}
TEST(Http, gzip_chunked_flow) {
auto str = rand_string('a', 'z', 1000000);
auto parts = rand_split(make_chunked(gzencode(str, 2.0).as_slice().str()));
ChainBufferWriter input_writer;
auto input = input_writer.extract_reader();
ByteFlowSource source(&input);
HttpChunkedByteFlow chunked_flow;
GzipByteFlow gzip_flow(Gzip::Mode::Decode);
ByteFlowSink sink;
source >> chunked_flow >> gzip_flow >> sink;
for (auto &part : parts) {
input_writer.append(part);
source.wakeup();
}
source.close_input(Status::OK());
ASSERT_TRUE(sink.is_ready());
LOG_IF(ERROR, sink.status().is_error()) << sink.status();
ASSERT_TRUE(sink.status().is_ok());
ASSERT_EQ(str, sink.result()->move_as_buffer_slice().as_slice().str());
}
TEST(Http, gzip_bomb_with_limit) {
std::string gzip_bomb_str;
{
ChainBufferWriter input_writer;
auto input = input_writer.extract_reader();
GzipByteFlow gzip_flow(Gzip::Mode::Encode);
ByteFlowSource source(&input);
ByteFlowSink sink;
source >> gzip_flow >> sink;
std::string s(1 << 16, 'a');
for (int i = 0; i < 1000; i++) {
input_writer.append(s);
source.wakeup();
}
source.close_input(Status::OK());
ASSERT_TRUE(sink.is_ready());
LOG_IF(ERROR, sink.status().is_error()) << sink.status();
ASSERT_TRUE(sink.status().is_ok());
gzip_bomb_str = sink.result()->move_as_buffer_slice().as_slice().str();
}
auto query = make_http_query("", false, false, true, 0.01, gzip_bomb_str);
auto parts = rand_split(query);
td::ChainBufferWriter input_writer;
auto input = input_writer.extract_reader();
HttpReader reader;
HttpQuery q;
reader.init(&input, 1000000);
bool ok = false;
for (auto &part : parts) {
input_writer.append(part);
input.sync_with_writer();
auto r_state = reader.read_next(&q);
if (r_state.is_error()) {
LOG(FATAL) << r_state.error();
return;
} else if (r_state.ok() == 0) {
ok = true;
}
}
ASSERT_TRUE(ok);
}
#if TD_DARWIN_WATCH_OS
struct Baton {
std::mutex mutex;
std::condition_variable cond;
bool is_ready{false};
void wait() {
std::unique_lock<std::mutex> lock(mutex);
cond.wait(lock, [&] { return is_ready; });
}
void post() {
{
std::unique_lock<std::mutex> lock(mutex);
is_ready = true;
}
cond.notify_all();
}
void reset() {
is_ready = false;
}
};
TEST(Http, Darwin) {
Baton baton;
//LOG(ERROR) << "???";
td::DarwinHttp::get("http://example.com", [&](td::BufferSlice data) {
//LOG(ERROR) << data.as_slice();
baton.post();
});
//LOG(ERROR) << "!!!";
baton.wait();
}
#endif