libmicrohttpd2

HTTP server C library (MHD 2.x, alpha)
Log | Files | Refs | README | LICENSE

commit 855516fb7b0121214919a8410f98d806a4096b95
parent dec4c5771e73c117905010705aebc9971198d134
Author: Evgeny Grin (Karlson2k) <k2k@drgrin.dev>
Date:   Wed, 18 Nov 2026 15:26:05 +0100

Implemented new detection of basic HTTP methods

The code automatically selects the optimal detection algorithm depending
on the host architecture and the build options.

Diffstat:
Msrc/mhd2/Makefile.am | 1+
Msrc/mhd2/h2/h2_req_fields.c | 4+++-
Asrc/mhd2/http_method_parse.c | 625+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/mhd2/http_method_parse.h | 71+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/mhd2/stream_process_request.c | 81++-----------------------------------------------------------------------------
Msrc/mhd2/stream_process_request.h | 12------------
6 files changed, 702 insertions(+), 92 deletions(-)

diff --git a/src/mhd2/Makefile.am b/src/mhd2/Makefile.am @@ -72,6 +72,7 @@ libmicrohttpd2_la_SOURCES = \ mhd_public_api.h \ mhd_request.h mhd_reply.h mhd_response.h \ http_method.h http_prot_ver.h \ + http_method_parse.c http_method_parse.h \ http_status_str.c http_status_str.h \ mhd_enum_to_str.c \ action.c mhd_action.h \ diff --git a/src/mhd2/h2/h2_req_fields.c b/src/mhd2/h2/h2_req_fields.c @@ -65,7 +65,7 @@ #include "mhd_str.h" -#include "stream_process_request.h" +#include "http_method_parse.h" #include "h2_req_item_kinds.h" #include "h2_req_item_struct.h" @@ -232,6 +232,8 @@ req_pseudoheaders_preprocess (struct mhd_H2Stream *restrict s, mhd_H2_REQ_PRBLM_PSEUDOHDR_DUP); have_method = true; s->req.pos_method = *pos; + mhd_assert (0 == buff[item->offset + ph_method.len + 1u + + item->val_len]); s->req.method = mhd_parse_http_method (item->val_len, buff + item->offset + ph_method.len + 1u); diff --git a/src/mhd2/http_method_parse.c b/src/mhd2/http_method_parse.c @@ -0,0 +1,625 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later OR (GPL-2.0-or-later WITH eCos-exception-2.0) */ +/* + This file is part of GNU libmicrohttpd. + Copyright (C) 2026 Evgeny Grin (Karlson2k) + + GNU libmicrohttpd is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + GNU libmicrohttpd is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + Alternatively, you can redistribute GNU libmicrohttpd and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of + the License, or (at your option) any later version, together + with the eCos exception, as follows: + + As a special exception, if other files instantiate templates or + use macros or inline functions from this file, or you compile this + file and link it with other works to produce a work based on this + file, this file does not by itself cause the resulting work to be + covered by the GNU General Public License. However the source code + for this file must still be made available in accordance with + section (3) of the GNU General Public License v2. + + This exception does not invalidate any other reasons why a work + based on this file might be covered by the GNU General Public + License. + + You should have received copies of the GNU Lesser General Public + License and the GNU General Public License along with this library; + if not, see <https://www.gnu.org/licenses/>. +*/ + +/** + * @file src/mhd2/http_method_parse.c + * @brief The implementation of the HTTP method string parsing function + * @author Karlson2k (Evgeny Grin) + * + * Identification of the standard HTTP method in a request. + * This is done for every request, so the code is optimised for speed. The file + * has three versions of the function: for 64-bit platforms and two for 32-bit + * platforms (fast and compact). One of them is selected at build time. + */ + +#include "mhd_sys_options.h" + +#include "mhd_assert.h" + +#include "sys_base_types.h" + +#if defined(_MSC_FULL_VER) && !defined(__clang__) +# include <string.h> +/* Request intrinsic memcpy() for the data loads: otherwise MSVC /O1 calls + the library function. This must precede the inline definitions in + mhd_bithelpers.h. */ +# pragma intrinsic(memcpy) +#endif + +#include "mhd_byteorder.h" +#include "mhd_bithelpers.h" +#include "mhd_constexpr.h" +#include "mhd_predict.h" + +#if defined(MHD_UNIT_TESTING) && defined(mhd_H_M_TEST_UNKNOWN_ORDER) +/* Exercise the lookup's unknown-byte-order path. Only the lookup's choice + of representation is changed; the load helpers retain the host settings. */ +# undef mhd_BYTE_ORDER +#endif + +#if defined(mhd_BYTE_ORDER) && (mhd_BYTE_ORDER == mhd_LITTLE_ENDIAN) +/* Define nothing: data is loaded and processed as little-endian. + On LE hosts data is loaded natively, on non-LE hosts the bytes are + swapped on load. */ +#elif defined(mhd_BYTE_ORDER) && (mhd_BYTE_ORDER == mhd_BIG_ENDIAN) +/** + * Process loaded data as big-endian. + * When not defined, data is loaded and processed as little-endian. + */ +# define mhd_H_M_PROCESS_AS_BE 1 +#endif + +#include "http_method_parse.h" + +/* All standard method names have lengths 3..7, except for "*", which is + handled separately. OTHER below denotes mhd_HTTP_METHOD_OTHER, returned + for every name outside this set. The bytes of a name are called letters + below, whatever their values. + + For names of at least three letters, two groups of four bytes are loaded + as 32-bit values: the first bytes (bytes 0..3) and the last bytes (bytes + len-3..len: the last three letters and the required terminating zero). + For example, OPTIONS gives "OPTI" and "ONS\0"; GET gives "GET\0" for both. + At lengths 3..7 the first and the last bytes cover every letter, so + comparing both at the correct length verifies the name. Shorter names are + handled before these loads, and no padding beyond the terminating zero is + needed. */ + +/* Select mhd_H_M_FUNC_FOR_64 (64-bit keys), mhd_H_M_FUNC_FOR_32_FAST or + mhd_H_M_FUNC_FOR_32_COMPACT (32-bit values), unless one is predefined. + Use 64-bit keys when size_t or uint_fast32_t is at least 64 bits wide. + Otherwise MHD_FAVOR_SMALL_CODE selects the compact version; the fast + version is the default. */ +#if !defined(mhd_H_M_FUNC_FOR_64) && !defined(mhd_H_M_FUNC_FOR_32_FAST) && \ + !defined(mhd_H_M_FUNC_FOR_32_COMPACT) +# if ((SIZEOF_SIZE_T + 0) >= 8) || ((SIZEOF_UINT_FAST32_T + 0) >= 8) +# define mhd_H_M_FUNC_FOR_64 1 +# elif defined(MHD_FAVOR_SMALL_CODE) +# define mhd_H_M_FUNC_FOR_32_COMPACT 1 +# else +# define mhd_H_M_FUNC_FOR_32_FAST 1 +# endif +#endif + +/* Sanity check */ +#if defined(mhd_H_M_FUNC_FOR_64) && (mhd_H_M_FUNC_FOR_64 + 0) != 1 +# error mhd_H_M_FUNC_FOR_64 must be defined as 1 or not defined +#endif +#if defined(mhd_H_M_FUNC_FOR_32_FAST) && (mhd_H_M_FUNC_FOR_32_FAST + 0) != 1 +# error mhd_H_M_FUNC_FOR_32_FAST must be defined as 1 or not defined +#endif +#if defined(mhd_H_M_FUNC_FOR_32_COMPACT) && \ + (mhd_H_M_FUNC_FOR_32_COMPACT + 0) != 1 +# error mhd_H_M_FUNC_FOR_32_COMPACT must be defined as 1 or not defined +#endif +#if (defined(mhd_H_M_FUNC_FOR_64) + defined(mhd_H_M_FUNC_FOR_32_FAST) \ + + defined(mhd_H_M_FUNC_FOR_32_COMPACT)) != 1 +# error Exactly one mhd_H_M_FUNC_FOR_* macro must be defined +#endif + +/* Loads of the name (mhd_H_M_LOAD32()) and table initialisers + (mhd_H_M_PACK32()) use the same byte order, so equal bytes give equal + 32-bit values. At a known byte order this allows native loads without + byte swaps; otherwise the helpers assemble bytes into little-endian 32-bit + values. A wider uint_least32_t only adds zero bits. */ + +/** + * Read four bytes at the possibly unaligned address @p p as a 32-bit value + */ +#if !defined(mhd_H_M_PROCESS_AS_BE) +# define mhd_H_M_LOAD32(p) mhd_GET_32BIT_LE_UNALIGN (p) +#else +# define mhd_H_M_LOAD32(p) mhd_GET_32BIT_BE_UNALIGN (p) +#endif + +/** + * Pack bytes @p b0 .. @p b3 in memory order into a 32-bit value, matching + * what #mhd_H_M_LOAD32() would return for those bytes. + * Expands to an integer constant expression for table initialisers. + */ +#if !defined(mhd_H_M_PROCESS_AS_BE) +# define mhd_H_M_PACK32(b0, b1, b2, b3) \ + ((uint_least32_t) ((uint_least32_t) (uint8_t) (b0) \ + | ((uint_least32_t) (uint8_t) (b1) << 8u) \ + | ((uint_least32_t) (uint8_t) (b2) << 16u) \ + | ((uint_least32_t) (uint8_t) (b3) << 24u))) +#else +# define mhd_H_M_PACK32(b0, b1, b2, b3) \ + ((uint_least32_t) (((uint_least32_t) (uint8_t) (b0) << 24u) \ + | ((uint_least32_t) (uint8_t) (b1) << 16u) \ + | ((uint_least32_t) (uint8_t) (b2) << 8u) \ + | (uint_least32_t) (uint8_t) (b3))) +#endif + +/** + * Encode @p code for storing in a table: XOR with OTHER. + * The stored code of OTHER is zero. + */ +#define mhd_H_M_XOR_CODE(code) ((uint8_t) ((code) ^ mhd_HTTP_METHOD_OTHER)) + + +/** + * Handle lengths outside 3..7; the only standard method here is "*". + * @param len the length of the @p mtd name + * @param mtd the method name + * @return #mhd_HTTP_METHOD_ASTERISK or #mhd_HTTP_METHOD_OTHER + */ +#define mhd_PARSE_HTTP_METHOD_OUTSIDE_3_7(len, mtd) \ + ((mhd_COND_VERY_RARELY ((1u == (len)) && ('*' == (mtd)[0]))) ? \ + mhd_HTTP_METHOD_ASTERISK : mhd_HTTP_METHOD_OTHER) + + +#if defined(mhd_H_M_FUNC_FOR_64) + +/* The 64-bit lookup compares 64-bit keys: the first bytes in the low half and + the last bytes in the high half of the key. + + The length selects row = min (len, 8) - 3. Rows 0..4 cover lengths 3..7; + row 5 handles longer names and has no standard methods. Each row has two + candidate keys, at slots "row" and "5 + row": + + row (length) 0 (3) 1 (4) 2 (5) 3 (6) 4 (7) 5 (above 7) + first GET POST TRACE DELETE OPTIONS slot 5 (PUT) + second PUT HEAD slot 7 slot 8 CONNECT slot 10 (zero) + + Lengths 5 and 6 each have only one method, so their second candidate slots + (7 and 8) hold the stored codes (the codes XORed with OTHER) instead of + keys. The stored code indices are "2 * row" and "2 * row + 1" for the + first and second candidates. Reusing these slots keeps the keys and the + stored codes in one table with a shared base address: it is smaller and + keeps related loads close together to favour data-cache locality on the + request path. Being both small and fast, this version serves both speed + and size builds. + + Row 5 also reuses the PUT key at slot 5 instead of reserving another key + slot. + Unused candidates and all of row 5 have zero stored codes. Matches + against the repurposed code slots or row 5's keys therefore yield OTHER. */ + +/** + * Build the key of a method from its first bytes @p b0 .. @p b3 and its last + * bytes: the last three letters @p e0 .. @p e2 and the terminating zero. + */ +# define mhd_H_M_KEY64(b0, b1, b2, b3, e0, e1, e2) \ + (((uint_least64_t) mhd_H_M_PACK32 ((b0), (b1), (b2), (b3))) \ + | (((uint_least64_t) mhd_H_M_PACK32 ((e0), (e1), (e2), 0u)) \ + << 32u)) + +/** + * Defined if the stored codes can be read directly from slots 7 and 8. + * Otherwise each stored code is extracted numerically from its slot value. + * + * Direct access requires a known byte order and no gap in memory between the + * stored codes of slots 7 and 8. A wider uint_least64_t would introduce such + * a gap. + * For little-endian values the packing is the same for either access method, + * so #mhd_H_M_CODE64() can check the slot width with sizeof(). Big-endian + * byte packing also depends on the slot width, which must therefore be known + * before choosing the packing. Other or unknown byte orders use numerical + * extraction throughout. + */ +# if defined(mhd_BYTE_ORDER) && (mhd_BYTE_ORDER == mhd_BIG_ENDIAN) && \ + defined(SIZEOF_UINT_LEAST64_T) && (8 == SIZEOF_UINT_LEAST64_T) +# define mhd_H_M_CODES_DIRECT 1 +# elif defined(mhd_BYTE_ORDER) && (mhd_BYTE_ORDER == mhd_LITTLE_ENDIAN) +# define mhd_H_M_CODES_DIRECT 1 +# endif + +/** + * Pack the stored codes @p b0 .. @p b7 into a slot value. + * With #mhd_H_M_CODES_DIRECT, @p b0 is the first byte in memory; otherwise it + * is the least significant byte, as expected by #mhd_H_M_CODE64_BY_VAL(). + */ +# if defined(mhd_H_M_CODES_DIRECT) && (mhd_BYTE_ORDER == mhd_BIG_ENDIAN) +# define mhd_H_M_PACK64(b0, b1, b2, b3, b4, b5, b6, b7) \ + (((uint_least64_t) (b0) << 56u) | ((uint_least64_t) (b1) << 48u) \ + | ((uint_least64_t) (b2) << 40u) | ((uint_least64_t) (b3) << 32u) \ + | ((uint_least64_t) (b4) << 24u) | ((uint_least64_t) (b5) << 16u) \ + | ((uint_least64_t) (b6) << 8u) | (uint_least64_t) (b7)) +# else +# define mhd_H_M_PACK64(b0, b1, b2, b3, b4, b5, b6, b7) \ + ((uint_least64_t) (b0) | ((uint_least64_t) (b1) << 8u) \ + | ((uint_least64_t) (b2) << 16u) | ((uint_least64_t) (b3) << 24u) \ + | ((uint_least64_t) (b4) << 32u) | ((uint_least64_t) (b5) << 40u) \ + | ((uint_least64_t) (b6) << 48u) | ((uint_least64_t) (b7) << 56u)) +# endif + +/** + * Method keys and stored codes, arranged as described above. + */ +mhd_constexpr uint_least64_t mhd_h_m_tbl_64[11] = { + mhd_H_M_KEY64 ('G', 'E', 'T', 0u, 'G', 'E', 'T'), /* Slot 0, row 0 */ + mhd_H_M_KEY64 ('P', 'O', 'S', 'T', 'O', 'S', 'T'), /* Slot 1, row 1 */ + mhd_H_M_KEY64 ('T', 'R', 'A', 'C', 'A', 'C', 'E'), /* Slot 2, row 2 */ + mhd_H_M_KEY64 ('D', 'E', 'L', 'E', 'E', 'T', 'E'), /* Slot 3, row 3 */ + mhd_H_M_KEY64 ('O', 'P', 'T', 'I', 'O', 'N', 'S'), /* Slot 4, row 4 */ + mhd_H_M_KEY64 ('P', 'U', 'T', 0u, 'P', 'U', 'T'), /* Slot 5, rows 0, 5 */ + mhd_H_M_KEY64 ('H', 'E', 'A', 'D', 'E', 'A', 'D'), /* Slot 6, row 1 */ + /* Slot 7, row 2: no key, the stored codes 0..7 of the rows 0..3 */ + mhd_H_M_PACK64 (mhd_H_M_XOR_CODE (mhd_HTTP_METHOD_GET), /* Row 0 */ + mhd_H_M_XOR_CODE (mhd_HTTP_METHOD_PUT), + mhd_H_M_XOR_CODE (mhd_HTTP_METHOD_POST), /* Row 1 */ + mhd_H_M_XOR_CODE (mhd_HTTP_METHOD_HEAD), + mhd_H_M_XOR_CODE (mhd_HTTP_METHOD_TRACE), /* Row 2 */ + 0u, + mhd_H_M_XOR_CODE (mhd_HTTP_METHOD_DELETE), /* Row 3 */ + 0u), + /* Slot 8, row 3: no key, the stored codes 8..11 of the rows 4..5 */ + mhd_H_M_PACK64 (mhd_H_M_XOR_CODE (mhd_HTTP_METHOD_OPTIONS), /* Row 4 */ + mhd_H_M_XOR_CODE (mhd_HTTP_METHOD_CONNECT), + 0u, /* Row 5 */ + 0u, + 0u, 0u, 0u, 0u), /* Unused */ + mhd_H_M_KEY64 ('C', 'O', 'N', 'N', 'E', 'C', 'T'), /* Slot 9, row 4 */ + 0u /* Slot 10, row 5 */ +}; + +/** + * Extract the stored code @p i from the value of its slot + */ +# define mhd_H_M_CODE64_BY_VAL(i) \ + ((uint8_t) (mhd_h_m_tbl_64[7u + (i) / 8u] >> (8u * ((i) % 8u)))) + +/** + * Read the stored code @p i from slots 7 and 8 + */ +# if defined(mhd_H_M_CODES_DIRECT) +# define mhd_H_M_CODE64(i) \ + ((uint8_t) ((8u == sizeof(mhd_h_m_tbl_64[0])) ? \ + ((const uint8_t *) (mhd_h_m_tbl_64 + 7u))[(i)] : \ + mhd_H_M_CODE64_BY_VAL (i))) +# else +# define mhd_H_M_CODE64(i) mhd_H_M_CODE64_BY_VAL (i) +# endif + +MHD_INTERNAL MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_IN_ (2) MHD_FN_PAR_CSTR_ (2) +MHD_FN_PURE_ enum mhd_HTTP_Method +mhd_parse_http_method (size_t len, + const char *mtd) +{ + mhd_assert ('\0' == mtd[len]); + /* The required terminating zero makes four-byte loads safe at length 3 */ + if (mhd_COND_ALMOST_ALWAYS (3u <= len)) + { + /* Rows 0..4 cover lengths 3..7. Longer names use row 5, whose stored + codes are zero, so they always yield OTHER. */ + const size_t row = ((7u < len) ? 8u : len) - 3u; + const uint_least32_t first_bytes = mhd_H_M_LOAD32 (mtd); + const uint_least32_t last_bytes = mhd_H_M_LOAD32 (mtd + len - 3u); + /* The first and the last bytes can overlap. At lengths 3..7 they cover + every letter, so the key includes the entire name. */ + const uint_least64_t key = + ((uint_least64_t)first_bytes) | (((uint_least64_t)last_bytes) << 32u); + /* Subtracting an equality result (0 or 1) from unsigned zero gives an + all-zero or all-ones mask. The candidate keys differ, so at most one + of mask0 and mask1 can be non-zero. */ + const unsigned int mask0 = + 0u - (unsigned int)(mhd_h_m_tbl_64[row] == key); + const unsigned int mask1 = + 0u - (unsigned int)(mhd_h_m_tbl_64[5u + row] == key); + + /* The stored codes are code ^ OTHER. Each mask retains the stored code + of its matching candidate and clears it otherwise. With no match the + result is OTHER; one match gives OTHER ^ (code ^ OTHER), restoring the + code. An unused candidate's stored code is zero, so even a matching + key leaves OTHER. */ + return (enum mhd_HTTP_Method)(mhd_HTTP_METHOD_OTHER + ^ (mhd_H_M_CODE64 (2u * row) & mask0) + ^ (mhd_H_M_CODE64 (2u * row + 1u) & mask1)); + } + return mhd_PARSE_HTTP_METHOD_OUTSIDE_3_7 (len, mtd); +} + + +#else /* mhd_H_M_FUNC_FOR_32_FAST || mhd_H_M_FUNC_FOR_32_COMPACT */ + +/* The 32-bit versions avoid 64-bit key construction and comparison by keeping + the first bytes and the last bytes as separate 32-bit values. The length + and bits of the first letter select one candidate, so only that record + needs to be checked. + + The last bytes contain the last three letters and the terminating zero. + The caller guarantees that zero, so its value is known and need not be + stored in the table. A record uses this byte for the stored code instead: + + last bytes of the name: [ letter ][ letter ][ letter ][ zero ] + last bytes of a record: [ letter ][ letter ][ letter ][ code ] + + The diagram follows the byte order of the name. mhd_H_M_LAST_LETTERS_MASK + excludes the stored code from comparison; mhd_H_M_NUL_BIT locates it for + extraction in the chosen byte order. Keeping the stored code in the same + record avoids an extra field or a separate code-table lookup. */ + +/** + * Bit offset of the first letter in the first bytes + */ +# if !defined(mhd_H_M_PROCESS_AS_BE) +# define mhd_H_M_FIRST_BIT 0u +# else +# define mhd_H_M_FIRST_BIT 24u +# endif + +/** + * Bit offset of the terminating zero in the last bytes. + * A record stores its code at the same offset. + */ +# if !defined(mhd_H_M_PROCESS_AS_BE) +# define mhd_H_M_NUL_BIT 24u +# else +# define mhd_H_M_NUL_BIT 0u +# endif + +/** + * Mask of the last bytes selecting the last three letters and excluding the + * terminating zero + */ +# define mhd_H_M_LAST_LETTERS_MASK mhd_H_M_PACK32 (0xFFu, 0xFFu, 0xFFu, 0u) + +/** + * A record with the letters and the stored code of a method + */ +struct mhd_HttpMethodEntry32 +{ + /** + * The first bytes, including the terminating zero for length 3 + */ + uint_least32_t first_bytes; + /** + * The last bytes: the last three letters of the method and the stored code + * in place of the terminating zero + */ + uint_least32_t last_bytes; +}; + + +# if defined(mhd_H_M_FUNC_FOR_32_FAST) + +/* The fast version uses direct indexing into rows of four records. + The length selects row = min (len, 8) - 3: rows 0..4 cover lengths 3..7, + and row 5 handles longer names. The selector, bits 3..4 of the first + letter, selects a record within the row, distinguishing methods of the + same length (letter, its code -> selector): + + length 3 GET G 0x47 -> 0 PUT P 0x50 -> 2 + length 4 HEAD H 0x48 -> 1 POST P 0x50 -> 2 + length 5 TRACE T 0x54 -> 2 + length 6 DELETE D 0x44 -> 0 + length 7 CONNECT C 0x43 -> 0 OPTIONS O 0x4F -> 1 + + The index of the selected record is 4 * row + selector. Leaving empty + records keeps this formula simple and avoids an intermediate index table. + + The codes are stored XORed with OTHER, so empty records have zero stored + codes and therefore yield OTHER. Row 5 uses this rule to keep longer + extension methods on the ordinary lookup path, avoiding a separate + rejection by length in mixed method traffic. */ + +/** + * Build a record from the first bytes @p b0 .. @p b3, the last three letters + * @p e0 .. @p e2 and the code @p code, stored encoded by #mhd_H_M_XOR_CODE(). + */ +# define mhd_H_M_ENTRY32F(b0, b1, b2, b3, e0, e1, e2, code) \ + { mhd_H_M_PACK32 ((b0), (b1), (b2), (b3)), \ + mhd_H_M_PACK32 ((e0), (e1), (e2), mhd_H_M_XOR_CODE (code)) } + +/** + * The empty record: may match a name, but gives OTHER + */ +# define mhd_H_M_ENTRY32F_EMPTY { 0u, 0u } + +/** + * Method records arranged for direct indexing as described above + */ +mhd_constexpr struct mhd_HttpMethodEntry32 mhd_h_m_tbl_32f[24] = { + /* Row 0, length 3 */ + mhd_H_M_ENTRY32F ('G', 'E', 'T', 0u, 'G', 'E', 'T', mhd_HTTP_METHOD_GET), + mhd_H_M_ENTRY32F_EMPTY, + mhd_H_M_ENTRY32F ('P', 'U', 'T', 0u, 'P', 'U', 'T', mhd_HTTP_METHOD_PUT), + mhd_H_M_ENTRY32F_EMPTY, + /* Row 1, length 4 */ + mhd_H_M_ENTRY32F_EMPTY, + mhd_H_M_ENTRY32F ('H', 'E', 'A', 'D', 'E', 'A', 'D', mhd_HTTP_METHOD_HEAD), + mhd_H_M_ENTRY32F ('P', 'O', 'S', 'T', 'O', 'S', 'T', mhd_HTTP_METHOD_POST), + mhd_H_M_ENTRY32F_EMPTY, + /* Row 2, length 5 */ + mhd_H_M_ENTRY32F_EMPTY, + mhd_H_M_ENTRY32F_EMPTY, + mhd_H_M_ENTRY32F ('T', 'R', 'A', 'C', 'A', 'C', 'E', mhd_HTTP_METHOD_TRACE), + mhd_H_M_ENTRY32F_EMPTY, + /* Row 3, length 6 */ + mhd_H_M_ENTRY32F ('D', 'E', 'L', 'E', 'E', 'T', 'E', mhd_HTTP_METHOD_DELETE), + mhd_H_M_ENTRY32F_EMPTY, + mhd_H_M_ENTRY32F_EMPTY, + mhd_H_M_ENTRY32F_EMPTY, + /* Row 4, length 7 */ + mhd_H_M_ENTRY32F ('C', 'O', 'N', 'N', 'E', 'C', 'T', mhd_HTTP_METHOD_CONNECT), + mhd_H_M_ENTRY32F ('O', 'P', 'T', 'I', 'O', 'N', 'S', mhd_HTTP_METHOD_OPTIONS), + mhd_H_M_ENTRY32F_EMPTY, + mhd_H_M_ENTRY32F_EMPTY, + /* Row 5, lengths above 7 */ + mhd_H_M_ENTRY32F_EMPTY, + mhd_H_M_ENTRY32F_EMPTY, + mhd_H_M_ENTRY32F_EMPTY, + mhd_H_M_ENTRY32F_EMPTY +}; + +MHD_INTERNAL MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_IN_ (2) MHD_FN_PAR_CSTR_ (2) +MHD_FN_PURE_ enum mhd_HTTP_Method +mhd_parse_http_method (size_t len, + const char *mtd) +{ + mhd_assert ('\0' == mtd[len]); + /* The required terminating zero makes four-byte loads safe at length 3 */ + if (mhd_COND_ALMOST_ALWAYS (3u <= len)) + { + /* Lengths above 7 use an empty row, so they follow the lookup path but + cannot produce a standard method code. */ + const size_t row = ((7u < len) ? 8u : len) - 3u; + const uint_least32_t first_bytes = mhd_H_M_LOAD32 (mtd); + const uint_least32_t last_bytes = mhd_H_M_LOAD32 (mtd + len - 3u); + /* Bits 3..4 of the first letter select one candidate within this row */ + const uint_least32_t selector = + (first_bytes >> (mhd_H_M_FIRST_BIT + 3u)) & 3u; + const struct mhd_HttpMethodEntry32 *const entry = + mhd_h_m_tbl_32f + 4u * row + selector; + /* The record stores its code in place of the terminating zero. Ignore + that byte and OR the XOR differences of the first and the last bytes. + Testing the combined difference against zero gives 1 on a match and + 0 otherwise. Subtract that result from zero to obtain an all-ones or + all-zero mask. */ + const uint_least32_t mask = + (uint_least32_t) + (0u + - (uint_least32_t)(0u == ((first_bytes ^ entry->first_bytes) + | ((last_bytes ^ entry->last_bytes) + & mhd_H_M_LAST_LETTERS_MASK)))); + + /* Keep only the stored code (code ^ OTHER). A zero mask or an empty + record leaves OTHER; otherwise the final XOR restores the code. */ + return (enum mhd_HTTP_Method) + (mhd_HTTP_METHOD_OTHER + ^ ((entry->last_bytes >> mhd_H_M_NUL_BIT) & 0xFFu & mask)); + } + return mhd_PARSE_HTTP_METHOD_OUTSIDE_3_7 (len, + mtd); +} + + +# else /* mhd_H_M_FUNC_FOR_32_COMPACT */ + +/* The compact version stores one record per method, with no empty records. + With no spare row for longer methods, lengths outside 3..7 must be rejected + before indexing. It trades extra index arithmetic and the range check for + a smaller table. + + The records of one length form a row of one or two records. With + row = len - 3, base = row + min (row, 2) locates the first record of the + row. The selector, bit 1 XOR bit 3 of the first letter, distinguishes + methods of the same length: P, T, D and O give 0; G, H and C give 1. + The index of the selected record is base + selector: + + length 3 4 5 6 7 + base 0 2 4 5 6 + selector 0 PUT POST TRACE DELETE OPTIONS + selector 1 GET HEAD - - CONNECT + + At lengths 5 and 6, selector 1 uses the next length's record, whose first + letter cannot match the first letter of the name. */ + +/** + * Build a record from the first bytes @p b0 .. @p b3, the last three letters + * @p e0 .. @p e2 and the code @p code, stored without XOR encoding. + */ +# define mhd_H_M_ENTRY32C(b0, b1, b2, b3, e0, e1, e2, code) \ + { mhd_H_M_PACK32 ((b0), (b1), (b2), (b3)), \ + mhd_H_M_PACK32 ((e0), (e1), (e2), (code)) } + +/** + * Method records arranged for compact indexing as described above + */ +mhd_constexpr struct mhd_HttpMethodEntry32 mhd_h_m_tbl_32c[8] = { + /* Length 3, base 0: selector 0, selector 1 */ + mhd_H_M_ENTRY32C ('P', 'U', 'T', 0u, 'P', 'U', 'T', mhd_HTTP_METHOD_PUT), + mhd_H_M_ENTRY32C ('G', 'E', 'T', 0u, 'G', 'E', 'T', mhd_HTTP_METHOD_GET), + /* Length 4, base 2: selector 0, selector 1 */ + mhd_H_M_ENTRY32C ('P', 'O', 'S', 'T', 'O', 'S', 'T', mhd_HTTP_METHOD_POST), + mhd_H_M_ENTRY32C ('H', 'E', 'A', 'D', 'E', 'A', 'D', mhd_HTTP_METHOD_HEAD), + /* Length 5, base 4 */ + mhd_H_M_ENTRY32C ('T', 'R', 'A', 'C', 'A', 'C', 'E', mhd_HTTP_METHOD_TRACE), + /* Length 6, base 5 */ + mhd_H_M_ENTRY32C ('D', 'E', 'L', 'E', 'E', 'T', 'E', mhd_HTTP_METHOD_DELETE), + /* Length 7, base 6: selector 0, selector 1 */ + mhd_H_M_ENTRY32C ('O', 'P', 'T', 'I', 'O', 'N', 'S', mhd_HTTP_METHOD_OPTIONS), + mhd_H_M_ENTRY32C ('C', 'O', 'N', 'N', 'E', 'C', 'T', mhd_HTTP_METHOD_CONNECT) +}; + +MHD_INTERNAL MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_IN_ (2) MHD_FN_PAR_CSTR_ (2) +MHD_FN_PURE_ enum mhd_HTTP_Method +mhd_parse_http_method (size_t len, + const char *mtd) +{ + uint_least32_t first_bytes; + uint_least32_t last_bytes; + uint_least32_t row; + uint_least32_t selector; + uint_least32_t rec_idx; + uint_least32_t diff; + uint_least32_t code; + const struct mhd_HttpMethodEntry32 *entry; + + mhd_assert ('\0' == mtd[len]); + + /* Lengths 3..7 become 0..4 after subtraction. Shorter lengths wrap to + large size_t values; longer lengths exceed 4. Reject both before the + loads of the first and the last bytes and before access to the table, + which has no spare records. */ + if (4u < (size_t)(len - 3u)) + return mhd_PARSE_HTTP_METHOD_OUTSIDE_3_7 (len, + mtd); + + /* The last bytes include the required terminating zero; the records store + the code in its place. */ + first_bytes = mhd_H_M_LOAD32 (mtd); + last_bytes = mhd_H_M_LOAD32 (mtd + len - 3u); + /* Lengths 3..7 give rows 0..4 */ + row = (uint_least32_t)len - 3u; + /* Bits 1 and 3 of the first letter distinguish methods of the same + length */ + selector = ((first_bytes >> (mhd_H_M_FIRST_BIT + 1u)) + ^ (first_bytes >> (mhd_H_M_FIRST_BIT + 3u))) & 1u; + /* base + selector, where the bases of rows 0..4 are 0, 2, 4, 5 and 6. + At lengths 5 and 6, selector 1 reaches the next length's record, whose + first letter has selector 0 and therefore cannot match the first letter + of the name. */ + rec_idx = row + ((2u > row) ? row : 2u) + selector; + entry = mhd_h_m_tbl_32c + rec_idx; + /* Compare the first bytes and the last three letters, excluding the + stored code. A zero combined difference means the name matches. */ + diff = (first_bytes ^ entry->first_bytes) + | ((last_bytes ^ entry->last_bytes) & mhd_H_M_LAST_LETTERS_MASK); + /* Shift the stored code into the low byte; the uint8_t cast keeps only + that byte. Return it only on a match, otherwise return OTHER. */ + code = entry->last_bytes >> mhd_H_M_NUL_BIT; + return (0u == diff) ? (enum mhd_HTTP_Method)(uint8_t)code : + mhd_HTTP_METHOD_OTHER; +} + + +# endif /* mhd_H_M_FUNC_FOR_32_COMPACT */ + +#endif /* mhd_H_M_FUNC_FOR_32_FAST || mhd_H_M_FUNC_FOR_32_COMPACT */ diff --git a/src/mhd2/http_method_parse.h b/src/mhd2/http_method_parse.h @@ -0,0 +1,71 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later OR (GPL-2.0-or-later WITH eCos-exception-2.0) */ +/* + This file is part of GNU libmicrohttpd. + Copyright (C) 2026 Evgeny Grin (Karlson2k) + + GNU libmicrohttpd is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + GNU libmicrohttpd is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + Alternatively, you can redistribute GNU libmicrohttpd and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of + the License, or (at your option) any later version, together + with the eCos exception, as follows: + + As a special exception, if other files instantiate templates or + use macros or inline functions from this file, or you compile this + file and link it with other works to produce a work based on this + file, this file does not by itself cause the resulting work to be + covered by the GNU General Public License. However the source code + for this file must still be made available in accordance with + section (3) of the GNU General Public License v2. + + This exception does not invalidate any other reasons why a work + based on this file might be covered by the GNU General Public + License. + + You should have received copies of the GNU Lesser General Public + License and the GNU General Public License along with this library; + if not, see <https://www.gnu.org/licenses/>. +*/ + +/** + * @file src/mhd2/http_method_parse.h + * @brief The declaration of the HTTP method string parsing function + * @author Karlson2k (Evgeny Grin) + */ + +#ifndef MHD_HTTP_METHOD_PARSE_H +#define MHD_HTTP_METHOD_PARSE_H 1 + +#include "mhd_sys_options.h" + +#include "sys_sizet_type.h" + +#include "http_method.h" + +/** + * Identify the standard HTTP method by its name. + * + * The comparison is case-sensitive (RFC 9110, section 9.1). + * @param len the number of bytes in @p mtd, excluding the terminating zero; + * embedded binary zeros are included in this length + * @param mtd the method name, may be unaligned and contain binary zeros; + * at least @p len + 1 bytes must be readable and @p mtd[@p len] + * must be a binary zero + * @return the code of the matching standard HTTP method listed in + * #mhd_HTTP_Method, or #mhd_HTTP_METHOD_OTHER if none matches + */ +MHD_INTERNAL enum mhd_HTTP_Method +mhd_parse_http_method (size_t len, + const char *mtd) +MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_IN_ (2) MHD_FN_PAR_CSTR_ (2) MHD_FN_PURE_; + +#endif /* ! MHD_HTTP_METHOD_PARSE_H */ diff --git a/src/mhd2/stream_process_request.c b/src/mhd2/stream_process_request.c @@ -83,6 +83,7 @@ #include "respond_with_error.h" #include "stream_funcs.h" #include "daemon_funcs.h" +#include "http_method_parse.h" #ifdef MHD_SUPPORT_POST_PARSER # include "post_parser_funcs.h" @@ -480,85 +481,6 @@ /** - * Parse HTTP method string. - * @param len the length of the @a mtd string - * @param mtd the method string, does not need to be zero-terminated - * @return enum mhd_HTTP_Method value - */ -MHD_INTERNAL MHD_FN_PAR_NONNULL_ALL_ -MHD_FN_PAR_IN_SIZE_ (2, 1) -MHD_FN_PURE_ enum mhd_HTTP_Method -mhd_parse_http_method (size_t len, - const char mtd[MHD_FN_PAR_DYN_ARR_SIZE_ (len)]) -{ - switch (len) - { - case 3: /* mhd_SSTR_LEN (MHD_HTTP_METHOD_STR_GET) */ - /* mhd_SSTR_LEN (MHD_HTTP_METHOD_STR_PUT) */ - mhd_assert (len == mhd_SSTR_LEN (MHD_HTTP_METHOD_STR_GET)); - mhd_assert (len == mhd_SSTR_LEN (MHD_HTTP_METHOD_STR_PUT)); - if (0 == memcmp (mtd, - MHD_HTTP_METHOD_STR_GET, - mhd_SSTR_LEN (MHD_HTTP_METHOD_STR_GET))) - return mhd_HTTP_METHOD_GET; - else if (0 == memcmp (mtd, - MHD_HTTP_METHOD_STR_PUT, - mhd_SSTR_LEN (MHD_HTTP_METHOD_STR_PUT))) - return mhd_HTTP_METHOD_PUT; - break; - case 4: /* mhd_SSTR_LEN (MHD_HTTP_METHOD_STR_HEAD) */ - /* mhd_SSTR_LEN (MHD_HTTP_METHOD_STR_POST) */ - mhd_assert (len == mhd_SSTR_LEN (MHD_HTTP_METHOD_STR_HEAD)); - mhd_assert (len == mhd_SSTR_LEN (MHD_HTTP_METHOD_STR_POST)); - if (0 == memcmp (mtd, - MHD_HTTP_METHOD_STR_HEAD, - mhd_SSTR_LEN (MHD_HTTP_METHOD_STR_HEAD))) - return mhd_HTTP_METHOD_HEAD; - else if (0 == memcmp (mtd, - MHD_HTTP_METHOD_STR_POST, - mhd_SSTR_LEN (MHD_HTTP_METHOD_STR_POST))) - return mhd_HTTP_METHOD_POST; - break; - case 6: /* mhd_SSTR_LEN (MHD_HTTP_METHOD_STR_DELETE) */ - mhd_assert (len == mhd_SSTR_LEN (MHD_HTTP_METHOD_STR_DELETE)); - if (0 == memcmp (mtd, - MHD_HTTP_METHOD_STR_DELETE, - mhd_SSTR_LEN (MHD_HTTP_METHOD_STR_DELETE))) - return mhd_HTTP_METHOD_DELETE; - break; - case 7: /* mhd_SSTR_LEN (MHD_HTTP_METHOD_STR_CONNECT) */ - /* mhd_SSTR_LEN (MHD_HTTP_METHOD_STR_OPTIONS) */ - mhd_assert (len == mhd_SSTR_LEN (MHD_HTTP_METHOD_STR_CONNECT)); - mhd_assert (len == mhd_SSTR_LEN (MHD_HTTP_METHOD_STR_OPTIONS)); - if (0 == memcmp (mtd, - MHD_HTTP_METHOD_STR_CONNECT, - mhd_SSTR_LEN (MHD_HTTP_METHOD_STR_CONNECT))) - return mhd_HTTP_METHOD_CONNECT; - else if (0 == memcmp (mtd, - MHD_HTTP_METHOD_STR_OPTIONS, - mhd_SSTR_LEN (MHD_HTTP_METHOD_STR_OPTIONS))) - return mhd_HTTP_METHOD_OPTIONS; - break; - case 5: /* mhd_SSTR_LEN (MHD_HTTP_METHOD_STR_TRACE) */ - mhd_assert (len == mhd_SSTR_LEN (MHD_HTTP_METHOD_STR_TRACE)); - if (0 == memcmp (mtd, - MHD_HTTP_METHOD_STR_TRACE, - mhd_SSTR_LEN (MHD_HTTP_METHOD_STR_TRACE))) - return mhd_HTTP_METHOD_TRACE; - break; - case 1: /* mhd_SSTR_LEN (MHD_HTTP_METHOD_STR_ASTERISK) */ - mhd_assert (len == mhd_SSTR_LEN (MHD_HTTP_METHOD_STR_ASTERISK)); - if ('*' == mtd[0]) - return mhd_HTTP_METHOD_ASTERISK; - break; - default: - break; /* Handled after the "switch()" body */ - } - return mhd_HTTP_METHOD_OTHER; -} - - -/** * Detect standard HTTP request method * * @param connection the connection to process @@ -570,6 +492,7 @@ parse_http_std_method (struct MHD_Connection *restrict connection) const size_t len = connection->rq.method.len; /**< short alias */ mhd_assert (NULL != m); mhd_assert (0 != len); + mhd_assert (0 == m[len]); connection->rq.http_mthd = mhd_parse_http_method (len, m); diff --git a/src/mhd2/stream_process_request.h b/src/mhd2/stream_process_request.h @@ -56,7 +56,6 @@ #include "sys_base_types.h" #include "mhd_str_types.h" -#include "http_method.h" struct MHD_Connection; /* forward declaration */ struct MHD_UploadAction; /* forward declaration */ @@ -76,17 +75,6 @@ typedef bool const struct MHD_StringNullable *restrict value); /** - * Parse HTTP method string. - * @param len the length of the @a mtd string - * @param mtd the method string, does not need to be zero-terminated - * @return enum mhd_HTTP_Method value - */ -MHD_INTERNAL enum mhd_HTTP_Method -mhd_parse_http_method (size_t len, - const char mtd[MHD_FN_PAR_DYN_ARR_SIZE_ (len)]) -MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_IN_SIZE_ (2, 1) MHD_FN_PURE_; - -/** * Parse and unescape the arguments given by the client * as part of the HTTP request URI. *