libmicrohttpd2

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

conn_data_recv.c (6272B)


      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       {
    120         c->sk.state.discnt_err = mhd_socket_error_get_from_socket (c->sk.fd);
    121         mhd_assert (mhd_SOCKET_ERR_NO_ERROR != c->sk.state.discnt_err);
    122       }
    123     }
    124 
    125     return;
    126   }
    127 
    128   if (0 == received)
    129     c->sk.state.rmt_shut_wr = true;
    130 
    131   c->read_buffer_offset += received;
    132   mhd_conn_update_activity_mark (c);
    133   return;
    134 }
    135 
    136 
    137 #if 0 // TODO: report disconnect
    138 if ((bytes_read < 0) || socket_error)
    139 {
    140   if (MHD_ERR_CONNRESET_ == bytes_read)
    141   {
    142     if ( (mhd_HTTP_STAGE_INIT < c->stage) &&
    143          (mhd_HTTP_STAGE_FULL_REQ_RECEIVED > c->stage) )
    144     {
    145 #ifdef HAVE_MESSAGES
    146       MHD_DLOG (c->daemon,
    147                 _ ("Socket has been disconnected when reading request.\n"));
    148 #endif
    149       c->discard_request = true;
    150     }
    151     MHD_connection_close_ (c,
    152                            MHD_REQUEST_TERMINATED_READ_ERROR);
    153     return;
    154   }
    155 
    156 #ifdef HAVE_MESSAGES
    157   if (mhd_HTTP_STAGE_INIT != c->stage)
    158     MHD_DLOG (c->daemon,
    159               _ ("Connection socket is closed when reading " \
    160                  "request due to the error: %s\n"),
    161               (bytes_read < 0) ? str_conn_error_ (bytes_read) :
    162               "detected c closure");
    163 #endif
    164   CONNECTION_CLOSE_ERROR (c,
    165                           NULL);
    166   return;
    167 }
    168 
    169 #if 0 // TODO: handle remote shut WR
    170 if (0 == bytes_read)
    171 { /* Remote side closed c. */   // FIXME: Actually NOT!
    172   c->sk.state.rmt_shut_wr = true;
    173   if ( (mhd_HTTP_STAGE_INIT < c->stage) &&
    174        (mhd_HTTP_STAGE_FULL_REQ_RECEIVED > c->stage) )
    175   {
    176 #ifdef HAVE_MESSAGES
    177     MHD_DLOG (c->daemon,
    178               _ ("Connection was closed by remote side with incomplete "
    179                  "request.\n"));
    180 #endif
    181     c->discard_request = true;
    182     MHD_connection_close_ (c,
    183                            MHD_REQUEST_TERMINATED_CLIENT_ABORT);
    184   }
    185   else if (mhd_HTTP_STAGE_INIT == c->stage)
    186     /* This termination code cannot be reported to the application
    187      * because application has not been informed yet about this request */
    188     MHD_connection_close_ (c,
    189                            MHD_REQUEST_TERMINATED_COMPLETED_OK);
    190   else
    191     MHD_connection_close_ (c,
    192                            MHD_REQUEST_TERMINATED_WITH_ERROR);
    193   return;
    194 }
    195 #endif
    196 #endif