// // HttpReq.cpp // // HTTP request functions. // Used by Scraper, GamesDBJSONScraper, GamesDBJSONScraperResources and // ScreenScraper to download game information and media files. // #include "HttpReq.h" #include "utils/FileSystemUtil.h" #include "Log.h" #include CURLM* HttpReq::s_multi_handle = curl_multi_init(); std::map HttpReq::s_requests; std::string HttpReq::urlEncode(const std::string &s) { const std::string unreserved = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~"; std::string escaped=""; for (size_t i=0; imsg == CURLMSG_DONE) { HttpReq* req = s_requests[msg->easy_handle]; if (req == NULL) { LOG(LogError) << "Cannot find easy handle!"; continue; } if (msg->data.result == CURLE_OK) { req->mStatus = REQ_SUCCESS; } else { req->mStatus = REQ_IO_ERROR; req->onError(curl_easy_strerror(msg->data.result)); } } } } return mStatus; } std::string HttpReq::getContent() const { assert(mStatus == REQ_SUCCESS); return mContent.str(); } void HttpReq::onError(const char* msg) { mErrorMsg = msg; } std::string HttpReq::getErrorMsg() { return mErrorMsg; } // Used as a curl callback. // size = size of an element, nmemb = number of elements. // Return value is number of elements successfully read. size_t HttpReq::write_content(void* buff, size_t size, size_t nmemb, void* req_ptr) { std::stringstream& ss = ((HttpReq*)req_ptr)->mContent; ss.write((char*)buff, size * nmemb); return nmemb; } // Used as a curl callback. //int HttpReq::update_progress(void* req_ptr, double dlTotal, // double dlNow, double ulTotal, double ulNow) //{ //}