| 29 | // Good description is here - https://github.com/trezor/trezord-go/blob/master/README.md |
| 30 | |
| 31 | static http::ResponseBody sync_http_request(tcp::socket &socket, const http::RequestBody &req) { |
| 32 | std::string str = req.r.to_string(); |
| 33 | boost::system::error_code error; |
| 34 | boost::asio::write(socket, boost::asio::buffer(str.data(), str.size()), error); |
| 35 | if (error) |
| 36 | throw boost::system::system_error(error); |
| 37 | boost::asio::write(socket, boost::asio::buffer(req.body.data(), req.body.size()), error); |
| 38 | if (error) |
| 39 | throw boost::system::system_error(error); |
| 40 | |
| 41 | http::ResponseBody response; |
| 42 | http::ResponseParser parser; |
| 43 | bool receiving_body = false; |
| 44 | |
| 45 | while (!receiving_body || response.body.size() < response.r.content_length) { |
| 46 | boost::array<char, 128> buf; |
| 47 | char *ptr = buf.data(); |
| 48 | |
| 49 | size_t len = socket.read_some(boost::asio::buffer(ptr, buf.size()), error); |
| 50 | if (error == boost::asio::error::eof) |
| 51 | throw boost::system::system_error(error); |
| 52 | if (error) |
| 53 | throw boost::system::system_error(error); |
| 54 | if (!receiving_body) { |
| 55 | auto header_end = parser.parse(response.r, ptr, ptr + len) - ptr; |
| 56 | if (!parser.is_bad() && !parser.is_good()) |
| 57 | continue; |
| 58 | if (!response.r.has_content_length()) |
| 59 | throw std::runtime_error("no content length in reply"); |
| 60 | ptr += header_end; |
| 61 | len -= header_end; |
| 62 | receiving_body = true; |
| 63 | } |
| 64 | if (len + response.body.size() > response.r.content_length) |
| 65 | throw std::runtime_error("too much body"); |
| 66 | response.body += std::string(ptr, len); |
| 67 | } |
| 68 | return response; |
| 69 | } |
| 70 | |
| 71 | static http::ResponseBody trezor_post(tcp::socket &socket, std::string uri, std::string &&body) { |
| 72 | http::RequestBody req; |
no test coverage detected