Browse code

Merge pull request #1666 from kamailio/lazedo/is-uri

siputils: add is_uri function

Daniel-Constantin Mierla authored on 05/10/2018 05:19:27 • GitHub committed on 05/10/2018 05:19:27
Showing 4 changed files
... ...
@@ -129,6 +129,37 @@ int w_has_totag(struct sip_msg* _m, char* _foo, char* _bar)
129 129
 	return has_totag(_m);
130 130
 }
131 131
 
132
+/*
133
+ * Check if pseudo variable contains a valid uri
134
+ */
135
+int is_uri(struct sip_msg* _m, char* _sp, char* _s2)
136
+{
137
+	pv_spec_t *sp;
138
+	pv_value_t pv_val;
139
+	sip_uri_t turi;
140
+
141
+	sp = (pv_spec_t *)_sp;
142
+
143
+	if (sp && (pv_get_spec_value(_m, sp, &pv_val) == 0)) {
144
+		if (pv_val.flags & PV_VAL_STR) {
145
+			if (pv_val.rs.len == 0 || pv_val.rs.s == NULL) {
146
+				LM_DBG("missing argument\n");
147
+				return -1;
148
+			}
149
+			if(parse_uri(pv_val.rs.s, pv_val.rs.len, &turi)!=0) {
150
+				return -1;
151
+			}
152
+			return 1;
153
+		} else {
154
+			LM_ERR("pseudo variable value is not string\n");
155
+			return -1;
156
+		}
157
+	} else {
158
+		LM_ERR("failed to get pseudo variable value\n");
159
+		return -1;
160
+	}
161
+}
162
+
132 163
 /*
133 164
  * Check if the username matches the username in credentials
134 165
  */
... ...
@@ -75,6 +75,10 @@ int add_uri_param(struct sip_msg* _msg, char* _param, char* _s2);
75 75
  */
76 76
 int tel2sip(struct sip_msg* _msg, char* _uri, char* _hostpart, char* _res);
77 77
 
78
+/*
79
+ * Check if pseudo variable contains a valid uri
80
+ */
81
+int is_uri(struct sip_msg* _m, char* _sp, char* _s2);
78 82
 
79 83
 /*
80 84
  * Check if user part of URI in pseudo variable is an e164 number
... ...
@@ -608,6 +608,33 @@ if (is_uri_user_e164("$avp(i:705)") {
608 608
 		</example>
609 609
 	</section>
610 610
 
611
+    <section id="siputils.f.is_uri">
612
+        <title>
613
+        <function moreinfo="none">is_uri(pseudo-variable)</function>
614
+        </title>
615
+        <para>
616
+        Checks if string value of pseudo variable argument is a
617
+        valid uri.
618
+        </para>
619
+        <para>
620
+        This function can be used from ANY_ROUTE.
621
+        </para>
622
+        <example>
623
+        <title><function>is_uri</function> usage</title>
624
+        <programlisting format="linespecific">
625
+...
626
+if (is_uri("$var(x)")) {  # Check if variable contains a uri
627
+   ...
628
+}
629
+if (is_uri("$avp(i:705)") {
630
+   # Check value stored in avp i:705
631
+   ...
632
+};
633
+...
634
+</programlisting>
635
+        </example>
636
+    </section>
637
+
611 638
 	<section id="siputils.f.is_tel_number">
612 639
 		<title>
613 640
 		<function moreinfo="none">is_tel_number(tval)</function>
... ...
@@ -137,6 +137,8 @@ static cmd_export_t cmds[]={
137 137
 		free_fixup_get_uri_param, REQUEST_ROUTE|LOCAL_ROUTE},
138 138
 	{"tel2sip", (cmd_function)tel2sip, 3, fixup_tel2sip, 0,
139 139
 		REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|ONREPLY_ROUTE},
140
+	{"is_uri",            (cmd_function)is_uri,           1, fixup_pvar_null,
141
+		fixup_free_pvar_null, ANY_ROUTE},
140 142
 	{"is_e164",            (cmd_function)is_e164,           1, fixup_pvar_null,
141 143
 		fixup_free_pvar_null, REQUEST_ROUTE|FAILURE_ROUTE|LOCAL_ROUTE},
142 144
 	{"is_uri_user_e164",   (cmd_function)w_is_uri_user_e164,  1, fixup_pvar_null,