2018-12-31 20:04:05 +01:00
|
|
|
//
|
2018-01-02 14:42:31 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018
|
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;
|
|
|
|
}
|
|
|
|
|
2018-04-19 14:23:54 +02:00
|
|
|
std::vector<std::pair<string, string>> HttpQuery::get_args() const {
|
2018-12-31 20:04:05 +01:00
|
|
|
std::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_) {
|
|
|
|
case HttpQuery::Type::EMPTY:
|
|
|
|
sb << "EMPTY";
|
|
|
|
return sb;
|
|
|
|
case HttpQuery::Type::GET:
|
|
|
|
sb << "GET";
|
|
|
|
break;
|
|
|
|
case HttpQuery::Type::POST:
|
|
|
|
sb << "POST";
|
|
|
|
break;
|
|
|
|
case HttpQuery::Type::RESPONSE:
|
|
|
|
sb << "RESPONSE";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (q.type_ == HttpQuery::Type::RESPONSE) {
|
|
|
|
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
|