libmicrohttpd

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

commit e0d851cee3ada233b7ede637fd9155dfa7756907
parent 203d885e1ef7d6349e7f8c8df0c47b0181d8c137
Author: Evgeny Grin (Karlson2k) <k2k@narod.ru>
Date:   Fri,  3 May 2019 18:59:41 +0300

MHD_set_connection_value*(): optimization to avoid double strlen().

Diffstat:
Msrc/microhttpd/connection.c | 98+++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------
1 file changed, 66 insertions(+), 32 deletions(-)

diff --git a/src/microhttpd/connection.c b/src/microhttpd/connection.c @@ -715,19 +715,11 @@ MHD_get_connection_values (struct MHD_Connection *connection, /** * This function can be used to add an arbitrary entry to connection. - * This function could add entry with binary zero, which is allowed - * for #MHD_GET_ARGUMENT_KIND. For other kind on entries it is - * recommended to use #MHD_set_connection_value. - * - * This function MUST only be called from within the - * #MHD_AccessHandlerCallback (otherwise, access maybe improperly - * synchronized). Furthermore, the client must guarantee that the key - * and value arguments are 0-terminated strings that are NOT freed - * until the connection is closed. (The easiest way to do this is by - * passing only arguments to permanently allocated strings.). + * Internal version of #MHD_set_connection_value_n() without checking + * of arguments values. * * @param connection the connection for which a - * value should be set + * value should be set * @param kind kind of the value * @param key key for the value, must be zero-terminated * @param key_size number of bytes in @a key (excluding 0-terminator) @@ -739,20 +731,15 @@ MHD_get_connection_values (struct MHD_Connection *connection, * @ingroup request */ int -MHD_set_connection_value_n (struct MHD_Connection *connection, - enum MHD_ValueKind kind, - const char *key, - size_t key_size, - const char *value, - size_t value_size) +MHD_set_connection_value_n_nocheck_ (struct MHD_Connection *connection, + enum MHD_ValueKind kind, + const char *key, + size_t key_size, + const char *value, + size_t value_size) { struct MHD_HTTP_Header *pos; - if ( (MHD_GET_ARGUMENT_KIND != kind) && - ( ((key ? strlen(key) : 0) != key_size) || - ((value ? strlen(value) : 0) != value_size) ) ) - return MHD_NO; /* binary zero is allowed only in GET arguments */ - pos = MHD_pool_allocate (connection->pool, sizeof (struct MHD_HTTP_Header), MHD_YES); @@ -780,6 +767,53 @@ MHD_set_connection_value_n (struct MHD_Connection *connection, /** + * This function can be used to add an arbitrary entry to connection. + * This function could add entry with binary zero, which is allowed + * for #MHD_GET_ARGUMENT_KIND. For other kind on entries it is + * recommended to use #MHD_set_connection_value. + * + * This function MUST only be called from within the + * #MHD_AccessHandlerCallback (otherwise, access maybe improperly + * synchronized). Furthermore, the client must guarantee that the key + * and value arguments are 0-terminated strings that are NOT freed + * until the connection is closed. (The easiest way to do this is by + * passing only arguments to permanently allocated strings.). + * + * @param connection the connection for which a + * value should be set + * @param kind kind of the value + * @param key key for the value, must be zero-terminated + * @param key_size number of bytes in @a key (excluding 0-terminator) + * @param value the value itself, must be zero-terminated + * @param value_size number of bytes in @a value (excluding 0-terminator) + * @return #MHD_NO if the operation could not be + * performed due to insufficient memory; + * #MHD_YES on success + * @ingroup request + */ +int +MHD_set_connection_value_n (struct MHD_Connection *connection, + enum MHD_ValueKind kind, + const char *key, + size_t key_size, + const char *value, + size_t value_size) +{ + if ( (MHD_GET_ARGUMENT_KIND != kind) && + ( ((key ? strlen(key) : 0) != key_size) || + ((value ? strlen(value) : 0) != value_size) ) ) + return MHD_NO; /* binary zero is allowed only in GET arguments */ + + return MHD_set_connection_value_n_nocheck_ (connection, + kind, + key, + key_size, + value, + value_size); +} + + +/** * This function can be used to add an entry to the HTTP headers of a * connection (so that the #MHD_get_connection_values function will * return them -- and the `struct MHD_PostProcessor` will also see @@ -810,16 +844,16 @@ MHD_set_connection_value (struct MHD_Connection *connection, const char *key, const char *value) { - return MHD_set_connection_value_n (connection, - kind, - key, - NULL != key - ? strlen (key) - : 0, - value, - NULL != value - ? strlen (value) - : 0); + return MHD_set_connection_value_n_nocheck_ (connection, + kind, + key, + NULL != key + ? strlen (key) + : 0, + value, + NULL != value + ? strlen (value) + : 0); }