libmicrohttpd2

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

respond_with_error.c (4874B)


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