Browse code

core: helper functions to search variants of str in another str

Daniel-Constantin Mierla authored on 03/09/2020 10:47:50
Showing 3 changed files
... ...
@@ -110,26 +110,6 @@ int tcp_set_clone_rcvbuf(int v)
110 110
 }
111 111
 
112 112
 #ifdef READ_HTTP11
113
-static inline char *strfindcasestrz(str *haystack, char *needlez)
114
-{
115
-	int i,j;
116
-	str needle;
117
-
118
-	needle.s = needlez;
119
-	needle.len = strlen(needlez);
120
-	for(i=0;i<haystack->len-needle.len;i++) {
121
-		for(j=0;j<needle.len;j++) {
122
-			if ( !((haystack->s[i+j]==needle.s[j]) ||
123
-					( isalpha((int)haystack->s[i+j])
124
-						&& ((haystack->s[i+j])^(needle.s[j]))==0x20 )) )
125
-				break;
126
-		}
127
-		if (j==needle.len)
128
-			return haystack->s+i;
129
-	}
130
-	return 0;
131
-}
132
-
133 113
 int tcp_http11_continue(struct tcp_connection *c)
134 114
 {
135 115
 	struct dest_info dst;
... ...
@@ -161,7 +141,7 @@ int tcp_http11_continue(struct tcp_connection *c)
161 141
 		return 0;
162 142
 
163 143
 	/* check for Expect header */
164
-	if(strfindcasestrz(&msg, "Expect: 100-continue")!=NULL)
144
+	if(str_casesearch_strz(&msg, "Expect: 100-continue")!=NULL)
165 145
 	{
166 146
 		init_dst_from_rcv(&dst, &c->rcv);
167 147
 		if (tcp_send(&dst, 0, HTTP11CONTINUE, HTTP11CONTINUE_LEN) < 0) {
... ...
@@ -169,7 +149,7 @@ int tcp_http11_continue(struct tcp_connection *c)
169 149
 		}
170 150
 	}
171 151
 	/* check for Transfer-Encoding header */
172
-	if(strfindcasestrz(&msg, "Transfer-Encoding: chunked")!=NULL)
152
+	if(str_casesearch_strz(&msg, "Transfer-Encoding: chunked")!=NULL)
173 153
 	{
174 154
 		c->req.flags |= F_TCP_REQ_BCHUNKED;
175 155
 		ret = 1;
... ...
@@ -178,7 +158,7 @@ int tcp_http11_continue(struct tcp_connection *c)
178 158
 	 * - HTTP Via format is different that SIP Via
179 159
 	 * - workaround: replace with Hia to be ignored by SIP parser
180 160
 	 */
181
-	if((p=strfindcasestrz(&msg, "\nVia:"))!=NULL)
161
+	if((p=str_casesearch_strz(&msg, "\nVia:"))!=NULL)
182 162
 	{
183 163
 		p++;
184 164
 		*p = 'H';
... ...
@@ -296,6 +296,53 @@ char *str_search(str *text, str *needle)
296 296
     return NULL;
297 297
 }
298 298
 
299
+/**
300
+ * @brief case insensitive search for occurence of needle in text
301
+ * @return pointer to start of needle in text or NULL if the needle
302
+ *	is not found
303
+ */
304
+char *str_casesearch(str *text, str *needle)
305
+{
306
+	int i,j;
307
+	for(i=0;i<text->len-needle->len;i++) {
308
+		for(j=0;j<needle->len;j++) {
309
+			if ( !((text->s[i+j]==needle->s[j]) ||
310
+					( isalpha((int)text->s[i+j])
311
+						&& ((text->s[i+j])^(needle->s[j]))==0x20 )) )
312
+				break;
313
+		}
314
+		if (j==needle->len)
315
+			return text->s+i;
316
+	}
317
+	return 0;
318
+}
319
+
320
+/**
321
+ * portable strcasestr() - the libc version requires #define _GNU_SOURCE
322
+ */
323
+char *strz_casesearch_strz(char *textz, char *needlez)
324
+{
325
+	str text;
326
+
327
+	text.s = textz;
328
+	text.len = strlen(textz);
329
+
330
+	return str_casesearch_strz(&text, needlez);
331
+}
332
+
333
+/**
334
+ * case insensitive search of a charz string 'needlez' inside str 'text'
335
+ */
336
+char *str_casesearch_strz(str *text, char *needlez)
337
+{
338
+	str needle;
339
+
340
+	needle.s = needlez;
341
+	needle.len = strlen(needlez);
342
+
343
+	return str_casesearch(text, &needle);
344
+}
345
+
299 346
 /*
300 347
  * ser_memmem() returns the location of the first occurrence of data
301 348
  * pattern b2 of size len2 in memory block b1 of size len1 or
... ...
@@ -1053,6 +1053,12 @@ char* get_abs_pathname(str* base, str* file);
1053 1053
  */
1054 1054
 char *str_search(str *text, str *needle);
1055 1055
 
1056
+char *str_casesearch(str *text, str *needle);
1057
+
1058
+char *strz_casesearch_strz(char *textz, char *needlez);
1059
+
1060
+char *str_casesearch_strz(str *text, char *needlez);
1061
+
1056 1062
 /*
1057 1063
  * ser_memmem() returns the location of the first occurrence of data
1058 1064
  * pattern b2 of size len2 in memory block b1 of size len1 or