-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpClient.h
More file actions
69 lines (68 loc) · 2.49 KB
/
HttpClient.h
File metadata and controls
69 lines (68 loc) · 2.49 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
60
61
62
63
64
65
66
67
68
69
#pragma once
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <string>
#include <curl/curl.h>
using namespace std;
class HttpClient {
public:
static size_t WriteCallback(void* contents, size_t size, size_t nmemb, string* data) {
data->append((char*)contents, size * nmemb);
return size * nmemb;
}
static string githubToken;
static string get(const string& url) {
string response;
CURL* curl = curl_easy_init();
if (curl) {
struct curl_slist* headers = NULL;
headers = curl_slist_append(headers, "User-Agent: TopSearch");
if (!githubToken.empty()) {
string auth = "Authorization: token " + githubToken;
headers = curl_slist_append(headers, auth.c_str());
}
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_perform(curl);
curl_easy_cleanup(curl);
curl_slist_free_all(headers);
}
return response;
}
static bool downloadFile(const string& url, const string& filename) {
CURL* curl = curl_easy_init();
if (!curl) return false;
FILE* fp = fopen(filename.c_str(), "wb");
if (!fp) return false;
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
struct curl_slist* headers = NULL;
headers = curl_slist_append(headers, "User-Agent: TopSearch");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
CURLcode res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
curl_slist_free_all(headers);
fclose(fp);
return (res == CURLE_OK);
}
static string normalizeQuery(string q) {
string cleaned;
for (char c : q) {
if (c == ' ') continue;
cleaned += tolower(c);
}
return cleaned;
}
};