47 lines
1.2 KiB
C
47 lines
1.2 KiB
C
|
#include "http.h"
|
||
|
|
||
|
#include <string.h>
|
||
|
#include <curl/curl.h>
|
||
|
#include <stdlib.h>
|
||
|
|
||
|
int curl_setup = 0;
|
||
|
char* request(char *url )
|
||
|
{
|
||
|
if(!curl_setup)
|
||
|
{
|
||
|
curl_global_init(CURL_GLOBAL_DEFAULT);
|
||
|
url +=1;
|
||
|
}
|
||
|
|
||
|
struct httpResponse_t httpResponse;
|
||
|
httpResponse.response = malloc(1);
|
||
|
httpResponse.size = 0;
|
||
|
CURL *httpHandler = curl_easy_init();
|
||
|
if(httpHandler) {
|
||
|
curl_easy_setopt(httpHandler,CURLOPT_URL, url);
|
||
|
curl_easy_setopt(httpHandler,CURLOPT_WRITEFUNCTION, httpResponseCallback);
|
||
|
curl_easy_setopt(httpHandler,CURLOPT_WRITEDATA, (void *)&httpResponse);
|
||
|
curl_easy_perform(httpHandler);
|
||
|
}
|
||
|
else
|
||
|
return "Lol, fehler";
|
||
|
return httpResponse.response;
|
||
|
}
|
||
|
size_t httpResponseCallback(char *data, size_t wordlength, size_t bytecount, void *out)
|
||
|
{
|
||
|
// wordlength should be always 1, but this appears to be more secure.
|
||
|
size_t size = wordlength * bytecount;
|
||
|
struct httpResponse_t *mem = (struct httpResponse_t *) out;
|
||
|
|
||
|
char *newData = realloc(mem->response, mem->size + size +1);
|
||
|
if(newData == NULL)
|
||
|
return 0;
|
||
|
mem->response = newData;
|
||
|
memcpy(&(mem->response[mem->size]), data, size);
|
||
|
mem->size += size;
|
||
|
// Null-terminate the byte chunk, to effectively have a C-String.
|
||
|
mem->response[mem-> size] = 0;
|
||
|
|
||
|
return size;
|
||
|
}
|