/
Boxapp
/
yadisk-helper
Обзор
Документация
Войти
/
Boxapp
/
yadisk-helper
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
http.cpp
106 строк
3 KB
DebianCougar
test build 2026.02-beta
30 янв 2026, 21:25
30 янв 2026, 21:25
eb3551d
Код
Авторство
О чём код?
#include <boost/asio/ssl/stream.hpp> // NOTE compile with flags -lssl -lcrypto #include <boost/beast/core.hpp> #include <boost/beast/http.hpp> #include <boost/beast/http/status.hpp> #include <boost/beast/version.hpp> #include <iostream> #include <string> #include "http.hpp" using boost::asio::io_context; using boost::asio::ip::tcp; using boost::asio::ssl::context; using boost::asio::ssl::stream; using boost::asio::ssl::stream_base; using boost::beast::error_code; using boost::beast::flat_buffer; using boost::beast::get_lowest_layer; using boost::beast::http::field; using boost::beast::http::request; using boost::beast::http::response; using boost::beast::http::status; using boost::beast::http::string_body; using boost::beast::http::verb; using std::cerr; using std::endl; using std::to_string; static string escapeUrlChar(unsigned char c) { string res; if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) res.push_back(c); else { res.push_back('%'); short a = c / 16, b = c % 16; res.push_back(a < 10 ? a + '0' : a - 10 + 'A'); res.push_back(b < 10 ? b + '0' : b - 10 + 'A'); } return res; } static string escapeUrl(const string &url) { string res; for (char c : url) res.append(escapeUrlChar(c)); return res; } string queryUrl(const string &target, const string &token) { // Our test endpoint for testing the json const string host = "cloud-api.yandex.net"; const string port = "443"; // The io_context is required for all I/O io_context ioc; // These objects perform our I/O tcp::resolver resolver(ioc); context ctx(context::tlsv13_client); stream<tcp::socket> sock(ioc, ctx); // Look up the domain name auto const results = resolver.resolve(host, port); // Make the connection on the IP address we get from a lookup connect(get_lowest_layer(sock), results); sock.handshake(stream_base::client); // Set up an HTTP POST request message request<string_body> req{verb::get, target, 11}; req.set(field::host, host); req.set(field::user_agent, BOOST_BEAST_VERSION_STRING); req.set(field::accept, "application/json"); req.set(field::authorization, "OAuth "+token); req.prepare_payload(); // Send the HTTP request to the remote host write(sock, req); // This buffer is used for reading and must be persisted flat_buffer buffer; // Get the response response<string_body> res; // Receive the HTTP response read(sock, buffer, res); string reply = res.body(); if (res.result() != status::ok) cerr << "[WARNING] response status " << res.result() << " is not OK" << endl; // Gracefully close the socket error_code ec; if (sock.shutdown(ec)) cerr << "Error!" << endl; return reply; } string queryCount(const string &dir, const string &token) { return queryUrl("/v1/disk/resources?path="+escapeUrl(dir)+"&fields="+escapeUrl("path,type,name,_embedded.total"), token); } string queryItems(const string &dir, size_t count, const string &token) { return queryUrl("/v1/disk/resources?path="+escapeUrl(dir)+"&fields="+escapeUrl("_embedded.items.type,_embedded.items.name")+"&limit="+to_string(count), token); }