Projects / httpsbench / http.hpp

git clone https://molodetz.nl/retoor/httpsbench.git

Raw source file available here .

#include <curl/curl.h>
#include <stdio.h>


// Callback function for handling data received from the server
size_t write_callback(void *buffer, size_t size, size_t nmemb, void *userp) {
size_t total_size = size * nmemb;
fwrite(buffer, size, nmemb, (FILE *)userp);
return total_size;
}

void http_get(const char * url){
CURL *curl;
CURLcode res;

curl = curl_easy_init();

if (curl) {
// Set the URL for the request
curl_easy_setopt(curl, CURLOPT_URL, url);

// Use HTTPS
curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);

// Verify the server's SSL certificate
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L); // Enable SSL certificate verification
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L); // Verify that the certificate matches the hostname

// Optional: Specify CA certificate if the default is not sufficient
// curl_easy_setopt(curl, CURLOPT_CAINFO, "/path/to/cacert.pem");

// Set the write callback function to handle the response
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, stdout);

// Perform the request
res = curl_easy_perform(curl);

// Check for errors
if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
}

// Cleanup
curl_easy_cleanup(curl);
}

}