libmicrohttpd2

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

daemon_create.c (4877B)


      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 (! mhd_lib_init_global_if_needed ())
     76   {
     77 #ifndef NDEBUG
     78     fprintf (stderr, "Failed to initialise MHD library.\n");
     79     fflush (stderr);
     80 #endif /* ! NDEBUG */
     81     return NULL;
     82   }
     83 
     84   if (NULL == req_cb)
     85     return NULL;
     86 
     87   d = (struct MHD_Daemon *) mhd_calloc (1, sizeof(struct MHD_Daemon));
     88   if (NULL == d)
     89     return NULL;
     90 
     91   s = (struct DaemonOptions *) mhd_calloc (1, sizeof(struct DaemonOptions));
     92   if (NULL == s)
     93   {
     94     free (d);
     95     return NULL;
     96   }
     97   /* calloc() does not guarantee that floating point values and pointers
     98      are initialised to zero and NULL (respectfully). */
     99   /* Any floating point and pointer members must be initialised manually here */
    100 #ifndef HAVE_NULL_PTR_ALL_ZEROS
    101   s->bind_sa.v_sa = NULL;
    102   s->tls_cert_key.v_mem_key = NULL;
    103   s->tls_cert_key.v_mem_cert = NULL;
    104   s->tls_cert_key.v_mem_pass = NULL;
    105   s->tls_client_ca = NULL;
    106   s->tls_psk_callback.v_psk_cb = NULL;
    107   s->tls_psk_callback.v_psk_cb_cls = NULL;
    108   s->accept_policy.v_apc = NULL;
    109   s->accept_policy.v_apc_cls = NULL;
    110   s->early_uri_logger.v_cb = NULL;
    111   s->early_uri_logger.v_cls = NULL;
    112   s->daemon_ready_callback.v_cb = NULL;
    113   s->daemon_ready_callback.v_cb_cls = NULL;
    114   s->notify_connection.v_ncc = NULL;
    115   s->notify_connection.v_cls = NULL;
    116   s->notify_stream.v_nsc = NULL;
    117   s->notify_stream.v_cls = NULL;
    118   s->random_entropy.v_buf = NULL;
    119   s->tls_cert_key.v_mem_cert = NULL;
    120   s->tls_cert_key.v_mem_key = NULL;
    121   s->tls_cert_key.v_mem_pass = NULL;
    122 
    123   /* d->log_params.v_log_cb = NULL; */ /* used directly */
    124 #endif /* !HAVE_NULL_PTR_ALL_ZEROS */
    125 
    126   s->large_pool_size = SIZE_MAX; /* The impossible value */
    127 
    128   s->listen_socket = MHD_INVALID_SOCKET;
    129   s->fd_number_limit = MHD_INVALID_SOCKET;
    130 
    131   d->log_params.v_log_cb = (MHD_LoggingCallback) mhd_logger_default;
    132   d->req_cfg.cb = req_cb;
    133   d->req_cfg.cb_cls = req_cb_cls;
    134   d->settings = s;
    135 
    136   return d;
    137 }
    138 
    139 
    140 /* This is a workaround for GCC/binutils bug.
    141  * To make sure that initialisation functions are called when MHD is used as
    142  * a static library, put initialisation functions to the file with function
    143  * that always referred/used by application/user of the library.
    144  * If application does not refer any function, the initialiser call could be
    145  * missed for the static library.
    146  */
    147 #define MHD_LIB_INIT_IMPL_H_IN_DAEMON_CREATE_C 1
    148 #include "mhd_lib_init_impl.h"