2020-11-03 17:34:10 +01:00
|
|
|
//
|
2024-05-30 18:40:54 +02:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2020-11-03 17:34:10 +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 "telegram-bot-api/HttpStatConnection.h"
|
|
|
|
|
|
|
|
#include "td/net/HttpHeaderCreator.h"
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
2022-06-30 19:27:08 +02:00
|
|
|
#include "td/utils/Promise.h"
|
2020-11-03 17:34:10 +01:00
|
|
|
|
|
|
|
namespace telegram_bot_api {
|
|
|
|
|
|
|
|
void HttpStatConnection::handle(td::unique_ptr<td::HttpQuery> http_query,
|
|
|
|
td::ActorOwn<td::HttpInboundConnection> connection) {
|
2022-09-23 19:29:42 +02:00
|
|
|
CHECK(connection_.empty());
|
2020-11-03 17:34:10 +01:00
|
|
|
connection_ = std::move(connection);
|
|
|
|
|
2022-06-30 18:59:30 +02:00
|
|
|
auto promise = td::PromiseCreator::lambda([actor_id = actor_id(this)](td::Result<td::BufferSlice> result) {
|
|
|
|
send_closure(actor_id, &HttpStatConnection::on_result, std::move(result));
|
|
|
|
});
|
2020-11-03 17:34:10 +01:00
|
|
|
send_closure(client_manager_, &ClientManager::get_stats, std::move(promise), http_query->get_args());
|
|
|
|
}
|
|
|
|
|
2022-06-30 18:59:30 +02:00
|
|
|
void HttpStatConnection::on_result(td::Result<td::BufferSlice> result) {
|
|
|
|
if (result.is_error()) {
|
|
|
|
send_closure(connection_.release(), &td::HttpInboundConnection::write_error,
|
|
|
|
td::Status::Error(500, "Internal Server Error: closing"));
|
2020-11-03 17:34:10 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-30 18:59:30 +02:00
|
|
|
auto content = result.move_as_ok();
|
2020-11-03 17:34:10 +01:00
|
|
|
td::HttpHeaderCreator hc;
|
|
|
|
hc.init_status_line(200);
|
|
|
|
hc.set_keep_alive();
|
|
|
|
hc.set_content_type("text/plain");
|
|
|
|
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
|