aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEvgeny Grin (Karlson2k) <k2k@narod.ru>2017-04-05 12:45:32 +0300
committerEvgeny Grin (Karlson2k) <k2k@narod.ru>2017-04-05 12:56:40 +0300
commit218694a400beaf164712121475a6e22fd01b7b71 (patch)
treeced3b1a430831759372112a8d84f1c4e22c9ee9b
parent6ac4608cd802dba435d5aa7602ace312cacc8909 (diff)
downloadlibmicrohttpd-218694a400beaf164712121475a6e22fd01b7b71.tar.gz
libmicrohttpd-218694a400beaf164712121475a6e22fd01b7b71.zip
Converted many 'strlen()' from run-time to compile-time processing
-rw-r--r--ChangeLog5
-rw-r--r--src/microhttpd/basicauth.c4
-rw-r--r--src/microhttpd/connection.c26
-rw-r--r--src/microhttpd/digestauth.c8
-rw-r--r--src/microhttpd/internal.h4
-rw-r--r--src/microhttpd/postprocessor.c22
-rw-r--r--src/microhttpd/response.c4
7 files changed, 41 insertions, 32 deletions
diff --git a/ChangeLog b/ChangeLog
index 643dd071..d9f9b5be 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
1Wed Apr 05 12:53:26 MSK 2017
2 Fixed some compiler warnings.
3 Fixed error snprintf() errors detection in digestauth.c.
4 Converted many run-time 'strlen()' to compile-time calculations. -EG
5
1Sun Mar 26 13:49:01 MSK 2017 6Sun Mar 26 13:49:01 MSK 2017
2 Internal refactoring for simplification and unification. 7 Internal refactoring for simplification and unification.
3 Minor optimizations and minor fixes. 8 Minor optimizations and minor fixes.
diff --git a/src/microhttpd/basicauth.c b/src/microhttpd/basicauth.c
index b25e54fa..b2c4180d 100644
--- a/src/microhttpd/basicauth.c
+++ b/src/microhttpd/basicauth.c
@@ -57,9 +57,9 @@ MHD_basic_auth_get_username_password (struct MHD_Connection *connection,
57 MHD_HTTP_HEADER_AUTHORIZATION))) || 57 MHD_HTTP_HEADER_AUTHORIZATION))) ||
58 (0 != strncmp (header, 58 (0 != strncmp (header,
59 _BASIC_BASE, 59 _BASIC_BASE,
60 strlen (_BASIC_BASE))) ) 60 MHD_STATICSTR_LEN_ (_BASIC_BASE))) )
61 return NULL; 61 return NULL;
62 header += strlen (_BASIC_BASE); 62 header += MHD_STATICSTR_LEN_ (_BASIC_BASE);
63 if (NULL == (decode = BASE64Decode (header))) 63 if (NULL == (decode = BASE64Decode (header)))
64 { 64 {
65#ifdef HAVE_MESSAGES 65#ifdef HAVE_MESSAGES
diff --git a/src/microhttpd/connection.c b/src/microhttpd/connection.c
index b86f1e52..95835d59 100644
--- a/src/microhttpd/connection.c
+++ b/src/microhttpd/connection.c
@@ -489,7 +489,7 @@ need_100_continue (struct MHD_Connection *connection)
489 (MHD_str_equal_caseless_(expect, 489 (MHD_str_equal_caseless_(expect,
490 "100-continue")) && 490 "100-continue")) &&
491 (connection->continue_message_write_offset < 491 (connection->continue_message_write_offset <
492 strlen (HTTP_100_CONTINUE)) ); 492 MHD_STATICSTR_LEN_ (HTTP_100_CONTINUE)) );
493} 493}
494 494
495 495
@@ -1201,11 +1201,11 @@ build_header_response (struct MHD_Connection *connection)
1201 } 1201 }
1202 1202
1203 if (must_add_close) 1203 if (must_add_close)
1204 size += strlen ("Connection: close\r\n"); 1204 size += MHD_STATICSTR_LEN_ ("Connection: close\r\n");
1205 if (must_add_keep_alive) 1205 if (must_add_keep_alive)
1206 size += strlen ("Connection: Keep-Alive\r\n"); 1206 size += MHD_MACROSTR_LEN_ ("Connection: Keep-Alive\r\n");
1207 if (must_add_chunked_encoding) 1207 if (must_add_chunked_encoding)
1208 size += strlen ("Transfer-Encoding: chunked\r\n"); 1208 size += MHD_MACROSTR_LEN_ ("Transfer-Encoding: chunked\r\n");
1209 if (must_add_content_length) 1209 if (must_add_content_length)
1210 size += content_length_len; 1210 size += content_length_len;
1211 EXTRA_CHECK (! (must_add_close && must_add_keep_alive) ); 1211 EXTRA_CHECK (! (must_add_close && must_add_keep_alive) );
@@ -1241,24 +1241,24 @@ build_header_response (struct MHD_Connection *connection)
1241 /* we must add the 'Connection: close' header */ 1241 /* we must add the 'Connection: close' header */
1242 memcpy (&data[off], 1242 memcpy (&data[off],
1243 "Connection: close\r\n", 1243 "Connection: close\r\n",
1244 strlen ("Connection: close\r\n")); 1244 MHD_STATICSTR_LEN_ ("Connection: close\r\n"));
1245 off += strlen ("Connection: close\r\n"); 1245 off += MHD_STATICSTR_LEN_ ("Connection: close\r\n");
1246 } 1246 }
1247 if (must_add_keep_alive) 1247 if (must_add_keep_alive)
1248 { 1248 {
1249 /* we must add the 'Connection: Keep-Alive' header */ 1249 /* we must add the 'Connection: Keep-Alive' header */
1250 memcpy (&data[off], 1250 memcpy (&data[off],
1251 "Connection: Keep-Alive\r\n", 1251 "Connection: Keep-Alive\r\n",
1252 strlen ("Connection: Keep-Alive\r\n")); 1252 MHD_STATICSTR_LEN_ ("Connection: Keep-Alive\r\n"));
1253 off += strlen ("Connection: Keep-Alive\r\n"); 1253 off += MHD_STATICSTR_LEN_ ("Connection: Keep-Alive\r\n");
1254 } 1254 }
1255 if (must_add_chunked_encoding) 1255 if (must_add_chunked_encoding)
1256 { 1256 {
1257 /* we must add the 'Transfer-Encoding: chunked' header */ 1257 /* we must add the 'Transfer-Encoding: chunked' header */
1258 memcpy (&data[off], 1258 memcpy (&data[off],
1259 "Transfer-Encoding: chunked\r\n", 1259 "Transfer-Encoding: chunked\r\n",
1260 strlen ("Transfer-Encoding: chunked\r\n")); 1260 MHD_STATICSTR_LEN_ ("Transfer-Encoding: chunked\r\n"));
1261 off += strlen ("Transfer-Encoding: chunked\r\n"); 1261 off += MHD_STATICSTR_LEN_ ("Transfer-Encoding: chunked\r\n");
1262 } 1262 }
1263 if (must_add_content_length) 1263 if (must_add_content_length)
1264 { 1264 {
@@ -2343,7 +2343,7 @@ parse_connection_headers (struct MHD_Connection *connection)
2343#endif 2343#endif
2344 EXTRA_CHECK (NULL == connection->response); 2344 EXTRA_CHECK (NULL == connection->response);
2345 response = 2345 response =
2346 MHD_create_response_from_buffer (strlen (REQUEST_LACKS_HOST), 2346 MHD_create_response_from_buffer (MHD_STATICSTR_LEN_ (REQUEST_LACKS_HOST),
2347 REQUEST_LACKS_HOST, 2347 REQUEST_LACKS_HOST,
2348 MHD_RESPMEM_PERSISTENT); 2348 MHD_RESPMEM_PERSISTENT);
2349 MHD_queue_response (connection, 2349 MHD_queue_response (connection,
@@ -2535,7 +2535,7 @@ MHD_connection_handle_write (struct MHD_Connection *connection)
2535 ret = connection->send_cls (connection, 2535 ret = connection->send_cls (connection,
2536 &HTTP_100_CONTINUE 2536 &HTTP_100_CONTINUE
2537 [connection->continue_message_write_offset], 2537 [connection->continue_message_write_offset],
2538 strlen (HTTP_100_CONTINUE) - 2538 MHD_STATICSTR_LEN_ (HTTP_100_CONTINUE) -
2539 connection->continue_message_write_offset); 2539 connection->continue_message_write_offset);
2540 if (ret < 0) 2540 if (ret < 0)
2541 { 2541 {
@@ -2912,7 +2912,7 @@ MHD_connection_handle_idle (struct MHD_Connection *connection)
2912 continue; 2912 continue;
2913 case MHD_CONNECTION_CONTINUE_SENDING: 2913 case MHD_CONNECTION_CONTINUE_SENDING:
2914 if (connection->continue_message_write_offset == 2914 if (connection->continue_message_write_offset ==
2915 strlen (HTTP_100_CONTINUE)) 2915 MHD_STATICSTR_LEN_ (HTTP_100_CONTINUE))
2916 { 2916 {
2917 connection->state = MHD_CONNECTION_CONTINUE_SENT; 2917 connection->state = MHD_CONNECTION_CONTINUE_SENT;
2918 if (MHD_NO != socket_flush_possible (connection)) 2918 if (MHD_NO != socket_flush_possible (connection))
diff --git a/src/microhttpd/digestauth.c b/src/microhttpd/digestauth.c
index aad8ac15..8b219296 100644
--- a/src/microhttpd/digestauth.c
+++ b/src/microhttpd/digestauth.c
@@ -477,9 +477,9 @@ MHD_digest_auth_get_username(struct MHD_Connection *connection)
477 return NULL; 477 return NULL;
478 if (0 != strncmp (header, 478 if (0 != strncmp (header,
479 _BASE, 479 _BASE,
480 strlen (_BASE))) 480 MHD_STATICSTR_LEN_ (_BASE)))
481 return NULL; 481 return NULL;
482 header += strlen (_BASE); 482 header += MHD_STATICSTR_LEN_ (_BASE);
483 if (0 == (len = lookup_sub_value (user, 483 if (0 == (len = lookup_sub_value (user,
484 sizeof (user), 484 sizeof (user),
485 header, 485 header,
@@ -699,9 +699,9 @@ MHD_digest_auth_check (struct MHD_Connection *connection,
699 return MHD_NO; 699 return MHD_NO;
700 if (0 != strncmp (header, 700 if (0 != strncmp (header,
701 _BASE, 701 _BASE,
702 strlen(_BASE))) 702 MHD_STATICSTR_LEN_(_BASE)))
703 return MHD_NO; 703 return MHD_NO;
704 header += strlen (_BASE); 704 header += MHD_STATICSTR_LEN_ (_BASE);
705 left = strlen (header); 705 left = strlen (header);
706 706
707 { 707 {
diff --git a/src/microhttpd/internal.h b/src/microhttpd/internal.h
index 52bd8520..1b9df892 100644
--- a/src/microhttpd/internal.h
+++ b/src/microhttpd/internal.h
@@ -117,6 +117,10 @@ extern void *mhd_panic_cls;
117#define BUILTIN_NOT_REACHED 117#define BUILTIN_NOT_REACHED
118#endif 118#endif
119 119
120/**
121 * Determine length of static string / macro strings at compile time.
122 */
123#define MHD_STATICSTR_LEN_(macro) (sizeof(macro)/sizeof(char) - 1)
120 124
121/** 125/**
122 * State of the socket with respect to epoll (bitmask). 126 * State of the socket with respect to epoll (bitmask).
diff --git a/src/microhttpd/postprocessor.c b/src/microhttpd/postprocessor.c
index cf3c31ec..208686bb 100644
--- a/src/microhttpd/postprocessor.c
+++ b/src/microhttpd/postprocessor.c
@@ -296,19 +296,19 @@ MHD_create_post_processor (struct MHD_Connection *connection,
296 boundary = NULL; 296 boundary = NULL;
297 if (! MHD_str_equal_caseless_n_ (MHD_HTTP_POST_ENCODING_FORM_URLENCODED, 297 if (! MHD_str_equal_caseless_n_ (MHD_HTTP_POST_ENCODING_FORM_URLENCODED,
298 encoding, 298 encoding,
299 strlen (MHD_HTTP_POST_ENCODING_FORM_URLENCODED))) 299 MHD_STATICSTR_LEN_ (MHD_HTTP_POST_ENCODING_FORM_URLENCODED)))
300 { 300 {
301 if (! MHD_str_equal_caseless_n_ (MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA, 301 if (! MHD_str_equal_caseless_n_ (MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA,
302 encoding, 302 encoding,
303 strlen (MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA))) 303 MHD_STATICSTR_LEN_ (MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA)))
304 return NULL; 304 return NULL;
305 boundary = 305 boundary =
306 &encoding[strlen (MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA)]; 306 &encoding[MHD_STATICSTR_LEN_ (MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA)];
307 /* Q: should this be "strcasestr"? */ 307 /* Q: should this be "strcasestr"? */
308 boundary = strstr (boundary, "boundary="); 308 boundary = strstr (boundary, "boundary=");
309 if (NULL == boundary) 309 if (NULL == boundary)
310 return NULL; /* failed to determine boundary */ 310 return NULL; /* failed to determine boundary */
311 boundary += strlen ("boundary="); 311 boundary += MHD_STATICSTR_LEN_ ("boundary=");
312 blen = strlen (boundary); 312 blen = strlen (boundary);
313 if ( (blen == 0) || 313 if ( (blen == 0) ||
314 (blen * 2 + 2 > buffer_size) ) 314 (blen * 2 + 2 > buffer_size) )
@@ -710,12 +710,12 @@ process_multipart_headers (struct MHD_PostProcessor *pp,
710 buf[newline] = '\0'; 710 buf[newline] = '\0';
711 if (MHD_str_equal_caseless_n_ ("Content-disposition: ", 711 if (MHD_str_equal_caseless_n_ ("Content-disposition: ",
712 buf, 712 buf,
713 strlen ("Content-disposition: "))) 713 MHD_STATICSTR_LEN_ ("Content-disposition: ")))
714 { 714 {
715 try_get_value (&buf[strlen ("Content-disposition: ")], 715 try_get_value (&buf[MHD_STATICSTR_LEN_ ("Content-disposition: ")],
716 "name", 716 "name",
717 &pp->content_name); 717 &pp->content_name);
718 try_get_value (&buf[strlen ("Content-disposition: ")], 718 try_get_value (&buf[MHD_STATICSTR_LEN_ ("Content-disposition: ")],
719 "filename", 719 "filename",
720 &pp->content_filename); 720 &pp->content_filename);
721 } 721 }
@@ -1043,7 +1043,7 @@ post_process_multipart (struct MHD_PostProcessor *pp,
1043 if ( (NULL != pp->content_type) && 1043 if ( (NULL != pp->content_type) &&
1044 (MHD_str_equal_caseless_n_ (pp->content_type, 1044 (MHD_str_equal_caseless_n_ (pp->content_type,
1045 "multipart/mixed", 1045 "multipart/mixed",
1046 strlen ("multipart/mixed")))) 1046 MHD_STATICSTR_LEN_ ("multipart/mixed"))))
1047 { 1047 {
1048 pp->nested_boundary = strstr (pp->content_type, 1048 pp->nested_boundary = strstr (pp->content_type,
1049 "boundary="); 1049 "boundary=");
@@ -1053,7 +1053,7 @@ post_process_multipart (struct MHD_PostProcessor *pp,
1053 return MHD_NO; 1053 return MHD_NO;
1054 } 1054 }
1055 pp->nested_boundary = 1055 pp->nested_boundary =
1056 strdup (&pp->nested_boundary[strlen ("boundary=")]); 1056 strdup (&pp->nested_boundary[MHD_STATICSTR_LEN_ ("boundary=")]);
1057 if (NULL == pp->nested_boundary) 1057 if (NULL == pp->nested_boundary)
1058 { 1058 {
1059 /* out of memory */ 1059 /* out of memory */
@@ -1221,13 +1221,13 @@ MHD_post_process (struct MHD_PostProcessor *pp,
1221 return MHD_NO; 1221 return MHD_NO;
1222 if (MHD_str_equal_caseless_n_ (MHD_HTTP_POST_ENCODING_FORM_URLENCODED, 1222 if (MHD_str_equal_caseless_n_ (MHD_HTTP_POST_ENCODING_FORM_URLENCODED,
1223 pp->encoding, 1223 pp->encoding,
1224 strlen(MHD_HTTP_POST_ENCODING_FORM_URLENCODED))) 1224 MHD_STATICSTR_LEN_(MHD_HTTP_POST_ENCODING_FORM_URLENCODED)))
1225 return post_process_urlencoded (pp, 1225 return post_process_urlencoded (pp,
1226 post_data, 1226 post_data,
1227 post_data_len); 1227 post_data_len);
1228 if (MHD_str_equal_caseless_n_ (MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA, 1228 if (MHD_str_equal_caseless_n_ (MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA,
1229 pp->encoding, 1229 pp->encoding,
1230 strlen (MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA))) 1230 MHD_STATICSTR_LEN_ (MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA)))
1231 return post_process_multipart (pp, 1231 return post_process_multipart (pp,
1232 post_data, 1232 post_data,
1233 post_data_len); 1233 post_data_len);
diff --git a/src/microhttpd/response.c b/src/microhttpd/response.c
index e8707480..a86e58fc 100644
--- a/src/microhttpd/response.c
+++ b/src/microhttpd/response.c
@@ -75,8 +75,8 @@ add_response_entry (struct MHD_Response *response,
75 if ( (NULL == response) || 75 if ( (NULL == response) ||
76 (NULL == header) || 76 (NULL == header) ||
77 (NULL == content) || 77 (NULL == content) ||
78 (0 == strlen (header)) || 78 (0 == header[0]) ||
79 (0 == strlen (content)) || 79 (0 == content[0]) ||
80 (NULL != strchr (header, '\t')) || 80 (NULL != strchr (header, '\t')) ||
81 (NULL != strchr (header, '\r')) || 81 (NULL != strchr (header, '\r')) ||
82 (NULL != strchr (header, '\n')) || 82 (NULL != strchr (header, '\n')) ||