diff options
Diffstat (limited to 'src/lib/daemon_create.c')
-rw-r--r-- | src/lib/daemon_create.c | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/src/lib/daemon_create.c b/src/lib/daemon_create.c new file mode 100644 index 00000000..6d903677 --- /dev/null +++ b/src/lib/daemon_create.c | |||
@@ -0,0 +1,134 @@ | |||
1 | /* | ||
2 | This file is part of libmicrohttpd | ||
3 | Copyright (C) 2007-2018 Daniel Pittman and Christian Grothoff | ||
4 | |||
5 | This library is free software; you can redistribute it and/or | ||
6 | modify it under the terms of the GNU Lesser General Public | ||
7 | License as published by the Free Software Foundation; either | ||
8 | version 2.1 of the License, or (at your option) any later version. | ||
9 | |||
10 | This library is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Lesser General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Lesser General Public | ||
16 | License along with this library; if not, write to the Free Software | ||
17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
18 | */ | ||
19 | |||
20 | /** | ||
21 | * @file lib/daemon_create.c | ||
22 | * @brief main functions to create a daemon | ||
23 | * @author Christian Grothoff | ||
24 | */ | ||
25 | #include "internal.h" | ||
26 | |||
27 | |||
28 | /** | ||
29 | * Logging implementation that logs to a file given | ||
30 | * as the @a cls. | ||
31 | * | ||
32 | * @param cls a `FILE *` to log to | ||
33 | * @param sc status code of the event (ignored) | ||
34 | * @param fm format string (`printf()`-style) | ||
35 | * @param ap arguments to @a fm | ||
36 | * @ingroup logging | ||
37 | */ | ||
38 | static void | ||
39 | file_logger (void *cls, | ||
40 | enum MHD_StatusCode sc, | ||
41 | const char *fm, | ||
42 | va_list ap) | ||
43 | { | ||
44 | FILE *f = cls; | ||
45 | |||
46 | (void) sc; | ||
47 | (void) vfprintf (f, | ||
48 | fm, | ||
49 | ap); | ||
50 | } | ||
51 | |||
52 | |||
53 | /** | ||
54 | * Process escape sequences ('%HH') Updates val in place; the | ||
55 | * result should be UTF-8 encoded and cannot be larger than the input. | ||
56 | * The result must also still be 0-terminated. | ||
57 | * | ||
58 | * @param cls closure (use NULL) | ||
59 | * @param req handle to request, not used | ||
60 | * @param val value to unescape (modified in the process) | ||
61 | * @return length of the resulting val (strlen(val) maybe | ||
62 | * shorter afterwards due to elimination of escape sequences) | ||
63 | */ | ||
64 | static size_t | ||
65 | unescape_wrapper (void *cls, | ||
66 | struct MHD_Request *req, | ||
67 | char *val) | ||
68 | { | ||
69 | (void) cls; /* Mute compiler warning. */ | ||
70 | (void) req; /* Mute compiler warning. */ | ||
71 | return MHD_http_unescape (val); | ||
72 | } | ||
73 | |||
74 | |||
75 | /** | ||
76 | * Create (but do not yet start) an MHD daemon. | ||
77 | * Usually, you will want to set various options before | ||
78 | * starting the daemon with #MHD_daemon_start(). | ||
79 | * | ||
80 | * @param cb function to be called for incoming requests | ||
81 | * @param cb_cls closure for @a cb | ||
82 | * @return NULL on error | ||
83 | */ | ||
84 | struct MHD_Daemon * | ||
85 | MHD_daemon_create (MHD_RequestCallback cb, | ||
86 | void *cb_cls) | ||
87 | { | ||
88 | struct MHD_Daemon *daemon; | ||
89 | |||
90 | MHD_check_global_init_(); | ||
91 | if (NULL == cb) | ||
92 | return NULL; | ||
93 | if (NULL == (daemon = malloc (sizeof (struct MHD_Daemon)))) | ||
94 | return NULL; | ||
95 | memset (daemon, | ||
96 | 0, | ||
97 | sizeof (struct MHD_Daemon)); | ||
98 | daemon->rc = cb; | ||
99 | daemon->rc_cls = cb_cls; | ||
100 | daemon->logger = &file_logger; | ||
101 | daemon->logger_cls = stderr; | ||
102 | daemon->unescape_cb = &unescape_wrapper; | ||
103 | daemon->tls_ciphers = TLS_CIPHERS_DEFAULT; | ||
104 | daemon->connection_memory_limit_b = MHD_POOL_SIZE_DEFAULT; | ||
105 | daemon->connection_memory_increment_b = BUF_INC_SIZE_DEFAULT; | ||
106 | #if ENABLE_DAUTH | ||
107 | daemon->digest_nc_length = DIGEST_NC_LENGTH_DEFAULT; | ||
108 | #endif | ||
109 | daemon->listen_backlog = LISTEN_BACKLOG_DEFAULT; | ||
110 | daemon->fo_queue_length = FO_QUEUE_LENGTH_DEFAULT; | ||
111 | daemon->listen_socket = MHD_INVALID_SOCKET; | ||
112 | |||
113 | if (! MHD_mutex_init_ (&daemon->cleanup_connection_mutex)) | ||
114 | { | ||
115 | free (daemon); | ||
116 | return NULL; | ||
117 | } | ||
118 | if (! MHD_mutex_init_ (&daemon->per_ip_connection_mutex)) | ||
119 | { | ||
120 | MHD_mutex_destroy_ (&daemon->cleanup_connection_mutex); | ||
121 | free (daemon); | ||
122 | return NULL; | ||
123 | } | ||
124 | #ifdef DAUTH_SUPPORT | ||
125 | if (! MHD_mutex_init_ (&daemon->nnc_lock)) | ||
126 | { | ||
127 | MHD_mutex_destroy_ (&daemon->cleanup_connection_mutex); | ||
128 | MHD_mutex_destroy_ (&daemon->per_ip_connection_mutex); | ||
129 | free (daemon); | ||
130 | return NULL; | ||
131 | } | ||
132 | #endif | ||
133 | return daemon; | ||
134 | } | ||