-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp-get.cpp
More file actions
59 lines (46 loc) · 1.55 KB
/
http-get.cpp
File metadata and controls
59 lines (46 loc) · 1.55 KB
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
#include <curl/curl.h>
#include <curl/easy.h>
#include <sstream>
#include <string>
struct http_downloader_t {
http_downloader_t();
~http_downloader_t();
std::string download(const std::string& url);
void* curl;
};
std::size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream) {
std::string data((const char*)ptr, (size_t)size * nmemb);
*((std::stringstream*)stream) << data;
return size * nmemb;
}
http_downloader_t::http_downloader_t() {
curl = curl_easy_init();
}
http_downloader_t::~http_downloader_t() {
curl_easy_cleanup(curl);
}
std::string http_downloader_t::download(const std::string& url) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
/* example.com is redirected, so we tell libcurl to follow redirection */
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); //Prevent "longjmp causes uninitialized stack frame" bug
curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "deflate");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
std::stringstream out;
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &out);
/* Perform the request, res will get the return code */
CURLcode res = curl_easy_perform(curl);
/* Check for errors */
if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
return "";
}
return out.str();
}
std::string http_get(const std::string& url)
{
http_downloader_t d;
return d.download(url);
}