Module: kamailio
Branch: master
Commit: 8f364976a81358de39cb5d675b3703f3f65a0d59
URL:
https://github.com/kamailio/kamailio/commit/8f364976a81358de39cb5d675b3703f…
Author: Daniel-Constantin Mierla <miconda(a)gmail.com>
Committer: Daniel-Constantin Mierla <miconda(a)gmail.com>
Date: 2024-04-07T09:20:33+02:00
http_client: option to store http response headers
- new modparam response_headers has to be set to 1
---
Modified: src/modules/http_client/functions.c
Modified: src/modules/http_client/http_client.c
Modified: src/modules/http_client/http_client.h
---
Diff:
https://github.com/kamailio/kamailio/commit/8f364976a81358de39cb5d675b3703f…
Patch:
https://github.com/kamailio/kamailio/commit/8f364976a81358de39cb5d675b3703f…
---
diff --git a/src/modules/http_client/functions.c b/src/modules/http_client/functions.c
index df6b8233b51..e910b514bbf 100644
--- a/src/modules/http_client/functions.c
+++ b/src/modules/http_client/functions.c
@@ -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.
@@ -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,
@@ -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));
diff --git a/src/modules/http_client/http_client.c
b/src/modules/http_client/http_client.c
index 91573425990..d640d772ad3 100644
--- a/src/modules/http_client/http_client.c
+++ b/src/modules/http_client/http_client.c
@@ -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;
@@ -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}
};
diff --git a/src/modules/http_client/http_client.h
b/src/modules/http_client/http_client.h
index c56f411df1f..42ee1e914f8 100644
--- a/src/modules/http_client/http_client.h
+++ b/src/modules/http_client/http_client.h
@@ -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
{