Add debug option to show http traffic

This commit is contained in:
Iwan Timmer
2017-06-11 18:14:32 +02:00
parent b0660e9dd3
commit cbf31be73b
7 changed files with 25 additions and 6 deletions

View File

@@ -20,6 +20,7 @@
#include "http.h"
#include "errors.h"
#include <stdbool.h>
#include <string.h>
#include <curl/curl.h>
@@ -28,6 +29,8 @@ static CURL *curl;
static const char *pCertFile = "./client.pem";
static const char *pKeyFile = "./key.pem";
static bool debug;
static size_t _write_curl(void *contents, size_t size, size_t nmemb, void *userp)
{
size_t realsize = size * nmemb;
@@ -44,8 +47,9 @@ static size_t _write_curl(void *contents, size_t size, size_t nmemb, void *userp
return realsize;
}
int http_init(const char* keyDirectory) {
int http_init(const char* keyDirectory, int logLevel) {
curl = curl_easy_init();
debug = logLevel >= 2;
if (!curl)
return GS_FAILED;
@@ -73,6 +77,9 @@ int http_request(char* url, PHTTP_DATA data) {
curl_easy_setopt(curl, CURLOPT_WRITEDATA, data);
curl_easy_setopt(curl, CURLOPT_URL, url);
if (debug)
printf("Request %s\n", url);
if (data->size > 0) {
free(data->memory);
data->memory = malloc(1);
@@ -89,6 +96,9 @@ int http_request(char* url, PHTTP_DATA data) {
} else if (data->memory == NULL) {
return GS_OUT_OF_MEMORY;
}
if (debug)
printf("Response:\n%s\n\n", data->memory);
return GS_OK;
}