2018-12-31 20:04:05 +01:00
|
|
|
//
|
2020-01-01 02:23:48 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2020
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// 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 "td/net/HttpConnectionBase.h"
|
|
|
|
|
|
|
|
#include "td/net/HttpHeaderCreator.h"
|
|
|
|
|
2019-02-12 22:26:36 +01:00
|
|
|
#include "td/utils/common.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/misc.h"
|
2018-09-07 02:41:21 +02:00
|
|
|
#include "td/utils/port/detail/PollableFd.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
namespace detail {
|
|
|
|
|
2018-08-15 14:41:42 +02:00
|
|
|
HttpConnectionBase::HttpConnectionBase(State state, SocketFd fd, SslStream ssl_stream, size_t max_post_size,
|
2020-07-23 18:47:12 +02:00
|
|
|
size_t max_files, int32 idle_timeout, int32 slow_scheduler_id)
|
2018-12-31 20:04:05 +01:00
|
|
|
: state_(state)
|
2018-08-15 14:41:42 +02:00
|
|
|
, fd_(std::move(fd))
|
|
|
|
, ssl_stream_(std::move(ssl_stream))
|
2018-12-31 20:04:05 +01:00
|
|
|
, max_post_size_(max_post_size)
|
|
|
|
, max_files_(max_files)
|
2020-07-23 18:47:12 +02:00
|
|
|
, idle_timeout_(idle_timeout)
|
|
|
|
, slow_scheduler_id_(slow_scheduler_id) {
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(state_ != State::Close);
|
2018-08-15 14:41:42 +02:00
|
|
|
|
|
|
|
if (ssl_stream_) {
|
|
|
|
read_source_ >> ssl_stream_.read_byte_flow() >> read_sink_;
|
|
|
|
write_source_ >> ssl_stream_.write_byte_flow() >> write_sink_;
|
|
|
|
} else {
|
|
|
|
read_source_ >> read_sink_;
|
|
|
|
write_source_ >> write_sink_;
|
|
|
|
}
|
2020-07-13 14:23:03 +02:00
|
|
|
peer_address_.init_peer_address(fd_).ignore();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void HttpConnectionBase::live_event() {
|
|
|
|
if (idle_timeout_ != 0) {
|
|
|
|
set_timeout_in(idle_timeout_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HttpConnectionBase::start_up() {
|
2018-09-11 00:20:41 +02:00
|
|
|
Scheduler::subscribe(fd_.get_poll_info().extract_pollable_fd(this));
|
2018-08-15 14:41:42 +02:00
|
|
|
reader_.init(read_sink_.get_output(), max_post_size_, max_files_);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (state_ == State::Read) {
|
|
|
|
current_query_ = make_unique<HttpQuery>();
|
|
|
|
}
|
|
|
|
live_event();
|
|
|
|
yield();
|
|
|
|
}
|
|
|
|
void HttpConnectionBase::tear_down() {
|
2018-09-11 00:20:41 +02:00
|
|
|
Scheduler::unsubscribe_before_close(fd_.get_poll_info().get_pollable_fd_ref());
|
2018-08-15 14:41:42 +02:00
|
|
|
fd_.close();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2020-07-13 10:55:43 +02:00
|
|
|
void HttpConnectionBase::write_next_noflush(BufferSlice buffer) {
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(state_ == State::Write);
|
2018-08-15 14:41:42 +02:00
|
|
|
write_buffer_.append(std::move(buffer));
|
2020-07-13 10:55:43 +02:00
|
|
|
}
|
|
|
|
void HttpConnectionBase::write_next(BufferSlice buffer) {
|
|
|
|
write_next_noflush(std::move(buffer));
|
2018-12-31 20:04:05 +01:00
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HttpConnectionBase::write_ok() {
|
|
|
|
CHECK(state_ == State::Write);
|
|
|
|
current_query_ = make_unique<HttpQuery>();
|
|
|
|
state_ = State::Read;
|
|
|
|
live_event();
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HttpConnectionBase::write_error(Status error) {
|
|
|
|
CHECK(state_ == State::Write);
|
2018-12-05 16:46:19 +01:00
|
|
|
LOG(WARNING) << "Close HTTP connection: " << error;
|
2018-12-31 20:04:05 +01:00
|
|
|
state_ = State::Close;
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HttpConnectionBase::timeout_expired() {
|
|
|
|
LOG(INFO) << "Idle timeout expired";
|
|
|
|
|
2018-08-15 14:41:42 +02:00
|
|
|
if (fd_.need_flush_write()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
on_error(Status::Error("Write timeout expired"));
|
|
|
|
} else if (state_ == State::Read) {
|
|
|
|
on_error(Status::Error("Read timeout expired"));
|
|
|
|
}
|
|
|
|
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
void HttpConnectionBase::loop() {
|
2020-07-24 17:49:33 +02:00
|
|
|
if (ssl_stream_) {
|
|
|
|
//ssl_stream_.read_byte_flow().set_need_size(0);
|
|
|
|
ssl_stream_.write_byte_flow().reset_need_size();
|
|
|
|
}
|
2020-07-21 17:29:39 +02:00
|
|
|
sync_with_poll(fd_);
|
|
|
|
if (can_read_local(fd_)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
LOG(DEBUG) << "Can read from the connection";
|
2018-08-15 14:41:42 +02:00
|
|
|
auto r = fd_.flush_read();
|
2018-12-31 20:04:05 +01:00
|
|
|
if (r.is_error()) {
|
|
|
|
if (!begins_with(r.error().message(), "SSL error {336134278")) { // if error is not yet outputed
|
2019-02-21 16:58:20 +01:00
|
|
|
LOG(INFO) << "Receive flush_read error: " << r.error();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
on_error(Status::Error(r.error().public_message()));
|
|
|
|
return stop();
|
|
|
|
}
|
|
|
|
}
|
2018-08-15 14:41:42 +02:00
|
|
|
read_source_.wakeup();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
// TODO: read_next even when state_ == State::Write
|
|
|
|
|
|
|
|
bool want_read = false;
|
2020-07-23 18:47:12 +02:00
|
|
|
bool can_be_slow = slow_scheduler_id_ == -1;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (state_ == State::Read) {
|
2020-07-23 18:47:12 +02:00
|
|
|
auto res = reader_.read_next(current_query_.get(), can_be_slow);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (res.is_error()) {
|
2020-07-23 18:47:12 +02:00
|
|
|
if (res.error().message() == "SLOW") {
|
|
|
|
LOG(INFO) << "Slow HTTP connection: migrate to " << slow_scheduler_id_;
|
|
|
|
CHECK(!can_be_slow);
|
|
|
|
yield();
|
|
|
|
migrate(slow_scheduler_id_);
|
|
|
|
slow_scheduler_id_ = -1;
|
|
|
|
return;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
live_event();
|
|
|
|
state_ = State::Write;
|
|
|
|
LOG(INFO) << res.error();
|
|
|
|
HttpHeaderCreator hc;
|
|
|
|
hc.init_status_line(res.error().code());
|
|
|
|
hc.set_content_size(0);
|
2018-08-15 14:41:42 +02:00
|
|
|
write_buffer_.append(hc.finish().ok());
|
2018-12-31 20:04:05 +01:00
|
|
|
close_after_write_ = true;
|
|
|
|
on_error(Status::Error(res.error().public_message()));
|
|
|
|
} else if (res.ok() == 0) {
|
|
|
|
state_ = State::Write;
|
2018-06-04 23:12:34 +02:00
|
|
|
LOG(DEBUG) << "Send query to handler";
|
2018-12-31 20:04:05 +01:00
|
|
|
live_event();
|
2020-07-13 14:23:03 +02:00
|
|
|
current_query_->peer_address_ = peer_address_;
|
2018-12-31 20:04:05 +01:00
|
|
|
on_query(std::move(current_query_));
|
|
|
|
} else {
|
|
|
|
want_read = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-15 14:41:42 +02:00
|
|
|
write_source_.wakeup();
|
|
|
|
|
2020-07-21 17:29:39 +02:00
|
|
|
if (can_write_local(fd_)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
LOG(DEBUG) << "Can write to the connection";
|
2018-08-15 14:41:42 +02:00
|
|
|
auto r = fd_.flush_write();
|
2018-12-31 20:04:05 +01:00
|
|
|
if (r.is_error()) {
|
2019-02-21 16:58:20 +01:00
|
|
|
LOG(INFO) << "Receive flush_write error: " << r.error();
|
2018-12-31 20:04:05 +01:00
|
|
|
on_error(Status::Error(r.error().public_message()));
|
|
|
|
}
|
2018-08-15 14:41:42 +02:00
|
|
|
if (close_after_write_ && !fd_.need_flush_write()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-15 14:41:42 +02:00
|
|
|
Status pending_error;
|
2020-07-21 17:29:39 +02:00
|
|
|
if (fd_.get_poll_info().get_flags_local().has_pending_error()) {
|
2018-08-15 14:41:42 +02:00
|
|
|
pending_error = fd_.get_pending_error();
|
|
|
|
}
|
|
|
|
if (pending_error.is_ok() && write_sink_.status().is_error()) {
|
|
|
|
pending_error = std::move(write_sink_.status());
|
|
|
|
}
|
|
|
|
if (pending_error.is_ok() && read_sink_.status().is_error()) {
|
|
|
|
pending_error = std::move(read_sink_.status());
|
|
|
|
}
|
|
|
|
if (pending_error.is_error()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
LOG(INFO) << pending_error;
|
|
|
|
if (!close_after_write_) {
|
|
|
|
on_error(Status::Error(pending_error.public_message()));
|
|
|
|
}
|
|
|
|
state_ = State::Close;
|
|
|
|
}
|
2018-08-15 14:41:42 +02:00
|
|
|
|
2020-07-21 17:29:39 +02:00
|
|
|
if (can_close_local(fd_)) {
|
2018-06-04 23:12:34 +02:00
|
|
|
LOG(DEBUG) << "Can close the connection";
|
2018-12-31 20:04:05 +01:00
|
|
|
state_ = State::Close;
|
|
|
|
}
|
|
|
|
if (state_ == State::Close) {
|
2018-08-15 14:41:42 +02:00
|
|
|
LOG_IF(INFO, fd_.need_flush_write()) << "Close nonempty connection";
|
2020-06-15 03:23:47 +02:00
|
|
|
LOG_IF(INFO, want_read && (fd_.input_buffer().size() > 0 || current_query_->type_ != HttpQuery::Type::Empty))
|
2018-12-31 20:04:05 +01:00
|
|
|
<< "Close connection while reading request/response";
|
|
|
|
return stop();
|
|
|
|
}
|
|
|
|
}
|
2018-07-03 21:29:04 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace detail
|
|
|
|
} // namespace td
|