2020-11-03 17:34:10 +01:00
|
|
|
//
|
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2020
|
|
|
|
//
|
|
|
|
// 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 "telegram-bot-api/HttpStatConnection.h"
|
|
|
|
|
|
|
|
#include "td/net/HttpHeaderCreator.h"
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
|
|
|
|
namespace telegram_bot_api {
|
|
|
|
|
|
|
|
void HttpStatConnection::handle(td::unique_ptr<td::HttpQuery> http_query,
|
|
|
|
td::ActorOwn<td::HttpInboundConnection> connection) {
|
|
|
|
CHECK(connection_->empty());
|
|
|
|
connection_ = std::move(connection);
|
|
|
|
|
|
|
|
td::PromiseActor<td::BufferSlice> promise;
|
|
|
|
td::FutureActor<td::BufferSlice> future;
|
|
|
|
init_promise_future(&promise, &future);
|
|
|
|
future.set_event(td::EventCreator::yield(actor_id()));
|
|
|
|
LOG(DEBUG) << "SEND";
|
2021-01-24 18:09:53 +01:00
|
|
|
td::Parser url_path_parser(http_query->url_path_);
|
|
|
|
as_json_ = url_path_parser.try_skip("/json");
|
|
|
|
send_closure(client_manager_, &ClientManager::get_stats, std::move(promise), http_query->get_args(), as_json_);
|
2020-11-03 17:34:10 +01:00
|
|
|
result_ = std::move(future);
|
|
|
|
}
|
|
|
|
|
|
|
|
void HttpStatConnection::wakeup() {
|
|
|
|
if (result_.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
LOG_CHECK(result_.is_ok()) << result_.move_as_error();
|
|
|
|
|
|
|
|
auto content = result_.move_as_ok();
|
|
|
|
td::HttpHeaderCreator hc;
|
|
|
|
hc.init_status_line(200);
|
|
|
|
hc.set_keep_alive();
|
2021-01-24 18:09:53 +01:00
|
|
|
if (as_json_) {
|
|
|
|
hc.set_content_type("application/json");
|
|
|
|
} else {
|
|
|
|
hc.set_content_type("text/plain");
|
|
|
|
}
|
2020-11-03 17:34:10 +01:00
|
|
|
hc.set_content_size(content.size());
|
|
|
|
|
|
|
|
auto r_header = hc.finish();
|
|
|
|
if (r_header.is_error()) {
|
|
|
|
send_closure(connection_.release(), &td::HttpInboundConnection::write_error, r_header.move_as_error());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
send_closure(connection_, &td::HttpInboundConnection::write_next_noflush, td::BufferSlice(r_header.ok()));
|
|
|
|
send_closure(connection_, &td::HttpInboundConnection::write_next_noflush, std::move(content));
|
|
|
|
send_closure(connection_.release(), &td::HttpInboundConnection::write_ok);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace telegram_bot_api
|