Browse code

textopsx: added hf_iterator_append(iname, htext) function

Daniel-Constantin Mierla authored on 26/07/2021 15:02:01
Showing 1 changed files
... ...
@@ -77,6 +77,7 @@ static int w_hf_iterator_start(sip_msg_t *msg, char *piname, char *p2);
77 77
 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
+static int w_hf_iterator_append(sip_msg_t *msg, char *piname, char *phtext);
80 81
 
81 82
 static int bind_textopsx(textopsx_api_t *tob);
82 83
 
... ...
@@ -139,6 +140,8 @@ static cmd_export_t cmds[] = {
139 140
 			fixup_free_spve_null, ANY_ROUTE},
140 141
 	{"hf_iterator_rm", w_hf_iterator_rm, 1, fixup_spve_null,
141 142
 			fixup_free_spve_null, ANY_ROUTE},
143
+	{"hf_iterator_append", w_hf_iterator_append, 2, fixup_spve_spve,
144
+			fixup_free_spve_spve, ANY_ROUTE},
142 145
 
143 146
 	{"bind_textopsx", (cmd_function)bind_textopsx, 1, 0, 0, ANY_ROUTE},
144 147
 
... ...
@@ -2057,6 +2060,63 @@ static int w_hf_iterator_rm(sip_msg_t *msg, char *piname, char *p2)
2057 2060
 	return ki_hf_iterator_rm(msg, &iname);
2058 2061
 }
2059 2062
 
2063
+/**
2064
+ *
2065
+ */
2066
+static int ki_hf_iterator_append(sip_msg_t *msg, str *iname, str *htext)
2067
+{
2068
+	int k;
2069
+	sr_lump_t *anchor;
2070
+	str sval = STR_NULL;
2071
+
2072
+	k = ki_hf_iterator_index(msg, iname);
2073
+	if(k<0 || _hf_iterators[k].it==NULL) {
2074
+		return -1;
2075
+	}
2076
+	anchor = anchor_lump(msg, _hf_iterators[k].it->name.s
2077
+			+ _hf_iterators[k].it->len - msg->buf, 0, 0);
2078
+	if (anchor==0) {
2079
+		LM_ERR("cannot append hdr after %.*s\n", _hf_iterators[k].it->name.len,
2080
+				_hf_iterators[k].it->name.s);
2081
+		return -1;
2082
+	}
2083
+	sval.s = (char*)pkg_malloc(htext->len + 1);
2084
+	if(sval.s==NULL) {
2085
+		LM_ERR("failed append hdr after %.*s\n", _hf_iterators[k].it->name.len,
2086
+				_hf_iterators[k].it->name.s);
2087
+		return -1;
2088
+	}
2089
+	memcpy(sval.s, htext->s, htext->len);
2090
+	sval.len = htext->len;
2091
+	sval.s[sval.len] = '\0';
2092
+
2093
+	if (insert_new_lump_before(anchor, sval.s, sval.len, 0) == 0) {
2094
+		LM_ERR("cannot insert lump\n");
2095
+		pkg_free(sval.s);
2096
+		return -1;
2097
+	}
2098
+	return 1;
2099
+}
2100
+
2101
+/**
2102
+ *
2103
+ */
2104
+static int w_hf_iterator_append(sip_msg_t *msg, char *piname, char *phtext)
2105
+{
2106
+	str iname = STR_NULL;
2107
+	str htext = STR_NULL;
2108
+	if(fixup_get_svalue(msg, (gparam_t*)piname, &iname)<0) {
2109
+		LM_ERR("failed to get iterator name\n");
2110
+		return -1;
2111
+	}
2112
+	if(fixup_get_svalue(msg, (gparam_t*)phtext, &htext)<0) {
2113
+		LM_ERR("failed to get header text\n");
2114
+		return -1;
2115
+	}
2116
+
2117
+	return ki_hf_iterator_append(msg, &iname, &htext);
2118
+}
2119
+
2060 2120
 /**
2061 2121
  *
2062 2122
  */