libmicrohttpd2

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

conn_data_recv.c (6184B)


      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-2025 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/conn_data_recv.c
     41  * @brief  The implementation of data receiving functions for connection
     42  * @author Karlson2k (Evgeny Grin)
     43  */
     44 
     45 #include "mhd_sys_options.h"
     46 
     47 #include "conn_data_recv.h"
     48 
     49 #include "mhd_sys_options.h"
     50 
     51 #include "sys_bool_type.h"
     52 #include "sys_base_types.h"
     53 
     54 #include "mhd_assert.h"
     55 
     56 #include "mhd_connection.h"
     57 
     58 #include "conn_timeout.h"
     59 #include "stream_funcs.h"
     60 #include "mhd_socket_error_funcs.h"
     61 #include "sckt_recv.h"
     62 
     63 #include "mhd_recv.h"
     64 
     65 MHD_INTERNAL MHD_FN_PAR_NONNULL_ALL_ void
     66 mhd_conn_data_recv (struct MHD_Connection *c,
     67                     bool has_err)
     68 {
     69   void *buf;
     70   size_t buf_size;
     71   size_t received;
     72   enum mhd_SocketError res;
     73 
     74   mhd_assert (mhd_HTTP_STAGE_CLOSED != c->stage);
     75   mhd_assert (NULL != c->read_buffer);
     76   mhd_assert (c->read_buffer_size > c->read_buffer_offset);
     77   mhd_assert (! has_err || \
     78               (0 != (c->sk.ready & mhd_SOCKET_NET_STATE_ERROR_READY)));
     79   mhd_assert ((0 == (c->sk.ready & mhd_SOCKET_NET_STATE_ERROR_READY)) || \
     80               has_err);
     81   mhd_assert (mhd_SOCKET_ERR_NO_ERROR == c->sk.state.discnt_err);
     82 
     83   buf = c->read_buffer + c->read_buffer_offset;
     84   buf_size = c->read_buffer_size - c->read_buffer_offset;
     85 
     86   res = mhd_recv (c,
     87                   buf_size,
     88                   (char *) buf,
     89                   &received);
     90 
     91   if ((mhd_SOCKET_ERR_NO_ERROR != res) || has_err)
     92   {
     93     /* Handle errors */
     94     if ((mhd_SOCKET_ERR_NO_ERROR == res) && (0 == received))
     95     {
     96       c->sk.state.rmt_shut_wr = true;
     97       res = mhd_SOCKET_ERR_REMT_DISCONN;
     98     }
     99 
    100     if (has_err && (mhd_SOCKET_ERR_NO_ERROR == c->sk.state.discnt_err))
    101     {
    102       /* Try to get the real error from the socket */
    103       if (! mhd_SOCKET_ERR_IS_HARD (res) && c->sk.props.is_nonblck)
    104       {
    105         /* Re-try the last time with direct socket recv() to detect the error */
    106         uint_fast64_t dummy_buf;
    107         res = mhd_sckt_recv (&(c->sk),
    108                              sizeof(dummy_buf),
    109                              (char *) &dummy_buf,
    110                              &received);
    111       }
    112       if (mhd_SOCKET_ERR_IS_HARD (res))
    113       {
    114         c->sk.state.discnt_err = res;
    115         mhd_SCKT_NET_ST_SET_FLAG (&(c->sk.ready),
    116                                   mhd_SOCKET_NET_STATE_ERROR_READY);
    117       }
    118       else
    119         c->sk.state.discnt_err = mhd_socket_error_get_from_socket (c->sk.fd);
    120     }
    121 
    122     return;
    123   }
    124 
    125   if (0 == received)
    126     c->sk.state.rmt_shut_wr = true;
    127 
    128   c->read_buffer_offset += received;
    129   mhd_conn_update_activity_mark (c);
    130   return;
    131 }
    132 
    133 
    134 #if 0 // TODO: report disconnect
    135 if ((bytes_read < 0) || socket_error)
    136 {
    137   if (MHD_ERR_CONNRESET_ == bytes_read)
    138   {
    139     if ( (mhd_HTTP_STAGE_INIT < c->stage) &&
    140          (mhd_HTTP_STAGE_FULL_REQ_RECEIVED > c->stage) )
    141     {
    142 #ifdef HAVE_MESSAGES
    143       MHD_DLOG (c->daemon,
    144                 _ ("Socket has been disconnected when reading request.\n"));
    145 #endif
    146       c->discard_request = true;
    147     }
    148     MHD_connection_close_ (c,
    149                            MHD_REQUEST_TERMINATED_READ_ERROR);
    150     return;
    151   }
    152 
    153 #ifdef HAVE_MESSAGES
    154   if (mhd_HTTP_STAGE_INIT != c->stage)
    155     MHD_DLOG (c->daemon,
    156               _ ("Connection socket is closed when reading " \
    157                  "request due to the error: %s\n"),
    158               (bytes_read < 0) ? str_conn_error_ (bytes_read) :
    159               "detected c closure");
    160 #endif
    161   CONNECTION_CLOSE_ERROR (c,
    162                           NULL);
    163   return;
    164 }
    165 
    166 #if 0 // TODO: handle remote shut WR
    167 if (0 == bytes_read)
    168 { /* Remote side closed c. */   // FIXME: Actually NOT!
    169   c->sk.state.rmt_shut_wr = true;
    170   if ( (mhd_HTTP_STAGE_INIT < c->stage) &&
    171        (mhd_HTTP_STAGE_FULL_REQ_RECEIVED > c->stage) )
    172   {
    173 #ifdef HAVE_MESSAGES
    174     MHD_DLOG (c->daemon,
    175               _ ("Connection was closed by remote side with incomplete "
    176                  "request.\n"));
    177 #endif
    178     c->discard_request = true;
    179     MHD_connection_close_ (c,
    180                            MHD_REQUEST_TERMINATED_CLIENT_ABORT);
    181   }
    182   else if (mhd_HTTP_STAGE_INIT == c->stage)
    183     /* This termination code cannot be reported to the application
    184      * because application has not been informed yet about this request */
    185     MHD_connection_close_ (c,
    186                            MHD_REQUEST_TERMINATED_COMPLETED_OK);
    187   else
    188     MHD_connection_close_ (c,
    189                            MHD_REQUEST_TERMINATED_WITH_ERROR);
    190   return;
    191 }
    192 #endif
    193 #endif