1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| Client::Client(boost::asio::io_service & io_service, const std::string & server):
io_service(io_service),
server(server),
query(server, "http"),
socket(io_service),
resolver(io_service)
{
Connect();
}
void Client::Connect()
{
/*
Try each endpoint until we successfully establish a connection.
Get a list of endpoints corresponding to the server name.
*/
boost::system::error_code error = boost::asio::error::host_not_found;
tcp::resolver::iterator end;
for(tcp::resolver::iterator it = resolver.resolve(query); error && it != end; ++it) //loop until connection or no more endpoint
{
socket.close();
socket.connect(*it, error);
}
if(error)
{
throw ConnectionException(server, boost::system::system_error(error).what());
}
}
void Client::Disconnect()
{
socket.close();
}
void Client::Send(const std::string & command)
{
//Connect(); //obligé de décommenter cette ligne pour que ça fonctionne
// 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 " << command << " HTTP/1.0\r\n";
request_stream << "Host: " << server << "\r\n";
request_stream << "Accept: */*\r\n";
request_stream << "Connection: Keep-Alive\r\n\r\n";
// Send the request.
boost::asio::write(socket, request);
// Read the response status line.
boost::asio::streambuf response_streambuf;
boost::asio::read_until(socket, response_streambuf, "\r\n");
// Check that response is OK.
std::istream response_stream(&response_streambuf);
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/")
{
throw(InvalidResponseException(server));
}
if(status_code != 200)
{
throw(HttpException(server, status_code));
}
// Read the response headers, which are terminated by a blank line.
boost::asio::read_until(socket, response_streambuf, "\r\n\r\n");
// Read until EOF, writing data to output as we go.
boost::system::error_code error;
while (boost::asio::read(socket, response_streambuf, boost::asio::transfer_at_least(1), error));
if(error != boost::asio::error::eof)
{
throw boost::system::system_error(error);
}
} |
Partager