19
#include "http-inbound-connection.h"
20
#include "td/utils/misc.h"
26
void HttpInboundConnection::send_client_error() {
28
"HTTP/1.0 400 Bad Request\r\n"
29
"Connection: Close\r\n"
30
"Content-length: 0\r\n"
32
buffered_fd_.output_buffer().append(td::Slice(s, strlen(s)));
33
close_after_write_ = true;
37
void HttpInboundConnection::send_server_error() {
39
"HTTP/1.1 502 Bad Gateway\r\n"
40
"Connection: keep-alive\r\n"
41
"Content-length: 0\r\n"
43
buffered_fd_.output_buffer().append(td::Slice(s, strlen(s)));
47
void HttpInboundConnection::send_proxy_error(td::Status error) {
48
if (error.code() == ErrorCode::timeout) {
50
"HTTP/1.1 504 Gateway Timeout\r\n"
51
"Connection: keep-alive\r\n"
52
"Content-length: 0\r\n"
54
buffered_fd_.output_buffer().append(td::Slice(s, strlen(s)));
57
"HTTP/1.1 502 Bad Gateway\r\n"
58
"Connection: keep-alive\r\n"
59
"Content-length: 0\r\n"
61
buffered_fd_.output_buffer().append(td::Slice(s, strlen(s)));
66
td::Status HttpInboundConnection::receive(td::ChainBufferReader &input) {
67
if (reading_payload_) {
68
return receive_payload(input);
71
if (!cur_request_ && !read_next_request_) {
72
return td::Status::OK();
75
while (!cur_request_ || !cur_request_->check_parse_header_completed()) {
77
auto R = HttpRequest::parse(std::move(cur_request_), cur_line_, exit_loop, input);
80
return td::Status::OK();
82
cur_request_ = R.move_as_ok();
84
return td::Status::OK();
88
auto payload = cur_request_->create_empty_payload().move_as_ok();
89
auto P = td::PromiseCreator::lambda(
90
[SelfId = actor_id(this)](td::Result<std::pair<std::unique_ptr<HttpResponse>, std::shared_ptr<HttpPayload>>> R) {
92
auto a = R.move_as_ok();
93
td::actor::send_closure(SelfId, &HttpInboundConnection::send_answer, std::move(a.first), std::move(a.second));
95
td::actor::send_closure(SelfId, &HttpInboundConnection::send_proxy_error, R.move_as_error());
98
http_callback_->receive_request(std::move(cur_request_), payload, std::move(P));
99
read_payload(std::move(payload));
101
return td::Status::OK();
104
void HttpInboundConnection::send_answer(std::unique_ptr<HttpResponse> response, std::shared_ptr<HttpPayload> payload) {
106
response->store_http(buffered_fd_.output_buffer());
108
write_payload(std::move(payload));