Module: sip-router Branch: master Commit: 6783e5e3e9a00cd60afcd9e25f3425f22f006a29 URL: http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=6783e5e3...
Author: Daniel-Constantin Mierla miconda@gmail.com Committer: Daniel-Constantin Mierla miconda@gmail.com Date: Tue Apr 20 16:04:55 2010 +0200
pv: added strip and striptail transformations
- s.strip and s.striptail transformations to remove prefix or suffix from pseudo-variables - straightforward option instead of more complex format of s.substr for these two common operations in sip routing
---
modules_k/pv/pv_trans.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++ modules_k/pv/pv_trans.h | 3 +- 2 files changed, 78 insertions(+), 1 deletions(-)
diff --git a/modules_k/pv/pv_trans.c b/modules_k/pv/pv_trans.c index d9120de..9ddd125 100644 --- a/modules_k/pv/pv_trans.c +++ b/modules_k/pv/pv_trans.c @@ -415,6 +415,42 @@ int tr_eval_string(struct sip_msg *msg, tr_param_t *tp, int subtype, val->rs = st; break;
+ case TR_S_STRIP: + case TR_S_STRIPTAIL: + if(tp==NULL) + { + LM_ERR("strip invalid parameters\n"); + return -1; + } + if(!(val->flags&PV_VAL_STR)) + val->rs.s = int2str(val->ri, &val->rs.len); + if(tp->type==TR_PARAM_NUMBER) + { + i = tp->v.n; + } else { + if(pv_get_spec_value(msg, (pv_spec_p)tp->v.data, &v)!=0 + || (!(v.flags&PV_VAL_INT))) + { + LM_ERR("select cannot get p1\n"); + return -1; + } + i = v.ri; + } + val->flags = PV_VAL_STR; + val->ri = 0; + if(i<=0) + break; + if(i>=val->rs.len) + { + val->rs.s = ""; + val->rs.len = 0; + break; + } + if(subtype==TR_S_STRIP) + val->rs.s += i; + val->rs.len -= i; + break; + default: LM_ERR("unknown subtype %d\n", subtype); @@ -1277,6 +1313,46 @@ char* tr_parse_string(str* in, trans_t *t) goto error; } goto done; + } else if(name.len==5 && strncasecmp(name.s, "strip", 5)==0) { + t->subtype = TR_S_STRIP; + if(*p!=TR_PARAM_MARKER) + { + LM_ERR("invalid strip transformation: %.*s!\n", + in->len, in->s); + goto error; + } + p++; + _tr_parse_nparam(p, p0, tp, spec, n, sign, in, s); + t->params = tp; + tp = 0; + while(*p && (*p==' ' || *p=='\t' || *p=='\n')) p++; + if(*p!=TR_RBRACKET) + { + LM_ERR("invalid strip transformation: %.*s!!\n", + in->len, in->s); + goto error; + } + goto done; + } else if(name.len==9 && strncasecmp(name.s, "striptail", 9)==0) { + t->subtype = TR_S_STRIPTAIL; + if(*p!=TR_PARAM_MARKER) + { + LM_ERR("invalid striptail transformation: %.*s!\n", + in->len, in->s); + goto error; + } + p++; + _tr_parse_nparam(p, p0, tp, spec, n, sign, in, s); + t->params = tp; + tp = 0; + while(*p && (*p==' ' || *p=='\t' || *p=='\n')) p++; + if(*p!=TR_RBRACKET) + { + LM_ERR("invalid striptail transformation: %.*s!!\n", + in->len, in->s); + goto error; + } + goto done; }
LM_ERR("unknown transformation: %.*s/%.*s/%d!\n", in->len, in->s, diff --git a/modules_k/pv/pv_trans.h b/modules_k/pv/pv_trans.h index c9e57c2..acd81db 100644 --- a/modules_k/pv/pv_trans.h +++ b/modules_k/pv/pv_trans.h @@ -38,7 +38,8 @@ enum _tr_s_subtype { TR_S_NONE=0, TR_S_LEN, TR_S_INT, TR_S_MD5, TR_S_SUBSTR, TR_S_SELECT, TR_S_ENCODEHEXA, TR_S_DECODEHEXA, TR_S_ESCAPECOMMON, TR_S_UNESCAPECOMMON, TR_S_ESCAPEUSER, TR_S_UNESCAPEUSER, - TR_S_ESCAPEPARAM, TR_S_UNESCAPEPARAM, TR_S_TOLOWER, TR_S_TOUPPER + TR_S_ESCAPEPARAM, TR_S_UNESCAPEPARAM, TR_S_TOLOWER, TR_S_TOUPPER, + TR_S_STRIP, TR_S_STRIPTAIL }; enum _tr_uri_subtype { TR_URI_NONE=0, TR_URI_USER, TR_URI_HOST, TR_URI_PASSWD, TR_URI_PORT,