libmicrohttpd2

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

test_incompatible.c (30411B)


      1 /* SPDX-License-Identifier: LGPL-2.1-or-later OR (GPL-2.0-or-later WITH eCos-exception-2.0) */
      2 /*
      3   This file is part of GNU libmicrohttpd.
      4   Copyright (C) 2025 Christian Grothoff
      5 
      6   GNU libmicrohttpd is free software; you can redistribute it and/or
      7   modify it under the terms of the GNU Lesser General Public
      8   License as published by the Free Software Foundation; either
      9   version 2.1 of the License, or (at your option) any later version.
     10 
     11   GNU libmicrohttpd is distributed in the hope that it will be useful,
     12   but WITHOUT ANY WARRANTY; without even the implied warranty of
     13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14   Lesser General Public License for more details.
     15 
     16   Alternatively, you can redistribute GNU libmicrohttpd and/or
     17   modify it under the terms of the GNU General Public License as
     18   published by the Free Software Foundation; either version 2 of
     19   the License, or (at your option) any later version, together
     20   with the eCos exception, as follows:
     21 
     22     As a special exception, if other files instantiate templates or
     23     use macros or inline functions from this file, or you compile this
     24     file and link it with other works to produce a work based on this
     25     file, this file does not by itself cause the resulting work to be
     26     covered by the GNU General Public License. However the source code
     27     for this file must still be made available in accordance with
     28     section (3) of the GNU General Public License v2.
     29 
     30     This exception does not invalidate any other reasons why a work
     31     based on this file might be covered by the GNU General Public
     32     License.
     33 
     34   You should have received copies of the GNU Lesser General Public
     35   License and the GNU General Public License along with this library;
     36   if not, see <https://www.gnu.org/licenses/>.
     37 */
     38 
     39 /**
     40  * @file test_incompatible.c
     41  * @brief tests server rejects incorrect or non-standard requests, either
     42  *   those that are:
     43  *   - incompatible to MUST requirements, or
     44  *   - non-standard and violate SHOULD requirements
     45  * @author Christian Grothoff
     46  */
     47 #include <stdio.h>
     48 #include <stdbool.h>
     49 #include <errno.h>
     50 #include <string.h>
     51 #include <stdlib.h>
     52 #include <unistd.h>
     53 #include <arpa/inet.h>
     54 #include <netinet/ip.h>
     55 #include "microhttpd2.h"
     56 
     57 #define LOG 0
     58 
     59 /**
     60  * Defines a test.
     61  */
     62 struct Test
     63 {
     64   /**
     65    * Human-readable name of the test. NULL to end test array.
     66    */
     67   const char *name;
     68 
     69   /**
     70    * Request to send to the server.
     71    */
     72   const char *upload;
     73 
     74 };
     75 
     76 
     77 /**
     78  * Tests with HTTP requests that violate MUST constraints
     79  * of the HTTP specifications.
     80  *
     81  * For example, using a bare LF instead of CRLF is forbidden, and
     82  * requests that include both a "Transfer-Encoding:" and a
     83  * "Content-Length:" headers are rejected.
     84  */
     85 static struct Test tests_must[] = {
     86   {
     87     .name = "HTTP 1.1 without Host",
     88     .upload = "GET / HTTP/1.1\r\n\r\n",
     89   },
     90   {
     91     .name = "HTTP 1.0 GET without CRLF",
     92     .upload = "GET / HTTP/1.0\n\n",
     93   },
     94   {
     95     .name = "POST with both Content-Length and Transfer-Encoding",
     96     .upload =
     97       "POST / HTTP/1.1\r\nHost: example.com\r\nContent-Length: 1\r\nTransfer-Encoding: chunked\r\n\r\n0\r\n",
     98   },
     99   {
    100     .name = "unsupported Ttransfer-Encoding",
    101     .upload =
    102       "POST / HTTP/1.1\r\nHost: example.com\r\nTransfer-Encoding: wild\r\n\r\n0\r\n",
    103   },
    104   {
    105     .name = "Invalid HTTP version format",
    106     .upload = "GET / HTTP/1\r\nHost: example.com\r\n\r\n",
    107     // RFC 9112 Section 2.3: HTTP-version must be "HTTP/" followed by two digits separated by "."
    108   },
    109   {
    110     .name = "Missing space after method",
    111     .upload = "GET/ HTTP/1.1\r\nHost: example.com\r\n\r\n",
    112     // RFC 9112 Section 3: Request-line requires SP between method and request-target
    113   },
    114   {
    115     .name = "Invalid request-target with space",
    116     .upload = "GET /path with space HTTP/1.1\r\nHost: example.com\r\n\r\n",
    117     // RFC 9112 Section 3.2: Request-target must not contain unencoded spaces
    118   },
    119   {
    120     .name = "Header field name with space",
    121     .upload = "GET / HTTP/1.1\r\nHost Name: example.com\r\n\r\n",
    122     // RFC 9110 Section 5.1: Field names must be tokens (no spaces allowed)
    123   },
    124 #ifdef MORE_PEER_VALIDATION
    125   {
    126     .name = "Header field name with colon",
    127     .upload = "GET / HTTP/1.1\r\nHost:Name: example.com\r\n\r\n",
    128     // RFC 9110 Section 5.1: Field names must be tokens (colons not allowed)
    129   },
    130 #endif
    131   {
    132     .name = "Missing colon after header field name",
    133     .upload = "GET / HTTP/1.1\r\nHost example.com\r\n\r\n",
    134     // RFC 9112 Section 5: Header field must have name, colon, and value
    135   },
    136   {
    137     .name = "Header line ending with bare CR",
    138     .upload = "GET / HTTP/1.1\rHost: example.com\r\n\r\n",
    139     // RFC 9112 Section 2.2: Lines must end with CRLF, not bare CR
    140   },
    141   {
    142     .name = "Request line ending with bare LF",
    143     .upload = "GET / HTTP/1.1\nHost: example.com\r\n\r\n",
    144     // RFC 9112 Section 2.2: Request-line must end with CRLF
    145   },
    146   {
    147     .name = "Multiple Host headers",
    148     .upload = "GET / HTTP/1.1\r\nHost: example.com\r\nHost: other.com\r\n\r\n",
    149     // RFC 9112 Section 3.2: A sender MUST NOT generate multiple Host header fields
    150   },
    151   {
    152     .name = "Negative Content-Length",
    153     .upload =
    154       "POST / HTTP/1.1\r\nHost: example.com\r\nContent-Length: -5\r\n\r\n",
    155     // RFC 9110 Section 8.6: Content-Length value must be non-negative decimal integer
    156   },
    157   {
    158     .name = "Non-numeric Content-Length",
    159     .upload =
    160       "POST / HTTP/1.1\r\nHost: example.com\r\nContent-Length: abc\r\n\r\n",
    161     // RFC 9110 Section 8.6: Content-Length must be a decimal integer
    162   },
    163   {
    164     .name = "Multiple Content-Length with different values",
    165     .upload =
    166       "POST / HTTP/1.1\r\nHost: example.com\r\nContent-Length: 5\r\nContent-Length: 10\r\n\r\n",
    167     // RFC 9110 Section 8.6: Multiple Content-Length values must be identical
    168   },
    169 #ifdef MORE_PEER_VALIDATION
    170   {
    171     .name = "Invalid method with control character",
    172     .upload = "GET\x01 / HTTP/1.1\r\nHost: example.com\r\n\r\n",
    173     // RFC 9110 Section 9.1: Method token must not contain control characters
    174   },
    175 #endif
    176   {
    177     .name = "Request-target starting with space",
    178     .upload = "GET  / HTTP/1.1\r\nHost: example.com\r\n\r\n",
    179     // RFC 9112 Section 3: Only single SP allowed between method and request-target
    180   },
    181   {
    182     .name = "HTTP/0.9 simple request with headers",
    183     .upload = "GET /\r\nHost: example.com\r\n\r\n",
    184     // RFC 9112 Section 2.3: HTTP/0.9 requests must not have headers
    185   },
    186   {
    187     .name = "Missing final CRLF after headers",
    188     .upload = "GET / HTTP/1.1\r\nHost: example.com\r\n",
    189     // RFC 9112 Section 6.1: Empty line (CRLF) required after headers
    190   },
    191   {
    192     .name = "Whitespace before header field name",
    193     .upload = "GET / HTTP/1.1\r\n Host: example.com\r\n\r\n",
    194     // RFC 9112 Section 5: No whitespace allowed before field name
    195   },
    196 
    197 
    198   {
    199     .name = "Empty request line",
    200     .upload = "\r\n\r\n",
    201     // RFC 9112 Section 3: Request-line is required
    202   },
    203   {
    204     .name = "Request line with only method",
    205     .upload = "GET\r\n\r\n",
    206     // RFC 9112 Section 3: Request-line must have method, target, and version
    207   },
    208   {
    209     .name = "Request line with only method and target",
    210     .upload = "GET /\r\n\r\n",
    211     // RFC 9112 Section 3: HTTP-version is required in request-line
    212   },
    213   {
    214     .name = "Request with only CR as line ending",
    215     .upload = "GET / HTTP/1.1\rHost: example.com\r\r",
    216     // RFC 9112 Section 2.2: CRLF required, not bare CR
    217   },
    218   {
    219     .name = "Missing space before HTTP version",
    220     .upload = "GET /HTTP/1.1\r\nHost: example.com\r\n\r\n",
    221     // RFC 9112 Section 3: SP required between request-target and HTTP-version
    222   },
    223   {
    224     .name = "HTTP version with extra dot",
    225     .upload = "GET / HTTP/1.1.0\r\nHost: example.com\r\n\r\n",
    226     // RFC 9112 Section 2.3: Version format is "HTTP/" DIGIT "." DIGIT
    227   },
    228   {
    229     .name = "HTTP version with letter",
    230     .upload = "GET / HTTP/1.A\r\nHost: example.com\r\n\r\n",
    231     // RFC 9112 Section 2.3: Version numbers must be digits
    232   },
    233 #ifdef NOT_A_BUG
    234   {
    235     .name = "Method with lowercase letters",
    236     .upload = "get / HTTP/1.1\r\nHost: example.com\r\n\r\n",
    237     // RFC 9110 Section 9.1: Method is case-sensitive
    238     /* This a valid non-standard ("custom") method. */
    239   },
    240 #endif
    241 #ifdef MORE_PEER_VALIDATION
    242   {
    243     .name = "Request-target with fragment identifier",
    244     .upload = "GET /path#fragment HTTP/1.1\r\nHost: example.com\r\n\r\n",
    245     // RFC 9112 Section 3.2.1: Fragment must not be sent in request-target
    246   },
    247 #endif
    248 #ifdef OTHER_USES
    249   {
    250     .name = "Absolute-form with userinfo in HTTP/1.1",
    251     .upload =
    252       "GET http://user:pass@example.com/ HTTP/1.1\r\nHost: example.com\r\n\r\n",
    253     // RFC 9110 Section 4.2.4: Userinfo (and its "@" delimiter) is now disallowed
    254     /* This is a valid string for HTTP proxy */
    255   },
    256 #endif
    257   {
    258     .name = "Request-target with bare CR",
    259     .upload = "GET /path\r/file HTTP/1.1\r\nHost: example.com\r\n\r\n",
    260     // RFC 9112 Section 3.2: Request-target must not contain CR
    261   },
    262   {
    263     .name = "Request-target with LF",
    264     .upload = "GET /path\n/file HTTP/1.1\r\nHost: example.com\r\n\r\n",
    265     // RFC 9112 Section 3.2: Request-target must not contain LF
    266   },
    267   {
    268     .name = "Header colon without field name",
    269     .upload = "GET / HTTP/1.1\r\n: value\r\nHost: example.com\r\n\r\n",
    270     // RFC 9110 Section 5.1: Field name is required before colon
    271   },
    272   {
    273     .name = "Content-Length with plus sign",
    274     .upload =
    275       "POST / HTTP/1.1\r\nHost: example.com\r\nContent-Length: +10\r\n\r\n0123456789",
    276     // RFC 9110 Section 8.6: Content-Length must be 1*DIGIT (no sign allowed)
    277   },
    278   {
    279     .name = "Content-Length with whitespace",
    280     .upload =
    281       "POST / HTTP/1.1\r\nHost: example.com\r\nContent-Length: 1 0\r\n\r\n0123456789",
    282     // RFC 9110 Section 8.6: Content-Length must be digits only
    283   },
    284   {
    285     .name = "Content-Length with decimal point",
    286     .upload =
    287       "POST / HTTP/1.1\r\nHost: example.com\r\nContent-Length: 10.0\r\n\r\n0123456789",
    288     // RFC 9110 Section 8.6: Content-Length must be decimal integer, not floating point
    289   },
    290   {
    291     .name = "Content-Length overflow value",
    292     .upload =
    293       "POST / HTTP/1.1\r\nHost: example.com\r\nContent-Length: 99999999999999999999999999999\r\n\r\n",
    294     // RFC 9110 Section 8.6: Content-Length must be valid decimal integer
    295   },
    296 #ifdef MORE_PEER_VALIDATION
    297   {
    298     .name = "Request with vertical tab in header",
    299     .upload = "GET / HTTP/1.1\r\nHost: example.com\r\nX-Custom:\vvalue\r\n\r\n",
    300     // RFC 9110 Section 5.5: Only HTAB, SP, and VCHAR allowed in field values (VT is 0x0B)
    301   },
    302 #endif
    303 #ifdef MORE_PEER_VALIDATION
    304   {
    305     .name = "Request with form feed in header value",
    306     .upload =
    307       "GET / HTTP/1.1\r\nHost: example.com\r\nX-Custom: val\fue\r\n\r\n",
    308     // RFC 9110 Section 5.5: Form feed (0x0C) not allowed in field values
    309   },
    310 #endif
    311   {
    312     .name = "Transfer-Encoding with unknown coding",
    313     .upload =
    314       "POST / HTTP/1.1\r\nHost: example.com\r\nTransfer-Encoding: gzip\r\n\r\n",
    315     // RFC 9112 Section 6.1: Only 'chunked' and specific codings defined
    316   },
    317   {
    318     .name = "Transfer-Encoding chunked not last",
    319     .upload =
    320       "POST / HTTP/1.1\r\nHost: example.com\r\nTransfer-Encoding: chunked, gzip\r\n\r\n",
    321     // RFC 9112 Section 6.1: 'chunked' must be last when present
    322   },
    323 #ifdef SPECIAL_STRICT_PEER_CHECKING
    324   {
    325     .name = "HTTP/1.0 with Transfer-Encoding",
    326     .upload =
    327       "POST / HTTP/1.0\r\nHost: example.com\r\nTransfer-Encoding: chunked\r\n\r\n0\r\n\r\n",
    328     // RFC 9112 Section 6.1: Transfer-Encoding must not be sent in HTTP/1.0
    329     /* RFC does not enforce the server to reject such requests. "Must not be sent" != "Must be rejected" */
    330   },
    331 #endif
    332 #ifdef NOT_A_BUG
    333   {
    334     .name = "POST without Content-Length or Transfer-Encoding",
    335     .upload = "POST / HTTP/1.1\r\nHost: example.com\r\n\r\n",
    336     // RFC 9112 Section 6.3: Message with body must have Content-Length or Transfer-Encoding
    337     /* This is a perfectly valid request with an empty body. */
    338   },
    339 #endif
    340 #ifdef MORE_PEER_VALIDATION
    341   {
    342     .name = "Request with CTL character in header name",
    343     .upload =
    344       "GET / HTTP/1.1\r\nHost: example.com\r\nX-Cu\x01stom: value\r\n\r\n",
    345     // RFC 9110 Section 5.1: Field names must be tokens, CTL chars not allowed
    346   },
    347 #endif
    348 #ifdef MORE_PEER_VALIDATION
    349   {
    350     .name = "Request with DEL character in header name",
    351     .upload =
    352       "GET / HTTP/1.1\r\nHost: example.com\r\nX-Custom\x7F: value\r\n\r\n",
    353     // RFC 9110 Section 5.1: DEL (0x7F) not allowed in field names
    354   },
    355 #endif
    356 #ifdef MORE_PEER_VALIDATION
    357   {
    358     .name = "Request with non-ASCII in header name",
    359     .upload = "GET / HTTP/1.1\r\nHost: example.com\r\nX-Cüstom: value\r\n\r\n",
    360     // RFC 9110 Section 5.1: Field names must be ASCII tokens
    361   },
    362 #endif
    363 #ifdef SPECIAL_STRICT_PEER_CHECKING
    364   {
    365     .name = "Request-target as asterisk for non-OPTIONS",
    366     .upload = "GET * HTTP/1.1\r\nHost: example.com\r\n\r\n",
    367     // RFC 9112 Section 3.2.4: Asterisk form only valid for OPTIONS
    368   },
    369 #endif
    370 #ifdef MORE_PEER_VALIDATION
    371   {
    372     .name = "Authority-form for non-CONNECT",
    373     .upload = "GET example.com:80 HTTP/1.1\r\nHost: example.com\r\n\r\n",
    374     // RFC 9112 Section 3.2.3: Authority-form only valid for CONNECT
    375   },
    376 #endif
    377   {
    378     .name = "HTTP/1.1 GET with empty Host header",
    379     .upload = "GET / HTTP/1.1\r\nHost: \r\n\r\n",
    380     // RFC 9112 Section 3.2: Empty Host header is invalid for this target form
    381   },
    382 #ifdef MORE_PEER_VALIDATION
    383   {
    384     .name = "Request with quoted string in field name",
    385     .upload =
    386       "GET / HTTP/1.1\r\nHost: example.com\r\n\"X-Custom\": value\r\n\r\n",
    387     // RFC 9110 Section 5.1: Field names must be tokens, not quoted strings
    388   },
    389 #endif
    390   {
    391     .name = "HTTP/1.1 request with HTTP/1.2 version",
    392     .upload = "GET / HTTP/1.2\r\nHost: example.com\r\n\r\n",
    393     // RFC 9112 Section 2.3: HTTP/1.2 is not defined
    394   },
    395   {
    396     .name = "HTTP version HTTP/2.0 on HTTP/1.1 connection",
    397     .upload = "GET / HTTP/2.0\r\nHost: example.com\r\n\r\n",
    398     // RFC 9112 Section 2.3: HTTP/2 uses different framing, not text-based
    399   },
    400   {
    401     .name = "Empty method",
    402     .upload = " / HTTP/1.1\r\nHost: example.com\r\n\r\n",
    403     // RFC 9112 Section 3: Method is required
    404   },
    405 #ifdef MORE_PEER_VALIDATION
    406   {
    407     .name = "Method with special character",
    408     .upload = "GET/ / HTTP/1.1\r\nHost: example.com\r\n\r\n",
    409     // RFC 9110 Section 9.1: Method must be a token (no '/' in method name)
    410   },
    411 #endif
    412   {
    413     .name = "Triple Host headers",
    414     .upload =
    415       "GET / HTTP/1.1\r\nHost: a.com\r\nHost: b.com\r\nHost: c.com\r\n\r\n",
    416     // RFC 9112 Section 3.2: Multiple Host headers forbidden
    417   },
    418   {
    419     .name = "Content-Length with hexadecimal",
    420     .upload =
    421       "POST / HTTP/1.1\r\nHost: example.com\r\nContent-Length: 0x10\r\n\r\n0123456789012345",
    422     // RFC 9110 Section 8.6: Content-Length must be decimal, not hexadecimal
    423   },
    424   {
    425     .name = NULL,
    426   }
    427 };
    428 
    429 
    430 /**
    431  * Tests with HTTP requests that violate MUST constraints
    432  * of the HTTP specifications during the upload.
    433  */
    434 static struct Test tests_must_upload[] = {
    435   {
    436     .name = "Chunked encoding with invalid hex",
    437     .upload =
    438       "POST / HTTP/1.1\r\nHost: example.com\r\nTransfer-Encoding: chunked\r\n\r\nGG\r\n\r\n",
    439     // RFC 9112 Section 7.1: Chunk size must be hexadecimal
    440   },
    441   {
    442     .name = "Chunked encoding with negative size",
    443     .upload =
    444       "POST / HTTP/1.1\r\nHost: example.com\r\nTransfer-Encoding: chunked\r\n\r\n-5\r\n\r\n",
    445     // RFC 9112 Section 7.1: Chunk size must be non-negative hex
    446   },
    447   {
    448     .name = "Chunked encoding missing CRLF after size",
    449     .upload =
    450       "POST / HTTP/1.1\r\nHost: example.com\r\nTransfer-Encoding: chunked\r\n\r\n5hello\r\n0\r\n\r\n",
    451     // RFC 9112 Section 7.1: CRLF required after chunk-size
    452   },
    453   {
    454     .name = "Chunked encoding missing CRLF after data",
    455     .upload =
    456       "POST / HTTP/1.1\r\nHost: example.com\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nhello0\r\n\r\n",
    457     // RFC 9112 Section 7.1: CRLF required after chunk-data
    458   },
    459   {
    460     .name = "Chunked with no final chunk",
    461     .upload =
    462       "POST / HTTP/1.1\r\nHost: example.com\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nhello\r\n",
    463     // RFC 9112 Section 7.1: Last chunk (size 0) is required
    464   },
    465   {
    466     .name = "Chunk size with leading whitespace",
    467     .upload =
    468       "POST / HTTP/1.1\r\nHost: example.com\r\nTransfer-Encoding: chunked\r\n\r\n 5\r\nhello\r\n0\r\n\r\n",
    469     // RFC 9112 Section 7.1: No whitespace before chunk-size
    470   },
    471   {
    472     .name = "Chunk size exceeding data provided",
    473     .upload =
    474       "POST / HTTP/1.1\r\nHost: example.com\r\nTransfer-Encoding: chunked\r\n\r\nA\r\nhello\r\n0\r\n\r\n",
    475     // RFC 9112 Section 7.1: Chunk-size must match chunk-data length (10 bytes expected, 5 provided)
    476   },
    477   {
    478     .name = NULL,
    479   }
    480 };
    481 
    482 
    483 /**
    484  * Tests with HTTP requests that violate SHOULD constraints
    485  * of the HTTP specifications.
    486  *
    487  * For example, for chunked encoding, this level (and more restrictive
    488  * ones) forbids whitespace in chunk extensions.  For cookie parsing,
    489  * this level (and more restrictive ones) rejects the entire cookie if
    490  * even a single value within it is incorrectly encoded.
    491  */
    492 static struct Test tests_should[] = {
    493   {
    494     .name = "Obsolete line folding in header",
    495     .upload = "GET / HTTP/1.1\r\nHost: example.com\r\n continuation\r\n\r\n",
    496     // RFC 9112 Section 5.2: Line folding is obsolete, recipients SHOULD reject or replace with SP
    497   },
    498 #ifdef NOT_A_BUG
    499   {
    500     .name = "Multiple spaces after colon in header",
    501     .upload = "GET / HTTP/1.1\r\nHost:     example.com\r\n\r\n",
    502     // RFC 9110 Section 5.6.3: Senders SHOULD NOT generate optional whitespace except as SP
    503     /* RFC 9110 Section 5.6.3: "OWS and RWS have the same semantics as a single SP."
    504      * RFC 9112 Section 5.1: "A field line value might be preceded and/or followed by optional whitespace (OWS)"
    505      * The server must parse this value as a correct value. */
    506   },
    507 #endif
    508 #ifdef NOT_A_BUG
    509   {
    510     .name = "Trailing whitespace in header value",
    511     .upload = "GET / HTTP/1.1\r\nHost: example.com   \r\n\r\n",
    512     // RFC 9110 Section 5.3: Trailing whitespace should be stripped
    513     /* RFC 9112 Section 5.1: "OWS occurring before the first non-whitespace octet of the field line value,
    514                               or after the last non-whitespace octet of the field line value, is excluded
    515                               by parsers when extracting the field line value from a field line."
    516      * This is a valid request. */
    517   },
    518 #endif
    519 #ifdef NOT_A_BUG
    520   {
    521     .name = "Leading whitespace in header value",
    522     .upload = "GET / HTTP/1.1\r\nHost:    example.com\r\n\r\n",
    523     // RFC 9110 Section 5.3: Leading whitespace should be stripped
    524     /* RFC 9112 Section 5.1: "OWS occurring before the first non-whitespace octet of the field line value,
    525                               or after the last non-whitespace octet of the field line value, is excluded
    526                               by parsers when extracting the field line value from a field line."
    527      * This is a valid request. */
    528   },
    529 #endif
    530 #ifdef NOT_A_BUG
    531   {
    532     .name = "Chunk extension with whitespace",
    533     .upload =
    534       "POST / HTTP/1.1\r\nHost: example.com\r\nTransfer-Encoding: chunked\r\n\r\n5 ; ext=val\r\nhello\r\n0\r\n\r\n",
    535     // RFC 9112 Section 7.1.1: Whitespace in chunk extensions should be minimal
    536     /* This is a valid request. */
    537   },
    538 #endif
    539 #ifdef NOT_A_BUG
    540   {
    541     .name = "Chunked with uppercase hex digits",
    542     .upload =
    543       "POST / HTTP/1.1\r\nHost: example.com\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nhello\r\n0\r\n\r\n",
    544     // RFC 9112 Section 7.1: Senders SHOULD use lowercase for hex digits (though uppercase is valid)
    545     /* This is a valid request. It must be parsed. */
    546   },
    547 #endif
    548 #ifdef NOT_A_BUG
    549   {
    550     .name = "Empty header field value",
    551     .upload = "GET / HTTP/1.1\r\nHost: example.com\r\nX-Empty:\r\n\r\n",
    552     // RFC 9110 Section 5.3: Empty field values are valid but some implementations may reject
    553     /* This is a valid request. It must be parsed. */
    554   },
    555 #endif
    556 #ifdef NOT_A_BUG
    557   {
    558     .name = "Header field name with uppercase and lowercase",
    559     .upload = "GET / HTTP/1.1\r\nHoSt: example.com\r\n\r\n",
    560     // RFC 9110 Section 5.1: Field names are case-insensitive, but conventional capitalization SHOULD be used
    561     /* This is a valid request. It must be parsed. */
    562   },
    563 #endif
    564   {
    565     .name = "Excessive whitespace in request line",
    566     .upload = "GET / HTTP/1.1 \r\nHost: example.com\r\n\r\n",
    567     // RFC 9112 Section 3: Trailing whitespace in request-line should not be present
    568   },
    569 #ifdef NOT_A_BUG
    570   {
    571     .name = "Content-Length with leading zeros",
    572     .upload =
    573       "POST / HTTP/1.1\r\nHost: example.com\r\nContent-Length: 0005\r\n\r\nhello",
    574     // RFC 9110 Section 8.6: Leading zeros should not be sent
    575     /* This is a valid request. It must be parsed. */
    576   },
    577 #endif
    578 #ifdef NOT_A_BUG
    579   {
    580     .name = "Cookie header with invalid encoding in value",
    581     .upload =
    582       "GET / HTTP/1.1\r\nHost: example.com\r\nCookie: name=val ue\r\n\r\n",
    583     // RFC 6265 Section 4.2: Cookie values should be properly encoded, spaces require encoding
    584     /* Rejected completely on stricter level. On default level valid part "name=val" is used. */
    585   },
    586 #endif
    587   {
    588     .name = NULL,
    589   }
    590 };
    591 
    592 
    593 static struct Test *current;
    594 
    595 
    596 /**
    597  * Our port.
    598  */
    599 static uint16_t port;
    600 
    601 /**
    602  * Set to true if a test failed.
    603  */
    604 static volatile bool failed;
    605 
    606 
    607 /**
    608  * Function to process data uploaded by a client.
    609  *
    610  * Given that we ONLY generate incorrect/malformed upload requests,
    611  * this function should never be called.
    612  *
    613  * @param upload_cls the argument given together with the function
    614  *                   pointer when the handler was registered with MHD
    615  * @param request the request is being processed
    616  * @param content_data_size the size of the @a content_data,
    617  *                          zero when all data have been processed
    618  * @param[in] content_data the uploaded content data,
    619  *                         may be modified in the callback,
    620  *                         valid only until return from the callback,
    621  *                         NULL when all data have been processed
    622  * @return action specifying how to proceed:
    623  *         #MHD_upload_action_continue() to continue upload (for incremental
    624  *         upload processing only),
    625  *         #MHD_upload_action_suspend() to stop reading the upload until
    626  *         the request is resumed,
    627  *         #MHD_upload_action_abort_request() to close the socket,
    628  *         or a response to discard the rest of the upload and transmit
    629  *         the response
    630  * @ingroup action
    631  */
    632 static const struct MHD_UploadAction *
    633 uc_fail (void *upload_cls,
    634          struct MHD_Request *request,
    635          size_t content_data_size,
    636          void *content_data)
    637 {
    638   fprintf (stderr,
    639            "Test `%s' failed\n",
    640            current->name);
    641   failed = true;
    642   return NULL;
    643 }
    644 
    645 
    646 /**
    647  * A client has requested the given url using the given method
    648  * (#MHD_HTTP_METHOD_GET, #MHD_HTTP_METHOD_PUT,
    649  * #MHD_HTTP_METHOD_DELETE, #MHD_HTTP_METHOD_POST, etc).
    650  * If @a upload_size is not zero and response action is provided by this
    651  * callback, then upload will be discarded and the stream (the connection for
    652  * HTTP/1.1) will be closed after sending the response.
    653  *
    654  * This function is expected to be called, but when we try to
    655  * process the upload it should always fail on the MHD side.
    656  *
    657  * @param cls argument given together with the function
    658  *        pointer when the handler was registered with MHD
    659  * @param request the request object
    660  * @param path the requested uri (without arguments after "?")
    661  * @param method the HTTP method used (#MHD_HTTP_METHOD_GET,
    662  *        #MHD_HTTP_METHOD_PUT, etc.)
    663  * @param upload_size the size of the message upload content payload,
    664  *                    #MHD_SIZE_UNKNOWN for chunked uploads (if the
    665  *                    final chunk has not been processed yet)
    666  * @return action how to proceed, NULL
    667  *         if the request must be aborted due to a serious
    668  *         error while handling the request (implies closure
    669  *         of underling data stream, for HTTP/1.1 it means
    670  *         socket closure).
    671  */
    672 static const struct MHD_Action *
    673 server_upload_req_cb (void *cls,
    674                       struct MHD_Request *MHD_RESTRICT request,
    675                       const struct MHD_String *MHD_RESTRICT path,
    676                       enum MHD_HTTP_Method method,
    677                       uint_fast64_t upload_size)
    678 {
    679   return MHD_action_process_upload (request,
    680                                     1024 * 1024,
    681                                     &uc_fail,
    682                                     NULL,
    683                                     &uc_fail,
    684                                     NULL);
    685 }
    686 
    687 
    688 /**
    689  * A client has requested the given url using the given method
    690  * (#MHD_HTTP_METHOD_GET, #MHD_HTTP_METHOD_PUT,
    691  * #MHD_HTTP_METHOD_DELETE, #MHD_HTTP_METHOD_POST, etc).
    692  * If @a upload_size is not zero and response action is provided by this
    693  * callback, then upload will be discarded and the stream (the connection for
    694  * HTTP/1.1) will be closed after sending the response.
    695  *
    696  * Given that we ONLY generate incorrect/malformed requests,
    697  * this function should never be called.
    698  *
    699  * @param cls argument given together with the function
    700  *        pointer when the handler was registered with MHD
    701  * @param request the request object
    702  * @param path the requested uri (without arguments after "?")
    703  * @param method the HTTP method used (#MHD_HTTP_METHOD_GET,
    704  *        #MHD_HTTP_METHOD_PUT, etc.)
    705  * @param upload_size the size of the message upload content payload,
    706  *                    #MHD_SIZE_UNKNOWN for chunked uploads (if the
    707  *                    final chunk has not been processed yet)
    708  * @return action how to proceed, NULL
    709  *         if the request must be aborted due to a serious
    710  *         error while handling the request (implies closure
    711  *         of underling data stream, for HTTP/1.1 it means
    712  *         socket closure).
    713  */
    714 static const struct MHD_Action *
    715 server_req_cb (void *cls,
    716                struct MHD_Request *MHD_RESTRICT request,
    717                const struct MHD_String *MHD_RESTRICT path,
    718                enum MHD_HTTP_Method method,
    719                uint_fast64_t upload_size)
    720 {
    721   fprintf (stderr,
    722            "Test `%s' failed\n",
    723            current->name);
    724   failed = true;
    725   return NULL;
    726 }
    727 
    728 
    729 /**
    730  * Helper function to deal with partial writes.
    731  * Fails hard (calls exit() on failures)!
    732  *
    733  * @param fd where to write to
    734  * @param buf what to write
    735  * @param buf_size number of bytes in @a buf
    736  */
    737 static void
    738 write_all (int fd,
    739            const void *buf,
    740            size_t buf_size)
    741 {
    742   const char *cbuf = buf;
    743   size_t off;
    744 
    745   off = 0;
    746   while (off < buf_size)
    747   {
    748     ssize_t ret;
    749 
    750     ret = write (fd,
    751                  &cbuf[off],
    752                  buf_size - off);
    753     if (ret <= 0)
    754     {
    755       fprintf (stderr,
    756                "Writing %u bytes to %d failed: %s\n",
    757                (unsigned int) (buf_size - off),
    758                fd,
    759                strerror (errno));
    760       exit (1);
    761     }
    762     off += ret;
    763   }
    764 }
    765 
    766 
    767 static int
    768 run_test ()
    769 {
    770   int s;
    771   struct sockaddr_in sa = {
    772     .sin_family = AF_INET,
    773     .sin_port = htons (port),
    774   };
    775   char dummy;
    776 
    777   s = socket (AF_INET, SOCK_STREAM, 0);
    778   if (-1 == s)
    779   {
    780     fprintf (stderr,
    781              "socket() failed: %s\n",
    782              strerror (errno));
    783     return 1;
    784   }
    785   inet_pton (AF_INET,
    786              "127.0.0.1",
    787              &sa.sin_addr);
    788   if (0 != connect (s,
    789                     (struct sockaddr *) &sa,
    790                     sizeof (sa)))
    791   {
    792     fprintf (stderr,
    793              "bind() failed: %s\n",
    794              strerror (errno));
    795     close (s);
    796     return 1;
    797   }
    798   write_all (s,
    799              current->upload,
    800              strlen (current->upload));
    801   shutdown (s,
    802             SHUT_WR);
    803   if (sizeof (dummy) !=
    804       read (s,
    805             &dummy,
    806             sizeof (dummy)))
    807   {
    808 #if LOG
    809     fprintf (stderr,
    810              "Server closed connection\n");
    811 #endif
    812   }
    813   close (s);
    814   if (failed)
    815     return 1;
    816   return 0;
    817 }
    818 
    819 
    820 static int
    821 run_tests (struct Test *tests)
    822 {
    823   for (unsigned int i = 0;
    824        NULL != tests[i].name;
    825        i++)
    826   {
    827     current = &tests[i];
    828 #if LOG || 1
    829     fprintf (stderr,
    830              "Running test `%s'\n",
    831              current->name);
    832 #endif
    833     if (0 != run_test ())
    834       return 1;
    835   }
    836   return 0;
    837 }
    838 
    839 
    840 static int
    841 run (MHD_RequestCallback cb,
    842      struct Test *tests,
    843      enum MHD_ProtocolStrictLevel psl)
    844 {
    845   struct MHD_Daemon *d;
    846 
    847   d = MHD_daemon_create (cb,
    848                          NULL);
    849   if (MHD_SC_OK !=
    850       MHD_DAEMON_SET_OPTIONS (
    851         d,
    852         MHD_D_OPTION_WM_WORKER_THREADS (2),
    853 #if ! LOG
    854         MHD_D_OPTION_LOG_CALLBACK (NULL,
    855                                    NULL),
    856 #endif
    857         MHD_D_OPTION_PROTOCOL_STRICT_LEVEL (psl,
    858                                             MHD_USL_PRECISE),
    859         MHD_D_OPTION_DEFAULT_TIMEOUT_MILSEC (1000),
    860         MHD_D_OPTION_BIND_PORT (MHD_AF_AUTO,
    861                                 0)))
    862   {
    863     fprintf (stderr,
    864              "Failed to configure daemon!");
    865     return 1;
    866   }
    867 
    868   {
    869     enum MHD_StatusCode sc;
    870 
    871     sc = MHD_daemon_start (d);
    872     if (MHD_SC_OK != sc)
    873     {
    874 #ifdef FIXME_STATUS_CODE_TO_STRING_NOT_IMPLEMENTED
    875       fprintf (stderr,
    876                "Failed to start server: %s\n",
    877                MHD_status_code_to_string_lazy (sc));
    878 #else
    879       fprintf (stderr,
    880                "Failed to start server: %u\n",
    881                (unsigned int) sc);
    882 #endif
    883       MHD_daemon_destroy (d);
    884       return 1;
    885     }
    886   }
    887 
    888   {
    889     union MHD_DaemonInfoFixedData info;
    890     enum MHD_StatusCode sc;
    891 
    892     sc = MHD_daemon_get_info_fixed (
    893       d,
    894       MHD_DAEMON_INFO_FIXED_BIND_PORT,
    895       &info);
    896     if (MHD_SC_OK != sc)
    897     {
    898       fprintf (stderr,
    899                "Failed to determine our port: %u\n",
    900                (unsigned int) sc);
    901       MHD_daemon_destroy (d);
    902       return 1;
    903     }
    904     port = info.v_bind_port_uint16;
    905   }
    906 
    907   {
    908     int result;
    909 
    910     result = run_tests (tests);
    911     MHD_daemon_destroy (d);
    912     return result;
    913   }
    914 }
    915 
    916 
    917 int
    918 main (void)
    919 {
    920   if (0 !=
    921       run (&server_req_cb,
    922            tests_must,
    923            MHD_PSL_STRICT))
    924     return 1;
    925   if (0 !=
    926       run (&server_upload_req_cb,
    927            tests_must_upload,
    928            MHD_PSL_STRICT))
    929     return 1;
    930   if (0 !=
    931       run (&server_req_cb,
    932            tests_should,
    933            MHD_PSL_VERY_STRICT))
    934     return 1;
    935   return 0;
    936 }