From 2a2a389a340561ff886d8349bb2802c99892c92d Mon Sep 17 00:00:00 2001 From: "Evgeny Grin (Karlson2k)" Date: Mon, 22 Dec 2014 19:42:00 +0000 Subject: Replace strcasecmp/strncasecmp with platform-independent macros --- src/include/platform_interface.h | 43 +++++++++++++++++++++++++++++++++++ src/microhttpd/connection.c | 48 ++++++++++++++++++++-------------------- src/microhttpd/digestauth.c | 4 ++-- src/microhttpd/postprocessor.c | 16 ++++++-------- 4 files changed, 76 insertions(+), 35 deletions(-) diff --git a/src/include/platform_interface.h b/src/include/platform_interface.h index f7373e7d..64b810c5 100644 --- a/src/include/platform_interface.h +++ b/src/include/platform_interface.h @@ -31,6 +31,49 @@ #include "w32functions.h" #endif +/* ***************************** + General function mapping + *****************************/ +#if !defined(_WIN32) || defined(__CYGWIN__) +/** + * Check two strings case-insensitive equality + * @param a first string to check + * @param b second string to check + * @return boolean true if strings are equal, boolean false if strings are unequal + */ +#define MHD_str_equal_caseless_(a,b) (0==strcasecmp((a),(b))) +#else +/** + * Check two strings case-insensitive equality + * @param a first string to check + * @param b second string to check + * @return boolean true if strings are equal, boolean false if strings are unequal + */ +#define MHD_str_equal_caseless_(a,b) (0==_stricmp((a),(b))) +#endif + +#if !defined(_WIN32) || defined(__CYGWIN__) +/** + * Check not more than n chars in two strings case-insensitive equality + * @param a first string to check + * @param b second string to check + * @param n maximum number of chars to check + * @return boolean true if strings are equal, boolean false if strings are unequal + */ +#define MHD_str_equal_caseless_n_(a,b,n) (0==strncasecmp((a),(b),(n))) +#else +/** + * Check not more than n chars in two strings case-insensitive equality + * @param a first string to check + * @param b second string to check + * @param n maximum number of chars to check + * @return boolean true if strings are equal, boolean false if strings are unequal + */ +#define MHD_str_equal_caseless_n_(a,b,n) (0==_strnicmp((a),(b),(n))) +#endif + + + /* MHD_socket_close_(fd) close any FDs (non-W32) / close only socket FDs (W32) */ #if !defined(_WIN32) || defined(__CYGWIN__) #define MHD_socket_close_(fd) close((fd)) diff --git a/src/microhttpd/connection.c b/src/microhttpd/connection.c index ae90e8a4..8464e909 100644 --- a/src/microhttpd/connection.c +++ b/src/microhttpd/connection.c @@ -226,7 +226,7 @@ MHD_lookup_connection_value (struct MHD_Connection *connection, ( (key == pos->header) || ( (NULL != pos->header) && (NULL != key) && - (0 == strcasecmp (key, pos->header))) )) + (MHD_str_equal_caseless_(key, pos->header))))) return pos->value; return NULL; } @@ -246,12 +246,12 @@ need_100_continue (struct MHD_Connection *connection) return ( (NULL == connection->response) && (NULL != connection->version) && - (0 == strcasecmp (connection->version, + (MHD_str_equal_caseless_(connection->version, MHD_HTTP_VERSION_1_1)) && (NULL != (expect = MHD_lookup_connection_value (connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_EXPECT))) && - (0 == strcasecmp (expect, "100-continue")) && + (MHD_str_equal_caseless_(expect, "100-continue")) && (connection->continue_message_write_offset < strlen (HTTP_100_CONTINUE)) ); } @@ -519,22 +519,22 @@ keepalive_possible (struct MHD_Connection *connection) end = MHD_lookup_connection_value (connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_CONNECTION); - if (0 == strcasecmp (connection->version, + if (MHD_str_equal_caseless_(connection->version, MHD_HTTP_VERSION_1_1)) { if (NULL == end) return MHD_YES; - if ( (0 == strcasecmp (end, "close")) || - (0 == strcasecmp (end, "upgrade")) ) + if ( (MHD_str_equal_caseless_ (end, "close")) || + (MHD_str_equal_caseless_ (end, "upgrade")) ) return MHD_NO; return MHD_YES; } - if (0 == strcasecmp (connection->version, + if (MHD_str_equal_caseless_(connection->version, MHD_HTTP_VERSION_1_0)) { if (NULL == end) return MHD_NO; - if (0 == strcasecmp (end, "Keep-Alive")) + if (MHD_str_equal_caseless_(end, "Keep-Alive")) return MHD_YES; return MHD_NO; } @@ -678,7 +678,7 @@ build_header_response (struct MHD_Connection *connection) "%s %u %s\r\n", (0 != (connection->responseCode & MHD_ICY_FLAG)) ? "ICY" - : ( (0 == strcasecmp (MHD_HTTP_VERSION_1_0, + : ( (MHD_str_equal_caseless_ (MHD_HTTP_VERSION_1_0, connection->version)) ? MHD_HTTP_VERSION_1_0 : MHD_HTTP_VERSION_1_1), @@ -717,16 +717,16 @@ build_header_response (struct MHD_Connection *connection) MHD_HTTP_HEADER_CONNECTION); response_has_keepalive = response_has_close; if ( (NULL != response_has_close) && - (0 != strcasecmp (response_has_close, "close")) ) + (!MHD_str_equal_caseless_ (response_has_close, "close")) ) response_has_close = NULL; if ( (NULL != response_has_keepalive) && - (0 != strcasecmp (response_has_keepalive, "Keep-Alive")) ) + (!MHD_str_equal_caseless_ (response_has_keepalive, "Keep-Alive")) ) response_has_keepalive = NULL; client_requested_close = MHD_lookup_connection_value (connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_CONNECTION); if ( (NULL != client_requested_close) && - (0 != strcasecmp (client_requested_close, "close")) ) + (!MHD_str_equal_caseless_ (client_requested_close, "close")) ) client_requested_close = NULL; /* now analyze chunked encoding situation */ @@ -750,7 +750,7 @@ build_header_response (struct MHD_Connection *connection) must_add_chunked_encoding = MHD_YES; connection->have_chunked_upload = MHD_YES; } - else if (0 == strcasecmp (have_encoding, "identity")) + else if (MHD_str_equal_caseless_(have_encoding, "identity")) { /* application forced identity encoding, can't do 'chunked' */ must_add_close = MHD_YES; @@ -782,7 +782,7 @@ build_header_response (struct MHD_Connection *connection) if ( (MHD_SIZE_UNKNOWN != connection->response->total_size) && (NULL == have_content_length) && ( (NULL == connection->method) || - (0 != strcasecmp (connection->method, + (!MHD_str_equal_caseless_ (connection->method, MHD_HTTP_METHOD_CONNECT)) ) ) { /* @@ -836,7 +836,7 @@ build_header_response (struct MHD_Connection *connection) if ( (pos->kind == kind) && (! ( (MHD_YES == must_add_close) && (pos->value == response_has_keepalive) && - (0 == strcasecmp (pos->header, + (MHD_str_equal_caseless_(pos->header, MHD_HTTP_HEADER_CONNECTION) ) ) ) ) size += strlen (pos->header) + strlen (pos->value) + 4; /* colon, space, linefeeds */ /* produce data */ @@ -889,7 +889,7 @@ build_header_response (struct MHD_Connection *connection) if ( (pos->kind == kind) && (! ( (pos->value == response_has_keepalive) && (MHD_YES == must_add_close) && - (0 == strcasecmp (pos->header, + (MHD_str_equal_caseless_(pos->header, MHD_HTTP_HEADER_CONNECTION) ) ) ) ) off += sprintf (&data[off], "%s: %s\r\n", @@ -1916,7 +1916,7 @@ parse_connection_headers (struct MHD_Connection *connection) parse_cookie_header (connection); if ( (0 != (MHD_USE_PEDANTIC_CHECKS & connection->daemon->options)) && (NULL != connection->version) && - (0 == strcasecmp (MHD_HTTP_VERSION_1_1, connection->version)) && + (MHD_str_equal_caseless_(MHD_HTTP_VERSION_1_1, connection->version)) && (NULL == MHD_lookup_connection_value (connection, MHD_HEADER_KIND, @@ -1947,7 +1947,7 @@ parse_connection_headers (struct MHD_Connection *connection) if (NULL != enc) { connection->remaining_upload_size = MHD_SIZE_UNKNOWN; - if (0 == strcasecmp (enc, "chunked")) + if (MHD_str_equal_caseless_(enc, "chunked")) connection->have_chunked_upload = MHD_YES; } else @@ -2390,9 +2390,9 @@ MHD_connection_handle_idle (struct MHD_Connection *connection) break; } if ( (NULL != connection->response) && - ( (0 == strcasecmp (connection->method, + ( (MHD_str_equal_caseless_ (connection->method, MHD_HTTP_METHOD_POST)) || - (0 == strcasecmp (connection->method, + (MHD_str_equal_caseless_ (connection->method, MHD_HTTP_METHOD_PUT))) ) { /* we refused (no upload allowed!) */ @@ -2607,7 +2607,7 @@ MHD_connection_handle_idle (struct MHD_Connection *connection) MHD_HTTP_HEADER_CONNECTION); if ( (MHD_YES == connection->read_closed) || (client_close) || - ((NULL != end) && (0 == strcasecmp (end, "close"))) ) + ((NULL != end) && (MHD_str_equal_caseless_ (end, "close"))) ) { connection->read_closed = MHD_YES; connection->read_buffer_offset = 0; @@ -2918,7 +2918,7 @@ MHD_queue_response (struct MHD_Connection *connection, connection->response = response; connection->responseCode = status_code; if ( (NULL != connection->method) && - (0 == strcasecmp (connection->method, MHD_HTTP_METHOD_HEAD)) ) + (MHD_str_equal_caseless_ (connection->method, MHD_HTTP_METHOD_HEAD)) ) { /* if this is a "HEAD" request, pretend that we have already sent the full message body */ @@ -2926,9 +2926,9 @@ MHD_queue_response (struct MHD_Connection *connection, } if ( (MHD_CONNECTION_HEADERS_PROCESSED == connection->state) && (NULL != connection->method) && - ( (0 == strcasecmp (connection->method, + ( (MHD_str_equal_caseless_ (connection->method, MHD_HTTP_METHOD_POST)) || - (0 == strcasecmp (connection->method, + (MHD_str_equal_caseless_ (connection->method, MHD_HTTP_METHOD_PUT))) ) { /* response was queued "early", refuse to read body / footers or diff --git a/src/microhttpd/digestauth.c b/src/microhttpd/digestauth.c index 0816b3ff..04676f53 100644 --- a/src/microhttpd/digestauth.c +++ b/src/microhttpd/digestauth.c @@ -114,7 +114,7 @@ digest_calc_ha1 (const char *alg, MD5Update (&md5, ":", 1); MD5Update (&md5, password, strlen (password)); MD5Final (ha1, &md5); - if (0 == strcasecmp (alg, "md5-sess")) + if (MHD_str_equal_caseless_(alg, "md5-sess")) { MD5Init (&md5); MD5Update (&md5, ha1, sizeof (ha1)); @@ -246,7 +246,7 @@ lookup_sub_value (char *dest, return 0; /* end quote not found */ qn = q2 + 1; } - if ( (0 == strncasecmp (ptr, + if ((MHD_str_equal_caseless_n_(ptr, key, keylen)) && (eq == &ptr[keylen]) ) diff --git a/src/microhttpd/postprocessor.c b/src/microhttpd/postprocessor.c index 95c7938f..c3814926 100644 --- a/src/microhttpd/postprocessor.c +++ b/src/microhttpd/postprocessor.c @@ -286,11 +286,10 @@ MHD_create_post_processor (struct MHD_Connection *connection, if (encoding == NULL) return NULL; boundary = NULL; - if (0 != strncasecmp (MHD_HTTP_POST_ENCODING_FORM_URLENCODED, encoding, + if (!MHD_str_equal_caseless_n_ (MHD_HTTP_POST_ENCODING_FORM_URLENCODED, encoding, strlen (MHD_HTTP_POST_ENCODING_FORM_URLENCODED))) { - if (0 != - strncasecmp (MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA, encoding, + if (!MHD_str_equal_caseless_n_ (MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA, encoding, strlen (MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA))) return NULL; boundary = @@ -503,7 +502,7 @@ try_match_header (const char *prefix, char *line, char **suffix) return MHD_NO; while (*line != 0) { - if (0 == strncasecmp (prefix, line, strlen (prefix))) + if (MHD_str_equal_caseless_n_ (prefix, line, strlen (prefix))) { *suffix = strdup (&line[strlen (prefix)]); return MHD_YES; @@ -663,7 +662,7 @@ process_multipart_headers (struct MHD_PostProcessor *pp, if (buf[newline] == '\r') pp->skip_rn = RN_OptN; buf[newline] = '\0'; - if (0 == strncasecmp ("Content-disposition: ", + if (MHD_str_equal_caseless_n_ ("Content-disposition: ", buf, strlen ("Content-disposition: "))) { try_get_value (&buf[strlen ("Content-disposition: ")], @@ -969,7 +968,7 @@ post_process_multipart (struct MHD_PostProcessor *pp, break; case PP_PerformCheckMultipart: if ((pp->content_type != NULL) && - (0 == strncasecmp (pp->content_type, + (MHD_str_equal_caseless_n_ (pp->content_type, "multipart/mixed", strlen ("multipart/mixed")))) { @@ -1137,11 +1136,10 @@ MHD_post_process (struct MHD_PostProcessor *pp, return MHD_YES; if (NULL == pp) return MHD_NO; - if (0 == strncasecmp (MHD_HTTP_POST_ENCODING_FORM_URLENCODED, pp->encoding, + if (MHD_str_equal_caseless_n_ (MHD_HTTP_POST_ENCODING_FORM_URLENCODED, pp->encoding, strlen(MHD_HTTP_POST_ENCODING_FORM_URLENCODED))) return post_process_urlencoded (pp, post_data, post_data_len); - if (0 == - strncasecmp (MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA, pp->encoding, + if (MHD_str_equal_caseless_n_ (MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA, pp->encoding, strlen (MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA))) return post_process_multipart (pp, post_data, post_data_len); /* this should never be reached */ -- cgit v1.2.3