Module: sip-router
Branch: master
Commit: b1b9d7c41189428671fa03efe88b657a6ae78b12
URL:
http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=b1b9d7c…
Author: Alex Hermann <alex(a)speakup.nl>
Committer: Alex Hermann <alex(a)speakup.nl>
Date: Thu Jul 21 15:24:09 2011 +0200
modules_k/pv: Add s.sql transformation to turn PV into suitable SQL value.
This transformation outputs valid SQL values for various PV values:
- <null> values are output as NULL
- integers are output as integers
- everything else is output as quoted and escaped string
Example:
$var(null) = $null;
$avp(null) = $null;
$avp(str) = "String with \ illegal \\characters";
$avp(nr) = 12345;
$avp(strnr) = "12345";
xlog("$$rm = $rm = $(rm{s.sql})");
xlog("$$var(null) = $var(null) = $(var(null){s.sql})");
xlog("$$avp(null) = $avp(null) = $(avp(null){s.sql})");
xlog("$$avp(str) = $avp(str) = $(avp(str){s.sql})");
xlog("$$avp(nr) = $avp(nr) = $(avp(nr){s.sql})");
xlog("$$avp(strnr) = $avp(strnr) = $(avp(strnr){s.sql})");
Output:
$rm = ACK = 'ACK'
$var(null) = 0 = 0
$avp(null) = <null> = NULL
$avp(str) = String with \ illegal \characters = 'String with \\ illegal
\\characters'
$avp(nr) = 12345 = 12345
$avp(strnr) = 12345 = '12345'
---
modules_k/pv/pv_trans.c | 31 ++++++++++++++++++++++++++++++-
modules_k/pv/pv_trans.h | 2 +-
2 files changed, 31 insertions(+), 2 deletions(-)
diff --git a/modules_k/pv/pv_trans.c b/modules_k/pv/pv_trans.c
index 8cb59f4..7b7b564 100644
--- a/modules_k/pv/pv_trans.c
+++ b/modules_k/pv/pv_trans.c
@@ -54,6 +54,7 @@
/*! transformation buffer */
static char _tr_buffer[TR_BUFFER_SIZE];
+static str _tr_null = { "NULL", 4 };
/*!
@@ -73,7 +74,7 @@ int tr_eval_string(struct sip_msg *msg, tr_param_t *tp, int subtype,
pv_value_t v, w;
void *vp;
- if(val==NULL || val->flags&PV_VAL_NULL)
+ if(val==NULL || (val->flags&PV_VAL_NULL && subtype != TR_S_SQL))
return -1;
switch(subtype)
@@ -593,6 +594,31 @@ int tr_eval_string(struct sip_msg *msg, tr_param_t *tp, int subtype,
val->rs.s = _tr_buffer;
break;
+ case TR_S_SQL:
+ if (val->flags&PV_VAL_NULL) {
+ val->flags = PV_VAL_STR;
+ val->rs = _tr_null;
+ break;
+ }
+ if(val->flags&PV_TYPE_INT || !(val->flags&PV_VAL_STR)) {
+ val->rs.s = int2str(val->ri, &val->rs.len);
+ val->flags = PV_VAL_STR;
+ break;
+ }
+ if(val->rs.len>TR_BUFFER_SIZE/2-1) {
+ LM_ERR("escape buffer to short");
+ return -1;
+ }
+ _tr_buffer[0] = '\'';
+ i = escape_common(_tr_buffer+1, val->rs.s, val->rs.len);
+ _tr_buffer[++i] = '\'';
+ _tr_buffer[++i] = '\0';
+ memset(val, 0, sizeof(pv_value_t));
+ val->flags = PV_VAL_STR;
+ val->rs.s = _tr_buffer;
+ val->rs.len = i;
+ break;
+
default:
LM_ERR("unknown subtype %d\n",
subtype);
@@ -1353,6 +1379,9 @@ char* tr_parse_string(str* in, trans_t *t)
} else if(name.len==3 && strncasecmp(name.s, "md5", 3)==0) {
t->subtype = TR_S_MD5;
goto done;
+ } else if(name.len==3 && strncasecmp(name.s, "sql", 3)==0) {
+ t->subtype = TR_S_SQL;
+ goto done;
} else if(name.len==7 && strncasecmp(name.s, "tolower", 7)==0) {
t->subtype = TR_S_TOLOWER;
goto done;
diff --git a/modules_k/pv/pv_trans.h b/modules_k/pv/pv_trans.h
index 0938ddc..2cd2fd0 100644
--- a/modules_k/pv/pv_trans.h
+++ b/modules_k/pv/pv_trans.h
@@ -40,7 +40,7 @@ enum _tr_s_subtype {
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_REPLACE,
- TR_S_TIMEFORMAT
+ TR_S_TIMEFORMAT, TR_S_SQL
};
enum _tr_uri_subtype {
TR_URI_NONE=0, TR_URI_USER, TR_URI_HOST, TR_URI_PASSWD, TR_URI_PORT,