5 #include "util/http/http_client.h" 7 #include <boost/asio/connect.hpp> 8 #include <boost/beast/core/flat_buffer.hpp> 9 #include <boost/beast/http/dynamic_body.hpp> 10 #include <boost/beast/http/read.hpp> 11 #include <boost/beast/http/string_body.hpp> 12 #include <boost/beast/http/write.hpp> 14 #include "base/logging.h" 15 #include "util/asio/fiber_socket.h" 16 #include "util/asio/yield.h" 21 using fibers_ext::yield;
22 using namespace boost;
24 namespace h2 = beast::http;
26 Client::Client(IoContext* io_context) : io_context_(*io_context) {}
30 system::error_code Client::Connect(StringPiece host, StringPiece service) {
32 new FiberSyncSocket(strings::AsString(host), strings::AsString(service), &io_context_));
34 return socket_->ClientWaitToConnect(connect_timeout_ms_);
37 ::boost::system::error_code Client::Send(Verb verb, StringPiece url, StringPiece body,
40 h2::request<h2::string_body> req{verb, boost::string_view(url.data(), url.size()), 11};
43 for (
const auto& k_v : headers_) {
44 req.set(k_v.first, k_v.second);
46 req.body().assign(body.begin(), body.end());
47 req.prepare_payload();
49 system::error_code ec;
52 h2::write(*socket_, req, ec);
54 VLOG(1) <<
"Error " << ec;
59 beast::flat_buffer buffer;
61 h2::read(*socket_, buffer, *response, ec);
62 VLOG(2) <<
"Resp: " << *response;
67 void Client::Shutdown() {
69 system::error_code ec;
70 socket_->Shutdown(ec);
75 bool Client::IsConnected()
const {
76 return socket_ && socket_->is_open() && !socket_->status();