#include <stdbool.h>
#include <inttypes.h>
#include <string.h>
-#include <strings.h>
+#include <ctype.h>
#include <errno.h>
#include <signal.h>
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;
}
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" ) ) )
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;
}