libmicrohttpd

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

commit 6ccaa5da57dbfb74e6131ce0ff0165522ef501d6
parent 5ba4b7709fc13ee3bbcb65083afa456be6fe3c7e
Author: Evgeny Grin (Karlson2k) <k2k@narod.ru>
Date:   Wed, 22 Jun 2022 20:57:50 +0300

MHD_parse_arguments_(): refactored, allow cls for the callback

Diffstat:
Msrc/microhttpd/connection.c | 9++++-----
Msrc/microhttpd/digestauth.c | 23+++++++++++++++++------
Msrc/microhttpd/internal.c | 17++++++-----------
Msrc/microhttpd/internal.h | 8++++----
4 files changed, 31 insertions(+), 26 deletions(-)

diff --git a/src/microhttpd/connection.c b/src/microhttpd/connection.c @@ -2725,8 +2725,7 @@ get_next_header_line (struct MHD_Connection *connection, * Add an entry to the HTTP headers of a connection. If this fails, * transmit an error response (request too big). * - * @param connection the connection for which a - * value should be set + * @param cls the context (connection) * @param kind kind of the value * @param key key for the value * @param key_size number of bytes in @a key @@ -2735,13 +2734,14 @@ get_next_header_line (struct MHD_Connection *connection, * @return #MHD_NO on failure (out of memory), #MHD_YES for success */ static enum MHD_Result -connection_add_header (struct MHD_Connection *connection, +connection_add_header (void *cls, const char *key, size_t key_size, const char *value, size_t value_size, enum MHD_ValueKind kind) { + struct MHD_Connection *connection = (struct MHD_Connection *) cls; if (MHD_NO == MHD_set_connection_value_n (connection, kind, @@ -3266,7 +3266,6 @@ parse_initial_message_line (struct MHD_Connection *connection, char *uri; char *http_version; char *args; - unsigned int unused_num_headers; if (NULL == (uri = memchr (line, ' ', @@ -3362,7 +3361,7 @@ parse_initial_message_line (struct MHD_Connection *connection, MHD_GET_ARGUMENT_KIND, args, &connection_add_header, - &unused_num_headers); + connection); } /* unescape URI *after* searching for arguments and log callback */ diff --git a/src/microhttpd/digestauth.c b/src/microhttpd/digestauth.c @@ -1185,11 +1185,17 @@ calculate_add_nonce_with_retry (struct MHD_Connection *const connection, } +struct test_header_param +{ + struct MHD_Connection *connection; + unsigned int num_headers; +}; + /** * Test if the given key-value pair is in the headers for the * given connection. * - * @param connection the connection + * @param cls the test context * @param key the key * @param key_size number of bytes in @a key * @param value the value, can be NULL @@ -1199,15 +1205,18 @@ calculate_add_nonce_with_retry (struct MHD_Connection *const connection, * #MHD_NO if not */ static enum MHD_Result -test_header (struct MHD_Connection *connection, +test_header (void *cls, const char *key, size_t key_size, const char *value, size_t value_size, enum MHD_ValueKind kind) { + struct test_header_param *const param = (struct test_header_param *) cls; + struct MHD_Connection *connection = param->connection; struct MHD_HTTP_Req_Header *pos; + param->num_headers++; for (pos = connection->headers_received; NULL != pos; pos = pos->next) { if (kind != pos->kind) @@ -1251,8 +1260,8 @@ check_argument_match (struct MHD_Connection *connection, { struct MHD_HTTP_Req_Header *pos; char *argb; - unsigned int num_headers; enum MHD_Result ret; + struct test_header_param param; argb = strdup (args); if (NULL == argb) @@ -1263,11 +1272,13 @@ check_argument_match (struct MHD_Connection *connection, #endif /* HAVE_MESSAGES */ return MHD_NO; } + param.connection = connection; + param.num_headers = 0; ret = MHD_parse_arguments_ (connection, MHD_GET_ARGUMENT_KIND, argb, &test_header, - &num_headers); + &param); free (argb); if (MHD_NO == ret) { @@ -1278,9 +1289,9 @@ check_argument_match (struct MHD_Connection *connection, { if (MHD_GET_ARGUMENT_KIND != pos->kind) continue; - num_headers--; + param.num_headers--; } - if (0 != num_headers) + if (0 != param.num_headers) { /* argument count mismatch */ return MHD_NO; diff --git a/src/microhttpd/internal.c b/src/microhttpd/internal.c @@ -186,7 +186,7 @@ MHD_http_unescape (char *val) * @param[in,out] args argument URI string (after "?" in URI), * clobbered in the process! * @param cb function to call on each key-value pair found - * @param[out] num_headers set to the number of headers found + * @param cls the iterator context * @return #MHD_NO on failure (@a cb returned #MHD_NO), * #MHD_YES for success (parsing succeeded, @a cb always * returned #MHD_YES) @@ -196,13 +196,12 @@ MHD_parse_arguments_ (struct MHD_Connection *connection, enum MHD_ValueKind kind, char *args, MHD_ArgumentIterator_ cb, - unsigned int *num_headers) + void *cls) { struct MHD_Daemon *daemon = connection->daemon; char *equals; char *amper; - *num_headers = 0; while ( (NULL != args) && ('\0' != args[0]) ) { @@ -220,14 +219,13 @@ MHD_parse_arguments_ (struct MHD_Connection *connection, key_len = daemon->unescape_callback (daemon->unescape_callback_cls, connection, args); - if (MHD_NO == cb (connection, + if (MHD_NO == cb (cls, args, key_len, NULL, 0, kind)) return MHD_NO; - (*num_headers)++; break; } /* got 'foo=bar' */ @@ -241,14 +239,13 @@ MHD_parse_arguments_ (struct MHD_Connection *connection, value_len = daemon->unescape_callback (daemon->unescape_callback_cls, connection, equals); - if (MHD_NO == cb (connection, + if (MHD_NO == cb (cls, args, key_len, equals, value_len, kind)) return MHD_NO; - (*num_headers)++; break; } /* amper is non-NULL here */ @@ -262,7 +259,7 @@ MHD_parse_arguments_ (struct MHD_Connection *connection, key_len = daemon->unescape_callback (daemon->unescape_callback_cls, connection, args); - if (MHD_NO == cb (connection, + if (MHD_NO == cb (cls, args, key_len, NULL, @@ -270,7 +267,6 @@ MHD_parse_arguments_ (struct MHD_Connection *connection, kind)) return MHD_NO; /* continue with 'bar' */ - (*num_headers)++; args = amper; continue; } @@ -286,14 +282,13 @@ MHD_parse_arguments_ (struct MHD_Connection *connection, value_len = daemon->unescape_callback (daemon->unescape_callback_cls, connection, equals); - if (MHD_NO == cb (connection, + if (MHD_NO == cb (cls, args, key_len, equals, value_len, kind)) return MHD_NO; - (*num_headers)++; args = amper; } return MHD_YES; diff --git a/src/microhttpd/internal.h b/src/microhttpd/internal.h @@ -2368,7 +2368,7 @@ MHD_unescape_plus (char *arg); * Callback invoked when iterating over @a key / @a value * argument pairs during parsing. * - * @param connection context of the iteration + * @param cls context of the iteration * @param key 0-terminated key string, never NULL * @param key_size number of bytes in key * @param value 0-terminated binary data, may include binary zeros, may be NULL @@ -2378,7 +2378,7 @@ MHD_unescape_plus (char *arg); * #MHD_NO to signal failure (and abort iteration) */ typedef enum MHD_Result -(*MHD_ArgumentIterator_)(struct MHD_Connection *connection, +(*MHD_ArgumentIterator_)(void *cls, const char *key, size_t key_size, const char *value, @@ -2395,7 +2395,7 @@ typedef enum MHD_Result * @param[in,out] args argument URI string (after "?" in URI), * clobbered in the process! * @param cb function to call on each key-value pair found - * @param[out] num_headers set to the number of headers found + * @param cls the iterator context * @return #MHD_NO on failure (@a cb returned #MHD_NO), * #MHD_YES for success (parsing succeeded, @a cb always * returned #MHD_YES) @@ -2405,7 +2405,7 @@ MHD_parse_arguments_ (struct MHD_Connection *connection, enum MHD_ValueKind kind, char *args, MHD_ArgumentIterator_ cb, - unsigned int *num_headers); + void *cls); /**