2020-11-03 17:34:10 +01:00
|
|
|
//
|
2022-12-31 22:31:16 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
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)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "telegram-bot-api/ClientParameters.h"
|
|
|
|
|
2022-01-25 16:18:44 +01:00
|
|
|
#include "td/net/HttpFile.h"
|
|
|
|
|
2020-11-03 17:34:10 +01:00
|
|
|
#include "td/actor/actor.h"
|
|
|
|
|
|
|
|
#include "td/utils/buffer.h"
|
|
|
|
#include "td/utils/common.h"
|
2022-03-16 10:41:12 +01:00
|
|
|
#include "td/utils/FlatHashMap.h"
|
2020-11-03 17:34:10 +01:00
|
|
|
#include "td/utils/JsonBuilder.h"
|
|
|
|
#include "td/utils/List.h"
|
|
|
|
#include "td/utils/port/IPAddress.h"
|
2022-06-30 19:27:08 +02:00
|
|
|
#include "td/utils/Promise.h"
|
2020-11-03 17:34:10 +01:00
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
#include "td/utils/StringBuilder.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
2022-10-09 19:16:45 +02:00
|
|
|
#include <atomic>
|
2020-11-03 17:34:10 +01:00
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace telegram_bot_api {
|
|
|
|
|
|
|
|
class BotStatActor;
|
|
|
|
|
2022-01-25 16:18:44 +01:00
|
|
|
class Query final : public td::ListNode {
|
2020-11-03 17:34:10 +01:00
|
|
|
public:
|
|
|
|
enum class State : td::int8 { Query, OK, Error };
|
|
|
|
|
|
|
|
td::Slice token() const {
|
|
|
|
return token_;
|
|
|
|
}
|
2020-12-12 00:45:36 +01:00
|
|
|
bool is_user() const {
|
|
|
|
return is_user_;
|
|
|
|
}
|
2020-11-03 17:34:10 +01:00
|
|
|
bool is_test_dc() const {
|
|
|
|
return is_test_dc_;
|
|
|
|
}
|
|
|
|
td::Slice method() const {
|
|
|
|
return method_;
|
|
|
|
}
|
|
|
|
bool has_arg(td::Slice key) const {
|
|
|
|
auto it = std::find_if(args_.begin(), args_.end(),
|
|
|
|
[&key](const std::pair<td::MutableSlice, td::MutableSlice> &s) { return s.first == key; });
|
|
|
|
return it != args_.end();
|
|
|
|
}
|
|
|
|
td::MutableSlice arg(td::Slice key) const {
|
|
|
|
auto it = std::find_if(args_.begin(), args_.end(),
|
|
|
|
[&key](const std::pair<td::MutableSlice, td::MutableSlice> &s) { return s.first == key; });
|
|
|
|
return it == args_.end() ? td::MutableSlice() : it->second;
|
|
|
|
}
|
|
|
|
const td::vector<std::pair<td::MutableSlice, td::MutableSlice>> &args() const {
|
|
|
|
return args_;
|
|
|
|
}
|
|
|
|
td::Slice get_header(td::Slice key) const {
|
|
|
|
auto it = std::find_if(headers_.begin(), headers_.end(),
|
|
|
|
[&key](const std::pair<td::MutableSlice, td::MutableSlice> &s) { return s.first == key; });
|
|
|
|
return it == headers_.end() ? td::Slice() : it->second;
|
|
|
|
}
|
|
|
|
const td::HttpFile *file(td::Slice key) const {
|
|
|
|
auto it = std::find_if(files_.begin(), files_.end(), [&key](const td::HttpFile &f) { return f.field_name == key; });
|
|
|
|
return it == files_.end() ? nullptr : &*it;
|
|
|
|
}
|
|
|
|
const td::vector<td::HttpFile> &files() const {
|
|
|
|
return files_;
|
|
|
|
}
|
|
|
|
|
2023-03-13 16:42:35 +01:00
|
|
|
td::string get_peer_ip_address() const;
|
2020-11-03 17:34:10 +01:00
|
|
|
|
|
|
|
td::BufferSlice &answer() {
|
|
|
|
return answer_;
|
|
|
|
}
|
|
|
|
|
|
|
|
int http_status_code() const {
|
|
|
|
return http_status_code_;
|
|
|
|
}
|
|
|
|
|
|
|
|
int retry_after() const {
|
|
|
|
return retry_after_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_ok(td::BufferSlice result);
|
|
|
|
|
|
|
|
void set_error(int http_status_code, td::BufferSlice result);
|
|
|
|
|
|
|
|
void set_retry_after_error(int retry_after);
|
|
|
|
|
|
|
|
bool is_ready() const {
|
|
|
|
return state_ != State::Query;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_internal() const {
|
|
|
|
return is_internal_;
|
|
|
|
}
|
|
|
|
|
2020-12-12 00:45:36 +01:00
|
|
|
Query(td::vector<td::BufferSlice> &&container, td::Slice token, bool is_user, bool is_test_dc, td::MutableSlice method,
|
2020-11-03 17:34:10 +01:00
|
|
|
td::vector<std::pair<td::MutableSlice, td::MutableSlice>> &&args,
|
|
|
|
td::vector<std::pair<td::MutableSlice, td::MutableSlice>> &&headers, td::vector<td::HttpFile> &&files,
|
2023-03-13 16:42:35 +01:00
|
|
|
std::shared_ptr<SharedData> shared_data, const td::IPAddress &peer_ip_address, bool is_internal);
|
2020-11-03 17:34:10 +01:00
|
|
|
Query(const Query &) = delete;
|
|
|
|
Query &operator=(const Query &) = delete;
|
|
|
|
Query(Query &&) = delete;
|
|
|
|
Query &operator=(Query &&) = delete;
|
|
|
|
~Query() {
|
|
|
|
if (shared_data_) {
|
2022-10-04 17:27:50 +02:00
|
|
|
shared_data_->query_count_.fetch_sub(1, std::memory_order_relaxed);
|
2021-08-10 11:50:02 +02:00
|
|
|
if (!empty()) {
|
2022-10-04 17:27:50 +02:00
|
|
|
shared_data_->query_list_size_.fetch_sub(1, std::memory_order_relaxed);
|
2021-08-10 11:50:02 +02:00
|
|
|
}
|
2023-01-23 17:27:19 +01:00
|
|
|
td::Scheduler::instance()->destroy_on_scheduler(SharedData::get_file_gc_scheduler_id(), container_, args_,
|
|
|
|
headers_, files_, answer_);
|
2020-11-03 17:34:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
double start_timestamp() const {
|
|
|
|
return start_timestamp_;
|
|
|
|
}
|
|
|
|
|
2021-06-14 21:47:01 +02:00
|
|
|
void set_stat_actor(td::ActorId<BotStatActor> stat_actor);
|
2020-11-03 17:34:10 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
State state_;
|
|
|
|
std::shared_ptr<SharedData> shared_data_;
|
|
|
|
double start_timestamp_;
|
2023-03-13 16:42:35 +01:00
|
|
|
td::IPAddress peer_ip_address_;
|
2020-11-03 17:34:10 +01:00
|
|
|
td::ActorId<BotStatActor> stat_actor_;
|
|
|
|
|
|
|
|
// request
|
|
|
|
td::vector<td::BufferSlice> container_;
|
|
|
|
td::Slice token_;
|
2020-12-12 00:45:36 +01:00
|
|
|
bool is_user_;
|
2020-11-03 17:34:10 +01:00
|
|
|
bool is_test_dc_;
|
|
|
|
td::MutableSlice method_;
|
|
|
|
td::vector<std::pair<td::MutableSlice, td::MutableSlice>> args_;
|
|
|
|
td::vector<std::pair<td::MutableSlice, td::MutableSlice>> headers_;
|
|
|
|
td::vector<td::HttpFile> files_;
|
|
|
|
bool is_internal_ = false;
|
|
|
|
|
|
|
|
// response
|
|
|
|
td::BufferSlice answer_;
|
|
|
|
int http_status_code_ = 0;
|
|
|
|
int retry_after_ = 0;
|
2021-06-14 21:47:01 +02:00
|
|
|
|
|
|
|
// for stats
|
|
|
|
td::int32 file_count() const {
|
|
|
|
return static_cast<td::int32>(files_.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
td::int64 query_size() const;
|
|
|
|
|
|
|
|
td::int64 files_size() const;
|
|
|
|
|
|
|
|
td::int64 files_max_size() const;
|
|
|
|
|
|
|
|
void send_request_stat() const;
|
|
|
|
|
|
|
|
void send_response_stat() const;
|
2020-11-03 17:34:10 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
td::StringBuilder &operator<<(td::StringBuilder &sb, const Query &query);
|
|
|
|
|
|
|
|
// fix for outdated C++14 libraries
|
|
|
|
// https://stackoverflow.com/questions/26947704/implicit-conversion-failure-from-initializer-list
|
2022-03-16 10:52:34 +01:00
|
|
|
extern td::FlatHashMap<td::string, td::unique_ptr<td::VirtuallyJsonable>> empty_parameters;
|
2020-11-03 17:34:10 +01:00
|
|
|
|
2022-01-25 16:18:44 +01:00
|
|
|
class JsonParameters final : public td::Jsonable {
|
2020-11-03 17:34:10 +01:00
|
|
|
public:
|
2022-03-16 10:52:34 +01:00
|
|
|
explicit JsonParameters(const td::FlatHashMap<td::string, td::unique_ptr<td::VirtuallyJsonable>> ¶meters)
|
2020-11-03 17:34:10 +01:00
|
|
|
: parameters_(parameters) {
|
|
|
|
}
|
|
|
|
void store(td::JsonValueScope *scope) const {
|
|
|
|
auto object = scope->enter_object();
|
|
|
|
for (auto ¶meter : parameters_) {
|
|
|
|
CHECK(parameter.second != nullptr);
|
|
|
|
object(parameter.first, *parameter.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-03-16 10:52:34 +01:00
|
|
|
const td::FlatHashMap<td::string, td::unique_ptr<td::VirtuallyJsonable>> ¶meters_;
|
2020-11-03 17:34:10 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
2022-01-25 16:18:44 +01:00
|
|
|
class JsonQueryOk final : public td::Jsonable {
|
2020-11-03 17:34:10 +01:00
|
|
|
public:
|
|
|
|
JsonQueryOk(const T &result, td::Slice description) : result_(result), description_(description) {
|
|
|
|
}
|
|
|
|
void store(td::JsonValueScope *scope) const {
|
|
|
|
auto object = scope->enter_object();
|
|
|
|
object("ok", td::JsonTrue());
|
|
|
|
object("result", result_);
|
|
|
|
if (!description_.empty()) {
|
|
|
|
object("description", description_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const T &result_;
|
|
|
|
td::Slice description_;
|
|
|
|
};
|
|
|
|
|
2022-01-25 16:18:44 +01:00
|
|
|
class JsonQueryError final : public td::Jsonable {
|
2020-11-03 17:34:10 +01:00
|
|
|
public:
|
|
|
|
JsonQueryError(
|
|
|
|
int error_code, td::Slice description,
|
2022-03-16 10:52:34 +01:00
|
|
|
const td::FlatHashMap<td::string, td::unique_ptr<td::VirtuallyJsonable>> ¶meters = empty_parameters)
|
2020-11-03 17:34:10 +01:00
|
|
|
: error_code_(error_code), description_(description), parameters_(parameters) {
|
|
|
|
}
|
|
|
|
void store(td::JsonValueScope *scope) const {
|
|
|
|
auto object = scope->enter_object();
|
|
|
|
object("ok", td::JsonFalse());
|
|
|
|
object("error_code", error_code_);
|
|
|
|
object("description", description_);
|
|
|
|
if (!parameters_.empty()) {
|
|
|
|
object("parameters", JsonParameters(parameters_));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int error_code_;
|
|
|
|
td::Slice description_;
|
2022-03-16 10:52:34 +01:00
|
|
|
const td::FlatHashMap<td::string, td::unique_ptr<td::VirtuallyJsonable>> ¶meters_;
|
2020-11-03 17:34:10 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class PromiseDeleter {
|
|
|
|
public:
|
2022-06-30 18:59:30 +02:00
|
|
|
explicit PromiseDeleter(td::Promise<td::unique_ptr<Query>> &&promise) : promise_(std::move(promise)) {
|
2020-11-03 17:34:10 +01:00
|
|
|
}
|
|
|
|
PromiseDeleter() = default;
|
|
|
|
PromiseDeleter(const PromiseDeleter &) = delete;
|
|
|
|
PromiseDeleter &operator=(const PromiseDeleter &) = delete;
|
|
|
|
PromiseDeleter(PromiseDeleter &&) = default;
|
|
|
|
PromiseDeleter &operator=(PromiseDeleter &&) = default;
|
|
|
|
void operator()(Query *raw_ptr) {
|
|
|
|
td::unique_ptr<Query> query(raw_ptr); // now I cannot forget to delete this pointer
|
2022-06-30 18:59:30 +02:00
|
|
|
if (promise_) {
|
2020-11-03 17:34:10 +01:00
|
|
|
if (!query->is_ready()) {
|
|
|
|
query->set_retry_after_error(5);
|
|
|
|
}
|
|
|
|
|
|
|
|
promise_.set_value(std::move(query));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
~PromiseDeleter() {
|
2022-06-30 18:59:30 +02:00
|
|
|
CHECK(!promise_);
|
2020-11-03 17:34:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-06-30 18:59:30 +02:00
|
|
|
td::Promise<td::unique_ptr<Query>> promise_;
|
2020-11-03 17:34:10 +01:00
|
|
|
};
|
|
|
|
using PromisedQueryPtr = std::unique_ptr<Query, PromiseDeleter>;
|
|
|
|
|
|
|
|
template <class Jsonable>
|
|
|
|
void answer_query(const Jsonable &result, PromisedQueryPtr query, td::Slice description = td::Slice()) {
|
|
|
|
query->set_ok(td::json_encode<td::BufferSlice>(JsonQueryOk<Jsonable>(result, description)));
|
|
|
|
query.reset(); // send query into promise explicitly
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void fail_query(
|
|
|
|
int http_status_code, td::Slice description, PromisedQueryPtr query,
|
2022-03-16 10:52:34 +01:00
|
|
|
const td::FlatHashMap<td::string, td::unique_ptr<td::VirtuallyJsonable>> ¶meters = empty_parameters) {
|
2020-11-03 17:34:10 +01:00
|
|
|
query->set_error(http_status_code,
|
|
|
|
td::json_encode<td::BufferSlice>(JsonQueryError(http_status_code, description, parameters)));
|
|
|
|
query.reset(); // send query into promise explicitly
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace telegram_bot_api
|