libmicrohttpd2

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

respond_with_error.c (4899B)


      1 /* SPDX-License-Identifier: LGPL-2.1-or-later OR (GPL-2.0-or-later WITH eCos-exception-2.0) */
      2 /*
      3   This file is part of GNU libmicrohttpd.
      4   Copyright (C) 2024 Evgeny Grin (Karlson2k)
      5 
      6   GNU libmicrohttpd is free software; you can redistribute it and/or
      7   modify it under the terms of the GNU Lesser General Public
      8   License as published by the Free Software Foundation; either
      9   version 2.1 of the License, or (at your option) any later version.
     10 
     11   GNU libmicrohttpd is distributed in the hope that it will be useful,
     12   but WITHOUT ANY WARRANTY; without even the implied warranty of
     13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14   Lesser General Public License for more details.
     15 
     16   Alternatively, you can redistribute GNU libmicrohttpd and/or
     17   modify it under the terms of the GNU General Public License as
     18   published by the Free Software Foundation; either version 2 of
     19   the License, or (at your option) any later version, together
     20   with the eCos exception, as follows:
     21 
     22     As a special exception, if other files instantiate templates or
     23     use macros or inline functions from this file, or you compile this
     24     file and link it with other works to produce a work based on this
     25     file, this file does not by itself cause the resulting work to be
     26     covered by the GNU General Public License. However the source code
     27     for this file must still be made available in accordance with
     28     section (3) of the GNU General Public License v2.
     29 
     30     This exception does not invalidate any other reasons why a work
     31     based on this file might be covered by the GNU General Public
     32     License.
     33 
     34   You should have received copies of the GNU Lesser General Public
     35   License and the GNU General Public License along with this library;
     36   if not, see <https://www.gnu.org/licenses/>.
     37 */
     38 
     39 /**
     40  * @file src/mhd2/respond_with_error.c
     41  * @brief  The implementation of error response functions
     42  * @author Karlson2k (Evgeny Grin)
     43  */
     44 
     45 #include "mhd_sys_options.h"
     46 #include "respond_with_error.h"
     47 
     48 #include "sys_base_types.h"
     49 #include "sys_null_macro.h"
     50 #include "mhd_str_macros.h"
     51 
     52 #include "mhd_assert.h"
     53 
     54 #include "sys_malloc.h"
     55 
     56 #include "mhd_connection.h"
     57 
     58 #include "mempool_funcs.h"
     59 
     60 #include "response_from.h"
     61 #include "daemon_logger.h"
     62 #include "response_destroy.h"
     63 #include "stream_funcs.h"
     64 #include "daemon_funcs.h"
     65 
     66 #include "mhd_public_api.h"
     67 
     68 MHD_INTERNAL
     69 MHD_FN_PAR_NONNULL_ (1)
     70 MHD_FN_PAR_CSTR_ (4) MHD_FN_PAR_CSTR_ (6) void
     71 respond_with_error_len (struct MHD_Connection *c,
     72                         unsigned int http_code,
     73                         size_t msg_len,
     74                         const char *msg,
     75                         size_t add_hdr_line_len,
     76                         char *add_hdr_line)
     77 {
     78   struct MHD_Response *err_res;
     79 
     80   mhd_assert (! c->stop_with_error); /* Do not send error twice */
     81   mhd_assert (mhd_HTTP_STAGE_REQ_RECV_FINISHED >= c->stage);
     82 
     83   /* Discard most of the request data */
     84 
     85   if (NULL != c->rq.cntn.lbuf.data)
     86     mhd_daemon_free_lbuf (c->daemon, &(c->rq.cntn.lbuf));
     87   c->rq.cntn.lbuf.data = NULL;
     88 
     89   c->write_buffer = NULL;
     90   c->write_buffer_size = 0;
     91   c->write_buffer_send_offset = 0;
     92   c->write_buffer_append_offset = 0;
     93 
     94   mhd_DLINKEDL_INIT_LIST (&(c->rq), fields);
     95   c->rq.version = NULL;
     96   c->rq.method.len = 0;
     97   c->rq.method.cstr = NULL;
     98   c->rq.url = NULL;
     99   c->continue_message_write_offset = 0;
    100   if (0 != c->read_buffer_size)
    101   {
    102     mhd_pool_deallocate (c->pool,
    103                          c->read_buffer,
    104                          c->read_buffer_size);
    105     c->read_buffer = NULL;
    106     c->read_buffer_size = 0;
    107     c->read_buffer_offset = 0;
    108   }
    109 
    110   c->stop_with_error = true;
    111   c->discard_request = true;
    112   if ((MHD_HTTP_STATUS_CONTENT_TOO_LARGE == http_code) ||
    113       (MHD_HTTP_STATUS_URI_TOO_LONG == http_code) ||
    114       (MHD_HTTP_STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE == http_code))
    115     c->rq.too_large = true;
    116 
    117   mhd_LOG_PRINT (c->daemon,
    118                  MHD_SC_REQ_PROCCESSING_ERR_REPLY,
    119                  mhd_LOG_FMT ("Error processing request. Sending %u " \
    120                               "error reply: %s"),
    121                  (unsigned int) http_code,
    122                  (NULL != msg) ? msg : "[EMPTY BODY]");
    123 
    124   if (NULL != c->rp.response)
    125   {
    126     mhd_response_dec_use_count (c->rp.response);
    127     c->rp.response = NULL;
    128   }
    129   err_res = mhd_response_special_for_error (http_code,
    130                                             msg_len,
    131                                             msg,
    132                                             add_hdr_line_len,
    133                                             add_hdr_line);
    134   if (NULL == err_res)
    135   {
    136     if (NULL != add_hdr_line)
    137       free (add_hdr_line);
    138     mhd_STREAM_ABORT (c, \
    139                       mhd_CONN_CLOSE_NO_MEM_FOR_ERR_RESPONSE, \
    140                       "No memory to create error response.");
    141     return;
    142   }
    143   c->rp.response = err_res;
    144   c->stage = mhd_HTTP_STAGE_START_REPLY;
    145 }