Browse code

textops: find start of header body for filter functions

- GH #2748

Daniel-Constantin Mierla authored on 02/08/2021 14:08:29
Showing 1 changed files
... ...
@@ -1598,7 +1598,7 @@ static inline int find_line_start(char *text, unsigned int text_len,
1598 1598
 }
1599 1599
 
1600 1600
 static inline int find_hdr_line_start(char *hname, unsigned int hname_len,
1601
-		char **buf, unsigned int *buf_len)
1601
+		char **buf, unsigned int *buf_len, char **bstart)
1602 1602
 {
1603 1603
 	hdr_field_t h1;
1604 1604
 	hdr_field_t h2;
... ...
@@ -1624,11 +1624,11 @@ static inline int find_hdr_line_start(char *hname, unsigned int hname_len,
1624 1624
 			if(h1.type>0 && h1.type==h2.type) {
1625 1625
 				*buf = start;
1626 1626
 				*buf_len = len;
1627
-				return 1;
1628
-			} else if(cmp_hdrname_str(&h1.name, &h2.name)==0) {
1627
+				goto found;
1628
+			} else if(cmpi_str(&h1.name, &h2.name)==0) {
1629 1629
 				*buf = start;
1630 1630
 				*buf_len = len;
1631
-				return 1;
1631
+				goto found;
1632 1632
 			}
1633 1633
 		}
1634 1634
 		/* jump to next line */
... ...
@@ -1645,6 +1645,21 @@ static inline int find_hdr_line_start(char *hname, unsigned int hname_len,
1645 1645
 		}
1646 1646
 	}
1647 1647
 	return 0;
1648
+
1649
+found:
1650
+	ch = memchr(start, ':', len - 1);
1651
+	if(ch==NULL) {
1652
+		LM_ERR("weird - no ':' found\n");
1653
+		return 0;
1654
+	}
1655
+	ch++;
1656
+	while((ch < start + len) && (*ch==' ' || *ch=='\t')) ch++;
1657
+	if(ch==start + len) {
1658
+		LM_ERR("no header body content\n");
1659
+		return 0;
1660
+	}
1661
+	*bstart = ch;
1662
+	return 1;
1648 1663
 }
1649 1664
 
1650 1665
 /**
... ...
@@ -1670,6 +1685,7 @@ static int check_multipart(struct sip_msg *msg)
1670 1685
 static int ki_filter_body(struct sip_msg* msg, str *content_type)
1671 1686
 {
1672 1687
 	char *start;
1688
+	char *end;
1673 1689
 	unsigned int len;
1674 1690
 	str body;
1675 1691
 	str boundary = {0,0};
... ...
@@ -1695,11 +1711,11 @@ static int ki_filter_body(struct sip_msg* msg, str *content_type)
1695 1711
 	start = body.s;
1696 1712
 	len = body.len;
1697 1713
 
1698
-	while (find_hdr_line_start("Content-Type: ", 14, &start, &len))
1714
+	while (find_hdr_line_start("Content-Type: ", 14, &start, &len, &end))
1699 1715
 	{
1700
-		start = start + 14;
1701
-		len = len - 14;
1716
+		len = len - (end - start);
1702 1717
 		LM_DBG("line: [%.*s]\n", len, start);
1718
+		start = end;
1703 1719
 		if (len > content_type->len + 2) {
1704 1720
 			if (strncasecmp(start, content_type->s, content_type->len)== 0)
1705 1721
 			{
... ...
@@ -3239,10 +3255,9 @@ static int ki_remove_multibody(sip_msg_t* msg, str* content_type)
3239 3255
 	start = body.s;
3240 3256
 	len = body.len;
3241 3257
 
3242
-	while (find_hdr_line_start("Content-Type: ", 14, &start, &len))
3258
+	while (find_hdr_line_start("Content-Type: ", 14, &start, &len, &end))
3243 3259
 	{
3244
-		end = start + 14;
3245
-		len = len - 14;
3260
+		len = len - (end - start);
3246 3261
 		if (len > (content_type->len + 2)) {
3247 3262
 			if (strncasecmp(end, content_type->s, content_type->len)== 0)
3248 3263
 			{
... ...
@@ -3345,10 +3360,9 @@ static int ki_get_body_part_helper(sip_msg_t* msg, str* ctype, pv_spec_t *dst,
3345 3360
 	len = body.len;
3346 3361
 
3347 3362
 	/* note: header body can follow just after name: - fixit */
3348
-	while (find_hdr_line_start("Content-Type: ", 14, &start, &len))
3363
+	while (find_hdr_line_start("Content-Type: ", 14, &start, &len, &end))
3349 3364
 	{
3350
-		end = start + 14;
3351
-		len = len - 14;
3365
+		len = len - (end - start);
3352 3366
 		if (len > (ctype->len + 2)) {
3353 3367
 			if (strncasecmp(end, ctype->s, ctype->len)== 0)
3354 3368
 			{