aboutsummaryrefslogtreecommitdiff
path: root/src/lib/daemon_create.c
blob: f10427896dffdfdd92aba4869a148222241c5f5b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
  This file is part of libmicrohttpd
  Copyright (C) 2007-2018 Daniel Pittman and Christian Grothoff

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

/**
 * @file lib/daemon_create.c
 * @brief main functions to create a daemon
 * @author Christian Grothoff
 */
#include "internal.h"


/**
 * Logging implementation that logs to a file given
 * as the @a cls.
 *
 * @param cls a `FILE *` to log to
 * @param sc status code of the event (ignored)
 * @param fm format string (`printf()`-style)
 * @param ap arguments to @a fm
 * @ingroup logging
 */
static void
file_logger (void *cls,
	     enum MHD_StatusCode sc,
	     const char *fm,
	     va_list ap)
{
  FILE *f = cls;

  (void) sc;
  (void) vfprintf (f,
		   fm,
		   ap);
}


/**
 * Process escape sequences ('%HH') Updates val in place; the
 * result should be UTF-8 encoded and cannot be larger than the input.
 * The result must also still be 0-terminated.
 *
 * @param cls closure (use NULL)
 * @param req handle to request, not used
 * @param val value to unescape (modified in the process)
 * @return length of the resulting val (strlen(val) maybe
 *  shorter afterwards due to elimination of escape sequences)
 */
static size_t
unescape_wrapper (void *cls,
                  struct MHD_Request *req,
                  char *val)
{
  (void) cls; /* Mute compiler warning. */
  (void) req; /* Mute compiler warning. */
  return MHD_http_unescape (val);
}


/**
 * Create (but do not yet start) an MHD daemon.
 * Usually, you will want to set various options before
 * starting the daemon with #MHD_daemon_start().
 *
 * @param cb function to be called for incoming requests
 * @param cb_cls closure for @a cb
 * @return NULL on error
 */
struct MHD_Daemon *
MHD_daemon_create (MHD_RequestCallback cb,
		   void *cb_cls)
{
  struct MHD_Daemon *daemon;

  MHD_check_global_init_();
  if (NULL == cb)
    return NULL;
  if (NULL == (daemon = malloc (sizeof (struct MHD_Daemon))))
    return NULL;
  memset (daemon,
	  0,
	  sizeof (struct MHD_Daemon));
  daemon->rc = cb;
  daemon->rc_cls = cb_cls;
  daemon->logger = &file_logger;
  daemon->logger_cls = stderr;
  daemon->unescape_cb = &unescape_wrapper;
  daemon->connection_memory_limit_b = POOL_SIZE_DEFAULT;
  daemon->connection_memory_increment_b = BUF_INC_SIZE_DEFAULT;
#if ENABLE_DAUTH
  daemon->digest_nc_length = DIGEST_NC_LENGTH_DEFAULT;
#endif
  daemon->listen_backlog = LISTEN_BACKLOG_DEFAULT;  
  daemon->fo_queue_length = FO_QUEUE_LENGTH_DEFAULT;
  daemon->listen_socket = MHD_INVALID_SOCKET;

  if (! MHD_mutex_init_ (&daemon->cleanup_connection_mutex))
    {
      free (daemon);
      return NULL;
    }  
  if (! MHD_mutex_init_ (&daemon->per_ip_connection_mutex))
    {
      (void) MHD_mutex_destroy_ (&daemon->cleanup_connection_mutex);
      free (daemon);
      return NULL;
    }
#ifdef DAUTH_SUPPORT
  if (! MHD_mutex_init_ (&daemon->nnc_lock))
    {
      (void) MHD_mutex_destroy_ (&daemon->cleanup_connection_mutex);
      (void) MHD_mutex_destroy_ (&daemon->per_ip_connection_mutex);
      free (daemon);
      return NULL;
    }
#endif
  return daemon;
}