I am trying to make a very simple program that get a user request (console) and send this request to a website in the headers (libcurl) and then get the response headers.
So I have 2 classes (in fact I got a lot of classes but for the problem here I will simplify)
My main.cpp get the request and send it to my service.
So here is my service Fuzzer.h :
#ifndef FUZZER_H
#define FUZZER_H
#include <string>
using namespace std;
namespace furez
{
class Fuzzer {
private:
string url;
string response;
static size_t writeH_static(void *, size_t, size_t, void *);
public:
Fuzzer(string);
virtual ~Fuzzer();
string sendRequest(string);
size_t writeH(void *, size_t, size_t);
};
}
#endif
and here is my Fuzzer.cpp:
#include <string>
#include <curl/curl.h>
#include <iostream>
#include "Fuzzer.h"
namespace furez
{
Fuzzer::Fuzzer(std::string u)
{
this->url = u;
}
Fuzzer::~Fuzzer(){}
string Fuzzer::sendRequest(std::string request)
{
CURL *curl;
CURLcode res;
static const char *headerfilename = "head.out";
static const char *bodyfilename = "body.out";
curl = curl_easy_init();
if (curl) {
struct curl_slist *chunk = NULL;
chunk = curl_slist_append(chunk, "Accept:");
chunk = curl_slist_append(chunk, "Another: yes");
chunk = curl_slist_append(chunk, "Host: example.com");
chunk = curl_slist_append(chunk, "X-silly-header;");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &Fuzzer::writeH);
Fuzzer instance(request);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &instance);
curl_easy_setopt(curl, CURLOPT_URL, "localhost");
res = curl_easy_perform(curl);
/* Check for errors */
if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
}
curl_easy_cleanup(curl);
curl_slist_free_all(chunk);
}
return request;
}
size_t Fuzzer::writeH_static(void *ptr, size_t size, size_t nmemb, void *stream)
{
return ((Fuzzer*)stream)->writeH(ptr, size, nmemb);
}
size_t Fuzzer::writeH(void *ptr, size_t size, size_t nmemb)
{
//std::string str = (CHAR *)ptr;
//std::string str2("Server:");
//if (str.find(str2) != string::npos) {
//this->response = str;
//}
size_t realsize = size * nmemb;
return realsize;
}
}
But when I try to execute this I got an Memory error. Because of this:
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &Fuzzer::writeH);
I am using libcurl for my project. I know that it's a C library maybe it's the problem?
In order to help me I followed theses instructions: libcurl callbacks w/c++ class members
and this one: curl WRITEFUNCTION and classes I'm on Windows 7 and got Visual Studio 2013.
(PS: When I put this code without &Fuzzer::writeH and static function inside main.cpp it works without any problem.)