Module: kamailio
Branch: master
Commit: 90d78b7b2bfc1899c1b64c2db8a353d43df68218
URL:
https://github.com/kamailio/kamailio/commit/90d78b7b2bfc1899c1b64c2db8a353d…
Author: Daniel-Constantin Mierla <miconda(a)gmail.com>
Committer: Daniel-Constantin Mierla <miconda(a)gmail.com>
Date: 2024-04-07T10:15:41+02:00
http_clinet: added variable $httprhdr(name) - get response header value
---
Modified: src/modules/http_client/functions.c
Modified: src/modules/http_client/functions.h
Modified: src/modules/http_client/http_client.c
---
Diff:
https://github.com/kamailio/kamailio/commit/90d78b7b2bfc1899c1b64c2db8a353d…
Patch:
https://github.com/kamailio/kamailio/commit/90d78b7b2bfc1899c1b64c2db8a353d…
---
diff --git a/src/modules/http_client/functions.c b/src/modules/http_client/functions.c
index e910b514bbf..0004eb3ddc1 100644
--- a/src/modules/http_client/functions.c
+++ b/src/modules/http_client/functions.c
@@ -39,6 +39,7 @@
#include "../../core/pvar.h"
#include "../../core/route_struct.h"
#include "../../core/ut.h"
+#include "../../core/trim.h"
#include "../../core/mem/mem.h"
#include "../../core/parser/msg_parser.h"
@@ -127,6 +128,46 @@ void http_client_response_headers_reset(void)
_http_client_response_headers = NULL;
}
+/**
+ *
+ */
+int http_client_response_headers_get(str *hname, str *hbody)
+{
+ httpc_hdr_t *it;
+ char *p;
+
+ if(_http_client_response_headers == NULL) {
+ return -1;
+ }
+ if(hname == NULL || hname->len <= 0 || hbody == NULL) {
+ return -1;
+ }
+ for(it = _http_client_response_headers; it != NULL; it = it->next) {
+ if(it->name.len == 0 && it->hbuf.s[0] != ' ' &&
it->hbuf.s[0] != '\t'
+ && it->hbuf.s[0] != '\r' && it->hbuf.s[0] !=
'\n') {
+ /* parsing */
+ p = strchr(it->hbuf.s, ':');
+ if(p == NULL) {
+ continue;
+ }
+ it->name.s = it->hbuf.s;
+ it->name.len = p - it->name.s;
+ trim(&it->name);
+ p++;
+ it->body.s = p;
+ it->body.len = it->hbuf.s + it->hbuf.len - it->body.s;
+ trim(&it->body);
+ }
+ if(it->name.len == hname->len
+ && strncasecmp(it->name.s, hname->s, hname->len) == 0) {
+ hbody->s = it->body.s;
+ hbody->len = it->body.len;
+ return 0;
+ }
+ }
+ return -1;
+}
+
/*
* curl write function that saves received data as zero terminated
* to stream. Returns the amount of data taken care of.
diff --git a/src/modules/http_client/functions.h b/src/modules/http_client/functions.h
index d0c66c64e66..30c326bd0f8 100644
--- a/src/modules/http_client/functions.h
+++ b/src/modules/http_client/functions.h
@@ -78,4 +78,7 @@ int http_client_request_c(sip_msg_t *_m, char *_url, str *_dst, char
*_body,
char *http_get_content_type(const str *connection);
+int http_client_response_headers_get(str *hname, str *hbody);
+void http_client_response_headers_reset(void);
+
#endif /* CURL_FUNCTIONS_H */
diff --git a/src/modules/http_client/http_client.c
b/src/modules/http_client/http_client.c
index d640d772ad3..4bea581d50f 100644
--- a/src/modules/http_client/http_client.c
+++ b/src/modules/http_client/http_client.c
@@ -164,6 +164,8 @@ static int curl_con_param(modparam_t type, void *val);
static int pv_parse_curlerror(pv_spec_p sp, str *in);
static int pv_get_curlerror(
struct sip_msg *msg, pv_param_t *param, pv_value_t *res);
+static int pv_parse_httprhdr(pv_spec_p sp, str *in);
+static int pv_get_httprhdr(sip_msg_t *msg, pv_param_t *param, pv_value_t *res);
/* clang-format off */
/* Exported functions */
@@ -237,6 +239,8 @@ static param_export_t params[] = {
static pv_export_t mod_pvs[] = {
{{"curlerror", (sizeof("curlerror")-1)}, /* Curl error codes */
PVT_OTHER, pv_get_curlerror, 0, pv_parse_curlerror, 0, 0, 0},
+ {{"httprhdr", (sizeof("httprhdr")-1)}, /* HTTP response header */
+ PVT_OTHER, pv_get_httprhdr, 0, pv_parse_httprhdr, 0, 0, 0},
{{0, 0}, 0, 0, 0, 0, 0, 0, 0}
};
@@ -1193,6 +1197,35 @@ static int pv_get_curlerror(
return pv_get_strval(msg, param, res, &curlerr);
}
+/**
+ *
+ */
+static int pv_parse_httprhdr(pv_spec_p sp, str *in)
+{
+ if(sp == NULL || in == NULL || in->len <= 0)
+ return -1;
+
+ sp->pvp.pvn.type = PV_NAME_INTSTR;
+ sp->pvp.pvn.u.isname.type = AVP_VAL_STR;
+ sp->pvp.pvn.u.isname.name.s = *in;
+
+ return 0;
+}
+
+/**
+ *
+ */
+static int pv_get_httprhdr(sip_msg_t *msg, pv_param_t *param, pv_value_t *res)
+{
+ str hbody = STR_NULL;
+
+ if(http_client_response_headers_get(¶m->pvn.u.isname.name.s, &hbody)
+ < 0) {
+ return pv_get_null(msg, param, res);
+ }
+
+ return pv_get_strval(msg, param, res, &hbody);
+}
/*
* Fix curl_get_redirect params: connection(string/pvar) url (string that may contain
pvars) and