2018-12-31 20:04:05 +01:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
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/HttpQuery.h"
|
|
|
|
|
2018-04-19 14:03:10 +02:00
|
|
|
#include "td/utils/misc.h"
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2018-04-19 14:23:54 +02:00
|
|
|
Slice HttpQuery::get_header(Slice key) const {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto it = std::find_if(headers_.begin(), headers_.end(),
|
|
|
|
[&key](const std::pair<MutableSlice, MutableSlice> &s) { return s.first == key; });
|
|
|
|
return it == headers_.end() ? Slice() : it->second;
|
|
|
|
}
|
|
|
|
|
2018-04-19 14:23:54 +02:00
|
|
|
MutableSlice HttpQuery::get_arg(Slice key) const {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto it = std::find_if(args_.begin(), args_.end(),
|
|
|
|
[&key](const std::pair<MutableSlice, MutableSlice> &s) { return s.first == key; });
|
|
|
|
return it == args_.end() ? MutableSlice() : it->second;
|
|
|
|
}
|
|
|
|
|
2020-06-26 01:24:13 +02:00
|
|
|
vector<std::pair<string, string>> HttpQuery::get_args() const {
|
|
|
|
vector<std::pair<string, string>> res;
|
2018-04-19 14:23:54 +02:00
|
|
|
res.reserve(args_.size());
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto &it : args_) {
|
2018-04-19 14:23:54 +02:00
|
|
|
res.emplace_back(it.first.str(), it.second.str());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-04-19 14:03:10 +02:00
|
|
|
int HttpQuery::get_retry_after() const {
|
2018-04-19 14:23:54 +02:00
|
|
|
auto value = get_header("retry-after");
|
2018-04-19 14:03:10 +02:00
|
|
|
if (value.empty()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
auto r_retry_after = to_integer_safe<int>(value);
|
|
|
|
if (r_retry_after.is_error()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return td::max(0, r_retry_after.ok());
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
StringBuilder &operator<<(StringBuilder &sb, const HttpQuery &q) {
|
|
|
|
switch (q.type_) {
|
2020-06-15 03:23:47 +02:00
|
|
|
case HttpQuery::Type::Empty:
|
2018-12-31 20:04:05 +01:00
|
|
|
sb << "EMPTY";
|
|
|
|
return sb;
|
2020-06-15 03:23:47 +02:00
|
|
|
case HttpQuery::Type::Get:
|
2018-12-31 20:04:05 +01:00
|
|
|
sb << "GET";
|
|
|
|
break;
|
2020-06-15 03:23:47 +02:00
|
|
|
case HttpQuery::Type::Post:
|
2018-12-31 20:04:05 +01:00
|
|
|
sb << "POST";
|
|
|
|
break;
|
2020-06-15 03:23:47 +02:00
|
|
|
case HttpQuery::Type::Response:
|
2018-12-31 20:04:05 +01:00
|
|
|
sb << "RESPONSE";
|
|
|
|
break;
|
|
|
|
}
|
2020-06-15 03:23:47 +02:00
|
|
|
if (q.type_ == HttpQuery::Type::Response) {
|
2018-12-31 20:04:05 +01:00
|
|
|
sb << ":" << q.code_ << ":" << q.reason_;
|
|
|
|
} else {
|
|
|
|
sb << ":" << q.url_path_;
|
|
|
|
for (auto &key_value : q.args_) {
|
|
|
|
sb << ":[" << key_value.first << ":" << key_value.second << "]";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (q.keep_alive_) {
|
|
|
|
sb << ":keep-alive";
|
|
|
|
}
|
|
|
|
sb << "\n";
|
|
|
|
for (auto &key_value : q.headers_) {
|
|
|
|
sb << key_value.first << "=" << key_value.second << "\n";
|
|
|
|
}
|
|
|
|
sb << "BEGIN CONTENT\n";
|
|
|
|
sb << q.content_;
|
|
|
|
sb << "END CONTENT\n";
|
|
|
|
|
|
|
|
return sb;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|