Skip to content

Commit

Permalink
Added get_glob_header_values
Browse files Browse the repository at this point in the history
  • Loading branch information
vladpaiu committed May 8, 2023
1 parent afeb858 commit 5c8b40b
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
41 changes: 41 additions & 0 deletions modules/sipmsgops/doc/sipmsgops_admin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1605,6 +1605,47 @@ list_hdr_add_option("Supported", "optionX");
</example>
</section>

<section id="get_glob_headers_values" xreflabel="get_glob_headers_values()">
<title>
<function moreinfo="none">get_glob_headers_values(hdr_name_glob, hdr_names_avp,hdr_vals_avp)</function>
</title>
<para>
Populates the hdr_names_avp and hdr_vals_avp AVPs with all the header names and values that match the hdr_name_glob pattern.
</para>
<para>Meaning of the parameters is as follows:</para>
<itemizedlist>
<listitem>
<para><emphasis>hdr_name_glob (string)</emphasis> - the glob pattern for matching the header names
</para>
</listitem>
<listitem>
<para><emphasis>hdr_names_avp (var)</emphasis> - the AVP which will get populated with all the header names that match the glob pattern
</para>
</listitem>
<listitem>
<para><emphasis>hdr_vals_avp (var)</emphasis> - the AVP which will get populated with all the header values corresponding to the header names that match the glob pattern
</para>
</listitem>
</itemizedlist>
<para>
<para>
The function returns true if at least 1 header was found that matches the glob pattern and false if no match is found.
</para>
This function can be used from REQUEST_ROUTE, ONREPLY_ROUTE,
FAILURE_ROUTE, BRANCH_ROUTE and LOCAL_ROUTE.
</para>
<example>
<title><function>get_glob_headers_values</function> usage</title>
<programlisting format="linespecific">
...
if (get_glob_headers_values("X-*",$avp(names),$avp(values))) {
xlog("All X- names are $(avp(names)[*]) and X- vals are $(avp(values)[*])\n");
}
...
</programlisting>
</example>
</section>


</section>
<section>
Expand Down
62 changes: 62 additions & 0 deletions modules/sipmsgops/sipmsgops.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
static int remove_hf(struct sip_msg* msg, int_str_t* hf);
static int remove_hf_re(struct sip_msg* msg, regex_t* re);
static int remove_hf_glob(struct sip_msg* msg, str* pattern);
static int get_glob_headers_values(struct sip_msg* msg, str* pattern,pv_spec_t* names, pv_spec_t *vals);
static int remove_hf_match_f(struct sip_msg* msg, void* pattern, int is_regex);
static int is_present_hf(struct sip_msg* msg, void* _match_hf);
static int append_to_reply_f(struct sip_msg* msg, str* key);
Expand Down Expand Up @@ -291,6 +292,12 @@ static const cmd_export_t cmds[]={
{"is_uri_user_e164", (cmd_function)is_uri_user_e164, {
{CMD_PARAM_STR, 0, 0}, {0, 0, 0}},
REQUEST_ROUTE|FAILURE_ROUTE|LOCAL_ROUTE},
{"get_glob_headers_values", (cmd_function)get_glob_headers_values, {
{CMD_PARAM_STR, 0, 0},
{CMD_PARAM_VAR, 0, 0},
{CMD_PARAM_VAR, 0, 0},
{0, 0, 0}},
REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},

{0,0,{{0,0,0}},0}
};
Expand Down Expand Up @@ -1984,3 +1991,58 @@ static int list_hdr_remove_option(struct sip_msg *msg, void *hdr, str *option)
return list_hdr_remove_val(msg, (int_str_t *)hdr, option);
}

static int get_glob_headers_values(struct sip_msg* msg, str* pattern,pv_spec_t* names, pv_spec_t *vals)
{
struct hdr_field *hf;
int cnt=0;
char tmp;
pv_value_t val;

if (names->type != PVT_AVP) {
LM_ERR("AVP needed for names pvar \n");
return -1;
}

if (vals->type != PVT_AVP) {
LM_ERR("AVP needed for vals pvar \n");
return -1;
}


/* we need to be sure we have seen all HFs */
if (parse_headers(msg, HDR_EOH_F, 0)!=0) {
LM_ERR("failed to parse SIP message\n");
return -1;
}
for (hf=msg->headers; hf; hf=hf->next) {
tmp = *(hf->name.s+hf->name.len);
*(hf->name.s+hf->name.len) = 0;
#ifdef FNM_CASEFOLD
if(fnmatch(pattern->s, hf->name.s, FNM_CASEFOLD) !=0 ){
#else
if(fnmatch(pattern->s, hf->name.s, 0) !=0 ){
#endif
*(hf->name.s+hf->name.len) = tmp;
continue;
}

*(hf->name.s+hf->name.len) = tmp;

val.rs = hf->name;
val.flags = PV_VAL_STR;
if (pv_set_value( msg, names, 0, &val)!=0) {
LM_ERR("failed to set the result to script var\n");
return -1;
}

val.rs = hf->body;
val.flags = PV_VAL_STR;
if (pv_set_value( msg, vals, 0, &val)!=0) {
LM_ERR("failed to set the result to script var\n");
return -1;
}

cnt++;
}
return cnt==0 ? -1 : 1;
}

0 comments on commit 5c8b40b

Please sign in to comment.