Created
December 6, 2017 21:44
-
-
Save fernandoc1/b2182b911ca591c76471b472e80debbe to your computer and use it in GitHub Desktop.
This code is to perform http request that requires Digest authentication.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <curl/curl.h> | |
#include <vector> | |
#include <chrono> | |
#include <thread> | |
#include <opencv2/core/core.hpp> | |
#include <opencv2/highgui/highgui.hpp> | |
class CurlGlobalObject | |
{ | |
public: | |
CurlGlobalObject() | |
{ | |
curl_global_init(CURL_GLOBAL_ALL); | |
std::cout << "Initialized Curl" << std::endl; | |
} | |
~CurlGlobalObject() | |
{ | |
curl_global_cleanup(); | |
std::cout << "Curl cleanup" << std::endl; | |
} | |
}; | |
CurlGlobalObject curlGlobalObject; | |
class CurlHttpGet | |
{ | |
public: | |
CURL* curlHandle; | |
std::vector<uint8_t> data; | |
std::string url; | |
std::string username; | |
std::string password; | |
bool verbose; | |
static size_t writeDataCallback(void* contents, size_t size, size_t nmemb, void* userp) | |
{ | |
std::vector<uint8_t>* data = reinterpret_cast<std::vector<uint8_t>*>(userp); | |
size_t realsize = size * nmemb; | |
uint8_t* beginning = (uint8_t*)contents; | |
uint8_t* ending = &(((uint8_t*)contents)[realsize]); | |
std::vector<uint8_t> newData(beginning, ending); | |
data->insert(data->end(), newData.begin(), newData.end()); | |
return realsize; | |
} | |
CurlHttpGet(std::string url, std::string username, std::string password, bool verbose) | |
: url(url) | |
, username(username) | |
, password(password) | |
, verbose(false) | |
{ | |
this->curlHandle = curl_easy_init(); | |
} | |
~CurlHttpGet() | |
{ | |
curl_easy_cleanup(this->curlHandle); | |
} | |
void setOptions() | |
{ | |
std::string userpwd(this->username + ":" + this->password); | |
curl_easy_setopt(this->curlHandle, CURLOPT_URL, this->url.c_str()); | |
curl_easy_setopt(this->curlHandle, CURLOPT_WRITEFUNCTION, CurlHttpGet::writeDataCallback); | |
curl_easy_setopt(this->curlHandle, CURLOPT_WRITEDATA, &this->data); | |
curl_easy_setopt(this->curlHandle, CURLOPT_USERAGENT, "libcurl-agent/1.0"); | |
curl_easy_setopt(this->curlHandle, CURLOPT_USERPWD, userpwd.c_str()); | |
curl_easy_setopt(this->curlHandle, CURLOPT_FOLLOWLOCATION, 1L); | |
curl_easy_setopt(this->curlHandle, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); | |
curl_easy_setopt(this->curlHandle, CURLOPT_HTTPGET, 1L); | |
if(this->verbose) | |
{ | |
curl_easy_setopt(this->curlHandle, CURLOPT_VERBOSE, 1L); | |
} | |
} | |
void perform() | |
{ | |
this->setOptions(); | |
CURLcode res = curl_easy_perform(this->curlHandle); | |
if(res != CURLE_OK) | |
{ | |
std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl; | |
} | |
else | |
{ | |
std::cout << "Received: " << data.size() << std::endl;; | |
} | |
} | |
}; | |
int main(void) | |
{ | |
while(true) | |
{ | |
CurlHttpGet httpGet("http://192.168.0.170/axis-cgi/jpg/image.cgi", "root", "pass", false); | |
httpGet.perform(); | |
cv::Mat image = cv::imdecode(httpGet.data, 1); | |
cv::imshow("DISPLAY", image); | |
cv::waitKey(1); | |
} | |
//FILE* outputFile = fopen("output.jpg", "wb"); | |
//fwrite(httpGet.data.data(), sizeof(uint8_t), httpGet.data.size(), outputFile); | |
//fclose(outputFile); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment