-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpClient.cpp
More file actions
86 lines (68 loc) · 2.44 KB
/
HttpClient.cpp
File metadata and controls
86 lines (68 loc) · 2.44 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//
// Created by reikooters on 1/11/25.
//
#include "HttpClient.h"
#include <cstring>
#include <curl/curl.h>
#include <fstream>
HttpClient::HttpClient() {
curl_global_init(CURL_GLOBAL_DEFAULT);
}
HttpClient::~HttpClient() {
curl_global_cleanup();
}
// Validate HTTP/HTTPS URI
bool HttpClient::is_valid_http_uri(const std::string &uri) {
CURLU *url = curl_url();
CURLUcode rc = curl_url_set(url, CURLUPART_URL, uri.c_str(), 0);
if (rc == CURLUE_OK) {
char *scheme = nullptr;
if (curl_url_get(url, CURLUPART_SCHEME, &scheme, 0) == CURLUE_OK) {
bool is_http = (strcmp(scheme, "http") == 0 || strcmp(scheme, "https") == 0);
curl_free(scheme);
curl_url_cleanup(url);
return is_http;
}
}
curl_url_cleanup(url);
return false;
}
// Download HTML content to string
bool HttpClient::download_html(const std::string &url, std::string &out_html) {
CURL *curl = curl_easy_init();
if (!curl) return false;
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &out_html);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); // Follow redirects
curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/5.0"); // Set user agent
CURLcode res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
return res == CURLE_OK;
}
// Download image to disk
bool HttpClient::download_image(const std::string &url, const std::string &output_path) {
CURL *curl = curl_easy_init();
if (!curl) return false;
FILE *fp = fopen(output_path.c_str(), "wb");
if (!fp) {
curl_easy_cleanup(curl);
return false;
}
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, nullptr); // Use default
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/5.0");
CURLcode res = curl_easy_perform(curl);
fclose(fp);
curl_easy_cleanup(curl);
return res == CURLE_OK;
}
// Callback for writing HTML to string
size_t HttpClient::write_callback(void *contents, size_t size, size_t nmemb, void *userp) {
size_t total_size = size * nmemb;
std::string *str = static_cast<std::string *>(userp);
str->append(static_cast<char *>(contents), total_size);
return total_size;
}