Module: sip-router
Branch: master
Commit: 6526248ff5204286e832df0576f0403931777646
URL:
http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=6526248…
Author: Alex Hermann <alex(a)speakup.nl>
Committer: Alex Hermann <alex(a)speakup.nl>
Date: Thu Mar 24 14:12:05 2011 +0100
modules_k/pv: Add s.replace transformation.
Syntax: {s.replace,<search>,<replace>}
Replaces every occurance of <search> with <replace>. Both parameters may
contain pvars.
---
modules_k/pv/pv_trans.c | 89 +++++++++++++++++++++++++++++++++++++++++++++-
modules_k/pv/pv_trans.h | 3 +-
2 files changed, 89 insertions(+), 3 deletions(-)
diff --git a/modules_k/pv/pv_trans.c b/modules_k/pv/pv_trans.c
index 4842a06..b60f529 100644
--- a/modules_k/pv/pv_trans.c
+++ b/modules_k/pv/pv_trans.c
@@ -69,8 +69,8 @@ int tr_eval_string(struct sip_msg *msg, tr_param_t *tp, int subtype,
{
int i, j, max;
char *p, *s;
- str st;
- pv_value_t v;
+ str st, st2;
+ pv_value_t v, w;
if(val==NULL || val->flags&PV_VAL_NULL)
return -1;
@@ -496,6 +496,61 @@ int tr_eval_string(struct sip_msg *msg, tr_param_t *tp, int subtype,
val->rs.len = j-1;
break;
+
+ case TR_S_REPLACE:
+ if(tp==NULL || tp->next==NULL)
+ {
+ LM_ERR("select 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_STRING)
+ {
+ st = tp->v.s;
+ } else {
+ if(pv_get_spec_value(msg, (pv_spec_p)tp->v.data, &v)!=0
+ || (!(v.flags&PV_VAL_STR)) || v.rs.len<=0)
+ {
+ LM_ERR("replace cannot get p1\n");
+ return -1;
+ }
+ st = v.rs;
+ }
+
+ if(tp->next->type==TR_PARAM_STRING)
+ {
+ st2 = tp->next->v.s;
+ } else {
+ if(pv_get_spec_value(msg, (pv_spec_p)tp->next->v.data, &w)!=0
+ || (!(w.flags&PV_VAL_STR)) || w.rs.len<=0)
+ {
+ LM_ERR("replace cannot get p2\n");
+ return -1;
+ }
+ st2 = w.rs;
+ }
+
+ val->flags = PV_VAL_STR;
+ val->ri = 0;
+
+ i = 0;
+ j = 0;
+ max = val->rs.len - st.len;
+ while (i < val->rs.len && j < TR_BUFFER_SIZE) {
+ if (i <= max && val->rs.s[i] == st.s[0] &&
strncmp(val->rs.s+i, st.s, st.len) == 0) {
+ strncpy(_tr_buffer+j, st2.s, st2.len);
+ i += st.len;
+ j += st2.len;
+ } else {
+ _tr_buffer[j++] = val->rs.s[i++];
+ }
+ }
+ val->rs.s = _tr_buffer;
+ val->rs.len = j;
+ break;
+
case TR_S_TIMEFORMAT:
if(tp==NULL)
{
@@ -1483,6 +1538,36 @@ char* tr_parse_string(str* in, trans_t *t)
goto error;
}
goto done;
+ } else if(name.len==7 && strncasecmp(name.s, "replace", 7)==0) {
+ t->subtype = TR_S_REPLACE;
+ if(*p!=TR_PARAM_MARKER)
+ {
+ LM_ERR("invalid replace transformation: %.*s!\n", in->len, in->s);
+ goto error;
+ }
+ p++;
+ _tr_parse_sparam(p, p0, tp, spec, ps, in, s);
+ t->params = tp;
+ tp = 0;
+ while(*p && (*p==' ' || *p=='\t' || *p=='\n')) p++;
+ if(*p!=TR_PARAM_MARKER)
+ {
+ LM_ERR("invalid replace transformation: %.*s!\n",
+ in->len, in->s);
+ goto error;
+ }
+ p++;
+ _tr_parse_sparam(p, p0, tp, spec, ps, in, s);
+ t->params->next = tp;
+ tp = 0;
+ while(*p && (*p==' ' || *p=='\t' || *p=='\n')) p++;
+ if(*p!=TR_RBRACKET)
+ {
+ LM_ERR("invalid replace 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 222c09e..0938ddc 100644
--- a/modules_k/pv/pv_trans.h
+++ b/modules_k/pv/pv_trans.h
@@ -39,7 +39,8 @@ enum _tr_s_subtype {
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_STRIP, TR_S_STRIPTAIL, TR_S_PREFIXES, TR_S_PREFIXES_QUOT, TR_S_TIMEFORMAT
+ TR_S_STRIP, TR_S_STRIPTAIL, TR_S_PREFIXES, TR_S_PREFIXES_QUOT, TR_S_REPLACE,
+ TR_S_TIMEFORMAT
};
enum _tr_uri_subtype {
TR_URI_NONE=0, TR_URI_USER, TR_URI_HOST, TR_URI_PASSWD, TR_URI_PORT,