Module: sip-router Branch: master Commit: 1b6d53726362de49065bbfaf11e0a5d07dae6b22 URL: http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=1b6d5372...
Author: Daniel-Constantin Mierla miconda@gmail.com Committer: Daniel-Constantin Mierla miconda@gmail.com Date: Fri Dec 16 20:50:50 2011 +0100
textopsx: added keep_hf("regexp")
- remove headers that don't match the regular expression regexp - several header are ignored always (thus not removed): Via, From, To, Call-ID, CSeq, Content-Lenght, Content-Type, Max-Forwards, Contact, Route, Record-Route -- these can be removed one by one with remove_hf() - the new function helps reducing the size of the sip message, by removing not mandatory headers
---
modules/textopsx/textopsx.c | 61 +++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 61 insertions(+), 0 deletions(-)
diff --git a/modules/textopsx/textopsx.c b/modules/textopsx/textopsx.c index 2228d3b..687cce4 100644 --- a/modules/textopsx/textopsx.c +++ b/modules/textopsx/textopsx.c @@ -32,6 +32,7 @@ #include "../../dprint.h" #include "../../data_lump.h" #include "../../msg_translator.h" +#include "../../mod_fix.h"
#include "api.h"
@@ -42,6 +43,8 @@ static int msg_apply_changes_f(sip_msg_t *msg, char *str1, char *str2); static int change_reply_status_f(struct sip_msg*, char*, char *); static int change_reply_status_fixup(void** param, int param_no);
+static int w_keep_hf_f(struct sip_msg*, char*, char *); + static int w_remove_body_f(struct sip_msg*, char*, char *); static int bind_textopsx(textopsx_api_t *tob);
@@ -53,6 +56,8 @@ static cmd_export_t cmds[] = { change_reply_status_fixup, ONREPLY_ROUTE }, {"remove_body", (cmd_function)w_remove_body_f, 0, 0, ANY_ROUTE }, + {"keep_hf", (cmd_function)w_keep_hf_f, 1, + fixup_regexp_null, ANY_ROUTE }, {"bind_textopsx", (cmd_function)bind_textopsx, 1, 0, ANY_ROUTE },
@@ -262,6 +267,62 @@ static int w_remove_body_f(struct sip_msg *msg, char *p1, char *p2) return 1; }
+ +/** + * + */ +static int w_keep_hf_f(struct sip_msg* msg, char* key, char* foo) +{ + struct hdr_field *hf; + regex_t *re; + regmatch_t pmatch; + char c; + struct lump* l; + + re = (regex_t*)key; + + /* we need to be sure we have seen all HFs */ + parse_headers(msg, HDR_EOH_F, 0); + for (hf=msg->headers; hf; hf=hf->next) + { + switch(hf->type) { + case HDR_FROM_T: + case HDR_TO_T: + case HDR_CALLID_T: + case HDR_CSEQ_T: + case HDR_VIA_T: + case HDR_VIA2_T: + case HDR_CONTACT_T: + case HDR_CONTENTLENGTH_T: + case HDR_CONTENTTYPE_T: + case HDR_ROUTE_T: + case HDR_RECORDROUTE_T: + case HDR_MAXFORWARDS_T: + continue; + default: + ; + } + + c = hf->name.s[hf->name.len]; + hf->name.s[hf->name.len] = '\0'; + if (regexec(re, hf->name.s, 1, &pmatch, 0)!=0) + { + /* no match => remove */ + 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; + } + } else { + hf->name.s[hf->name.len] = c; + } + } + + return -1; +} + /* * Function to load the textops api. */