libmicrohttpd

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

reason_phrase.c (10070B)


      1 /*
      2      This file is part of libmicrohttpd
      3      Copyright (C) 2007, 2011, 2017, 2019 Christian Grothoff
      4      Copyright (C) 2017-2023 Karlson2k (Evgeny Grin)
      5 
      6      This library 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      This library 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      You should have received a copy of the GNU Lesser General Public
     17      License along with this library; if not, write to the Free Software
     18      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
     19 
     20 */
     21 /**
     22  * @file reason_phrase.c
     23  * @brief  Tables of the string response phrases
     24  * @author Elliot Glaysher
     25  * @author Christian Grothoff (minor code clean up)
     26  * @author Karlson2k (Evgeny Grin)
     27  */
     28 #include "platform.h"
     29 #include "microhttpd.h"
     30 #include "mhd_str.h"
     31 
     32 #ifndef NULL
     33 #define NULL ((void*) 0)
     34 #endif
     35 
     36 static const struct _MHD_cstr_w_len invalid_hundred[] = {
     37   { NULL, 0 }
     38 };
     39 
     40 static const struct _MHD_cstr_w_len one_hundred[] = {
     41   /* 100 */ _MHD_S_STR_W_LEN ("Continue"),       /* RFC9110, Section 15.2.1 */
     42   /* 101 */ _MHD_S_STR_W_LEN ("Switching Protocols"), /* RFC9110, Section 15.2.2 */
     43   /* 102 */ _MHD_S_STR_W_LEN ("Processing"),     /* RFC2518 */
     44   /* 103 */ _MHD_S_STR_W_LEN ("Early Hints")     /* RFC8297 */
     45 };
     46 
     47 static const struct _MHD_cstr_w_len two_hundred[] = {
     48   /* 200 */ _MHD_S_STR_W_LEN ("OK"),             /* RFC9110, Section 15.3.1 */
     49   /* 201 */ _MHD_S_STR_W_LEN ("Created"),        /* RFC9110, Section 15.3.2 */
     50   /* 202 */ _MHD_S_STR_W_LEN ("Accepted"),       /* RFC9110, Section 15.3.3 */
     51   /* 203 */ _MHD_S_STR_W_LEN ("Non-Authoritative Information"), /* RFC9110, Section 15.3.4 */
     52   /* 204 */ _MHD_S_STR_W_LEN ("No Content"),     /* RFC9110, Section 15.3.5 */
     53   /* 205 */ _MHD_S_STR_W_LEN ("Reset Content"),  /* RFC9110, Section 15.3.6 */
     54   /* 206 */ _MHD_S_STR_W_LEN ("Partial Content"), /* RFC9110, Section 15.3.7 */
     55   /* 207 */ _MHD_S_STR_W_LEN ("Multi-Status"),   /* RFC4918 */
     56   /* 208 */ _MHD_S_STR_W_LEN ("Already Reported"), /* RFC5842 */
     57   /* 209 */ {"Unknown", 0},                      /* Not used */
     58   /* 210 */ {"Unknown", 0},                      /* Not used */
     59   /* 211 */ {"Unknown", 0},                      /* Not used */
     60   /* 212 */ {"Unknown", 0},                      /* Not used */
     61   /* 213 */ {"Unknown", 0},                      /* Not used */
     62   /* 214 */ {"Unknown", 0},                      /* Not used */
     63   /* 215 */ {"Unknown", 0},                      /* Not used */
     64   /* 216 */ {"Unknown", 0},                      /* Not used */
     65   /* 217 */ {"Unknown", 0},                      /* Not used */
     66   /* 218 */ {"Unknown", 0},                      /* Not used */
     67   /* 219 */ {"Unknown", 0},                      /* Not used */
     68   /* 220 */ {"Unknown", 0},                      /* Not used */
     69   /* 221 */ {"Unknown", 0},                      /* Not used */
     70   /* 222 */ {"Unknown", 0},                      /* Not used */
     71   /* 223 */ {"Unknown", 0},                      /* Not used */
     72   /* 224 */ {"Unknown", 0},                      /* Not used */
     73   /* 225 */ {"Unknown", 0},                      /* Not used */
     74   /* 226 */ _MHD_S_STR_W_LEN ("IM Used")         /* RFC3229 */
     75 };
     76 
     77 static const struct _MHD_cstr_w_len three_hundred[] = {
     78   /* 300 */ _MHD_S_STR_W_LEN ("Multiple Choices"), /* RFC9110, Section 15.4.1 */
     79   /* 301 */ _MHD_S_STR_W_LEN ("Moved Permanently"), /* RFC9110, Section 15.4.2 */
     80   /* 302 */ _MHD_S_STR_W_LEN ("Found"),          /* RFC9110, Section 15.4.3 */
     81   /* 303 */ _MHD_S_STR_W_LEN ("See Other"),      /* RFC9110, Section 15.4.4 */
     82   /* 304 */ _MHD_S_STR_W_LEN ("Not Modified"),   /* RFC9110, Section 15.4.5 */
     83   /* 305 */ _MHD_S_STR_W_LEN ("Use Proxy"),      /* RFC9110, Section 15.4.6 */
     84   /* 306 */ _MHD_S_STR_W_LEN ("Switch Proxy"),   /* Not used! RFC9110, Section 15.4.7 */
     85   /* 307 */ _MHD_S_STR_W_LEN ("Temporary Redirect"), /* RFC9110, Section 15.4.8 */
     86   /* 308 */ _MHD_S_STR_W_LEN ("Permanent Redirect") /* RFC9110, Section 15.4.9 */
     87 };
     88 
     89 static const struct _MHD_cstr_w_len four_hundred[] = {
     90   /* 400 */ _MHD_S_STR_W_LEN ("Bad Request"),    /* RFC9110, Section 15.5.1 */
     91   /* 401 */ _MHD_S_STR_W_LEN ("Unauthorized"),   /* RFC9110, Section 15.5.2 */
     92   /* 402 */ _MHD_S_STR_W_LEN ("Payment Required"), /* RFC9110, Section 15.5.3 */
     93   /* 403 */ _MHD_S_STR_W_LEN ("Forbidden"),      /* RFC9110, Section 15.5.4 */
     94   /* 404 */ _MHD_S_STR_W_LEN ("Not Found"),      /* RFC9110, Section 15.5.5 */
     95   /* 405 */ _MHD_S_STR_W_LEN ("Method Not Allowed"), /* RFC9110, Section 15.5.6 */
     96   /* 406 */ _MHD_S_STR_W_LEN ("Not Acceptable"), /* RFC9110, Section 15.5.7 */
     97   /* 407 */ _MHD_S_STR_W_LEN ("Proxy Authentication Required"), /* RFC9110, Section 15.5.8 */
     98   /* 408 */ _MHD_S_STR_W_LEN ("Request Timeout"), /* RFC9110, Section 15.5.9 */
     99   /* 409 */ _MHD_S_STR_W_LEN ("Conflict"),       /* RFC9110, Section 15.5.10 */
    100   /* 410 */ _MHD_S_STR_W_LEN ("Gone"),           /* RFC9110, Section 15.5.11 */
    101   /* 411 */ _MHD_S_STR_W_LEN ("Length Required"), /* RFC9110, Section 15.5.12 */
    102   /* 412 */ _MHD_S_STR_W_LEN ("Precondition Failed"), /* RFC9110, Section 15.5.13 */
    103   /* 413 */ _MHD_S_STR_W_LEN ("Content Too Large"), /* RFC9110, Section 15.5.14 */
    104   /* 414 */ _MHD_S_STR_W_LEN ("URI Too Long"),   /* RFC9110, Section 15.5.15 */
    105   /* 415 */ _MHD_S_STR_W_LEN ("Unsupported Media Type"), /* RFC9110, Section 15.5.16 */
    106   /* 416 */ _MHD_S_STR_W_LEN ("Range Not Satisfiable"), /* RFC9110, Section 15.5.17 */
    107   /* 417 */ _MHD_S_STR_W_LEN ("Expectation Failed"), /* RFC9110, Section 15.5.18 */
    108   /* 418 */ {"Unknown", 0},                      /* Not used */
    109   /* 419 */ {"Unknown", 0},                      /* Not used */
    110   /* 420 */ {"Unknown", 0},                      /* Not used */
    111   /* 421 */ _MHD_S_STR_W_LEN ("Misdirected Request"), /* RFC9110, Section 15.5.20 */
    112   /* 422 */ _MHD_S_STR_W_LEN ("Unprocessable Content"), /* RFC9110, Section 15.5.21 */
    113   /* 423 */ _MHD_S_STR_W_LEN ("Locked"),         /* RFC4918 */
    114   /* 424 */ _MHD_S_STR_W_LEN ("Failed Dependency"), /* RFC4918 */
    115   /* 425 */ _MHD_S_STR_W_LEN ("Too Early"),      /* RFC8470 */
    116   /* 426 */ _MHD_S_STR_W_LEN ("Upgrade Required"), /* RFC9110, Section 15.5.22 */
    117   /* 427 */ {"Unknown", 0},                      /* Not used */
    118   /* 428 */ _MHD_S_STR_W_LEN ("Precondition Required"), /* RFC6585 */
    119   /* 429 */ _MHD_S_STR_W_LEN ("Too Many Requests"), /* RFC6585 */
    120   /* 430 */ {"Unknown", 0},                      /* Not used */
    121   /* 431 */ _MHD_S_STR_W_LEN ("Request Header Fields Too Large"), /* RFC6585 */
    122   /* 432 */ {"Unknown", 0},                      /* Not used */
    123   /* 433 */ {"Unknown", 0},                      /* Not used */
    124   /* 434 */ {"Unknown", 0},                      /* Not used */
    125   /* 435 */ {"Unknown", 0},                      /* Not used */
    126   /* 436 */ {"Unknown", 0},                      /* Not used */
    127   /* 437 */ {"Unknown", 0},                      /* Not used */
    128   /* 438 */ {"Unknown", 0},                      /* Not used */
    129   /* 439 */ {"Unknown", 0},                      /* Not used */
    130   /* 440 */ {"Unknown", 0},                      /* Not used */
    131   /* 441 */ {"Unknown", 0},                      /* Not used */
    132   /* 442 */ {"Unknown", 0},                      /* Not used */
    133   /* 443 */ {"Unknown", 0},                      /* Not used */
    134   /* 444 */ {"Unknown", 0},                      /* Not used */
    135   /* 445 */ {"Unknown", 0},                      /* Not used */
    136   /* 446 */ {"Unknown", 0},                      /* Not used */
    137   /* 447 */ {"Unknown", 0},                      /* Not used */
    138   /* 448 */ {"Unknown", 0},                      /* Not used */
    139   /* 449 */ _MHD_S_STR_W_LEN ("Reply With"),     /* MS IIS extension */
    140   /* 450 */ _MHD_S_STR_W_LEN ("Blocked by Windows Parental Controls"), /* MS extension */
    141   /* 451 */ _MHD_S_STR_W_LEN ("Unavailable For Legal Reasons") /* RFC7725 */
    142 };
    143 
    144 static const struct _MHD_cstr_w_len five_hundred[] = {
    145   /* 500 */ _MHD_S_STR_W_LEN ("Internal Server Error"), /* RFC9110, Section 15.6.1 */
    146   /* 501 */ _MHD_S_STR_W_LEN ("Not Implemented"), /* RFC9110, Section 15.6.2 */
    147   /* 502 */ _MHD_S_STR_W_LEN ("Bad Gateway"),    /* RFC9110, Section 15.6.3 */
    148   /* 503 */ _MHD_S_STR_W_LEN ("Service Unavailable"), /* RFC9110, Section 15.6.4 */
    149   /* 504 */ _MHD_S_STR_W_LEN ("Gateway Timeout"), /* RFC9110, Section 15.6.5 */
    150   /* 505 */ _MHD_S_STR_W_LEN ("HTTP Version Not Supported"), /* RFC9110, Section 15.6.6 */
    151   /* 506 */ _MHD_S_STR_W_LEN ("Variant Also Negotiates"), /* RFC2295 */
    152   /* 507 */ _MHD_S_STR_W_LEN ("Insufficient Storage"), /* RFC4918 */
    153   /* 508 */ _MHD_S_STR_W_LEN ("Loop Detected"),  /* RFC5842 */
    154   /* 509 */ _MHD_S_STR_W_LEN ("Bandwidth Limit Exceeded"), /* Apache extension */
    155   /* 510 */ _MHD_S_STR_W_LEN ("Not Extended"),   /* (OBSOLETED) RFC2774; status-change-http-experiments-to-historic */
    156   /* 511 */ _MHD_S_STR_W_LEN ("Network Authentication Required") /* RFC6585 */
    157 };
    158 
    159 
    160 struct MHD_Reason_Block
    161 {
    162   size_t max;
    163   const struct _MHD_cstr_w_len *const data;
    164 };
    165 
    166 #define BLOCK(m) { (sizeof(m) / sizeof(m[0])), m }
    167 
    168 static const struct MHD_Reason_Block reasons[] = {
    169   BLOCK (invalid_hundred),
    170   BLOCK (one_hundred),
    171   BLOCK (two_hundred),
    172   BLOCK (three_hundred),
    173   BLOCK (four_hundred),
    174   BLOCK (five_hundred),
    175 };
    176 
    177 
    178 _MHD_EXTERN const char *
    179 MHD_get_reason_phrase_for (unsigned int code)
    180 {
    181   if ( (code >= 100) &&
    182        (code < 600) &&
    183        (reasons[code / 100].max > (code % 100)) )
    184     return reasons[code / 100].data[code % 100].str;
    185   return "Unknown";
    186 }
    187 
    188 
    189 _MHD_EXTERN size_t
    190 MHD_get_reason_phrase_len_for (unsigned int code)
    191 {
    192   if ( (code >= 100) &&
    193        (code < 600) &&
    194        (reasons[code / 100].max > (code % 100)) )
    195     return reasons[code / 100].data[code % 100].len;
    196   return 0;
    197 }