[sr-dev] git:master:d7972d83: textops: new function remove_hf_exp(match, skip)

Daniel-Constantin Mierla miconda at gmail.com
Fri Jul 7 14:10:42 CEST 2017


Module: kamailio
Branch: master
Commit: d7972d83b3f12f64d4f70d8e6eacb88ec34648f1
URL: https://github.com/kamailio/kamailio/commit/d7972d83b3f12f64d4f70d8e6eacb88ec34648f1

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date: 2017-07-07T14:03:52+02:00

textops: new function remove_hf_exp(match, skip)

- remove header fields that do not match regex ''skip', but match regex
  'match'

---

Modified: src/modules/textops/textops.c

---

Diff:  https://github.com/kamailio/kamailio/commit/d7972d83b3f12f64d4f70d8e6eacb88ec34648f1.diff
Patch: https://github.com/kamailio/kamailio/commit/d7972d83b3f12f64d4f70d8e6eacb88ec34648f1.patch

---

diff --git a/src/modules/textops/textops.c b/src/modules/textops/textops.c
index cd116a8fe9..cd32f0074a 100644
--- a/src/modules/textops/textops.c
+++ b/src/modules/textops/textops.c
@@ -129,6 +129,7 @@ static int cmp_str_f(struct sip_msg *msg, char *str1, char *str2 );
 static int cmp_istr_f(struct sip_msg *msg, char *str1, char *str2 );
 static int starts_with_f(struct sip_msg *msg, char *str1, char *str2 );
 static int remove_hf_re_f(struct sip_msg* msg, char* key, char* foo);
+static int remove_hf_exp_f(sip_msg_t* msg, char* ematch, char* eskip);
 static int is_present_hf_re_f(struct sip_msg* msg, char* key, char* foo);
 static int is_audio_on_hold_f(struct sip_msg *msg, char *str1, char *str2 );
 static int fixup_substre(void**, int);
@@ -207,6 +208,9 @@ static cmd_export_t cmds[]={
 	{"remove_hf_re",     (cmd_function)remove_hf_re_f,    1,
 		fixup_regexp_null, fixup_free_regexp_null,
 		ANY_ROUTE},
+	{"remove_hf_exp",     (cmd_function)remove_hf_exp_f,  1,
+		fixup_regexp_regexp, fixup_free_regexp_regexp,
+		ANY_ROUTE},
 	{"is_present_hf",    (cmd_function)is_present_hf_f,   1,
 		hname_fixup, free_hname_fixup,
 		ANY_ROUTE},
@@ -1052,16 +1056,14 @@ int remove_hf_f(struct sip_msg* msg, char* str_hf, char* foo)
 	return cnt==0 ? -1 : 1;
 }
 
-static int remove_hf_re_f(struct sip_msg* msg, char* key, char* foo)
+static int remove_hf_re(sip_msg_t* msg, regex_t *re)
 {
 	struct hdr_field *hf;
 	struct lump* l;
 	int cnt;
-	regex_t *re;
 	char c;
 	regmatch_t pmatch;
 
-	re = (regex_t*)key;
 	cnt=0;
 
 	/* we need to be sure we have seen all HFs */
@@ -1091,6 +1093,59 @@ static int remove_hf_re_f(struct sip_msg* msg, char* key, char* foo)
 	return cnt==0 ? -1 : 1;
 }
 
+static int remove_hf_re_f(struct sip_msg* msg, char* key, char* foo)
+{
+	return remove_hf_re(msg, (regex_t*)key);
+}
+
+static int remove_hf_exp_re(sip_msg_t* msg, regex_t *mre, regex_t *sre)
+{
+	struct hdr_field *hf;
+	struct lump* l;
+	int cnt;
+	char c;
+	regmatch_t pmatch;
+
+	cnt=0;
+
+	/* we need to be sure we have seen all HFs */
+	if(parse_headers(msg, HDR_EOH_F, 0)<0) {
+		LM_ERR("error while parsing message headers\n");
+		return -1;
+	}
+
+	for (hf=msg->headers; hf; hf=hf->next)
+	{
+		c = hf->name.s[hf->name.len];
+		hf->name.s[hf->name.len] = '\0';
+		if (regexec(sre, hf->name.s, 1, &pmatch, 0)==0)
+		{
+			hf->name.s[hf->name.len] = c;
+			continue;
+		}
+		if (regexec(mre, hf->name.s, 1, &pmatch, 0)!=0)
+		{
+			hf->name.s[hf->name.len] = c;
+			continue;
+		}
+		hf->name.s[hf->name.len] = c;
+		l=del_lump(msg, hf->name.s-msg->buf, hf->len, 0);
+		if (l==0)
+		{
+			LM_ERR("cannot remove header\n");
+			return -1;
+		}
+		cnt++;
+	}
+
+	return cnt==0 ? -1 : 1;
+}
+
+static int remove_hf_exp_f(struct sip_msg* msg, char* ematch, char* eskip)
+{
+	return remove_hf_exp_re(msg, (regex_t*)ematch, (regex_t*)eskip);
+}
+
 static int is_present_hf_helper_f(struct sip_msg* msg, gparam_t* gp)
 {
 	struct hdr_field *hf;




More information about the sr-dev mailing list