forked from newton-blockchain/ton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp-inbound-connection.cpp
105 lines (87 loc) · 3.21 KB
/
http-inbound-connection.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
/*
This file is part of TON Blockchain Library.
TON Blockchain Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
TON Blockchain Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
Copyright 2019-2020 Telegram Systems LLP
*/
#include "http-inbound-connection.h"
#include "td/utils/misc.h"
namespace ton {
namespace http {
void HttpInboundConnection::send_client_error() {
static const auto s =
"HTTP/1.0 400 Bad Request\r\n"
"Connection: Close\r\n"
"Content-length: 0\r\n"
"\r\n";
buffered_fd_.output_buffer().append(td::Slice(s, strlen(s)));
close_after_write_ = true;
loop();
}
void HttpInboundConnection::send_server_error() {
static const auto s =
"HTTP/1.1 502 Bad Gateway\r\n"
"Connection: keep-alive\r\n"
"Content-length: 0\r\n"
"\r\n";
buffered_fd_.output_buffer().append(td::Slice(s, strlen(s)));
loop();
}
void HttpInboundConnection::send_proxy_error() {
static const auto s =
"HTTP/1.1 502 Bad Gateway\r\n"
"Connection: keep-alive\r\n"
"Content-length: 0\r\n"
"\r\n";
buffered_fd_.output_buffer().append(td::Slice(s, strlen(s)));
loop();
}
td::Status HttpInboundConnection::receive(td::ChainBufferReader &input) {
if (reading_payload_) {
return receive_payload(input);
}
if (!cur_request_ && !read_next_request_) {
return td::Status::OK();
}
while (!cur_request_ || !cur_request_->check_parse_header_completed()) {
bool exit_loop;
auto R = HttpRequest::parse(std::move(cur_request_), cur_line_, exit_loop, input);
if (R.is_error()) {
send_client_error();
return td::Status::OK();
}
if (exit_loop) {
return td::Status::OK();
}
cur_request_ = R.move_as_ok();
}
auto payload = cur_request_->create_empty_payload().move_as_ok();
auto P = td::PromiseCreator::lambda(
[SelfId = actor_id(this)](td::Result<std::pair<std::unique_ptr<HttpResponse>, std::shared_ptr<HttpPayload>>> R) {
if (R.is_ok()) {
auto a = R.move_as_ok();
td::actor::send_closure(SelfId, &HttpInboundConnection::send_answer, std::move(a.first), std::move(a.second));
} else {
td::actor::send_closure(SelfId, &HttpInboundConnection::send_proxy_error);
}
});
http_callback_->receive_request(std::move(cur_request_), payload, std::move(P));
read_payload(std::move(payload));
return td::Status::OK();
}
void HttpInboundConnection::send_answer(std::unique_ptr<HttpResponse> response, std::shared_ptr<HttpPayload> payload) {
CHECK(payload);
response->store_http(buffered_fd_.output_buffer());
write_payload(std::move(payload));
loop();
}
} // namespace http
} // namespace ton