libmicrohttpd

HTTP/1.x server C library (MHD 1.x, stable)
Log | Files | Refs | Submodules | README | LICENSE

commit 218694a400beaf164712121475a6e22fd01b7b71
parent 6ac4608cd802dba435d5aa7602ace312cacc8909
Author: Evgeny Grin (Karlson2k) <k2k@narod.ru>
Date:   Wed,  5 Apr 2017 12:45:32 +0300

Converted many 'strlen()' from run-time to compile-time processing

Diffstat:
MChangeLog | 5+++++
Msrc/microhttpd/basicauth.c | 4++--
Msrc/microhttpd/connection.c | 26+++++++++++++-------------
Msrc/microhttpd/digestauth.c | 8++++----
Msrc/microhttpd/internal.h | 4++++
Msrc/microhttpd/postprocessor.c | 22+++++++++++-----------
Msrc/microhttpd/response.c | 4++--
7 files changed, 41 insertions(+), 32 deletions(-)

diff --git a/ChangeLog b/ChangeLog @@ -1,3 +1,8 @@ +Wed Apr 05 12:53:26 MSK 2017 + Fixed some compiler warnings. + Fixed error snprintf() errors detection in digestauth.c. + Converted many run-time 'strlen()' to compile-time calculations. -EG + Sun Mar 26 13:49:01 MSK 2017 Internal refactoring for simplification and unification. Minor optimizations and minor fixes. diff --git a/src/microhttpd/basicauth.c b/src/microhttpd/basicauth.c @@ -57,9 +57,9 @@ MHD_basic_auth_get_username_password (struct MHD_Connection *connection, MHD_HTTP_HEADER_AUTHORIZATION))) || (0 != strncmp (header, _BASIC_BASE, - strlen (_BASIC_BASE))) ) + MHD_STATICSTR_LEN_ (_BASIC_BASE))) ) return NULL; - header += strlen (_BASIC_BASE); + header += MHD_STATICSTR_LEN_ (_BASIC_BASE); if (NULL == (decode = BASE64Decode (header))) { #ifdef HAVE_MESSAGES diff --git a/src/microhttpd/connection.c b/src/microhttpd/connection.c @@ -489,7 +489,7 @@ need_100_continue (struct MHD_Connection *connection) (MHD_str_equal_caseless_(expect, "100-continue")) && (connection->continue_message_write_offset < - strlen (HTTP_100_CONTINUE)) ); + MHD_STATICSTR_LEN_ (HTTP_100_CONTINUE)) ); } @@ -1201,11 +1201,11 @@ build_header_response (struct MHD_Connection *connection) } if (must_add_close) - size += strlen ("Connection: close\r\n"); + size += MHD_STATICSTR_LEN_ ("Connection: close\r\n"); if (must_add_keep_alive) - size += strlen ("Connection: Keep-Alive\r\n"); + size += MHD_MACROSTR_LEN_ ("Connection: Keep-Alive\r\n"); if (must_add_chunked_encoding) - size += strlen ("Transfer-Encoding: chunked\r\n"); + size += MHD_MACROSTR_LEN_ ("Transfer-Encoding: chunked\r\n"); if (must_add_content_length) size += content_length_len; EXTRA_CHECK (! (must_add_close && must_add_keep_alive) ); @@ -1241,24 +1241,24 @@ build_header_response (struct MHD_Connection *connection) /* we must add the 'Connection: close' header */ memcpy (&data[off], "Connection: close\r\n", - strlen ("Connection: close\r\n")); - off += strlen ("Connection: close\r\n"); + MHD_STATICSTR_LEN_ ("Connection: close\r\n")); + off += MHD_STATICSTR_LEN_ ("Connection: close\r\n"); } if (must_add_keep_alive) { /* we must add the 'Connection: Keep-Alive' header */ memcpy (&data[off], "Connection: Keep-Alive\r\n", - strlen ("Connection: Keep-Alive\r\n")); - off += strlen ("Connection: Keep-Alive\r\n"); + MHD_STATICSTR_LEN_ ("Connection: Keep-Alive\r\n")); + off += MHD_STATICSTR_LEN_ ("Connection: Keep-Alive\r\n"); } if (must_add_chunked_encoding) { /* we must add the 'Transfer-Encoding: chunked' header */ memcpy (&data[off], "Transfer-Encoding: chunked\r\n", - strlen ("Transfer-Encoding: chunked\r\n")); - off += strlen ("Transfer-Encoding: chunked\r\n"); + MHD_STATICSTR_LEN_ ("Transfer-Encoding: chunked\r\n")); + off += MHD_STATICSTR_LEN_ ("Transfer-Encoding: chunked\r\n"); } if (must_add_content_length) { @@ -2343,7 +2343,7 @@ parse_connection_headers (struct MHD_Connection *connection) #endif EXTRA_CHECK (NULL == connection->response); response = - MHD_create_response_from_buffer (strlen (REQUEST_LACKS_HOST), + MHD_create_response_from_buffer (MHD_STATICSTR_LEN_ (REQUEST_LACKS_HOST), REQUEST_LACKS_HOST, MHD_RESPMEM_PERSISTENT); MHD_queue_response (connection, @@ -2535,7 +2535,7 @@ MHD_connection_handle_write (struct MHD_Connection *connection) ret = connection->send_cls (connection, &HTTP_100_CONTINUE [connection->continue_message_write_offset], - strlen (HTTP_100_CONTINUE) - + MHD_STATICSTR_LEN_ (HTTP_100_CONTINUE) - connection->continue_message_write_offset); if (ret < 0) { @@ -2912,7 +2912,7 @@ MHD_connection_handle_idle (struct MHD_Connection *connection) continue; case MHD_CONNECTION_CONTINUE_SENDING: if (connection->continue_message_write_offset == - strlen (HTTP_100_CONTINUE)) + MHD_STATICSTR_LEN_ (HTTP_100_CONTINUE)) { connection->state = MHD_CONNECTION_CONTINUE_SENT; if (MHD_NO != socket_flush_possible (connection)) diff --git a/src/microhttpd/digestauth.c b/src/microhttpd/digestauth.c @@ -477,9 +477,9 @@ MHD_digest_auth_get_username(struct MHD_Connection *connection) return NULL; if (0 != strncmp (header, _BASE, - strlen (_BASE))) + MHD_STATICSTR_LEN_ (_BASE))) return NULL; - header += strlen (_BASE); + header += MHD_STATICSTR_LEN_ (_BASE); if (0 == (len = lookup_sub_value (user, sizeof (user), header, @@ -699,9 +699,9 @@ MHD_digest_auth_check (struct MHD_Connection *connection, return MHD_NO; if (0 != strncmp (header, _BASE, - strlen(_BASE))) + MHD_STATICSTR_LEN_(_BASE))) return MHD_NO; - header += strlen (_BASE); + header += MHD_STATICSTR_LEN_ (_BASE); left = strlen (header); { diff --git a/src/microhttpd/internal.h b/src/microhttpd/internal.h @@ -117,6 +117,10 @@ extern void *mhd_panic_cls; #define BUILTIN_NOT_REACHED #endif +/** + * Determine length of static string / macro strings at compile time. + */ +#define MHD_STATICSTR_LEN_(macro) (sizeof(macro)/sizeof(char) - 1) /** * State of the socket with respect to epoll (bitmask). diff --git a/src/microhttpd/postprocessor.c b/src/microhttpd/postprocessor.c @@ -296,19 +296,19 @@ MHD_create_post_processor (struct MHD_Connection *connection, boundary = NULL; if (! MHD_str_equal_caseless_n_ (MHD_HTTP_POST_ENCODING_FORM_URLENCODED, encoding, - strlen (MHD_HTTP_POST_ENCODING_FORM_URLENCODED))) + MHD_STATICSTR_LEN_ (MHD_HTTP_POST_ENCODING_FORM_URLENCODED))) { if (! MHD_str_equal_caseless_n_ (MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA, encoding, - strlen (MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA))) + MHD_STATICSTR_LEN_ (MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA))) return NULL; boundary = - &encoding[strlen (MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA)]; + &encoding[MHD_STATICSTR_LEN_ (MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA)]; /* Q: should this be "strcasestr"? */ boundary = strstr (boundary, "boundary="); if (NULL == boundary) return NULL; /* failed to determine boundary */ - boundary += strlen ("boundary="); + boundary += MHD_STATICSTR_LEN_ ("boundary="); blen = strlen (boundary); if ( (blen == 0) || (blen * 2 + 2 > buffer_size) ) @@ -710,12 +710,12 @@ process_multipart_headers (struct MHD_PostProcessor *pp, buf[newline] = '\0'; if (MHD_str_equal_caseless_n_ ("Content-disposition: ", buf, - strlen ("Content-disposition: "))) + MHD_STATICSTR_LEN_ ("Content-disposition: "))) { - try_get_value (&buf[strlen ("Content-disposition: ")], + try_get_value (&buf[MHD_STATICSTR_LEN_ ("Content-disposition: ")], "name", &pp->content_name); - try_get_value (&buf[strlen ("Content-disposition: ")], + try_get_value (&buf[MHD_STATICSTR_LEN_ ("Content-disposition: ")], "filename", &pp->content_filename); } @@ -1043,7 +1043,7 @@ post_process_multipart (struct MHD_PostProcessor *pp, if ( (NULL != pp->content_type) && (MHD_str_equal_caseless_n_ (pp->content_type, "multipart/mixed", - strlen ("multipart/mixed")))) + MHD_STATICSTR_LEN_ ("multipart/mixed")))) { pp->nested_boundary = strstr (pp->content_type, "boundary="); @@ -1053,7 +1053,7 @@ post_process_multipart (struct MHD_PostProcessor *pp, return MHD_NO; } pp->nested_boundary = - strdup (&pp->nested_boundary[strlen ("boundary=")]); + strdup (&pp->nested_boundary[MHD_STATICSTR_LEN_ ("boundary=")]); if (NULL == pp->nested_boundary) { /* out of memory */ @@ -1221,13 +1221,13 @@ MHD_post_process (struct MHD_PostProcessor *pp, return MHD_NO; if (MHD_str_equal_caseless_n_ (MHD_HTTP_POST_ENCODING_FORM_URLENCODED, pp->encoding, - strlen(MHD_HTTP_POST_ENCODING_FORM_URLENCODED))) + MHD_STATICSTR_LEN_(MHD_HTTP_POST_ENCODING_FORM_URLENCODED))) return post_process_urlencoded (pp, post_data, post_data_len); if (MHD_str_equal_caseless_n_ (MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA, pp->encoding, - strlen (MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA))) + MHD_STATICSTR_LEN_ (MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA))) return post_process_multipart (pp, post_data, post_data_len); diff --git a/src/microhttpd/response.c b/src/microhttpd/response.c @@ -75,8 +75,8 @@ add_response_entry (struct MHD_Response *response, if ( (NULL == response) || (NULL == header) || (NULL == content) || - (0 == strlen (header)) || - (0 == strlen (content)) || + (0 == header[0]) || + (0 == content[0]) || (NULL != strchr (header, '\t')) || (NULL != strchr (header, '\r')) || (NULL != strchr (header, '\n')) ||