diff options
Diffstat (limited to 'src/lib/daemon.c')
-rw-r--r-- | src/lib/daemon.c | 172 |
1 files changed, 172 insertions, 0 deletions
diff --git a/src/lib/daemon.c b/src/lib/daemon.c new file mode 100644 index 00000000..6396cf9f --- /dev/null +++ b/src/lib/daemon.c | |||
@@ -0,0 +1,172 @@ | |||
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.c | ||
22 | * @brief main functions to create, start, quiesce and destroy 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 | return daemon; | ||
113 | } | ||
114 | |||
115 | |||
116 | /** | ||
117 | * Start a webserver. | ||
118 | * | ||
119 | * @param daemon daemon to start; you can no longer set | ||
120 | * options on this daemon after this call! | ||
121 | * @return #MHD_SC_OK on success | ||
122 | * @ingroup event | ||
123 | */ | ||
124 | enum MHD_StatusCode | ||
125 | MHD_daemon_start (struct MHD_Daemon *daemon) | ||
126 | { | ||
127 | |||
128 | return -1; | ||
129 | } | ||
130 | |||
131 | |||
132 | /** | ||
133 | * Stop accepting connections from the listening socket. Allows | ||
134 | * clients to continue processing, but stops accepting new | ||
135 | * connections. Note that the caller is responsible for closing the | ||
136 | * returned socket; however, if MHD is run using threads (anything but | ||
137 | * external select mode), it must not be closed until AFTER | ||
138 | * #MHD_stop_daemon has been called (as it is theoretically possible | ||
139 | * that an existing thread is still using it). | ||
140 | * | ||
141 | * Note that some thread modes require the caller to have passed | ||
142 | * #MHD_USE_ITC when using this API. If this daemon is | ||
143 | * in one of those modes and this option was not given to | ||
144 | * #MHD_start_daemon, this function will return #MHD_INVALID_SOCKET. | ||
145 | * | ||
146 | * @param daemon daemon to stop accepting new connections for | ||
147 | * @return old listen socket on success, #MHD_INVALID_SOCKET if | ||
148 | * the daemon was already not listening anymore, or | ||
149 | * was never started | ||
150 | * @ingroup specialized | ||
151 | */ | ||
152 | MHD_socket | ||
153 | MHD_daemon_quiesce (struct MHD_Daemon *daemon) | ||
154 | { | ||
155 | return -1; | ||
156 | } | ||
157 | |||
158 | |||
159 | /** | ||
160 | * Shutdown and destroy an HTTP daemon. | ||
161 | * | ||
162 | * @param daemon daemon to stop | ||
163 | * @ingroup event | ||
164 | */ | ||
165 | void | ||
166 | MHD_daemon_destroy (struct MHD_Daemon *daemon) | ||
167 | { | ||
168 | free (daemon); | ||
169 | } | ||
170 | |||
171 | |||
172 | /* end of daemon.c */ | ||