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

Author: Daniel-Constantin Mierla <[email protected]>
Committer: Daniel-Constantin Mierla <[email protected]>
Date: 2018-07-13T12:46:45+02:00

textops: new function replace_hdrs_str()

- replace the matched string in the headers zone

---

Modified: src/modules/textops/textops.c

---

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

---

diff --git a/src/modules/textops/textops.c b/src/modules/textops/textops.c
index cc08a96089..a986f78ed3 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_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*);
 static int replace_body_atonce_f(struct sip_msg*, char*, char*);
@@ -189,6 +190,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_str", (cmd_function)replace_hdrs_str_f,3,
+               fixup_spve_all, fixup_free_spve_all,
+               ANY_ROUTE},
        {"replace_all",      (cmd_function)replace_all_f,     2,
                fixup_regexp_none, fixup_free_regexp_none,
                ANY_ROUTE},
@@ -1010,6 +1014,50 @@ 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 ki_replace_hdrs_str(sip_msg_t* msg, str* mkey, str* rval, str 
*rmode)
+{
+       str lbuf;
+
+       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;
+       }
+
+       return ki_replace_str_helper(msg, &lbuf, mkey, rval, rmode);
+}
+
+static int replace_hdrs_str_f(sip_msg_t* msg, char* pmkey, char* prval, char* 
prmode)
+{
+       str mkey;
+       str rval;
+       str rmode;
+
+       if(fixup_get_svalue(msg, (gparam_t*)pmkey, &mkey)<0) {
+               LM_ERR("failed to get the matching string parameter\n");
+               return -1;
+       }
+
+       if(fixup_get_svalue(msg, (gparam_t*)prval, &rval)<0) {
+               LM_ERR("failed to get the replacement string parameter\n");
+               return -1;
+       }
+
+       if(fixup_get_svalue(msg, (gparam_t*)prmode, &rmode)<0) {
+               LM_ERR("failed to get the replacement mode parameter\n");
+               return -1;
+       }
+
+       return ki_replace_hdrs_str(msg, &mkey, &rval, &rmode);
+}
+
 /* sed-perl style re: s/regular expression/replacement/flags */
 static int subst_helper_f(sip_msg_t* msg, struct subst_expr* se)
 {
@@ -4420,6 +4468,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_str"),
+               SR_KEMIP_INT, ki_replace_hdrs_str,
+               { SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_STR,
+                       SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
+       },
        { str_init("textops"), str_init("replace_body_all"),
                SR_KEMIP_INT, ki_replace_body_all,
                { SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,


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

Reply via email to