Skip to content

Commit

Permalink
http_client: option to store http response headers
Browse files Browse the repository at this point in the history
- new modparam response_headers has to be set to 1
  • Loading branch information
miconda committed Apr 7, 2024
1 parent b453772 commit 8f36497
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
72 changes: 72 additions & 0 deletions src/modules/http_client/functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,58 @@ typedef struct
} curl_query_t;


/**
*
*/
typedef struct httpc_hdr
{
str hbuf;
str name;
str body;
struct httpc_hdr *next;
} httpc_hdr_t;

/**
*
*/
httpc_hdr_t *_http_client_response_headers = NULL;

/**
*
*/
httpc_hdr_t *httpc_hdr_block_add(httpc_hdr_t **head, char *s, int len)
{
httpc_hdr_t *nv;
nv = pkg_mallocxz(sizeof(httpc_hdr_t) + (len + 1) * sizeof(char));
if(!nv) {
PKG_MEM_ERROR;
return 0;
}
nv->hbuf.s = (char *)nv + sizeof(httpc_hdr_t);
memcpy(nv->hbuf.s, s, len);
nv->hbuf.len = len;
nv->next = *head;
*head = nv;

return nv;
}

/**
*
*/
void http_client_response_headers_reset(void)
{
httpc_hdr_t *it0;
httpc_hdr_t *it1;
it0 = _http_client_response_headers;
while(it0 != NULL) {
it1 = it0->next;
pkg_free(it0);
it0 = it1;
}
_http_client_response_headers = NULL;
}

/*
* curl write function that saves received data as zero terminated
* to stream. Returns the amount of data taken care of.
Expand Down Expand Up @@ -112,6 +164,20 @@ size_t write_function(void *ptr, size_t size, size_t nmemb, void *stream_ptr)
}


size_t http_client_response_header_cb(
char *b, size_t size, size_t nitems, void *userdata)
{
size_t numbytes;

numbytes = size * nitems;
LM_DBG("http response header [%.*s]\n", (int)numbytes, b);

httpc_hdr_block_add(&_http_client_response_headers, b, (int)numbytes);

return numbytes;
;
}

/*! Send query to server, optionally post data.
*/
static int curL_request_url(struct sip_msg *_m, const char *_met,
Expand Down Expand Up @@ -288,6 +354,12 @@ static int curL_request_url(struct sip_msg *_m, const char *_met,
res |= curl_easy_setopt(curl, CURLOPT_INTERFACE, params->netinterface);
}

if(http_client_response_headers_param != 0) {
http_client_response_headers_reset();
res |= curl_easy_setopt(
curl, CURLOPT_HEADERFUNCTION, http_client_response_header_cb);
}

res |= curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_function);
res |= curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)(&stream));

Expand Down
3 changes: 3 additions & 0 deletions src/modules/http_client/http_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ unsigned int default_query_result = 1;
/*!< Default download size for result of query function. 0=disabled (no limit) */
unsigned int default_query_maxdatasize = 0;

int http_client_response_headers_param = 0;

str http_client_config_file = STR_NULL;

static curl_version_info_data *curl_info;
Expand Down Expand Up @@ -224,6 +226,7 @@ static param_export_t params[] = {
{"query_result", PARAM_INT, &default_query_result },
{"query_maxdatasize", PARAM_INT, &default_query_maxdatasize },
{"netinterface", PARAM_STRING, &default_netinterface },
{"response_headers", PARAM_INT, &http_client_response_headers_param },
{0, 0, 0}
};

Expand Down
2 changes: 2 additions & 0 deletions src/modules/http_client/http_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ extern counter_handle_t connfail; /* Failed Connection attempts */

extern char *default_netinterface;

extern int http_client_response_headers_param; /* store http response headers */

/* Curl stream object */
typedef struct
{
Expand Down

0 comments on commit 8f36497

Please sign in to comment.