Browse code

textopsx: added hf_iterator_insert() function

Daniel-Constantin Mierla authored on 29/07/2021 07:01:25
Showing 2 changed files
... ...
@@ -618,6 +618,35 @@ hf_iterator_end("i1");
618 618
     }
619 619
     hf_iterator_end("i1");
620 620
 ...
621
+</programlisting>
622
+		</example>
623
+	</section>
624
+	<section id="textopsx.f.hf_iterator_insert">
625
+		<title>
626
+		<function moreinfo="none">hf_iterator_insert(iname, htext)</function>
627
+		</title>
628
+		<para>
629
+			Add headers before the one at the current iterator possition.
630
+		</para>
631
+		<para>
632
+			The parameters can be dynamic strings with variables.
633
+		</para>
634
+		<para>
635
+			This function can be used from ANY_ROUTE.
636
+		</para>
637
+		<example>
638
+		<title><function>hf_iterator_insert</function> usage</title>
639
+		<programlisting format="linespecific">
640
+...
641
+    hf_iterator_start("i1");
642
+    while(hf_iterator_next("i1")) {
643
+        if($hfitname(i1)=="My-Header") {
644
+            hf_iterator_insert("i1", "My-New-Header: abc\r\n");
645
+            break;
646
+        }
647
+    }
648
+    hf_iterator_end("i1");
649
+...
621 650
 </programlisting>
622 651
 		</example>
623 652
 	</section>
... ...
@@ -78,6 +78,7 @@ static int w_hf_iterator_next(sip_msg_t *msg, char *piname, char *p2);
78 78
 static int w_hf_iterator_end(sip_msg_t *msg, char *piname, char *p2);
79 79
 static int w_hf_iterator_rm(sip_msg_t *msg, char *piname, char *p2);
80 80
 static int w_hf_iterator_append(sip_msg_t *msg, char *piname, char *phtext);
81
+static int w_hf_iterator_insert(sip_msg_t *msg, char *piname, char *phtext);
81 82
 
82 83
 static int bind_textopsx(textopsx_api_t *tob);
83 84
 
... ...
@@ -142,6 +143,8 @@ static cmd_export_t cmds[] = {
142 143
 			fixup_free_spve_null, ANY_ROUTE},
143 144
 	{"hf_iterator_append", w_hf_iterator_append, 2, fixup_spve_spve,
144 145
 			fixup_free_spve_spve, ANY_ROUTE},
146
+	{"hf_iterator_insert", w_hf_iterator_insert, 2, fixup_spve_spve,
147
+			fixup_free_spve_spve, ANY_ROUTE},
145 148
 
146 149
 	{"bind_textopsx", (cmd_function)bind_textopsx, 1, 0, 0, ANY_ROUTE},
147 150
 
... ...
@@ -2117,6 +2120,62 @@ static int w_hf_iterator_append(sip_msg_t *msg, char *piname, char *phtext)
2117 2120
 	return ki_hf_iterator_append(msg, &iname, &htext);
2118 2121
 }
2119 2122
 
2123
+/**
2124
+ *
2125
+ */
2126
+static int ki_hf_iterator_insert(sip_msg_t *msg, str *iname, str *htext)
2127
+{
2128
+	int k;
2129
+	sr_lump_t *anchor;
2130
+	str sval = STR_NULL;
2131
+
2132
+	k = ki_hf_iterator_index(msg, iname);
2133
+	if(k<0 || _hf_iterators[k].it==NULL) {
2134
+		return -1;
2135
+	}
2136
+	anchor = anchor_lump(msg, _hf_iterators[k].it->name.s - msg->buf, 0, 0);
2137
+	if (anchor==0) {
2138
+		LM_ERR("cannot insert hdr after %.*s\n", _hf_iterators[k].it->name.len,
2139
+				_hf_iterators[k].it->name.s);
2140
+		return -1;
2141
+	}
2142
+	sval.s = (char*)pkg_malloc(htext->len + 1);
2143
+	if(sval.s==NULL) {
2144
+		LM_ERR("failed to insert hdr after %.*s\n", _hf_iterators[k].it->name.len,
2145
+				_hf_iterators[k].it->name.s);
2146
+		return -1;
2147
+	}
2148
+	memcpy(sval.s, htext->s, htext->len);
2149
+	sval.len = htext->len;
2150
+	sval.s[sval.len] = '\0';
2151
+
2152
+	if (insert_new_lump_before(anchor, sval.s, sval.len, 0) == 0) {
2153
+		LM_ERR("cannot insert lump\n");
2154
+		pkg_free(sval.s);
2155
+		return -1;
2156
+	}
2157
+	return 1;
2158
+}
2159
+
2160
+/**
2161
+ *
2162
+ */
2163
+static int w_hf_iterator_insert(sip_msg_t *msg, char *piname, char *phtext)
2164
+{
2165
+	str iname = STR_NULL;
2166
+	str htext = STR_NULL;
2167
+	if(fixup_get_svalue(msg, (gparam_t*)piname, &iname)<0) {
2168
+		LM_ERR("failed to get iterator name\n");
2169
+		return -1;
2170
+	}
2171
+	if(fixup_get_svalue(msg, (gparam_t*)phtext, &htext)<0) {
2172
+		LM_ERR("failed to get header text\n");
2173
+		return -1;
2174
+	}
2175
+
2176
+	return ki_hf_iterator_insert(msg, &iname, &htext);
2177
+}
2178
+
2120 2179
 /**
2121 2180
  *
2122 2181
  */
... ...
@@ -2735,6 +2794,16 @@ static sr_kemi_t sr_kemi_textopsx_exports[] = {
2735 2794
 		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
2736 2795
 			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2737 2796
 	},
2797
+	{ str_init("textopsx"), str_init("hf_iterator_append"),
2798
+		SR_KEMIP_INT, ki_hf_iterator_append,
2799
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,
2800
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2801
+	},
2802
+	{ str_init("textopsx"), str_init("hf_iterator_insert"),
2803
+		SR_KEMIP_INT, ki_hf_iterator_insert,
2804
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,
2805
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2806
+	},
2738 2807
 
2739 2808
 	{ {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
2740 2809
 };