Browse code

textops: added remove_hf_idx(hname, idx)

- function to remove a header by name and index

Daniel-Constantin Mierla authored on 26/05/2021 13:29:11
Showing 1 changed files
... ...
@@ -143,6 +143,7 @@ static int remove_hf_re_f(struct sip_msg* msg, char* key, char* foo);
143 143
 static int remove_hf_exp_f(sip_msg_t* msg, char* ematch, char* eskip);
144 144
 static int is_present_hf_re_f(struct sip_msg* msg, char* key, char* foo);
145 145
 static int remove_hf_pv_f(sip_msg_t* msg, char* phf, char* foo);
146
+static int remove_hf_idx_f(sip_msg_t* msg, char* phname, char* pidx);
146 147
 static int remove_hf_re_pv_f(sip_msg_t* msg, char* key, char* foo);
147 148
 static int remove_hf_exp_pv_f(sip_msg_t* msg, char* ematch, char* eskip);
148 149
 static int is_present_hf_pv_f(sip_msg_t* msg, char* key, char* foo);
... ...
@@ -239,6 +240,9 @@ static cmd_export_t cmds[]={
239 240
 	{"remove_hf",        (cmd_function)remove_hf_f,       1,
240 241
 		hname_fixup, free_hname_fixup,
241 242
 		ANY_ROUTE},
243
+	{"remove_hf_idx",    (cmd_function)remove_hf_idx_f,   2,
244
+		fixup_spve_igp,  fixup_free_spve_igp,
245
+		ANY_ROUTE},
242 246
 	{"remove_hf_re",     (cmd_function)remove_hf_re_f,    1,
243 247
 		fixup_regexp_null, fixup_free_regexp_null,
244 248
 		ANY_ROUTE},
... ...
@@ -1747,6 +1751,84 @@ int remove_hf_f(struct sip_msg* msg, char* str_hf, char* foo)
1747 1751
 	return cnt==0 ? -1 : 1;
1748 1752
 }
1749 1753
 
1754
+static int ki_remove_hf_idx(sip_msg_t* msg, str* hname, int idx)
1755
+{
1756
+	hdr_field_t hfm = {0};
1757
+	hdr_field_t *hfi = NULL;
1758
+	sr_lump_t *anchor = NULL;
1759
+	int i = 0;
1760
+	int rm = 0;
1761
+	int pos = 0;
1762
+
1763
+	/* ensure all headers are parsed */
1764
+	if(parse_headers(msg, HDR_EOH_F, 0)<0) {
1765
+		LM_ERR("error parsing headers\n");
1766
+		return -1;
1767
+	}
1768
+
1769
+	if(parse_hname2_str(hname, &hfm)==NULL) {
1770
+		LM_ERR("failed to parse header name [%.*s]\n", hname->len, hname->s);
1771
+		return -1;
1772
+	}
1773
+
1774
+	LM_DBG("trying to remove hf: %.*s - index: %d\n", hname->len, hname->s, idx);
1775
+	if(idx>=0) {
1776
+		rm = 1;
1777
+	}
1778
+	pos = idx;
1779
+
1780
+again:
1781
+	for (hfi=msg->headers; hfi; hfi=hfi->next) {
1782
+		if (hfm.type!=HDR_OTHER_T && hfm.type!=HDR_ERROR_T) {
1783
+			if (hfm.type!=hfi->type) {
1784
+				continue;
1785
+			}
1786
+		} else {
1787
+			if (hfi->name.len!=hname->len) {
1788
+				continue;
1789
+			}
1790
+			if(strncasecmp(hfi->name.s, hname->s, hname->len)!=0) {
1791
+				continue;
1792
+			}
1793
+		}
1794
+		if(rm==1 && i==pos) {
1795
+			anchor=del_lump(msg, hfi->name.s - msg->buf, hfi->len, 0);
1796
+			if (anchor==0) {
1797
+				LM_ERR("cannot remove hdr %.*s\n", hname->len, hname->s);
1798
+				return -1;
1799
+			}
1800
+			return 1;
1801
+		}
1802
+		i++;
1803
+	}
1804
+	if(rm==1) {
1805
+		/* header not found */
1806
+		return 2;
1807
+	}
1808
+	pos = i + idx;
1809
+	if(pos>=0) {
1810
+		rm = 1;
1811
+		goto again;
1812
+	}
1813
+	return 1;
1814
+}
1815
+
1816
+static int remove_hf_idx_f(sip_msg_t* msg, char* phname, char* pidx)
1817
+{
1818
+	str hname = STR_NULL;
1819
+	int idx = 0;
1820
+
1821
+	if(fixup_get_svalue(msg, (gparam_t*)phname, &hname)<0) {
1822
+		LM_ERR("failed to get header name\n");
1823
+		return -1;
1824
+	}
1825
+	if(fixup_get_ivalue(msg, (gparam_t*)pidx, &idx)<0) {
1826
+		LM_ERR("failed to get header index\n");
1827
+		return -1;
1828
+	}
1829
+	return  ki_remove_hf_idx(msg, &hname, idx);
1830
+}
1831
+
1750 1832
 static int remove_hf_re(sip_msg_t* msg, regex_t *re)
1751 1833
 {
1752 1834
 	struct hdr_field *hf;
... ...
@@ -4984,6 +5066,11 @@ static sr_kemi_t sr_kemi_textops_exports[] = {
4984 5066
 		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
4985 5067
 			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
4986 5068
 	},
5069
+	{ str_init("textops"), str_init("remove_hf_idx"),
5070
+		SR_KEMIP_INT, ki_remove_hf_idx,
5071
+		{ SR_KEMIP_STR, SR_KEMIP_INT, SR_KEMIP_NONE,
5072
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
5073
+	},
4987 5074
 	{ str_init("textops"), str_init("remove_hf_exp"),
4988 5075
 		SR_KEMIP_INT, ki_remove_hf_exp,
4989 5076
 		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,