Browse code

textops: fix append_hf function

Before: append_hf adds header after first header occurance

After: append_hf adds header after last header occurance

(cherry-picked from commit 4adea97a4f8a9b35273d4d28b0518964abb7f0d8)

Stefan Mititelu authored on 22/10/2020 13:54:01
Showing 1 changed files
... ...
@@ -3229,7 +3229,7 @@ int add_hf_helper(struct sip_msg* msg, str *str1, str *str2,
3229 3229
 		gparam_p hfval, int mode, gparam_p hfanc)
3230 3230
 {
3231 3231
 	struct lump* anchor;
3232
-	struct hdr_field *hf;
3232
+	struct hdr_field *hf, *append_hf;
3233 3233
 	char *s;
3234 3234
 	int len;
3235 3235
 	str s0;
... ...
@@ -3240,6 +3240,7 @@ int add_hf_helper(struct sip_msg* msg, str *str1, str *str2,
3240 3240
 	}
3241 3241
 
3242 3242
 	hf = 0;
3243
+	append_hf = 0;
3243 3244
 	if(hfanc!=NULL) {
3244 3245
 		for (hf=msg->headers; hf; hf=hf->next) {
3245 3246
 			if(hfanc->type==GPARAM_TYPE_INT)
... ...
@@ -3252,20 +3253,25 @@ int add_hf_helper(struct sip_msg* msg, str *str1, str *str2,
3252 3253
 				if (cmp_hdrname_str(&hf->name,&hfanc->v.str)!=0)
3253 3254
 					continue;
3254 3255
 			}
3255
-			break;
3256
+			if (mode == 0) { /* append */
3257
+				append_hf = hf;
3258
+				continue;
3259
+			} else { /* insert */
3260
+				break;
3261
+			}
3256 3262
 		}
3257 3263
 	}
3258 3264
 
3259 3265
 	if(mode == 0) { /* append */
3260
-		if(hf==0) { /* after last header */
3266
+		if(append_hf==0) { /* after last header */
3261 3267
 			anchor = anchor_lump(msg, msg->unparsed - msg->buf, 0, 0);
3262
-		} else { /* after hf */
3263
-			anchor = anchor_lump(msg, hf->name.s + hf->len - msg->buf, 0, 0);
3268
+		} else { /* after last hf */
3269
+			anchor = anchor_lump(msg, append_hf->name.s + append_hf->len - msg->buf, 0, 0);
3264 3270
 		}
3265 3271
 	} else { /* insert */
3266 3272
 		if(hf==0) { /* before first header */
3267 3273
 			anchor = anchor_lump(msg, msg->headers->name.s - msg->buf, 0, 0);
3268
-		} else { /* before hf */
3274
+		} else { /* before first hf */
3269 3275
 			anchor = anchor_lump(msg, hf->name.s - msg->buf, 0, 0);
3270 3276
 		}
3271 3277
 	}