2013-09-15 17:56:47 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <boost/asio.hpp>
|
|
|
|
|
|
|
|
using boost::asio::ip::tcp;
|
|
|
|
|
|
|
|
//Based on: http://www.boost.org/doc/libs/1_51_0/doc/html/boost_asio/example/http/client/async_client.cpp
|
|
|
|
|
|
|
|
/* Usage:
|
|
|
|
* HttpReq myRequest("www.google.com", "/index.html");
|
|
|
|
* //for blocking behavior: while(myRequest.status() == HttpReq::REQ_IN_PROGRESS);
|
|
|
|
* //for non-blocking behavior: check if(myRequest.status() != HttpReq::REQ_IN_PROGRESS) in some sort of update method
|
|
|
|
*
|
|
|
|
* //once one of those completes, the request is ready
|
|
|
|
* if(myRequest.status() != REQ_SUCCESS)
|
|
|
|
* {
|
|
|
|
* //an error occured
|
|
|
|
* LOG(LogError) << "HTTP request error - " << myRequest.getErrorMessage();
|
|
|
|
* return;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* std::string content = myRequest.getContent();
|
|
|
|
* //process contents...
|
|
|
|
*/
|
|
|
|
|
|
|
|
class HttpReq
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
HttpReq(const std::string& server, const std::string& path);
|
2013-09-20 23:55:05 +00:00
|
|
|
HttpReq(const std::string& url);
|
|
|
|
|
|
|
|
~HttpReq();
|
2013-09-15 17:56:47 +00:00
|
|
|
|
|
|
|
enum Status
|
|
|
|
{
|
|
|
|
REQ_IN_PROGRESS, //request is in progress
|
|
|
|
REQ_SUCCESS, //request completed successfully, get it with getContent()
|
|
|
|
|
|
|
|
REQ_IO_ERROR, //some boost::asio error happened, get it with getErrorMsg()
|
|
|
|
REQ_BAD_STATUS_CODE, //some invalid HTTP response status code happened (non-200)
|
|
|
|
REQ_INVALID_RESPONSE //the HTTP response was invalid
|
|
|
|
};
|
|
|
|
|
|
|
|
Status status(); //process any received data and return the status afterwards
|
|
|
|
|
|
|
|
std::string getErrorMsg();
|
|
|
|
|
|
|
|
std::string getContent();
|
|
|
|
|
2013-09-24 07:02:14 +00:00
|
|
|
static std::string urlEncode(const std::string &s);
|
2013-10-10 00:50:42 +00:00
|
|
|
static bool isUrl(const std::string& s);
|
2013-09-24 07:02:14 +00:00
|
|
|
|
2013-09-15 17:56:47 +00:00
|
|
|
private:
|
|
|
|
static boost::asio::io_service io_service;
|
|
|
|
|
2013-09-20 23:55:05 +00:00
|
|
|
void start(const std::string& server, const std::string& path);
|
2013-09-15 17:56:47 +00:00
|
|
|
void handleResolve(const boost::system::error_code& err, tcp::resolver::iterator endpoint_iterator);
|
|
|
|
void handleConnect(const boost::system::error_code& err);
|
|
|
|
void handleWriteRequest(const boost::system::error_code& err);
|
|
|
|
void handleReadStatusLine(const boost::system::error_code& err);
|
|
|
|
void handleReadHeaders(const boost::system::error_code& err);
|
|
|
|
void handleReadContent(const boost::system::error_code& err);
|
|
|
|
|
|
|
|
void onError(const boost::system::error_code& error);
|
|
|
|
|
|
|
|
tcp::resolver mResolver;
|
|
|
|
tcp::socket mSocket;
|
|
|
|
boost::asio::streambuf mRequest;
|
|
|
|
boost::asio::streambuf mResponse;
|
|
|
|
|
|
|
|
Status mStatus;
|
|
|
|
std::stringstream mContent;
|
|
|
|
unsigned int mResponseStatusCode;
|
|
|
|
boost::system::error_code mError;
|
|
|
|
};
|