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

Author: Daniel-Constantin Mierla <[email protected]>
Committer: Daniel-Constantin Mierla <[email protected]>
Date: 2018-07-17T07:50:50+02:00

textops: added replace_hdrs(re, sval)

- replace matching regexp with sval inside sip headers part

---

Modified: src/modules/textops/textops.c

---

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

---

diff --git a/src/modules/textops/textops.c b/src/modules/textops/textops.c
index a986f78ed3..8da8998803 100644
--- a/src/modules/textops/textops.c
+++ b/src/modules/textops/textops.c
@@ -91,6 +91,7 @@ static int replace_f(struct sip_msg*, char*, char*);
 static int replace_str_f(struct sip_msg*, char*, char*, char*);
 static int replace_body_f(struct sip_msg*, char*, char*);
 static int replace_body_str_f(struct sip_msg*, char*, char*, char*);
+static int replace_hdrs_f(struct sip_msg*, char*, char*);
 static int replace_hdrs_str_f(struct sip_msg*, char*, char*, char*);
 static int replace_all_f(struct sip_msg*, char*, char*);
 static int replace_body_all_f(struct sip_msg*, char*, char*);
@@ -190,6 +191,9 @@ static cmd_export_t cmds[]={
        {"replace_body_str", (cmd_function)replace_body_str_f,3,
                fixup_spve_all, fixup_free_spve_all,
                ANY_ROUTE},
+       {"replace_hdrs",     (cmd_function)replace_hdrs_f,    2,
+               fixup_regexp_none, fixup_free_regexp_none,
+               ANY_ROUTE},
        {"replace_hdrs_str", (cmd_function)replace_hdrs_str_f,3,
                fixup_spve_all, fixup_free_spve_all,
                ANY_ROUTE},
@@ -1014,6 +1018,87 @@ static int replace_body_str_f(sip_msg_t* msg, char* 
pmkey, char* prval, char* pr
        return ki_replace_body_str(msg, &mkey, &rval, &rmode);
 }
 
+static int replace_hdrs_helper(sip_msg_t* msg, regex_t *re, str *val)
+{
+       struct lump* l;
+       regmatch_t pmatch;
+       char* s;
+       int off;
+       str lbuf;
+       char bk;
+
+       if ( parse_headers(msg, HDR_EOH_F, 0)==-1 ) {
+               LM_ERR("failed to parse to end of headers\n");
+               return -1;
+       }
+
+       lbuf.s = get_header(msg);
+       lbuf.len = (int)(msg->unparsed - lbuf.s);
+
+       if (lbuf.len==0) {
+               LM_DBG("message headers part has zero length\n");
+               return -1;
+       }
+
+       bk = lbuf.s[lbuf.len];
+       lbuf.s[lbuf.len] = '\0';
+       if (regexec(re, lbuf.s, 1, &pmatch, 0)!=0) {
+               lbuf.s[lbuf.len] = bk;
+               return -1;
+       }
+       lbuf.s[lbuf.len] = bk;
+
+       off=lbuf.s-msg->buf;
+
+       if (pmatch.rm_so!=-1){
+               if ((l=del_lump(msg, pmatch.rm_so+off,
+                                               pmatch.rm_eo-pmatch.rm_so, 
0))==0)
+                       return -1;
+               s=pkg_malloc(val->len+1);
+               if (s==0){
+                       LM_ERR("memory allocation failure\n");
+                       return -1;
+               }
+               memcpy(s, val->s, val->len);
+               if (insert_new_lump_after(l, s, val->len, 0)==0){
+                       LM_ERR("could not insert new lump\n");
+                       pkg_free(s);
+                       return -1;
+               }
+
+               return 1;
+       }
+       return -1;
+}
+
+static int replace_hdrs_f(struct sip_msg* msg, char* key, char* str2)
+{
+       str val;
+
+       val.s = str2;
+       val.len = strlen(val.s);
+
+       return replace_hdrs_helper(msg, (regex_t*)key, &val);
+}
+
+static int ki_replace_hdrs(sip_msg_t* msg, str* sre, str* sval)
+{
+       regex_t mre;
+       int ret;
+
+       memset(&mre, 0, sizeof(regex_t));
+       if (regcomp(&mre, sre->s, REG_EXTENDED|REG_ICASE|REG_NEWLINE)!=0) {
+               LM_ERR("failed to compile regex: %.*s\n", sre->len, sre->s);
+               return -1;
+       }
+
+       ret = replace_hdrs_helper(msg, &mre, sval);
+
+       regfree(&mre);
+
+       return ret;
+}
+
 static int ki_replace_hdrs_str(sip_msg_t* msg, str* mkey, str* rval, str 
*rmode)
 {
        str lbuf;
@@ -4468,6 +4553,11 @@ static sr_kemi_t sr_kemi_textops_exports[] = {
                { SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_STR,
                        SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
        },
+       { str_init("textops"), str_init("replace_hdrs"),
+               SR_KEMIP_INT, ki_replace_hdrs,
+               { SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,
+                       SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
+       },
        { str_init("textops"), str_init("replace_hdrs_str"),
                SR_KEMIP_INT, ki_replace_hdrs_str,
                { SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_STR,


_______________________________________________
Kamailio (SER) - Development Mailing List
[email protected]
https://lists.kamailio.org/cgi-bin/mailman/listinfo/sr-dev

Reply via email to