libmicrohttpd2

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

daemon_create.c (5159B)


      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/daemon_create.c
     41  * @brief  The implementation of the MHD_daemon_create()
     42  * @author Karlson2k (Evgeny Grin)
     43  */
     44 
     45 #include "mhd_sys_options.h"
     46 
     47 #include "sys_base_types.h"
     48 #include "sys_malloc.h"
     49 #ifndef NDEBUG
     50 #  include <stdio.h> /* For debug print */
     51 #endif /* ! NDEBUG */
     52 
     53 #include "mhd_public_api.h"
     54 
     55 #include "compat_calloc.h"
     56 
     57 #include "mhd_daemon.h"
     58 #include "daemon_options.h"
     59 
     60 #include "daemon_logger_default.h"
     61 
     62 #include "mhd_lib_init.h"
     63 
     64 #ifndef SIZE_MAX
     65 #  define SIZE_MAX ((size_t) (~((size_t) 0)))
     66 #endif
     67 
     68 MHD_FN_MUST_CHECK_RESULT_ MHD_EXTERN_ struct MHD_Daemon *
     69 MHD_daemon_create (MHD_RequestCallback req_cb,
     70                    void *req_cb_cls)
     71 {
     72   struct MHD_Daemon *d;
     73   struct DaemonOptions *s;
     74 
     75   if (NULL == req_cb)
     76     return NULL;
     77 
     78   if (!mhd_lib_init_global_if_needed ())
     79   {
     80 #ifndef NDEBUG
     81     fprintf (stderr, "Failed to initialise MHD library.\n");
     82     fflush (stderr);
     83 #endif /* ! NDEBUG */
     84     return NULL;
     85   }
     86 
     87   d = (struct MHD_Daemon *)mhd_calloc (1, sizeof(struct MHD_Daemon));
     88   if (NULL != d)
     89   {
     90     s = (struct DaemonOptions *)mhd_calloc (1, sizeof(struct DaemonOptions));
     91     if (NULL != s)
     92     {
     93       /* calloc() does not guarantee that floating point values and pointers
     94          are initialised to zero and NULL (respectively). */
     95       /* Any floating point and pointer members must be initialised manually here */
     96 #ifndef HAVE_NULL_PTR_ALL_ZEROS
     97       s->bind_sa.v_sa = NULL;
     98       s->tls_cert_key.v_mem_key = NULL;
     99       s->tls_cert_key.v_mem_cert = NULL;
    100       s->tls_cert_key.v_mem_pass = NULL;
    101       s->tls_app_name.v_app_name = NULL;
    102       s->tls_openssl_def_file.v_pathname = NULL;
    103       s->tls_client_ca = NULL;
    104       s->tls_psk_callback.v_psk_cb = NULL;
    105       s->tls_psk_callback.v_psk_cb_cls = NULL;
    106       s->accept_policy.v_apc = NULL;
    107       s->accept_policy.v_apc_cls = NULL;
    108       s->early_uri_logger.v_cb = NULL;
    109       s->early_uri_logger.v_cls = NULL;
    110       s->daemon_ready_callback.v_cb = NULL;
    111       s->daemon_ready_callback.v_cb_cls = NULL;
    112       s->notify_connection.v_ncc = NULL;
    113       s->notify_connection.v_cls = NULL;
    114       s->notify_stream.v_nsc = NULL;
    115       s->notify_stream.v_cls = NULL;
    116       s->random_entropy.v_buf = NULL;
    117 
    118       /* d->log_params.v_log_cb = NULL; */ /* used directly */
    119       /* d->log_params.v_log_cb_cls = NULL;*/ /* used directly */
    120 #endif /* !HAVE_NULL_PTR_ALL_ZEROS */
    121 
    122       s->large_pool_size = SIZE_MAX; /* The impossible value */
    123 
    124       s->listen_socket = MHD_INVALID_SOCKET;
    125       s->fd_number_limit = MHD_INVALID_SOCKET;
    126 
    127       d->log_params.v_log_cb = (MHD_LoggingCallback)mhd_logger_default;
    128       d->req_cfg.cb = req_cb;
    129       d->req_cfg.cb_cls = req_cb_cls;
    130       d->settings = s;
    131 
    132       return d; /* Success exit point */
    133     }
    134     /* Below is a clean-up path */
    135     free (d);
    136   }
    137   mhd_lib_deinit_global_if_needed ();
    138 
    139   return NULL; /* Failure exit point */
    140 }
    141 
    142 
    143 /* This is a workaround for GCC/binutils bug.
    144  * To make sure that initialisation functions are called when MHD is used as
    145  * a static library, put initialisation functions to the file with function
    146  * that always referred/used by application/user of the library.
    147  * If application does not refer any function, the initialiser call could be
    148  * missed for the static library.
    149  */
    150 #define MHD_LIB_INIT_IMPL_H_IN_DAEMON_CREATE_C 1
    151 #include "mhd_lib_init_impl.h"