* Replaced str[n]casecmp() with own str_[n]icmp(), fixed case agnostic string search.
authorUrban Wallasch <urban.wallasch@freenet.de>
Sun, 27 Oct 2019 00:23:20 +0000 (02:23 +0200)
committerUrban Wallasch <urban.wallasch@freenet.de>
Sun, 27 Oct 2019 00:23:20 +0000 (02:23 +0200)
telehttpd.c

index ec0cacdda4bb64b85de81ed04cb726ddec17a1fe..27298e058b364d83a165134beaca2d2047969bf4 100644 (file)
@@ -7,7 +7,7 @@
 #include <stdbool.h>
 #include <inttypes.h>
 #include <string.h>
-#include <strings.h>
+#include <ctype.h>
 #include <errno.h>
 #include <signal.h>
 
@@ -99,11 +99,32 @@ static int check_shm( bool retry ) {
     return 0;
 }
 
-static char *strcasestr( const char *haystack, const char *needle ) {
-    size_t l = strlen( needle );
-    for ( char *p = (char *)haystack; *p; ++p )
-        if ( strncasecmp( p, needle, l ) == 0 )
-            return p;
+static int str_icmp( const char *s1, const char *s2 ) {
+    int r;
+    do {
+        r = tolower( (unsigned char)*s1 ) - tolower( (unsigned char)*s2 );
+    }
+    while ( 0 == r && *s1++ && *s2++ );
+    return r;
+}
+
+static int str_nicmp( const char *s1, const char *s2, size_t n ) {
+    int r;
+    if ( 0 == n )
+        return 0;
+    do {
+        r = tolower( (unsigned char)*s1 ) - tolower( (unsigned char)*s2 );
+    }
+    while ( 0 == r && --n && *s1++ && *s2++ );
+    return r;
+}
+
+static char *str_istr( const char *haystack, const char *needle ) {
+    size_t n = strlen( needle );
+    do {
+        if ( str_nicmp( haystack, needle, n ) == 0 )
+            return (char *)haystack;
+    } while ( *haystack++ );
     return NULL;
 }
 
@@ -111,7 +132,7 @@ static size_t copy_field_val( char *buf, size_t bufsz, const char *needle, const
     const char *s;
     size_t l;
 
-    if ( NULL != ( s = strcasestr( haystack, needle ) ) ) {
+    if ( NULL != ( s = str_istr( haystack, needle ) ) ) {
         const char *e;
         s += strlen( needle );
         if ( NULL != ( e = strstr( s, "\r\n" ) ) )
@@ -249,7 +270,7 @@ static int respond( int sock, const char *req ) {
             sockwrite( sock, hdr, n );
         break;
     }
-    if ( 0 < ret && 0 != strcasecmp( conn, "keep-alive" ) )
+    if ( 0 < ret && 0 != str_icmp( conn, "keep-alive" ) )
         ret = 0;
     return ret;
 }