libmicrohttpd

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

response.c (77644B)


      1 /*
      2      This file is part of libmicrohttpd
      3      Copyright (C) 2007-2021 Daniel Pittman and Christian Grothoff
      4      Copyright (C) 2015-2023 Evgeny Grin (Karlson2k)
      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  * @file response.c
     22  * @brief  Methods for managing response objects
     23  * @author Daniel Pittman
     24  * @author Christian Grothoff
     25  * @author Karlson2k (Evgeny Grin)
     26  */
     27 
     28 #define MHD_NO_DEPRECATION 1
     29 
     30 #include "mhd_options.h"
     31 #ifdef HAVE_SYS_IOCTL_H
     32 #include <sys/ioctl.h>
     33 #endif /* HAVE_SYS_IOCTL_H */
     34 #if defined(_WIN32) && ! defined(__CYGWIN__)
     35 #include <windows.h>
     36 #endif /* _WIN32 && !__CYGWIN__ */
     37 
     38 #include "internal.h"
     39 #include "response.h"
     40 #include "mhd_limits.h"
     41 #include "mhd_sockets.h"
     42 #include "mhd_itc.h"
     43 #include "mhd_str.h"
     44 #include "connection.h"
     45 #include "memorypool.h"
     46 #include "mhd_send.h"
     47 #include "mhd_compat.h"
     48 #include "mhd_assert.h"
     49 
     50 
     51 #if defined(MHD_W32_MUTEX_)
     52 #ifndef WIN32_LEAN_AND_MEAN
     53 #define WIN32_LEAN_AND_MEAN 1
     54 #endif /* !WIN32_LEAN_AND_MEAN */
     55 #include <windows.h>
     56 #endif /* MHD_W32_MUTEX_ */
     57 #if defined(_WIN32)
     58 #include <io.h> /* for lseek(), read() */
     59 #endif /* _WIN32 */
     60 
     61 
     62 /**
     63  * Size of single file read operation for
     64  * file-backed responses.
     65  */
     66 #ifndef MHD_FILE_READ_BLOCK_SIZE
     67 #ifdef _WIN32
     68 #define MHD_FILE_READ_BLOCK_SIZE 16384 /* 16k */
     69 #else /* _WIN32 */
     70 #define MHD_FILE_READ_BLOCK_SIZE 4096 /* 4k */
     71 #endif /* _WIN32 */
     72 #endif /* !MHD_FD_BLOCK_SIZE */
     73 
     74 /**
     75  * Insert a new header at the first position of the response
     76  */
     77 #define _MHD_insert_header_first(presponse, phdr) do { \
     78   mhd_assert (NULL == phdr->next); \
     79   mhd_assert (NULL == phdr->prev); \
     80   if (NULL == presponse->first_header) \
     81   { \
     82     mhd_assert (NULL == presponse->last_header); \
     83     presponse->first_header = phdr; \
     84     presponse->last_header = phdr;  \
     85   } \
     86   else \
     87   { \
     88     mhd_assert (NULL != presponse->last_header);        \
     89     presponse->first_header->prev = phdr; \
     90     phdr->next = presponse->first_header; \
     91     presponse->first_header = phdr;       \
     92   } \
     93 } while (0)
     94 
     95 /**
     96  * Insert a new header at the last position of the response
     97  */
     98 #define _MHD_insert_header_last(presponse, phdr) do { \
     99   mhd_assert (NULL == phdr->next); \
    100   mhd_assert (NULL == phdr->prev); \
    101   if (NULL == presponse->last_header) \
    102   { \
    103     mhd_assert (NULL == presponse->first_header); \
    104     presponse->last_header = phdr;  \
    105     presponse->first_header = phdr; \
    106   } \
    107   else \
    108   { \
    109     mhd_assert (NULL != presponse->first_header);      \
    110     presponse->last_header->next = phdr; \
    111     phdr->prev = presponse->last_header; \
    112     presponse->last_header = phdr;       \
    113   } \
    114 } while (0)
    115 
    116 
    117 /**
    118  * Remove a header from the response
    119  */
    120 #define _MHD_remove_header(presponse, phdr) do { \
    121   mhd_assert (NULL != presponse->first_header); \
    122   mhd_assert (NULL != presponse->last_header);  \
    123   if (NULL == phdr->prev) \
    124   { \
    125     mhd_assert (phdr == presponse->first_header); \
    126     presponse->first_header = phdr->next; \
    127   } \
    128   else \
    129   { \
    130     mhd_assert (phdr != presponse->first_header); \
    131     mhd_assert (phdr == phdr->prev->next); \
    132     phdr->prev->next = phdr->next; \
    133   } \
    134   if (NULL == phdr->next) \
    135   { \
    136     mhd_assert (phdr == presponse->last_header); \
    137     presponse->last_header = phdr->prev; \
    138   } \
    139   else \
    140   { \
    141     mhd_assert (phdr != presponse->last_header); \
    142     mhd_assert (phdr == phdr->next->prev); \
    143     phdr->next->prev = phdr->prev; \
    144   } \
    145 } while (0)
    146 
    147 /**
    148  * Add preallocated strings a header or footer line to the response without
    149  * checking.
    150  *
    151  * Header/footer strings are not checked and assumed to be correct.
    152  *
    153  * The string must not be statically allocated!
    154  * The strings must be malloc()'ed and zero terminated. The strings will
    155  * be free()'ed when the response is destroyed.
    156  *
    157  * @param response response to add a header to
    158  * @param kind header or footer
    159  * @param header the header string to add, must be malloc()'ed and
    160  *               zero-terminated
    161  * @param header_len the length of the @a header
    162  * @param content the value string to add, must be malloc()'ed and
    163  *                zero-terminated
    164  * @param content_len the length of the @a content
    165  */
    166 bool
    167 MHD_add_response_entry_no_alloc_ (struct MHD_Response *response,
    168                                   enum MHD_ValueKind kind,
    169                                   char *header,
    170                                   size_t header_len,
    171                                   char *content,
    172                                   size_t content_len)
    173 {
    174   struct MHD_HTTP_Res_Header *hdr;
    175 
    176   mhd_assert (0 != header_len);
    177   mhd_assert (0 != content_len);
    178   if (NULL == (hdr = MHD_calloc_ (1, sizeof (struct MHD_HTTP_Res_Header))))
    179     return false;
    180 
    181   hdr->header = header;
    182   hdr->header_size = header_len;
    183   hdr->value = content;
    184   hdr->value_size = content_len;
    185   hdr->kind = kind;
    186   _MHD_insert_header_last (response, hdr);
    187 
    188   return true; /* Success exit point */
    189 }
    190 
    191 
    192 /**
    193  * Add a header or footer line to the response without checking.
    194  *
    195  * It is assumed that parameters are correct.
    196  *
    197  * @param response response to add a header to
    198  * @param kind header or footer
    199  * @param header the header to add, does not need to be zero-terminated
    200  * @param header_len the length of the @a header
    201  * @param content value to add, does not need to be zero-terminated
    202  * @param content_len the length of the @a content
    203  * @return false on error (like out-of-memory),
    204  *         true if succeed
    205  */
    206 bool
    207 MHD_add_response_entry_no_check_ (struct MHD_Response *response,
    208                                   enum MHD_ValueKind kind,
    209                                   const char *header,
    210                                   size_t header_len,
    211                                   const char *content,
    212                                   size_t content_len)
    213 {
    214   char *header_malloced;
    215   char *value_malloced;
    216 
    217   mhd_assert (0 != header_len);
    218   mhd_assert (0 != content_len);
    219   header_malloced = malloc (header_len + 1);
    220   if (NULL == header_malloced)
    221     return false;
    222 
    223   memcpy (header_malloced, header, header_len);
    224   header_malloced[header_len] = 0;
    225 
    226   value_malloced = malloc (content_len + 1);
    227   if (NULL != value_malloced)
    228   {
    229     memcpy (value_malloced, content, content_len);
    230     value_malloced[content_len] = 0;
    231 
    232     if (MHD_add_response_entry_no_alloc_ (response, kind,
    233                                           header_malloced, header_len,
    234                                           value_malloced, content_len))
    235       return true; /* Success exit point */
    236 
    237     free (value_malloced);
    238   }
    239   free (header_malloced);
    240 
    241   return false; /* Failure exit point */
    242 }
    243 
    244 
    245 /**
    246  * Add a header or footer line to the response.
    247  *
    248  * @param header the header to add, does not need to be zero-terminated
    249  * @param header_len the length of the @a header
    250  * @param content value to add, does not need to be zero-terminated
    251  * @param content_len the length of the @a content
    252  * @return false on error (out-of-memory, invalid header or content),
    253  *         true if succeed
    254  */
    255 static bool
    256 add_response_entry_n (struct MHD_Response *response,
    257                       enum MHD_ValueKind kind,
    258                       const char *header,
    259                       size_t header_len,
    260                       const char *content,
    261                       size_t content_len)
    262 {
    263   if (NULL == response)
    264     return false;
    265   if (0 == header_len)
    266     return false;
    267   if (0 == content_len)
    268     return false;
    269   if (NULL != memchr (header, '\t', header_len))
    270     return false;
    271   if (NULL != memchr (header, ' ', header_len))
    272     return false;
    273   if (NULL != memchr (header, '\r', header_len))
    274     return false;
    275   if (NULL != memchr (header, '\n', header_len))
    276     return false;
    277   if (NULL != memchr (content, '\r', content_len))
    278     return false;
    279   if (NULL != memchr (content, '\n', content_len))
    280     return false;
    281 
    282   return MHD_add_response_entry_no_check_ (response, kind, header, header_len,
    283                                            content, content_len);
    284 }
    285 
    286 
    287 /**
    288  * Add a header or footer line to the response.
    289  *
    290  * @param response response to add a header to
    291  * @param kind header or footer
    292  * @param header the header to add
    293  * @param content value to add
    294  * @return #MHD_NO on error (i.e. invalid header or content format).
    295  */
    296 static enum MHD_Result
    297 add_response_entry (struct MHD_Response *response,
    298                     enum MHD_ValueKind kind,
    299                     const char *header,
    300                     const char *content)
    301 {
    302   size_t header_len;
    303   size_t content_len;
    304 
    305   if (NULL == content)
    306     return MHD_NO;
    307   if (NULL == header)
    308     return MHD_NO;
    309   header_len = strlen (header);
    310   content_len = strlen (content);
    311   return add_response_entry_n (response, kind, header,
    312                                header_len, content,
    313                                content_len) ? MHD_YES : MHD_NO;
    314 }
    315 
    316 
    317 /**
    318  * Add "Connection:" header to the response with special processing.
    319  *
    320  * "Connection:" header value will be combined with any existing "Connection:"
    321  * header, "close" token (if any) will be de-duplicated and moved to the first
    322  * position.
    323  *
    324  * @param response the response to add a header to
    325  * @param value the value to add
    326  * @return #MHD_NO on error (no memory).
    327  */
    328 static enum MHD_Result
    329 add_response_header_connection (struct MHD_Response *response,
    330                                 const char *value)
    331 {
    332   static const char *key = MHD_HTTP_HEADER_CONNECTION;
    333   /** the length of the "Connection" key */
    334   static const size_t key_len =
    335     MHD_STATICSTR_LEN_ (MHD_HTTP_HEADER_CONNECTION);
    336   size_t value_len; /**< the length of the @a value */
    337   size_t old_value_len; /**< the length of the existing "Connection" value */
    338   size_t buf_size;  /**< the size of the buffer */
    339   size_t norm_len;  /**< the length of the normalised value */
    340   char *buf;        /**< the temporal buffer */
    341   struct MHD_HTTP_Res_Header *hdr; /**< existing "Connection" header */
    342   bool value_has_close; /**< the @a value has "close" token */
    343   bool already_has_close; /**< existing "Connection" header has "close" token */
    344   size_t pos = 0;   /**< position of addition in the @a buf */
    345 
    346   if ( (NULL != strchr (value, '\r')) ||
    347        (NULL != strchr (value, '\n')) )
    348     return MHD_NO;
    349 
    350   if (0 != (response->flags_auto & MHD_RAF_HAS_CONNECTION_HDR))
    351   {
    352     hdr = MHD_get_response_element_n_ (response, MHD_HEADER_KIND,
    353                                        key, key_len);
    354     already_has_close =
    355       (0 != (response->flags_auto & MHD_RAF_HAS_CONNECTION_CLOSE));
    356     mhd_assert (NULL != hdr);
    357     mhd_assert (already_has_close == (0 == memcmp (hdr->value, "close", 5)));
    358   }
    359   else
    360   {
    361     hdr = NULL;
    362     already_has_close = false;
    363     mhd_assert (NULL == MHD_get_response_element_n_ (response,
    364                                                      MHD_HEADER_KIND,
    365                                                      key, key_len));
    366     mhd_assert (0 == (response->flags_auto & MHD_RAF_HAS_CONNECTION_CLOSE));
    367   }
    368   if (NULL != hdr)
    369     old_value_len = hdr->value_size + 2; /* additional size for ", " */
    370   else
    371     old_value_len = 0;
    372 
    373   value_len = strlen (value);
    374   if (value_len >= SSIZE_MAX)
    375     return MHD_NO;
    376   /* Additional space for normalisation and zero-termination */
    377   norm_len = value_len + value_len / 2 + 1;
    378   if (norm_len >= SSIZE_MAX)
    379     return MHD_NO;
    380   buf_size = old_value_len + (size_t) norm_len;
    381 
    382   buf = malloc (buf_size);
    383   if (NULL == buf)
    384     return MHD_NO;
    385   if (1)
    386   { /* local scope */
    387     ssize_t norm_len_s = (ssize_t) norm_len;
    388     /* Remove "close" token (if any), it will be moved to the front */
    389     value_has_close = MHD_str_remove_token_caseless_ (value, value_len, "close",
    390                                                       MHD_STATICSTR_LEN_ ( \
    391                                                         "close"),
    392                                                       buf + old_value_len,
    393                                                       &norm_len_s);
    394     mhd_assert (0 <= norm_len_s);
    395     if (0 > norm_len_s)
    396     {
    397       /* Must never happen with realistic sizes */
    398       free (buf);
    399       return MHD_NO;
    400     }
    401     else
    402       norm_len = (size_t) norm_len_s;
    403   }
    404 #ifdef UPGRADE_SUPPORT
    405   if ( (NULL != response->upgrade_handler) && value_has_close)
    406   { /* The "close" token cannot be used with connection "upgrade" */
    407     free (buf);
    408     return MHD_NO;
    409   }
    410 #endif /* UPGRADE_SUPPORT */
    411   if (0 != norm_len)
    412     MHD_str_remove_tokens_caseless_ (buf + old_value_len, &norm_len,
    413                                      "keep-alive",
    414                                      MHD_STATICSTR_LEN_ ("keep-alive"));
    415   if (0 == norm_len)
    416   { /* New value is empty after normalisation */
    417     if (! value_has_close)
    418     { /* The new value had no tokens */
    419       free (buf);
    420       return MHD_NO;
    421     }
    422     if (already_has_close)
    423     { /* The "close" token is already present, nothing to modify */
    424       free (buf);
    425       return MHD_YES;
    426     }
    427   }
    428   /* Add "close" token if required */
    429   if (value_has_close && ! already_has_close)
    430   {
    431     /* Need to insert "close" token at the first position */
    432     mhd_assert (buf_size >= old_value_len + norm_len   \
    433                 + MHD_STATICSTR_LEN_ ("close, ") + 1);
    434     if (0 != norm_len)
    435       memmove (buf + MHD_STATICSTR_LEN_ ("close, ") + old_value_len,
    436                buf + old_value_len, norm_len + 1);
    437     memcpy (buf, "close", MHD_STATICSTR_LEN_ ("close"));
    438     pos += MHD_STATICSTR_LEN_ ("close");
    439   }
    440   /* Add old value tokens (if any) */
    441   if (0 != old_value_len)
    442   {
    443     if (0 != pos)
    444     {
    445       buf[pos++] = ',';
    446       buf[pos++] = ' ';
    447     }
    448     memcpy (buf + pos, hdr->value,
    449             hdr->value_size);
    450     pos += hdr->value_size;
    451   }
    452   /* Add new value token (if any) */
    453   if (0 != norm_len)
    454   {
    455     if (0 != pos)
    456     {
    457       buf[pos++] = ',';
    458       buf[pos++] = ' ';
    459     }
    460     /* The new value tokens must be already at the correct position */
    461     mhd_assert ((value_has_close && ! already_has_close) ? \
    462                 (MHD_STATICSTR_LEN_ ("close, ") + old_value_len == pos) : \
    463                 (old_value_len == pos));
    464     pos += norm_len;
    465   }
    466   mhd_assert (buf_size > pos);
    467   buf[pos] = 0; /* Null terminate the result */
    468 
    469   if (NULL == hdr)
    470   {
    471     struct MHD_HTTP_Res_Header *new_hdr; /**< new "Connection" header */
    472     /* Create new response header entry */
    473     new_hdr = MHD_calloc_ (1, sizeof (struct MHD_HTTP_Res_Header));
    474     if (NULL != new_hdr)
    475     {
    476       new_hdr->header = malloc (key_len + 1);
    477       if (NULL != new_hdr->header)
    478       {
    479         memcpy (new_hdr->header, key, key_len + 1);
    480         new_hdr->header_size = key_len;
    481         new_hdr->value = buf;
    482         new_hdr->value_size = pos;
    483         new_hdr->kind = MHD_HEADER_KIND;
    484         if (value_has_close)
    485           response->flags_auto = (MHD_RAF_HAS_CONNECTION_HDR
    486                                   | MHD_RAF_HAS_CONNECTION_CLOSE);
    487         else
    488           response->flags_auto = MHD_RAF_HAS_CONNECTION_HDR;
    489         _MHD_insert_header_first (response, new_hdr);
    490         return MHD_YES;
    491       }
    492       free (new_hdr);
    493     }
    494     free (buf);
    495     return MHD_NO;
    496   }
    497 
    498   /* Update existing header entry */
    499   free (hdr->value);
    500   hdr->value = buf;
    501   hdr->value_size = pos;
    502   if (value_has_close && ! already_has_close)
    503     response->flags_auto |= MHD_RAF_HAS_CONNECTION_CLOSE;
    504   return MHD_YES;
    505 }
    506 
    507 
    508 /**
    509  * Remove tokens from "Connection:" header of the response.
    510  *
    511  * Provided tokens will be removed from "Connection:" header value.
    512  *
    513  * @param response the response to manipulate "Connection:" header
    514  * @param value the tokens to remove
    515  * @return #MHD_NO on error (no headers or tokens found).
    516  */
    517 static enum MHD_Result
    518 del_response_header_connection (struct MHD_Response *response,
    519                                 const char *value)
    520 {
    521   struct MHD_HTTP_Res_Header *hdr; /**< existing "Connection" header */
    522 
    523   hdr = MHD_get_response_element_n_ (response, MHD_HEADER_KIND,
    524                                      MHD_HTTP_HEADER_CONNECTION,
    525                                      MHD_STATICSTR_LEN_ ( \
    526                                        MHD_HTTP_HEADER_CONNECTION));
    527   if (NULL == hdr)
    528     return MHD_NO;
    529 
    530   if (! MHD_str_remove_tokens_caseless_ (hdr->value,
    531                                          &hdr->value_size,
    532                                          value,
    533                                          strlen (value)))
    534     return MHD_NO;
    535   if (0 == hdr->value_size)
    536   {
    537     _MHD_remove_header (response, hdr);
    538     free (hdr->value);
    539     free (hdr->header);
    540     free (hdr);
    541     response->flags_auto &=
    542       ~((enum MHD_ResponseAutoFlags) MHD_RAF_HAS_CONNECTION_HDR
    543         | (enum MHD_ResponseAutoFlags) MHD_RAF_HAS_CONNECTION_CLOSE);
    544   }
    545   else
    546   {
    547     hdr->value[hdr->value_size] = 0; /* Null-terminate the result */
    548     if (0 != (response->flags_auto
    549               & ((enum MHD_ResponseAutoFlags) MHD_RAF_HAS_CONNECTION_CLOSE)))
    550     {
    551       if (MHD_STATICSTR_LEN_ ("close") == hdr->value_size)
    552       {
    553         if (0 != memcmp (hdr->value,
    554                          "close",
    555                          MHD_STATICSTR_LEN_ ("close")))
    556           response->flags_auto &=
    557             ~((enum MHD_ResponseAutoFlags) MHD_RAF_HAS_CONNECTION_CLOSE);
    558       }
    559       else if (MHD_STATICSTR_LEN_ ("close, ") < hdr->value_size)
    560       {
    561         if (0 != memcmp (hdr->value, "close, ",
    562                          MHD_STATICSTR_LEN_ ("close, ")))
    563           response->flags_auto &=
    564             ~((enum MHD_ResponseAutoFlags) MHD_RAF_HAS_CONNECTION_CLOSE);
    565       }
    566       else
    567         response->flags_auto &=
    568           ~((enum MHD_ResponseAutoFlags) MHD_RAF_HAS_CONNECTION_CLOSE);
    569     }
    570   }
    571   return MHD_YES;
    572 }
    573 
    574 
    575 /**
    576  * Add a header line to the response.
    577  *
    578  * When reply is generated with queued response, some headers are generated
    579  * automatically. Automatically generated headers are only sent to the client,
    580  * but not added back to the response object.
    581  *
    582  * The list of automatic headers:
    583  * + "Date" header is added automatically unless already set by
    584  *   this function
    585  *   @see #MHD_USE_SUPPRESS_DATE_NO_CLOCK
    586  * + "Content-Length" is added automatically when required, attempt to set
    587  *   it manually by this function is ignored.
    588  *   @see #MHD_RF_INSANITY_HEADER_CONTENT_LENGTH
    589  * + "Transfer-Encoding" with value "chunked" is added automatically,
    590  *   when chunked transfer encoding is used automatically. Same header with
    591  *   the same value can be set manually by this function to enforce chunked
    592  *   encoding, however for HTTP/1.0 clients chunked encoding will not be used
    593  *   and manually set "Transfer-Encoding" header is automatically removed
    594  *   for HTTP/1.0 clients
    595  * + "Connection" may be added automatically with value "Keep-Alive" (only
    596  *   for HTTP/1.0 clients) or "Close". The header "Connection" with value
    597  *   "Close" could be set by this function to enforce closure of
    598  *   the connection after sending this response. "Keep-Alive" cannot be
    599  *   enforced and will be removed automatically.
    600  *   @see #MHD_RF_SEND_KEEP_ALIVE_HEADER
    601  *
    602  * Some headers are pre-processed by this function:
    603  * * "Connection" headers are combined into single header entry, value is
    604  *   normilised, "Keep-Alive" tokens are removed.
    605  * * "Transfer-Encoding" header: the only one header is allowed, the only
    606  *   allowed value is "chunked".
    607  * * "Date" header: the only one header is allowed, the second added header
    608  *   replaces the first one.
    609  * * "Content-Length" application-defined header is not allowed.
    610  *   @see #MHD_RF_INSANITY_HEADER_CONTENT_LENGTH
    611  *
    612  * Headers are used in order as they were added.
    613  *
    614  * @param response the response to add a header to
    615  * @param header the header name to add, no need to be static, an internal copy
    616  *               will be created automatically
    617  * @param content the header value to add, no need to be static, an internal
    618  *                copy will be created automatically
    619  * @return #MHD_YES on success,
    620  *         #MHD_NO on error (i.e. invalid header or content format),
    621  *         or out of memory
    622  * @ingroup response
    623  */
    624 _MHD_EXTERN enum MHD_Result
    625 MHD_add_response_header (struct MHD_Response *response,
    626                          const char *header,
    627                          const char *content)
    628 {
    629   if (MHD_str_equal_caseless_ (header, MHD_HTTP_HEADER_CONNECTION))
    630     return add_response_header_connection (response, content);
    631 
    632   if (MHD_str_equal_caseless_ (header,
    633                                MHD_HTTP_HEADER_TRANSFER_ENCODING))
    634   {
    635     if (! MHD_str_equal_caseless_ (content, "chunked"))
    636       return MHD_NO;   /* Only "chunked" encoding is allowed */
    637     if (0 != (response->flags_auto & MHD_RAF_HAS_TRANS_ENC_CHUNKED))
    638       return MHD_YES;  /* Already has "chunked" encoding header */
    639     if ( (0 != (response->flags_auto & MHD_RAF_HAS_CONTENT_LENGTH)) &&
    640          (0 == (MHD_RF_INSANITY_HEADER_CONTENT_LENGTH & response->flags)) )
    641       return MHD_NO; /* Has "Content-Length" header and no "Insanity" flag */
    642     if (MHD_NO != add_response_entry (response,
    643                                       MHD_HEADER_KIND,
    644                                       header,
    645                                       content))
    646     {
    647       response->flags_auto |= MHD_RAF_HAS_TRANS_ENC_CHUNKED;
    648       return MHD_YES;
    649     }
    650     return MHD_NO;
    651   }
    652   if (MHD_str_equal_caseless_ (header,
    653                                MHD_HTTP_HEADER_DATE))
    654   {
    655     if (0 != (response->flags_auto & MHD_RAF_HAS_DATE_HDR))
    656     {
    657       struct MHD_HTTP_Res_Header *hdr;
    658       hdr = MHD_get_response_element_n_ (response, MHD_HEADER_KIND,
    659                                          MHD_HTTP_HEADER_DATE,
    660                                          MHD_STATICSTR_LEN_ ( \
    661                                            MHD_HTTP_HEADER_DATE));
    662       mhd_assert (NULL != hdr);
    663       _MHD_remove_header (response, hdr);
    664       if (NULL != hdr->value)
    665         free (hdr->value);
    666       free (hdr->header);
    667       free (hdr);
    668     }
    669     if (MHD_NO != add_response_entry (response,
    670                                       MHD_HEADER_KIND,
    671                                       header,
    672                                       content))
    673     {
    674       response->flags_auto |= MHD_RAF_HAS_DATE_HDR;
    675       return MHD_YES;
    676     }
    677     return MHD_NO;
    678   }
    679 
    680   if (MHD_str_equal_caseless_ (header,
    681                                MHD_HTTP_HEADER_CONTENT_LENGTH))
    682   {
    683     /* Generally MHD sets automatically correct "Content-Length" always when
    684      * needed.
    685      * Custom "Content-Length" header is allowed only in special cases. */
    686     if ( (0 != (MHD_RF_INSANITY_HEADER_CONTENT_LENGTH & response->flags)) ||
    687          ((0 != (MHD_RF_HEAD_ONLY_RESPONSE & response->flags)) &&
    688           (0 == (response->flags_auto & (MHD_RAF_HAS_TRANS_ENC_CHUNKED
    689                                          | MHD_RAF_HAS_CONTENT_LENGTH)))) )
    690     {
    691       if (MHD_NO != add_response_entry (response,
    692                                         MHD_HEADER_KIND,
    693                                         header,
    694                                         content))
    695       {
    696         response->flags_auto |= MHD_RAF_HAS_CONTENT_LENGTH;
    697         return MHD_YES;
    698       }
    699     }
    700     return MHD_NO;
    701   }
    702 
    703   return add_response_entry (response,
    704                              MHD_HEADER_KIND,
    705                              header,
    706                              content);
    707 }
    708 
    709 
    710 /**
    711  * Add a footer line to the response.
    712  *
    713  * @param response response to remove a header from
    714  * @param footer the footer to delete
    715  * @param content value to delete
    716  * @return #MHD_NO on error (i.e. invalid footer or content format).
    717  * @ingroup response
    718  */
    719 _MHD_EXTERN enum MHD_Result
    720 MHD_add_response_footer (struct MHD_Response *response,
    721                          const char *footer,
    722                          const char *content)
    723 {
    724   return add_response_entry (response,
    725                              MHD_FOOTER_KIND,
    726                              footer,
    727                              content);
    728 }
    729 
    730 
    731 /**
    732  * Delete a header (or footer) line from the response.
    733  *
    734  * For "Connection" headers this function remove all tokens from existing
    735  * value. Successful result means that at least one token has been removed.
    736  * If all tokens are removed from "Connection" header, the empty "Connection"
    737  * header removed.
    738  *
    739  * @param response response to remove a header from
    740  * @param header the header to delete
    741  * @param content value to delete
    742  * @return #MHD_NO on error (no such header known)
    743  * @ingroup response
    744  */
    745 _MHD_EXTERN enum MHD_Result
    746 MHD_del_response_header (struct MHD_Response *response,
    747                          const char *header,
    748                          const char *content)
    749 {
    750   struct MHD_HTTP_Res_Header *pos;
    751   size_t header_len;
    752   size_t content_len;
    753 
    754   if ( (NULL == header) ||
    755        (NULL == content) )
    756     return MHD_NO;
    757   header_len = strlen (header);
    758 
    759   if ((0 != (response->flags_auto & MHD_RAF_HAS_CONNECTION_HDR)) &&
    760       (MHD_STATICSTR_LEN_ (MHD_HTTP_HEADER_CONNECTION) == header_len) &&
    761       MHD_str_equal_caseless_bin_n_ (header, MHD_HTTP_HEADER_CONNECTION,
    762                                      header_len))
    763     return del_response_header_connection (response, content);
    764 
    765   content_len = strlen (content);
    766   pos = response->first_header;
    767   while (NULL != pos)
    768   {
    769     if ((header_len == pos->header_size) &&
    770         (content_len == pos->value_size) &&
    771         (0 == memcmp (header,
    772                       pos->header,
    773                       header_len)) &&
    774         (0 == memcmp (content,
    775                       pos->value,
    776                       content_len)))
    777     {
    778       _MHD_remove_header (response, pos);
    779       free (pos->header);
    780       free (pos->value);
    781       free (pos);
    782       if ( (MHD_STATICSTR_LEN_ (MHD_HTTP_HEADER_TRANSFER_ENCODING) ==
    783             header_len) &&
    784            MHD_str_equal_caseless_bin_n_ (header,
    785                                           MHD_HTTP_HEADER_TRANSFER_ENCODING,
    786                                           header_len) )
    787         response->flags_auto &=
    788           ~((enum MHD_ResponseAutoFlags) MHD_RAF_HAS_TRANS_ENC_CHUNKED);
    789       else if ( (MHD_STATICSTR_LEN_ (MHD_HTTP_HEADER_DATE) ==
    790                  header_len) &&
    791                 MHD_str_equal_caseless_bin_n_ (header,
    792                                                MHD_HTTP_HEADER_DATE,
    793                                                header_len) )
    794         response->flags_auto &=
    795           ~((enum MHD_ResponseAutoFlags) MHD_RAF_HAS_DATE_HDR);
    796       else if ( (MHD_STATICSTR_LEN_ (MHD_HTTP_HEADER_CONTENT_LENGTH) ==
    797                  header_len) &&
    798                 MHD_str_equal_caseless_bin_n_ (header,
    799                                                MHD_HTTP_HEADER_CONTENT_LENGTH,
    800                                                header_len) )
    801       {
    802         if (NULL == MHD_get_response_element_n_ (response,
    803                                                  MHD_HEADER_KIND,
    804                                                  MHD_HTTP_HEADER_CONTENT_LENGTH,
    805                                                  header_len))
    806           response->flags_auto &=
    807             ~((enum MHD_ResponseAutoFlags) MHD_RAF_HAS_CONTENT_LENGTH);
    808       }
    809       return MHD_YES;
    810     }
    811     pos = pos->next;
    812   }
    813   return MHD_NO;
    814 }
    815 
    816 
    817 /**
    818  * Get all of the headers (and footers) added to a response.
    819  *
    820  * @param response response to query
    821  * @param iterator callback to call on each header;
    822  *        maybe NULL (then just count headers)
    823  * @param iterator_cls extra argument to @a iterator
    824  * @return number of entries iterated over
    825  * @ingroup response
    826  */
    827 _MHD_EXTERN int
    828 MHD_get_response_headers (struct MHD_Response *response,
    829                           MHD_KeyValueIterator iterator,
    830                           void *iterator_cls)
    831 {
    832   int numHeaders = 0;
    833   struct MHD_HTTP_Res_Header *pos;
    834 
    835   for (pos = response->first_header;
    836        NULL != pos;
    837        pos = pos->next)
    838   {
    839     numHeaders++;
    840     if ((NULL != iterator) &&
    841         (MHD_NO == iterator (iterator_cls,
    842                              pos->kind,
    843                              pos->header,
    844                              pos->value)))
    845       break;
    846   }
    847   return numHeaders;
    848 }
    849 
    850 
    851 /**
    852  * Get a particular header (or footer) from the response.
    853  *
    854  * @param response response to query
    855  * @param key which header to get
    856  * @return NULL if header does not exist
    857  * @ingroup response
    858  */
    859 _MHD_EXTERN const char *
    860 MHD_get_response_header (struct MHD_Response *response,
    861                          const char *key)
    862 {
    863   struct MHD_HTTP_Res_Header *pos;
    864   size_t key_size;
    865 
    866   if (NULL == key)
    867     return NULL;
    868 
    869   key_size = strlen (key);
    870   for (pos = response->first_header;
    871        NULL != pos;
    872        pos = pos->next)
    873   {
    874     if ((pos->header_size == key_size) &&
    875         (MHD_str_equal_caseless_bin_n_ (pos->header, key, pos->header_size)))
    876       return pos->value;
    877   }
    878   return NULL;
    879 }
    880 
    881 
    882 /**
    883  * Get a particular header (or footer) element from the response.
    884  *
    885  * Function returns the first found element.
    886  * @param response response to query
    887  * @param kind the kind of element: header or footer
    888  * @param key the key which header to get
    889  * @param key_len the length of the @a key
    890  * @return NULL if header element does not exist
    891  * @ingroup response
    892  */
    893 struct MHD_HTTP_Res_Header *
    894 MHD_get_response_element_n_ (struct MHD_Response *response,
    895                              enum MHD_ValueKind kind,
    896                              const char *key,
    897                              size_t key_len)
    898 {
    899   struct MHD_HTTP_Res_Header *pos;
    900 
    901   mhd_assert (NULL != key);
    902   mhd_assert (0 != key[0]);
    903   mhd_assert (0 != key_len);
    904 
    905   for (pos = response->first_header;
    906        NULL != pos;
    907        pos = pos->next)
    908   {
    909     if ((pos->header_size == key_len) &&
    910         (kind == pos->kind) &&
    911         (MHD_str_equal_caseless_bin_n_ (pos->header, key, pos->header_size)))
    912       return pos;
    913   }
    914   return NULL;
    915 }
    916 
    917 
    918 /**
    919  * Check whether response header contains particular token.
    920  *
    921  * Token could be surrounded by spaces and tabs and delimited by comma.
    922  * Case-insensitive match used for header names and tokens.
    923  *
    924  * @param response  the response to query
    925  * @param key       header name
    926  * @param key_len   the length of @a key, not including optional
    927  *                  terminating null-character.
    928  * @param token     the token to find
    929  * @param token_len the length of @a token, not including optional
    930  *                  terminating null-character.
    931  * @return true if token is found in specified header,
    932  *         false otherwise
    933  */
    934 bool
    935 MHD_check_response_header_token_ci (const struct MHD_Response *response,
    936                                     const char *key,
    937                                     size_t key_len,
    938                                     const char *token,
    939                                     size_t token_len)
    940 {
    941   struct MHD_HTTP_Res_Header *pos;
    942 
    943   if ( (NULL == key) ||
    944        ('\0' == key[0]) ||
    945        (NULL == token) ||
    946        ('\0' == token[0]) )
    947     return false;
    948 
    949   /* Token must not contain binary zero! */
    950   mhd_assert (strlen (token) == token_len);
    951 
    952   for (pos = response->first_header;
    953        NULL != pos;
    954        pos = pos->next)
    955   {
    956     if ( (pos->kind == MHD_HEADER_KIND) &&
    957          (key_len == pos->header_size) &&
    958          MHD_str_equal_caseless_bin_n_ (pos->header,
    959                                         key,
    960                                         key_len) &&
    961          MHD_str_has_token_caseless_ (pos->value,
    962                                       token,
    963                                       token_len) )
    964       return true;
    965   }
    966   return false;
    967 }
    968 
    969 
    970 /**
    971  * Create a response object.
    972  * The response object can be extended with header information and then be used
    973  * any number of times.
    974  *
    975  * If response object is used to answer HEAD request then the body of the
    976  * response is not used, while all headers (including automatic headers) are
    977  * used.
    978  *
    979  * @param size size of the data portion of the response, #MHD_SIZE_UNKNOWN for unknown
    980  * @param block_size preferred block size for querying crc (advisory only,
    981  *                   MHD may still call @a crc using smaller chunks); this
    982  *                   is essentially the buffer size used for IO, clients
    983  *                   should pick a value that is appropriate for IO and
    984  *                   memory performance requirements
    985  * @param crc callback to use to obtain response data
    986  * @param crc_cls extra argument to @a crc
    987  * @param crfc callback to call to free @a crc_cls resources
    988  * @return NULL on error (i.e. invalid arguments, out of memory)
    989  * @ingroup response
    990  */
    991 _MHD_EXTERN struct MHD_Response *
    992 MHD_create_response_from_callback (uint64_t size,
    993                                    size_t block_size,
    994                                    MHD_ContentReaderCallback crc,
    995                                    void *crc_cls,
    996                                    MHD_ContentReaderFreeCallback crfc)
    997 {
    998   struct MHD_Response *response;
    999 
   1000   if ((NULL == crc) || (0 == block_size))
   1001     return NULL;
   1002   if (NULL == (response = MHD_calloc_ (1, sizeof (struct MHD_Response)
   1003                                        + block_size)))
   1004     return NULL;
   1005   response->fd = -1;
   1006   response->data = (void *) &response[1];
   1007   response->data_buffer_size = block_size;
   1008 #if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
   1009   if (! MHD_mutex_init_ (&response->mutex))
   1010   {
   1011     free (response);
   1012     return NULL;
   1013   }
   1014 #endif
   1015   response->crc = crc;
   1016   response->crfc = crfc;
   1017   response->crc_cls = crc_cls;
   1018   response->reference_count = 1;
   1019   response->total_size = size;
   1020   return response;
   1021 }
   1022 
   1023 
   1024 /**
   1025  * Set special flags and options for a response.
   1026  *
   1027  * @param response the response to modify
   1028  * @param flags to set for the response
   1029  * @param ... #MHD_RO_END terminated list of options
   1030  * @return #MHD_YES on success, #MHD_NO on error
   1031  */
   1032 _MHD_EXTERN enum MHD_Result
   1033 MHD_set_response_options (struct MHD_Response *response,
   1034                           enum MHD_ResponseFlags flags,
   1035                           ...)
   1036 {
   1037   va_list ap;
   1038   enum MHD_Result ret;
   1039   enum MHD_ResponseOptions ro;
   1040 
   1041   if (0 != (response->flags_auto & MHD_RAF_HAS_CONTENT_LENGTH))
   1042   { /* Response has custom "Content-Lengh" header */
   1043     if ( (0 != (response->flags & MHD_RF_INSANITY_HEADER_CONTENT_LENGTH)) &&
   1044          (0 == (flags & MHD_RF_INSANITY_HEADER_CONTENT_LENGTH)))
   1045     { /* Request to remove MHD_RF_INSANITY_HEADER_CONTENT_LENGTH flag */
   1046       return MHD_NO;
   1047     }
   1048     if ( (0 != (response->flags & MHD_RF_HEAD_ONLY_RESPONSE)) &&
   1049          (0 == (flags & MHD_RF_HEAD_ONLY_RESPONSE)))
   1050     { /* Request to remove MHD_RF_HEAD_ONLY_RESPONSE flag */
   1051       if (0 == (flags & MHD_RF_INSANITY_HEADER_CONTENT_LENGTH))
   1052         return MHD_NO;
   1053     }
   1054   }
   1055 
   1056   if ( (0 != (flags & MHD_RF_HEAD_ONLY_RESPONSE)) &&
   1057        (0 != response->total_size) )
   1058     return MHD_NO;
   1059 
   1060   ret = MHD_YES;
   1061   response->flags = flags;
   1062 
   1063   va_start (ap, flags);
   1064   while (MHD_RO_END != (ro = va_arg (ap, enum MHD_ResponseOptions)))
   1065   {
   1066     switch (ro)
   1067     {
   1068     case MHD_RO_END: /* Not possible */
   1069       break;
   1070     default:
   1071       ret = MHD_NO;
   1072       break;
   1073     }
   1074   }
   1075   va_end (ap);
   1076   return ret;
   1077 }
   1078 
   1079 
   1080 /**
   1081  * Given a file descriptor, read data from the file
   1082  * to generate the response.
   1083  *
   1084  * @param cls pointer to the response
   1085  * @param pos offset in the file to access
   1086  * @param buf where to write the data
   1087  * @param max number of bytes to write at most
   1088  * @return number of bytes written
   1089  */
   1090 static ssize_t
   1091 file_reader (void *cls,
   1092              uint64_t pos,
   1093              char *buf,
   1094              size_t max)
   1095 {
   1096   struct MHD_Response *response = cls;
   1097 #if ! defined(_WIN32) || defined(__CYGWIN__)
   1098   ssize_t n;
   1099 #else  /* _WIN32 && !__CYGWIN__ */
   1100   const HANDLE fh = (HANDLE) (uintptr_t) _get_osfhandle (response->fd);
   1101 #endif /* _WIN32 && !__CYGWIN__ */
   1102   const int64_t offset64 = (int64_t) (pos + response->fd_off);
   1103 
   1104   if (offset64 < 0)
   1105     return MHD_CONTENT_READER_END_WITH_ERROR; /* seek to required position is not possible */
   1106 
   1107 #if ! defined(_WIN32) || defined(__CYGWIN__)
   1108   if (max > SSIZE_MAX)
   1109     max = SSIZE_MAX; /* Clamp to maximum return value. */
   1110 
   1111 #if defined(HAVE_PREAD64)
   1112   n = pread64 (response->fd, buf, max, offset64);
   1113 #elif defined(HAVE_PREAD)
   1114   if ( (sizeof(off_t) < sizeof (uint64_t)) &&
   1115        (offset64 > (uint64_t) INT32_MAX) )
   1116     return MHD_CONTENT_READER_END_WITH_ERROR; /* Read at required position is not possible. */
   1117 
   1118   n = pread (response->fd, buf, max, (off_t) offset64);
   1119 #else  /* ! HAVE_PREAD */
   1120 #if defined(HAVE_LSEEK64)
   1121   if (lseek64 (response->fd,
   1122                offset64,
   1123                SEEK_SET) != offset64)
   1124     return MHD_CONTENT_READER_END_WITH_ERROR; /* can't seek to required position */
   1125 #else  /* ! HAVE_LSEEK64 */
   1126   if ( (sizeof(off_t) < sizeof (uint64_t)) &&
   1127        (offset64 > (uint64_t) INT32_MAX) )
   1128     return MHD_CONTENT_READER_END_WITH_ERROR; /* seek to required position is not possible */
   1129 
   1130   if (lseek (response->fd,
   1131              (off_t) offset64,
   1132              SEEK_SET) != (off_t) offset64)
   1133     return MHD_CONTENT_READER_END_WITH_ERROR; /* can't seek to required position */
   1134 #endif /* ! HAVE_LSEEK64 */
   1135   n = read (response->fd,
   1136             buf,
   1137             max);
   1138 
   1139 #endif /* ! HAVE_PREAD */
   1140   if (0 == n)
   1141     return MHD_CONTENT_READER_END_OF_STREAM;
   1142   if (n < 0)
   1143     return MHD_CONTENT_READER_END_WITH_ERROR;
   1144   return n;
   1145 #else /* _WIN32 && !__CYGWIN__ */
   1146   if (INVALID_HANDLE_VALUE == fh)
   1147     return MHD_CONTENT_READER_END_WITH_ERROR; /* Value of 'response->fd' is not valid. */
   1148   else
   1149   {
   1150     OVERLAPPED f_ol = {0, 0, {{0, 0}}, 0};   /* Initialize to zero. */
   1151     ULARGE_INTEGER pos_uli;
   1152     DWORD toRead = (max > INT32_MAX) ? INT32_MAX : (DWORD) max;
   1153     DWORD resRead;
   1154 
   1155     pos_uli.QuadPart = (uint64_t) offset64;   /* Simple transformation 64bit -> 2x32bit. */
   1156     f_ol.Offset = pos_uli.LowPart;
   1157     f_ol.OffsetHigh = pos_uli.HighPart;
   1158     if (! ReadFile (fh, (void *) buf, toRead, &resRead, &f_ol))
   1159       return MHD_CONTENT_READER_END_WITH_ERROR;   /* Read error. */
   1160     if (0 == resRead)
   1161       return MHD_CONTENT_READER_END_OF_STREAM;
   1162     return (ssize_t) resRead;
   1163   }
   1164 #endif /* _WIN32 && !__CYGWIN__ */
   1165 }
   1166 
   1167 
   1168 /**
   1169  * Given a pipe descriptor, read data from the pipe
   1170  * to generate the response.
   1171  *
   1172  * @param cls pointer to the response
   1173  * @param pos offset in the pipe to access (ignored)
   1174  * @param buf where to write the data
   1175  * @param max number of bytes to write at most
   1176  * @return number of bytes written
   1177  */
   1178 static ssize_t
   1179 pipe_reader (void *cls,
   1180              uint64_t pos,
   1181              char *buf,
   1182              size_t max)
   1183 {
   1184   struct MHD_Response *response = cls;
   1185   ssize_t n;
   1186 
   1187   (void) pos;
   1188 
   1189 #ifndef _WIN32
   1190   if (SSIZE_MAX < max)
   1191     max = SSIZE_MAX;
   1192   n = read (response->fd,
   1193             buf,
   1194             (MHD_SCKT_SEND_SIZE_) max);
   1195 #else  /* _WIN32 */
   1196   if (INT_MAX < max)
   1197     max = INT_MAX;
   1198   n = read (response->fd,
   1199             buf,
   1200             (unsigned int) max);
   1201 #endif /* _WIN32 */
   1202 
   1203   if (0 == n)
   1204     return MHD_CONTENT_READER_END_OF_STREAM;
   1205   if (n < 0)
   1206     return MHD_CONTENT_READER_END_WITH_ERROR;
   1207   return n;
   1208 }
   1209 
   1210 
   1211 /**
   1212  * Destroy file reader context.  Closes the file
   1213  * descriptor.
   1214  *
   1215  * @param cls pointer to file descriptor
   1216  */
   1217 static void
   1218 free_callback (void *cls)
   1219 {
   1220   struct MHD_Response *response = cls;
   1221 
   1222   (void) close (response->fd);
   1223   response->fd = -1;
   1224 }
   1225 
   1226 
   1227 #undef MHD_create_response_from_fd_at_offset
   1228 
   1229 /**
   1230  * Create a response object with the content of provided file with
   1231  * specified offset used as the response body.
   1232  *
   1233  * The response object can be extended with header information and then
   1234  * be used any number of times.
   1235  *
   1236  * If response object is used to answer HEAD request then the body
   1237  * of the response is not used, while all headers (including automatic
   1238  * headers) are used.
   1239  *
   1240  * @param size size of the data portion of the response
   1241  * @param fd file descriptor referring to a file on disk with the
   1242  *        data; will be closed when response is destroyed;
   1243  *        fd should be in 'blocking' mode
   1244  * @param offset offset to start reading from in the file;
   1245  *        Be careful! `off_t` may have been compiled to be a
   1246  *        64-bit variable for MHD, in which case your application
   1247  *        also has to be compiled using the same options! Read
   1248  *        the MHD manual for more details.
   1249  * @return NULL on error (i.e. invalid arguments, out of memory)
   1250  * @ingroup response
   1251  */
   1252 _MHD_EXTERN struct MHD_Response *
   1253 MHD_create_response_from_fd_at_offset (size_t size,
   1254                                        int fd,
   1255                                        off_t offset)
   1256 {
   1257   if (0 > offset)
   1258     return NULL;
   1259   return MHD_create_response_from_fd_at_offset64 (size,
   1260                                                   fd,
   1261                                                   (uint64_t) offset);
   1262 }
   1263 
   1264 
   1265 /**
   1266  * Create a response object with the content of provided file with
   1267  * specified offset used as the response body.
   1268  *
   1269  * The response object can be extended with header information and then
   1270  * be used any number of times.
   1271  *
   1272  * If response object is used to answer HEAD request then the body
   1273  * of the response is not used, while all headers (including automatic
   1274  * headers) are used.
   1275  *
   1276  * @param size size of the data portion of the response;
   1277  *        sizes larger than 2 GiB may be not supported by OS or
   1278  *        MHD build; see ::MHD_FEATURE_LARGE_FILE
   1279  * @param fd file descriptor referring to a file on disk with the
   1280  *        data; will be closed when response is destroyed;
   1281  *        fd should be in 'blocking' mode
   1282  * @param offset offset to start reading from in the file;
   1283  *        reading file beyond 2 GiB may be not supported by OS or
   1284  *        MHD build; see ::MHD_FEATURE_LARGE_FILE
   1285  * @return NULL on error (i.e. invalid arguments, out of memory)
   1286  * @ingroup response
   1287  */
   1288 _MHD_EXTERN struct MHD_Response *
   1289 MHD_create_response_from_fd_at_offset64 (uint64_t size,
   1290                                          int fd,
   1291                                          uint64_t offset)
   1292 {
   1293   struct MHD_Response *response;
   1294 
   1295 #if ! defined(HAVE___LSEEKI64) && ! defined(HAVE_LSEEK64)
   1296   if ( (sizeof(uint64_t) > sizeof(off_t)) &&
   1297        ( (size > (uint64_t) INT32_MAX) ||
   1298          (offset > (uint64_t) INT32_MAX) ||
   1299          ((size + offset) >= (uint64_t) INT32_MAX) ) )
   1300     return NULL;
   1301 #endif
   1302   if ( ((int64_t) size < 0) ||
   1303        ((int64_t) offset < 0) ||
   1304        ((int64_t) (size + offset) < 0) )
   1305     return NULL;
   1306 
   1307   response = MHD_create_response_from_callback (size,
   1308                                                 MHD_FILE_READ_BLOCK_SIZE,
   1309                                                 &file_reader,
   1310                                                 NULL,
   1311                                                 &free_callback);
   1312   if (NULL == response)
   1313     return NULL;
   1314   response->fd = fd;
   1315   response->is_pipe = false;
   1316   response->fd_off = offset;
   1317   response->crc_cls = response;
   1318   return response;
   1319 }
   1320 
   1321 
   1322 /**
   1323  * Create a response object with the response body created by reading
   1324  * the provided pipe.
   1325  *
   1326  * The response object can be extended with header information and
   1327  * then be used ONLY ONCE.
   1328  *
   1329  * If response object is used to answer HEAD request then the body
   1330  * of the response is not used, while all headers (including automatic
   1331  * headers) are used.
   1332  *
   1333  * @param fd file descriptor referring to a read-end of a pipe with the
   1334  *        data; will be closed when response is destroyed;
   1335  *        fd should be in 'blocking' mode
   1336  * @return NULL on error (i.e. invalid arguments, out of memory)
   1337  * @ingroup response
   1338  */
   1339 _MHD_EXTERN struct MHD_Response *
   1340 MHD_create_response_from_pipe (int fd)
   1341 {
   1342   struct MHD_Response *response;
   1343 
   1344   response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
   1345                                                 MHD_FILE_READ_BLOCK_SIZE,
   1346                                                 &pipe_reader,
   1347                                                 NULL,
   1348                                                 &free_callback);
   1349   if (NULL == response)
   1350     return NULL;
   1351   response->fd = fd;
   1352   response->is_pipe = true;
   1353   response->crc_cls = response;
   1354   return response;
   1355 }
   1356 
   1357 
   1358 /**
   1359  * Create a response object with the content of provided file used as
   1360  * the response body.
   1361  *
   1362  * The response object can be extended with header information and then
   1363  * be used any number of times.
   1364  *
   1365  * If response object is used to answer HEAD request then the body
   1366  * of the response is not used, while all headers (including automatic
   1367  * headers) are used.
   1368  *
   1369  * @param size size of the data portion of the response
   1370  * @param fd file descriptor referring to a file on disk with the data
   1371  * @return NULL on error (i.e. invalid arguments, out of memory)
   1372  * @ingroup response
   1373  */
   1374 _MHD_EXTERN struct MHD_Response *
   1375 MHD_create_response_from_fd (size_t size,
   1376                              int fd)
   1377 {
   1378   return MHD_create_response_from_fd_at_offset64 (size,
   1379                                                   fd,
   1380                                                   0);
   1381 }
   1382 
   1383 
   1384 /**
   1385  * Create a response object with the content of provided file used as
   1386  * the response body.
   1387  *
   1388  * The response object can be extended with header information and then
   1389  * be used any number of times.
   1390  *
   1391  * If response object is used to answer HEAD request then the body
   1392  * of the response is not used, while all headers (including automatic
   1393  * headers) are used.
   1394  *
   1395  * @param size size of the data portion of the response;
   1396  *        sizes larger than 2 GiB may be not supported by OS or
   1397  *        MHD build; see ::MHD_FEATURE_LARGE_FILE
   1398  * @param fd file descriptor referring to a file on disk with the
   1399  *        data; will be closed when response is destroyed;
   1400  *        fd should be in 'blocking' mode
   1401  * @return NULL on error (i.e. invalid arguments, out of memory)
   1402  * @ingroup response
   1403  */
   1404 _MHD_EXTERN struct MHD_Response *
   1405 MHD_create_response_from_fd64 (uint64_t size,
   1406                                int fd)
   1407 {
   1408   return MHD_create_response_from_fd_at_offset64 (size,
   1409                                                   fd,
   1410                                                   0);
   1411 }
   1412 
   1413 
   1414 /**
   1415  * Create a response object.
   1416  * The response object can be extended with header information and then be used
   1417  * any number of times.
   1418  *
   1419  * If response object is used to answer HEAD request then the body of the
   1420  * response is not used, while all headers (including automatic headers) are
   1421  * used.
   1422  *
   1423  * @param size size of the @a data portion of the response
   1424  * @param data the data itself
   1425  * @param must_free libmicrohttpd should free data when done
   1426  * @param must_copy libmicrohttpd must make a copy of @a data
   1427  *        right away, the data may be released anytime after
   1428  *        this call returns
   1429  * @return NULL on error (i.e. invalid arguments, out of memory)
   1430  * @deprecated use #MHD_create_response_from_buffer instead
   1431  * @ingroup response
   1432  */
   1433 _MHD_EXTERN struct MHD_Response *
   1434 MHD_create_response_from_data (size_t size,
   1435                                void *data,
   1436                                int must_free,
   1437                                int must_copy)
   1438 {
   1439   enum MHD_ResponseMemoryMode mode;
   1440 
   1441   if (0 != must_copy)
   1442     mode = MHD_RESPMEM_MUST_COPY;
   1443   else if (0 != must_free)
   1444     mode = MHD_RESPMEM_MUST_FREE;
   1445   else
   1446     mode = MHD_RESPMEM_PERSISTENT;
   1447 
   1448   return MHD_create_response_from_buffer (size, data, mode);
   1449 }
   1450 
   1451 
   1452 /**
   1453  * Create a response object with the content of provided buffer used as
   1454  * the response body.
   1455  *
   1456  * The response object can be extended with header information and then
   1457  * be used any number of times.
   1458  *
   1459  * If response object is used to answer HEAD request then the body
   1460  * of the response is not used, while all headers (including automatic
   1461  * headers) are used.
   1462  *
   1463  * @param size size of the data portion of the response
   1464  * @param buffer size bytes containing the response's data portion
   1465  * @param mode flags for buffer management
   1466  * @return NULL on error (i.e. invalid arguments, out of memory)
   1467  * @ingroup response
   1468  */
   1469 _MHD_EXTERN struct MHD_Response *
   1470 MHD_create_response_from_buffer (size_t size,
   1471                                  void *buffer,
   1472                                  enum MHD_ResponseMemoryMode mode)
   1473 {
   1474   if (MHD_RESPMEM_MUST_FREE == mode)
   1475     return MHD_create_response_from_buffer_with_free_callback_cls (size,
   1476                                                                    buffer,
   1477                                                                    &free,
   1478                                                                    buffer);
   1479   if (MHD_RESPMEM_MUST_COPY == mode)
   1480     return MHD_create_response_from_buffer_copy (size,
   1481                                                  buffer);
   1482 
   1483   return MHD_create_response_from_buffer_with_free_callback_cls (size,
   1484                                                                  buffer,
   1485                                                                  NULL,
   1486                                                                  NULL);
   1487 }
   1488 
   1489 
   1490 /**
   1491  * Create a response object with the content of provided statically allocated
   1492  * buffer used as the response body.
   1493  *
   1494  * The buffer must be valid for the lifetime of the response. The easiest way
   1495  * to achieve this is to use a statically allocated buffer.
   1496  *
   1497  * The response object can be extended with header information and then
   1498  * be used any number of times.
   1499  *
   1500  * If response object is used to answer HEAD request then the body
   1501  * of the response is not used, while all headers (including automatic
   1502  * headers) are used.
   1503  *
   1504  * @param size the size of the data in @a buffer, can be zero
   1505  * @param buffer the buffer with the data for the response body, can be NULL
   1506  *               if @a size is zero
   1507  * @return NULL on error (i.e. invalid arguments, out of memory)
   1508  * @note Available since #MHD_VERSION 0x00097701
   1509  * @ingroup response
   1510  */
   1511 _MHD_EXTERN struct MHD_Response *
   1512 MHD_create_response_from_buffer_static (size_t size,
   1513                                         const void *buffer)
   1514 {
   1515   return MHD_create_response_from_buffer_with_free_callback_cls (size,
   1516                                                                  buffer,
   1517                                                                  NULL,
   1518                                                                  NULL);
   1519 }
   1520 
   1521 
   1522 /**
   1523  * Create a response object with the content of provided temporal buffer
   1524  * used as the response body.
   1525  *
   1526  * An internal copy of the buffer will be made automatically, so buffer have
   1527  * to be valid only during the call of this function (as a typical example:
   1528  * buffer is a local (non-static) array).
   1529  *
   1530  * The response object can be extended with header information and then
   1531  * be used any number of times.
   1532  *
   1533  * If response object is used to answer HEAD request then the body
   1534  * of the response is not used, while all headers (including automatic
   1535  * headers) are used.
   1536  *
   1537  * @param size the size of the data in @a buffer, can be zero
   1538  * @param buffer the buffer with the data for the response body, can be NULL
   1539  *               if @a size is zero
   1540  * @return NULL on error (i.e. invalid arguments, out of memory)
   1541  * @note Available since #MHD_VERSION 0x00097701
   1542  * @ingroup response
   1543  */
   1544 _MHD_EXTERN struct MHD_Response *
   1545 MHD_create_response_from_buffer_copy (size_t size,
   1546                                       const void *buffer)
   1547 {
   1548   struct MHD_Response *r;
   1549   void *mhd_copy;
   1550 
   1551   if (0 == size)
   1552     return MHD_create_response_from_buffer_with_free_callback_cls (0,
   1553                                                                    NULL,
   1554                                                                    NULL,
   1555                                                                    NULL);
   1556   if (NULL == buffer)
   1557     return NULL;
   1558 
   1559   mhd_copy = malloc (size);
   1560   if (NULL == mhd_copy)
   1561     return NULL;
   1562   memcpy (mhd_copy, buffer, size);
   1563 
   1564   r = MHD_create_response_from_buffer_with_free_callback_cls (size,
   1565                                                               mhd_copy,
   1566                                                               &free,
   1567                                                               mhd_copy);
   1568   if (NULL == r)
   1569     free (mhd_copy);
   1570   else
   1571   {
   1572     /* TODO: remove the next assignment, the buffer should not be modifiable */
   1573     r->data_buffer_size = size;
   1574   }
   1575 
   1576   return r;
   1577 }
   1578 
   1579 
   1580 /**
   1581  * Create a response object with the content of provided buffer used as
   1582  * the response body.
   1583  *
   1584  * The response object can be extended with header information and then
   1585  * be used any number of times.
   1586  *
   1587  * If response object is used to answer HEAD request then the body
   1588  * of the response is not used, while all headers (including automatic
   1589  * headers) are used.
   1590  *
   1591  * @param size size of the data portion of the response
   1592  * @param buffer size bytes containing the response's data portion
   1593  * @param crfc function to call to free the @a buffer
   1594  * @return NULL on error (i.e. invalid arguments, out of memory)
   1595  * @ingroup response
   1596  */
   1597 _MHD_EXTERN struct MHD_Response *
   1598 MHD_create_response_from_buffer_with_free_callback (size_t size,
   1599                                                     void *buffer,
   1600                                                     MHD_ContentReaderFreeCallback
   1601                                                     crfc)
   1602 {
   1603   return MHD_create_response_from_buffer_with_free_callback_cls (size,
   1604                                                                  buffer,
   1605                                                                  crfc,
   1606                                                                  buffer);
   1607 }
   1608 
   1609 
   1610 /**
   1611  * Create a response object with the content of provided buffer used as
   1612  * the response body.
   1613  *
   1614  * The response object can be extended with header information and then
   1615  * be used any number of times.
   1616  *
   1617  * If response object is used to answer HEAD request then the body
   1618  * of the response is not used, while all headers (including automatic
   1619  * headers) are used.
   1620  *
   1621  * @param size size of the data portion of the response
   1622  * @param buffer size bytes containing the response's data portion
   1623  * @param crfc function to call to cleanup, if set to NULL then callback
   1624  *             is not called
   1625  * @param crfc_cls an argument for @a crfc
   1626  * @return NULL on error (i.e. invalid arguments, out of memory)
   1627  * @note Available since #MHD_VERSION 0x00097302
   1628  * @note 'const' qualifier is used for @a buffer since #MHD_VERSION 0x00097701
   1629  * @ingroup response
   1630  */
   1631 _MHD_EXTERN struct MHD_Response *
   1632 MHD_create_response_from_buffer_with_free_callback_cls (size_t size,
   1633                                                         const void *buffer,
   1634                                                         MHD_ContentReaderFreeCallback
   1635                                                         crfc,
   1636                                                         void *crfc_cls)
   1637 {
   1638   struct MHD_Response *r;
   1639 
   1640   if ((NULL == buffer) && (size > 0))
   1641     return NULL;
   1642 #if SIZEOF_SIZE_T >= SIZEOF_UINT64_T
   1643   if (MHD_SIZE_UNKNOWN == size)
   1644     return NULL;
   1645 #endif /* SIZEOF_SIZE_T >= SIZEOF_UINT64_T */
   1646   r = MHD_calloc_ (1, sizeof (struct MHD_Response));
   1647   if (NULL == r)
   1648     return NULL;
   1649 #if defined(MHD_USE_THREADS)
   1650   if (! MHD_mutex_init_ (&r->mutex))
   1651   {
   1652     free (r);
   1653     return NULL;
   1654   }
   1655 #endif
   1656   r->fd = -1;
   1657   r->reference_count = 1;
   1658   r->total_size = size;
   1659   r->data = buffer;
   1660   r->data_size = size;
   1661   r->crfc = crfc;
   1662   r->crc_cls = crfc_cls;
   1663   return r;
   1664 }
   1665 
   1666 
   1667 /**
   1668  * Create a response object with an array of memory buffers
   1669  * used as the response body.
   1670  *
   1671  * The response object can be extended with header information and then
   1672  * be used any number of times.
   1673  *
   1674  * If response object is used to answer HEAD request then the body
   1675  * of the response is not used, while all headers (including automatic
   1676  * headers) are used.
   1677  *
   1678  * @param iov the array for response data buffers, an internal copy of this
   1679  *        will be made
   1680  * @param iovcnt the number of elements in @a iov
   1681  * @param free_cb the callback to clean up any data associated with @a iov when
   1682  *        the response is destroyed.
   1683  * @param cls the argument passed to @a free_cb
   1684  * @return NULL on error (i.e. invalid arguments, out of memory)
   1685  */
   1686 _MHD_EXTERN struct MHD_Response *
   1687 MHD_create_response_from_iovec (const struct MHD_IoVec *iov,
   1688                                 unsigned int iovcnt,
   1689                                 MHD_ContentReaderFreeCallback free_cb,
   1690                                 void *cls)
   1691 {
   1692   struct MHD_Response *response;
   1693   unsigned int i;
   1694   int i_cp = 0;   /**< Index in the copy of iov */
   1695   uint64_t total_size = 0;
   1696   const void *last_valid_buffer = NULL;
   1697 
   1698   if ((NULL == iov) && (0 < iovcnt))
   1699     return NULL;
   1700 
   1701   response = MHD_calloc_ (1, sizeof (struct MHD_Response));
   1702   if (NULL == response)
   1703     return NULL;
   1704   if (! MHD_mutex_init_ (&response->mutex))
   1705   {
   1706     free (response);
   1707     return NULL;
   1708   }
   1709   /* Calculate final size, number of valid elements, and check 'iov' */
   1710   for (i = 0; i < iovcnt; ++i)
   1711   {
   1712     if (0 == iov[i].iov_len)
   1713       continue;     /* skip zero-sized elements */
   1714     if (NULL == iov[i].iov_base)
   1715     {
   1716       i_cp = -1;     /* error */
   1717       break;
   1718     }
   1719     if ( (total_size > (total_size + iov[i].iov_len)) ||
   1720          (INT_MAX == i_cp) ||
   1721          (SSIZE_MAX < (total_size + iov[i].iov_len)) )
   1722     {
   1723       i_cp = -1;     /* overflow */
   1724       break;
   1725     }
   1726     last_valid_buffer = iov[i].iov_base;
   1727     total_size += iov[i].iov_len;
   1728 #if defined(MHD_POSIX_SOCKETS) || ! defined(_WIN64)
   1729     i_cp++;
   1730 #else  /* ! MHD_POSIX_SOCKETS && _WIN64 */
   1731     {
   1732       int64_t i_add;
   1733 
   1734       i_add = (int64_t) (iov[i].iov_len / ULONG_MAX);
   1735       if (0 != iov[i].iov_len % ULONG_MAX)
   1736         i_add++;
   1737       if (INT_MAX < (i_add + i_cp))
   1738       {
   1739         i_cp = -1;   /* overflow */
   1740         break;
   1741       }
   1742       i_cp += (int) i_add;
   1743     }
   1744 #endif /* ! MHD_POSIX_SOCKETS && _WIN64 */
   1745   }
   1746   if (-1 == i_cp)
   1747   {
   1748     /* Some error condition */
   1749     MHD_mutex_destroy_chk_ (&response->mutex);
   1750     free (response);
   1751     return NULL;
   1752   }
   1753   response->fd = -1;
   1754   response->reference_count = 1;
   1755   response->total_size = total_size;
   1756   response->crc_cls = cls;
   1757   response->crfc = free_cb;
   1758   if (0 == i_cp)
   1759   {
   1760     mhd_assert (0 == total_size);
   1761     return response;
   1762   }
   1763   if (1 == i_cp)
   1764   {
   1765     mhd_assert (NULL != last_valid_buffer);
   1766     response->data = last_valid_buffer;
   1767     response->data_size = (size_t) total_size;
   1768     return response;
   1769   }
   1770   mhd_assert (1 < i_cp);
   1771   if (1)
   1772   { /* for local variables local scope only */
   1773     MHD_iovec_ *iov_copy;
   1774     int num_copy_elements = i_cp;
   1775 
   1776     iov_copy = MHD_calloc_ ((size_t) num_copy_elements, \
   1777                             sizeof(MHD_iovec_));
   1778     if (NULL == iov_copy)
   1779     {
   1780       MHD_mutex_destroy_chk_ (&response->mutex);
   1781       free (response);
   1782       return NULL;
   1783     }
   1784     i_cp = 0;
   1785     for (i = 0; i < iovcnt; ++i)
   1786     {
   1787       size_t element_size = iov[i].iov_len;
   1788       const uint8_t *buf = (const uint8_t *) iov[i].iov_base;
   1789 
   1790       if (0 == element_size)
   1791         continue;         /* skip zero-sized elements */
   1792 #if defined(MHD_WINSOCK_SOCKETS) && defined(_WIN64)
   1793       while (MHD_IOV_ELMN_MAX_SIZE < element_size)
   1794       {
   1795         iov_copy[i_cp].iov_base = (char *) _MHD_DROP_CONST (buf);
   1796         iov_copy[i_cp].iov_len = ULONG_MAX;
   1797         buf += ULONG_MAX;
   1798         element_size -= ULONG_MAX;
   1799         i_cp++;
   1800       }
   1801 #endif /* MHD_WINSOCK_SOCKETS && _WIN64 */
   1802       iov_copy[i_cp].iov_base = _MHD_DROP_CONST (buf);
   1803       iov_copy[i_cp].iov_len = (MHD_iov_size_) element_size;
   1804       i_cp++;
   1805     }
   1806     mhd_assert (num_copy_elements == i_cp);
   1807     mhd_assert (0 <= i_cp);
   1808     response->data_iov = iov_copy;
   1809     response->data_iovcnt = (unsigned int) i_cp;
   1810   }
   1811   return response;
   1812 }
   1813 
   1814 
   1815 /**
   1816  * Create a response object with empty (zero size) body.
   1817  *
   1818  * The response object can be extended with header information and then be used
   1819  * any number of times.
   1820  *
   1821  * This function is a faster equivalent of #MHD_create_response_from_buffer call
   1822  * with zero size combined with call of #MHD_set_response_options.
   1823  *
   1824  * @param flags the flags for the new response object
   1825  * @return NULL on error (i.e. invalid arguments, out of memory),
   1826  *         the pointer to the created response object otherwise
   1827  * @note Available since #MHD_VERSION 0x00097701
   1828  * @ingroup response
   1829  */
   1830 _MHD_EXTERN struct MHD_Response *
   1831 MHD_create_response_empty (enum MHD_ResponseFlags flags)
   1832 {
   1833   struct MHD_Response *r;
   1834   r = (struct MHD_Response *) MHD_calloc_ (1, sizeof (struct MHD_Response));
   1835   if (NULL != r)
   1836   {
   1837     if (MHD_mutex_init_ (&r->mutex))
   1838     {
   1839       r->fd = -1;
   1840       r->reference_count = 1;
   1841       /* If any flags combination will be not allowed, replace the next
   1842        * assignment with MHD_set_response_options() call. */
   1843       r->flags = flags;
   1844 
   1845       return r; /* Successful result */
   1846     }
   1847     free (r);
   1848   }
   1849   return NULL; /* Something failed */
   1850 }
   1851 
   1852 
   1853 #ifdef UPGRADE_SUPPORT
   1854 /**
   1855  * This connection-specific callback is provided by MHD to
   1856  * applications (unusual) during the #MHD_UpgradeHandler.
   1857  * It allows applications to perform 'special' actions on
   1858  * the underlying socket from the upgrade.
   1859  *
   1860  * @param urh the handle identifying the connection to perform
   1861  *            the upgrade @a action on.
   1862  * @param action which action should be performed
   1863  * @param ... arguments to the action (depends on the action)
   1864  * @return #MHD_NO on error, #MHD_YES on success
   1865  */
   1866 _MHD_EXTERN enum MHD_Result
   1867 MHD_upgrade_action (struct MHD_UpgradeResponseHandle *urh,
   1868                     enum MHD_UpgradeAction action,
   1869                     ...)
   1870 {
   1871   struct MHD_Connection *connection;
   1872   struct MHD_Daemon *daemon;
   1873 
   1874   if (NULL == urh)
   1875     return MHD_NO;
   1876   connection = urh->connection;
   1877 
   1878   /* Precaution checks on external data. */
   1879   if (NULL == connection)
   1880     return MHD_NO;
   1881   daemon = connection->daemon;
   1882   if (NULL == daemon)
   1883     return MHD_NO;
   1884 
   1885   switch (action)
   1886   {
   1887   case MHD_UPGRADE_ACTION_CLOSE:
   1888     if (urh->was_closed)
   1889       return MHD_NO; /* Already closed. */
   1890 
   1891     /* transition to special 'closed' state for start of cleanup */
   1892 #ifdef HTTPS_SUPPORT
   1893     if (0 != (daemon->options & MHD_USE_TLS) )
   1894     {
   1895       /* signal that app is done by shutdown() of 'app' socket */
   1896       /* Application will not use anyway this socket after this command. */
   1897       shutdown (urh->app.socket,
   1898                 SHUT_RDWR);
   1899     }
   1900 #endif /* HTTPS_SUPPORT */
   1901     mhd_assert (MHD_CONNECTION_UPGRADE == connection->state);
   1902     /* The next function will mark the connection as closed by application
   1903      * by setting 'urh->was_closed'.
   1904      * As soon as connection will be marked with BOTH
   1905      * 'urh->was_closed' AND 'urh->clean_ready', it will
   1906      * be moved to cleanup list by MHD_resume_connection(). */
   1907     MHD_upgraded_connection_mark_app_closed_ (connection);
   1908     return MHD_YES;
   1909   case MHD_UPGRADE_ACTION_CORK_ON:
   1910     /* Unportable API. TODO: replace with portable action. */
   1911     return MHD_connection_set_cork_state_ (connection,
   1912                                            true) ? MHD_YES : MHD_NO;
   1913   case MHD_UPGRADE_ACTION_CORK_OFF:
   1914     /* Unportable API. TODO: replace with portable action. */
   1915     return MHD_connection_set_cork_state_ (connection,
   1916                                            false) ? MHD_YES : MHD_NO;
   1917   default:
   1918     /* we don't understand this one */
   1919     return MHD_NO;
   1920   }
   1921 }
   1922 
   1923 
   1924 /**
   1925  * We are done sending the header of a given response to the client.
   1926  * Now it is time to perform the upgrade and hand over the connection
   1927  * to the application.
   1928  * @remark To be called only from thread that process connection's
   1929  * recv(), send() and response. Must be called right after sending
   1930  * response headers.
   1931  *
   1932  * @param response the response that was created for an upgrade
   1933  * @param connection the specific connection we are upgrading
   1934  * @return #MHD_YES on success, #MHD_NO on failure (will cause
   1935  *        connection to be closed)
   1936  */
   1937 enum MHD_Result
   1938 MHD_response_execute_upgrade_ (struct MHD_Response *response,
   1939                                struct MHD_Connection *connection)
   1940 {
   1941 #if defined(HTTPS_SUPPORT) || defined(_DEBUG) || defined(HAVE_MESSAGES)
   1942   struct MHD_Daemon *const daemon = connection->daemon;
   1943 #endif /* HTTPS_SUPPORT || _DEBUG || HAVE_MESSAGES */
   1944   struct MHD_UpgradeResponseHandle *urh;
   1945   size_t rbo;
   1946 
   1947 #ifdef MHD_USE_THREADS
   1948   mhd_assert ( (! MHD_D_IS_USING_THREADS_ (daemon)) || \
   1949                MHD_thread_handle_ID_is_current_thread_ (connection->tid) );
   1950 #endif /* MHD_USE_THREADS */
   1951 
   1952   /* "Upgrade" responses accepted only if MHD_ALLOW_UPGRADE is enabled */
   1953   mhd_assert (0 != (daemon->options & MHD_ALLOW_UPGRADE));
   1954   /* The header was checked when response queued */
   1955   mhd_assert (NULL != \
   1956               MHD_get_response_element_n_ (response, MHD_HEADER_KIND,
   1957                                            MHD_HTTP_HEADER_UPGRADE,
   1958                                            MHD_STATICSTR_LEN_ ( \
   1959                                              MHD_HTTP_HEADER_UPGRADE)));
   1960 
   1961   if (! connection->sk_nonblck)
   1962   {
   1963 #ifdef HAVE_MESSAGES
   1964     MHD_DLOG (daemon,
   1965               _ ("Cannot execute \"upgrade\" as the socket is in " \
   1966                  "the blocking mode.\n"));
   1967 #endif
   1968     return MHD_NO;
   1969   }
   1970   urh = MHD_calloc_ (1, sizeof (struct MHD_UpgradeResponseHandle));
   1971   if (NULL == urh)
   1972     return MHD_NO;
   1973   urh->connection = connection;
   1974   rbo = connection->read_buffer_offset;
   1975   connection->read_buffer_offset = 0;
   1976   MHD_connection_set_nodelay_state_ (connection, false);
   1977   MHD_connection_set_cork_state_ (connection, false);
   1978 #ifdef HTTPS_SUPPORT
   1979   if (0 != (daemon->options & MHD_USE_TLS) )
   1980   {
   1981     MHD_socket sv[2];
   1982 #if defined(MHD_socket_nosignal_) || ! defined(MHD_socket_pair_nblk_)
   1983     int res1;
   1984     int res2;
   1985 #endif /* MHD_socket_nosignal_ || !MHD_socket_pair_nblk_ */
   1986 
   1987 #ifdef MHD_socket_pair_nblk_
   1988     if (! MHD_socket_pair_nblk_ (sv))
   1989     {
   1990       free (urh);
   1991       return MHD_NO;
   1992     }
   1993 #else  /* !MHD_socket_pair_nblk_ */
   1994     if (! MHD_socket_pair_ (sv))
   1995     {
   1996       free (urh);
   1997       return MHD_NO;
   1998     }
   1999     res1 = MHD_socket_nonblocking_ (sv[0]);
   2000     res2 = MHD_socket_nonblocking_ (sv[1]);
   2001     if ( (! res1) || (! res2) )
   2002     {
   2003 #ifdef HAVE_MESSAGES
   2004       MHD_DLOG (daemon,
   2005                 _ ("Failed to make loopback sockets non-blocking.\n"));
   2006 #endif
   2007       if (! res2)
   2008       {
   2009         /* Socketpair cannot be used. */
   2010         MHD_socket_close_chk_ (sv[0]);
   2011         MHD_socket_close_chk_ (sv[1]);
   2012         free (urh);
   2013         return MHD_NO;
   2014       }
   2015     }
   2016 #endif /* !MHD_socket_pair_nblk_ */
   2017 #ifdef MHD_socket_nosignal_
   2018     res1 = MHD_socket_nosignal_ (sv[0]);
   2019     res2 = MHD_socket_nosignal_ (sv[1]);
   2020     if ( (! res1) || (! res2) )
   2021     {
   2022 #ifdef HAVE_MESSAGES
   2023       MHD_DLOG (daemon,
   2024                 _ ("Failed to set SO_NOSIGPIPE on loopback sockets.\n"));
   2025 #endif
   2026 #ifndef MSG_NOSIGNAL
   2027       if (! res2)
   2028       {
   2029         /* Socketpair cannot be used. */
   2030         MHD_socket_close_chk_ (sv[0]);
   2031         MHD_socket_close_chk_ (sv[1]);
   2032         free (urh);
   2033         return MHD_NO;
   2034       }
   2035 #endif /* ! MSG_NOSIGNAL */
   2036     }
   2037 #endif /* MHD_socket_nosignal_ */
   2038     if (MHD_D_IS_USING_SELECT_ (daemon) &&
   2039         (! MHD_D_DOES_SCKT_FIT_FDSET_ (sv[1], daemon)) )
   2040     {
   2041 #ifdef HAVE_MESSAGES
   2042       MHD_DLOG (daemon,
   2043                 _ ("Socketpair descriptor is not less than FD_SETSIZE: " \
   2044                    "%d >= %d\n"),
   2045                 (int) sv[1],
   2046                 (int) MHD_D_GET_FD_SETSIZE_ (daemon));
   2047 #endif
   2048       MHD_socket_close_chk_ (sv[0]);
   2049       MHD_socket_close_chk_ (sv[1]);
   2050       free (urh);
   2051       return MHD_NO;
   2052     }
   2053     urh->app.socket = sv[0];
   2054     urh->app.urh = urh;
   2055     urh->app.celi = MHD_EPOLL_STATE_UNREADY;
   2056     urh->mhd.socket = sv[1];
   2057     urh->mhd.urh = urh;
   2058     urh->mhd.celi = MHD_EPOLL_STATE_UNREADY;
   2059 #ifdef EPOLL_SUPPORT
   2060     /* Launch IO processing by the event loop */
   2061     if (MHD_D_IS_USING_EPOLL_ (daemon))
   2062     {
   2063       /* We're running with epoll(), need to add the sockets
   2064          to the event set of the daemon's `epoll_upgrade_fd` */
   2065       struct epoll_event event;
   2066 
   2067       mhd_assert (-1 != daemon->epoll_upgrade_fd);
   2068       /* First, add network socket */
   2069       event.events = EPOLLIN | EPOLLOUT | EPOLLPRI | EPOLLET;
   2070       event.data.ptr = &urh->app;
   2071       if (0 != epoll_ctl (daemon->epoll_upgrade_fd,
   2072                           EPOLL_CTL_ADD,
   2073                           connection->socket_fd,
   2074                           &event))
   2075       {
   2076 #ifdef HAVE_MESSAGES
   2077         MHD_DLOG (daemon,
   2078                   _ ("Call to epoll_ctl failed: %s\n"),
   2079                   MHD_socket_last_strerr_ ());
   2080 #endif
   2081         MHD_socket_close_chk_ (sv[0]);
   2082         MHD_socket_close_chk_ (sv[1]);
   2083         free (urh);
   2084         return MHD_NO;
   2085       }
   2086 
   2087       /* Second, add our end of the UNIX socketpair() */
   2088       event.events = EPOLLIN | EPOLLOUT | EPOLLPRI | EPOLLET;
   2089       event.data.ptr = &urh->mhd;
   2090       if (0 != epoll_ctl (daemon->epoll_upgrade_fd,
   2091                           EPOLL_CTL_ADD,
   2092                           urh->mhd.socket,
   2093                           &event))
   2094       {
   2095         event.events = EPOLLIN | EPOLLOUT | EPOLLPRI;
   2096         event.data.ptr = &urh->app;
   2097         if (0 != epoll_ctl (daemon->epoll_upgrade_fd,
   2098                             EPOLL_CTL_DEL,
   2099                             connection->socket_fd,
   2100                             &event))
   2101           MHD_PANIC (_ ("Error cleaning up while handling epoll error.\n"));
   2102 #ifdef HAVE_MESSAGES
   2103         MHD_DLOG (daemon,
   2104                   _ ("Call to epoll_ctl failed: %s\n"),
   2105                   MHD_socket_last_strerr_ ());
   2106 #endif
   2107         MHD_socket_close_chk_ (sv[0]);
   2108         MHD_socket_close_chk_ (sv[1]);
   2109         free (urh);
   2110         return MHD_NO;
   2111       }
   2112       EDLL_insert (daemon->eready_urh_head,
   2113                    daemon->eready_urh_tail,
   2114                    urh);
   2115       urh->in_eready_list = true;
   2116     }
   2117 #endif /* EPOLL_SUPPORT */
   2118     if (! MHD_D_IS_USING_THREAD_PER_CONN_ (daemon))
   2119     {
   2120       /* This takes care of further processing for most event loops:
   2121          simply add to DLL for bi-direcitonal processing */
   2122       DLL_insert (daemon->urh_head,
   2123                   daemon->urh_tail,
   2124                   urh);
   2125     }
   2126     /* In thread-per-connection mode, thread will switch to forwarding once
   2127      * connection.urh is not NULL and connection.state == MHD_CONNECTION_UPGRADE.
   2128      */
   2129   }
   2130   else
   2131   {
   2132     urh->app.socket = MHD_INVALID_SOCKET;
   2133     urh->mhd.socket = MHD_INVALID_SOCKET;
   2134     /* Non-TLS connection do not hold any additional resources. */
   2135     urh->clean_ready = true;
   2136   }
   2137 #else  /* ! HTTPS_SUPPORT */
   2138   urh->clean_ready = true;
   2139 #endif /* ! HTTPS_SUPPORT */
   2140   connection->urh = urh;
   2141   /* As far as MHD's event loops are concerned, this connection is
   2142      suspended; it will be resumed once application is done by the
   2143      #MHD_upgrade_action() function */
   2144   internal_suspend_connection_ (connection);
   2145 
   2146   /* hand over socket to application */
   2147   response->upgrade_handler (response->upgrade_handler_cls,
   2148                              connection,
   2149                              connection->rq.client_context,
   2150                              connection->read_buffer,
   2151                              rbo,
   2152 #ifdef HTTPS_SUPPORT
   2153                              (0 == (daemon->options & MHD_USE_TLS) ) ?
   2154                              connection->socket_fd : urh->app.socket,
   2155 #else  /* ! HTTPS_SUPPORT */
   2156                              connection->socket_fd,
   2157 #endif /* ! HTTPS_SUPPORT */
   2158                              urh);
   2159 
   2160 #ifdef HTTPS_SUPPORT
   2161   if (0 != (daemon->options & MHD_USE_TLS))
   2162   {
   2163     struct MemoryPool *const pool = connection->pool;
   2164     size_t avail;
   2165     char *buf;
   2166 
   2167     /* All data should be sent already */
   2168     mhd_assert (connection->write_buffer_send_offset == \
   2169                 connection->write_buffer_append_offset);
   2170     MHD_pool_deallocate (pool, connection->write_buffer,
   2171                          connection->write_buffer_size);
   2172     connection->write_buffer_append_offset = 0;
   2173     connection->write_buffer_send_offset = 0;
   2174     connection->write_buffer_size = 0;
   2175     connection->write_buffer = NULL;
   2176 
   2177     /* Extra read data should be processed already by the application */
   2178     MHD_pool_deallocate (pool, connection->read_buffer,
   2179                          connection->read_buffer_size);
   2180     connection->read_buffer_offset = 0;
   2181     connection->read_buffer_size = 0;
   2182     connection->read_buffer = NULL;
   2183 
   2184     avail = MHD_pool_get_free (pool);
   2185     if (avail < RESERVE_EBUF_SIZE)
   2186     {
   2187       /* connection's pool is totally at the limit,
   2188          use our 'emergency' buffer of #RESERVE_EBUF_SIZE bytes. */
   2189       avail = RESERVE_EBUF_SIZE;
   2190       buf = urh->e_buf;
   2191 #ifdef HAVE_MESSAGES
   2192       MHD_DLOG (daemon,
   2193                 _ ("Memory shortage in connection's memory pool. " \
   2194                    "The \"upgraded\" communication will be inefficient.\n"));
   2195 #endif
   2196     }
   2197     else
   2198     {
   2199       /* Normal case: grab all remaining memory from the
   2200          connection's pool for the IO buffers; the connection
   2201          certainly won't need it anymore as we've upgraded
   2202          to another protocol. */
   2203       buf = MHD_pool_allocate (pool,
   2204                                avail,
   2205                                false);
   2206     }
   2207     /* use half the buffer for inbound, half for outbound */
   2208     urh->in_buffer_size = avail / 2;
   2209     urh->out_buffer_size = avail - urh->in_buffer_size;
   2210     urh->in_buffer = buf;
   2211     urh->out_buffer = buf + urh->in_buffer_size;
   2212   }
   2213 #endif /* HTTPS_SUPPORT */
   2214   return MHD_YES;
   2215 }
   2216 
   2217 
   2218 /**
   2219  * Create a response object that can be used for 101 UPGRADE
   2220  * responses, for example to implement WebSockets.  After sending the
   2221  * response, control over the data stream is given to the callback (which
   2222  * can then, for example, start some bi-directional communication).
   2223  * If the response is queued for multiple connections, the callback
   2224  * will be called for each connection.  The callback
   2225  * will ONLY be called after the response header was successfully passed
   2226  * to the OS; if there are communication errors before, the usual MHD
   2227  * connection error handling code will be performed.
   2228  *
   2229  * Setting the correct HTTP code (i.e. MHD_HTTP_SWITCHING_PROTOCOLS)
   2230  * and setting correct HTTP headers for the upgrade must be done
   2231  * manually (this way, it is possible to implement most existing
   2232  * WebSocket versions using this API; in fact, this API might be useful
   2233  * for any protocol switch, not just WebSockets).  Note that
   2234  * draft-ietf-hybi-thewebsocketprotocol-00 cannot be implemented this
   2235  * way as the header "HTTP/1.1 101 WebSocket Protocol Handshake"
   2236  * cannot be generated; instead, MHD will always produce "HTTP/1.1 101
   2237  * Switching Protocols" (if the response code 101 is used).
   2238  *
   2239  * As usual, the response object can be extended with header
   2240  * information and then be used any number of times (as long as the
   2241  * header information is not connection-specific).
   2242  *
   2243  * @param upgrade_handler function to call with the 'upgraded' socket
   2244  * @param upgrade_handler_cls closure for @a upgrade_handler
   2245  * @return NULL on error (i.e. invalid arguments, out of memory)
   2246  */
   2247 _MHD_EXTERN struct MHD_Response *
   2248 MHD_create_response_for_upgrade (MHD_UpgradeHandler upgrade_handler,
   2249                                  void *upgrade_handler_cls)
   2250 {
   2251   struct MHD_Response *response;
   2252 
   2253   if (NULL == upgrade_handler)
   2254     return NULL; /* invalid request */
   2255   response = MHD_calloc_ (1, sizeof (struct MHD_Response));
   2256   if (NULL == response)
   2257     return NULL;
   2258 #if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
   2259   if (! MHD_mutex_init_ (&response->mutex))
   2260   {
   2261     free (response);
   2262     return NULL;
   2263   }
   2264 #endif
   2265   response->upgrade_handler = upgrade_handler;
   2266   response->upgrade_handler_cls = upgrade_handler_cls;
   2267   response->total_size = 0;
   2268   response->reference_count = 1;
   2269   if (MHD_NO ==
   2270       MHD_add_response_header (response,
   2271                                MHD_HTTP_HEADER_CONNECTION,
   2272                                "Upgrade"))
   2273   {
   2274     MHD_destroy_response (response);
   2275     return NULL;
   2276   }
   2277   return response;
   2278 }
   2279 
   2280 
   2281 #endif /* UPGRADE_SUPPORT */
   2282 
   2283 
   2284 /**
   2285  * Destroy a response object and associated resources.  Note that
   2286  * libmicrohttpd may keep some of the resources around if the response
   2287  * is still in the queue for some clients, so the memory may not
   2288  * necessarily be freed immediately.
   2289  *
   2290  * @param response response to destroy
   2291  * @ingroup response
   2292  */
   2293 _MHD_EXTERN void
   2294 MHD_destroy_response (struct MHD_Response *response)
   2295 {
   2296   struct MHD_HTTP_Res_Header *pos;
   2297 
   2298   if (NULL == response)
   2299     return;
   2300 #if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
   2301   MHD_mutex_lock_chk_ (&response->mutex);
   2302 #endif
   2303   if (0 != --(response->reference_count))
   2304   {
   2305 #if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
   2306     MHD_mutex_unlock_chk_ (&response->mutex);
   2307 #endif
   2308     return;
   2309   }
   2310 #if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
   2311   MHD_mutex_unlock_chk_ (&response->mutex);
   2312   MHD_mutex_destroy_chk_ (&response->mutex);
   2313 #endif
   2314   if (NULL != response->crfc)
   2315     response->crfc (response->crc_cls);
   2316 
   2317   if (NULL != response->data_iov)
   2318   {
   2319     free (response->data_iov);
   2320   }
   2321 
   2322   while (NULL != response->first_header)
   2323   {
   2324     pos = response->first_header;
   2325     response->first_header = pos->next;
   2326     free (pos->header);
   2327     free (pos->value);
   2328     free (pos);
   2329   }
   2330   free (response);
   2331 }
   2332 
   2333 
   2334 /**
   2335  * Increments the reference counter for the @a response.
   2336  *
   2337  * @param response object to modify
   2338  */
   2339 void
   2340 MHD_increment_response_rc (struct MHD_Response *response)
   2341 {
   2342 #if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
   2343   MHD_mutex_lock_chk_ (&response->mutex);
   2344 #endif
   2345   (response->reference_count)++;
   2346 #if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
   2347   MHD_mutex_unlock_chk_ (&response->mutex);
   2348 #endif
   2349 }
   2350 
   2351 
   2352 /* end of response.c */