in src/common/sync_http_client.cpp [56:150]
bool SyncfetchNsAddr(const Url& url_s, std::string& body) {
bool ret = true;
try {
boost::asio::io_service io_service;
// Get a list of endpoints corresponding to the server name.
tcp::resolver resolver(io_service);
tcp::resolver::query query(url_s.host_, url_s.port_);
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
boost::system::error_code ec = boost::asio::error::would_block;
deadline_timer deadline(io_service);
// TODO hardcode
boost::posix_time::seconds timeout(3);
deadline.expires_from_now(timeout);
// Try each endpoint until we successfully establish a connection.
tcp::socket socket(io_service);
boost::system::error_code deadline_ec;
check_deadline(&deadline, &socket, deadline_ec);
boost::asio::async_connect(socket, endpoint_iterator, boost::lambda::var(ec) = boost::lambda::_1);
do {
io_service.run_one();
} while (ec == boost::asio::error::would_block);
if (ec || !socket.is_open()) {
LOG_ERROR("socket connect failure, connect timeout or connect failure");
return false;
}
// Form the request. We specify the "Connection: close" header so that the
// server will close the socket after transmitting the response. This will
// allow us to treat all data up until the EOF as the content.
boost::asio::streambuf request;
std::ostream request_stream(&request);
request_stream << "GET " << url_s.path_ << " HTTP/1.0\r\n";
request_stream << "Host: " << url_s.host_ << "\r\n";
request_stream << "Accept: */*\r\n";
request_stream << "Connection: close\r\n\r\n";
// Send the request.
boost::asio::write(socket, request);
// Read the response status line. The response streambuf will automatically
// grow to accommodate the entire line. The growth may be limited by passing
// a maximum size to the streambuf constructor.
boost::asio::streambuf response;
boost::asio::read_until(socket, response, "\r\n");
// Check that response is OK.
std::istream response_stream(&response);
std::string http_version;
response_stream >> http_version;
unsigned int status_code;
response_stream >> status_code;
std::string status_message;
std::getline(response_stream, status_message);
if (!response_stream || http_version.substr(0, 5) != "HTTP/") {
LOG_INFO("Invalid response %s\n", status_message.c_str());
return false;
}
if (status_code != 200) {
LOG_INFO("Response returned with status code %d ", status_code);
return false;
}
// Read the response headers, which are terminated by a blank line.
boost::asio::read_until(socket, response, "\r\n\r\n");
// Process the response headers.
std::string header;
while (std::getline(response_stream, header) && header != "\r")
;
// Write whatever content we already have to output.
if (response.size() > 0) {
boost::asio::streambuf::const_buffers_type cbt = response.data();
body.clear();
body.insert(body.begin(), boost::asio::buffers_begin(cbt), boost::asio::buffers_end(cbt));
}
// Read until EOF, writing data to output as we go.
boost::system::error_code error;
while (boost::asio::read(socket, response, boost::asio::transfer_at_least(1), error))
std::cout << &response;
if (error != boost::asio::error::eof)
throw boost::system::system_error(error);
} catch (std::exception& e) {
LOG_ERROR("Exception: %s", e.what());
ret = false;
}
return ret;
}