libmicrohttpd2

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

respond_with_error.c (4973B)


      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_NONNULL_IF_NONZERO_ (4, 3)
     71 MHD_FN_PAR_NONNULL_IF_NONZERO_ (6, 5)
     72 MHD_FN_PAR_CSTR_ (4) MHD_FN_PAR_CSTR_ (6) void
     73 respond_with_error_len (struct MHD_Connection *c,
     74                         unsigned int http_code,
     75                         size_t msg_len,
     76                         const char *msg,
     77                         size_t add_hdr_line_len,
     78                         char *add_hdr_line)
     79 {
     80   struct MHD_Response *err_res;
     81 
     82   mhd_assert (!c->stop_with_error);  /* Do not send error twice */
     83   mhd_assert (mhd_HTTP_STAGE_REQ_RECV_FINISHED >= c->stage);
     84 
     85   /* Discard most of the request data */
     86 
     87   if (NULL != c->rq.cntn.lbuf.data)
     88     mhd_daemon_free_lbuf (c->daemon, &(c->rq.cntn.lbuf));
     89   c->rq.cntn.lbuf.data = NULL;
     90 
     91   c->write_buffer = NULL;
     92   c->write_buffer_size = 0;
     93   c->write_buffer_send_offset = 0;
     94   c->write_buffer_append_offset = 0;
     95 
     96   mhd_DLINKEDL_INIT_LIST (&(c->rq), fields);
     97   c->rq.version = NULL;
     98   c->rq.method.len = 0;
     99   c->rq.method.cstr = NULL;
    100   c->rq.url = NULL;
    101   c->continue_message_write_offset = 0;
    102   if (0 != c->read_buffer_size)
    103   {
    104     mhd_pool_deallocate (c->pool,
    105                          c->read_buffer,
    106                          c->read_buffer_size);
    107     c->read_buffer = NULL;
    108     c->read_buffer_size = 0;
    109     c->read_buffer_offset = 0;
    110   }
    111 
    112   c->stop_with_error = true;
    113   c->discard_request = true;
    114   if ((MHD_HTTP_STATUS_CONTENT_TOO_LARGE == http_code)
    115       || (MHD_HTTP_STATUS_URI_TOO_LONG == http_code)
    116       || (MHD_HTTP_STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE == http_code))
    117     c->rq.too_large = true;
    118 
    119   mhd_LOG_PRINT (c->daemon,
    120                  MHD_SC_REQ_PROCESSING_ERR_REPLY,
    121                  mhd_LOG_FMT ("Error processing request. Sending %u " \
    122                               "error reply: %s"),
    123                  (unsigned int)http_code,
    124                  (NULL != msg) ? msg : "[EMPTY BODY]");
    125 
    126   if (NULL != c->rp.response)
    127   {
    128     mhd_response_dec_use_count (c->rp.response);
    129     c->rp.response = NULL;
    130   }
    131   err_res = mhd_response_special_for_error (http_code,
    132                                             msg_len,
    133                                             msg,
    134                                             add_hdr_line_len,
    135                                             add_hdr_line);
    136   if (NULL == err_res)
    137   {
    138     if (NULL != add_hdr_line)
    139       free (add_hdr_line);
    140     mhd_STREAM_ABORT (c, \
    141                       mhd_CONN_CLOSE_NO_MEM_FOR_ERR_RESPONSE, \
    142                       "No memory to create error response.");
    143     return;
    144   }
    145   c->rp.response = err_res;
    146   c->stage = mhd_HTTP_STAGE_START_REPLY;
    147 }