2013-09-15 17:56:47 +00:00
|
|
|
#pragma once
|
2017-10-31 17:12:50 +00:00
|
|
|
#ifndef ES_CORE_HTTP_REQ_H
|
|
|
|
#define ES_CORE_HTTP_REQ_H
|
2013-09-15 17:56:47 +00:00
|
|
|
|
2013-10-10 18:11:01 +00:00
|
|
|
#include <curl/curl.h>
|
|
|
|
#include <map>
|
2017-11-01 22:21:10 +00:00
|
|
|
#include <sstream>
|
2013-09-15 17:56:47 +00:00
|
|
|
|
|
|
|
/* 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:
|
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();
|
|
|
|
|
2014-03-19 00:55:37 +00:00
|
|
|
std::string getContent() const; // mStatus must be REQ_SUCCESS
|
2013-09-15 17:56:47 +00:00
|
|
|
|
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:
|
2013-10-10 18:11:01 +00:00
|
|
|
static size_t write_content(void* buff, size_t size, size_t nmemb, void* req_ptr);
|
|
|
|
//static int update_progress(void* req_ptr, double dlTotal, double dlNow, double ulTotal, double ulNow);
|
2013-09-15 17:56:47 +00:00
|
|
|
|
2013-10-10 18:11:01 +00:00
|
|
|
//god dammit libcurl why can't you have some way to check the status of an individual handle
|
|
|
|
//why do I have to handle ALL messages at once
|
|
|
|
static std::map<CURL*, HttpReq*> s_requests;
|
2013-09-15 17:56:47 +00:00
|
|
|
|
2013-10-10 18:11:01 +00:00
|
|
|
static CURLM* s_multi_handle;
|
2013-09-15 17:56:47 +00:00
|
|
|
|
2013-10-10 18:11:01 +00:00
|
|
|
void onError(const char* msg);
|
|
|
|
|
|
|
|
CURL* mHandle;
|
2013-09-15 17:56:47 +00:00
|
|
|
|
|
|
|
Status mStatus;
|
2013-10-10 18:11:01 +00:00
|
|
|
|
2013-09-15 17:56:47 +00:00
|
|
|
std::stringstream mContent;
|
2013-10-10 18:11:01 +00:00
|
|
|
std::string mErrorMsg;
|
2013-09-15 17:56:47 +00:00
|
|
|
};
|
2017-10-31 17:12:50 +00:00
|
|
|
|
|
|
|
#endif // ES_CORE_HTTP_REQ_H
|