Module: kamailio
Branch: master
Commit: 42228552b72267786561704f120e3da3aac5fd89
URL:
https://github.com/kamailio/kamailio/commit/42228552b72267786561704f120e3da…
Author: Daniel-Constantin Mierla <miconda(a)gmail.com>
Committer: Daniel-Constantin Mierla <miconda(a)gmail.com>
Date: 2020-09-03T12:47:50+02:00
core: helper functions to search variants of str in another str
---
Modified: src/core/tcp_read.c
Modified: src/core/ut.c
Modified: src/core/ut.h
---
Diff:
https://github.com/kamailio/kamailio/commit/42228552b72267786561704f120e3da…
Patch:
https://github.com/kamailio/kamailio/commit/42228552b72267786561704f120e3da…
---
diff --git a/src/core/tcp_read.c b/src/core/tcp_read.c
index 655db7da7b..6a005a9a32 100644
--- a/src/core/tcp_read.c
+++ b/src/core/tcp_read.c
@@ -110,26 +110,6 @@ int tcp_set_clone_rcvbuf(int v)
}
#ifdef READ_HTTP11
-static inline char *strfindcasestrz(str *haystack, char *needlez)
-{
- int i,j;
- str needle;
-
- needle.s = needlez;
- needle.len = strlen(needlez);
- for(i=0;i<haystack->len-needle.len;i++) {
- for(j=0;j<needle.len;j++) {
- if ( !((haystack->s[i+j]==needle.s[j]) ||
- ( isalpha((int)haystack->s[i+j])
- && ((haystack->s[i+j])^(needle.s[j]))==0x20 )) )
- break;
- }
- if (j==needle.len)
- return haystack->s+i;
- }
- return 0;
-}
-
int tcp_http11_continue(struct tcp_connection *c)
{
struct dest_info dst;
@@ -161,7 +141,7 @@ int tcp_http11_continue(struct tcp_connection *c)
return 0;
/* check for Expect header */
- if(strfindcasestrz(&msg, "Expect: 100-continue")!=NULL)
+ if(str_casesearch_strz(&msg, "Expect: 100-continue")!=NULL)
{
init_dst_from_rcv(&dst, &c->rcv);
if (tcp_send(&dst, 0, HTTP11CONTINUE, HTTP11CONTINUE_LEN) < 0) {
@@ -169,7 +149,7 @@ int tcp_http11_continue(struct tcp_connection *c)
}
}
/* check for Transfer-Encoding header */
- if(strfindcasestrz(&msg, "Transfer-Encoding: chunked")!=NULL)
+ if(str_casesearch_strz(&msg, "Transfer-Encoding: chunked")!=NULL)
{
c->req.flags |= F_TCP_REQ_BCHUNKED;
ret = 1;
@@ -178,7 +158,7 @@ int tcp_http11_continue(struct tcp_connection *c)
* - HTTP Via format is different that SIP Via
* - workaround: replace with Hia to be ignored by SIP parser
*/
- if((p=strfindcasestrz(&msg, "\nVia:"))!=NULL)
+ if((p=str_casesearch_strz(&msg, "\nVia:"))!=NULL)
{
p++;
*p = 'H';
diff --git a/src/core/ut.c b/src/core/ut.c
index 90b01d1026..0189b642ab 100644
--- a/src/core/ut.c
+++ b/src/core/ut.c
@@ -296,6 +296,53 @@ char *str_search(str *text, str *needle)
return NULL;
}
+/**
+ * @brief case insensitive search for occurence of needle in text
+ * @return pointer to start of needle in text or NULL if the needle
+ * is not found
+ */
+char *str_casesearch(str *text, str *needle)
+{
+ int i,j;
+ for(i=0;i<text->len-needle->len;i++) {
+ for(j=0;j<needle->len;j++) {
+ if ( !((text->s[i+j]==needle->s[j]) ||
+ ( isalpha((int)text->s[i+j])
+ && ((text->s[i+j])^(needle->s[j]))==0x20 )) )
+ break;
+ }
+ if (j==needle->len)
+ return text->s+i;
+ }
+ return 0;
+}
+
+/**
+ * portable strcasestr() - the libc version requires #define _GNU_SOURCE
+ */
+char *strz_casesearch_strz(char *textz, char *needlez)
+{
+ str text;
+
+ text.s = textz;
+ text.len = strlen(textz);
+
+ return str_casesearch_strz(&text, needlez);
+}
+
+/**
+ * case insensitive search of a charz string 'needlez' inside str 'text'
+ */
+char *str_casesearch_strz(str *text, char *needlez)
+{
+ str needle;
+
+ needle.s = needlez;
+ needle.len = strlen(needlez);
+
+ return str_casesearch(text, &needle);
+}
+
/*
* ser_memmem() returns the location of the first occurrence of data
* pattern b2 of size len2 in memory block b1 of size len1 or
diff --git a/src/core/ut.h b/src/core/ut.h
index 56c1f9e157..6ef2e079bc 100644
--- a/src/core/ut.h
+++ b/src/core/ut.h
@@ -1053,6 +1053,12 @@ char* get_abs_pathname(str* base, str* file);
*/
char *str_search(str *text, str *needle);
+char *str_casesearch(str *text, str *needle);
+
+char *strz_casesearch_strz(char *textz, char *needlez);
+
+char *str_casesearch_strz(str *text, char *needlez);
+
/*
* ser_memmem() returns the location of the first occurrence of data
* pattern b2 of size len2 in memory block b1 of size len1 or