diff options
Diffstat (limited to 'src/daemon')
-rw-r--r-- | src/daemon/connection.c | 2 | ||||
-rw-r--r-- | src/daemon/daemon.c | 12 | ||||
-rw-r--r-- | src/daemon/internal.c | 4 | ||||
-rw-r--r-- | src/daemon/internal.h | 723 |
4 files changed, 422 insertions, 319 deletions
diff --git a/src/daemon/connection.c b/src/daemon/connection.c index 49d32c5c..b30f9726 100644 --- a/src/daemon/connection.c +++ b/src/daemon/connection.c | |||
@@ -208,7 +208,7 @@ MHD_lookup_connection_value (struct MHD_Connection *connection, | |||
208 | 208 | ||
209 | /** | 209 | /** |
210 | * Queue a response to be transmitted to the client (as soon as | 210 | * Queue a response to be transmitted to the client (as soon as |
211 | * possible). | 211 | * possible but after MHD_AccessHandlerCallback returns). |
212 | * | 212 | * |
213 | * @param connection the connection identifying the client | 213 | * @param connection the connection identifying the client |
214 | * @param status_code HTTP status code (i.e. 200 for OK) | 214 | * @param status_code HTTP status code (i.e. 200 for OK) |
diff --git a/src/daemon/daemon.c b/src/daemon/daemon.c index c5219f8c..6729c420 100644 --- a/src/daemon/daemon.c +++ b/src/daemon/daemon.c | |||
@@ -834,6 +834,10 @@ MHD_start_daemon_va (unsigned int options, | |||
834 | retVal->max_connections = MHD_MAX_CONNECTIONS_DEFAULT; | 834 | retVal->max_connections = MHD_MAX_CONNECTIONS_DEFAULT; |
835 | retVal->pool_size = MHD_POOL_SIZE_DEFAULT; | 835 | retVal->pool_size = MHD_POOL_SIZE_DEFAULT; |
836 | retVal->connection_timeout = 0; /* no timeout */ | 836 | retVal->connection_timeout = 0; /* no timeout */ |
837 | #if HAVE_MESSAGES | ||
838 | retVal->custom_error_log = (void(*)(void*,const char *,va_list)) &vfprintf; | ||
839 | retVal->custom_error_log_cls = stderr; | ||
840 | #endif | ||
837 | #if HTTPS_SUPPORT | 841 | #if HTTPS_SUPPORT |
838 | if (options & MHD_USE_SSL) | 842 | if (options & MHD_USE_SSL) |
839 | { | 843 | { |
@@ -892,6 +896,14 @@ MHD_start_daemon_va (unsigned int options, | |||
892 | va_arg (ap, const int *)); | 896 | va_arg (ap, const int *)); |
893 | break; | 897 | break; |
894 | #endif | 898 | #endif |
899 | case MHD_OPTION_EXTERNAL_LOGGER: | ||
900 | #if HAVE_MESSAGES | ||
901 | retVal->custom_error_log = va_arg(ap, void (*)(void*cls, const char *, va_list)); | ||
902 | retVal->custom_error_log_cls = va_arg(ap, void *); | ||
903 | #else | ||
904 | va_arg(ap, void (*)(void*cls, const char *,...)); | ||
905 | va_arg(ap, void *); | ||
906 | #endif | ||
895 | default: | 907 | default: |
896 | #if HAVE_MESSAGES | 908 | #if HAVE_MESSAGES |
897 | if ((opt >= MHD_OPTION_HTTPS_MEM_KEY) && | 909 | if ((opt >= MHD_OPTION_HTTPS_MEM_KEY) && |
diff --git a/src/daemon/internal.c b/src/daemon/internal.c index abf718de..277e9699 100644 --- a/src/daemon/internal.c +++ b/src/daemon/internal.c | |||
@@ -102,7 +102,9 @@ MHD_DLOG (const struct MHD_Daemon *daemon, const char *format, ...) | |||
102 | if ((daemon->options & MHD_USE_DEBUG) == 0) | 102 | if ((daemon->options & MHD_USE_DEBUG) == 0) |
103 | return; | 103 | return; |
104 | va_start (va, format); | 104 | va_start (va, format); |
105 | VFPRINTF (stderr, format, va); | 105 | daemon->custom_error_log(daemon->custom_error_log_cls, |
106 | format, | ||
107 | va); | ||
106 | va_end (va); | 108 | va_end (va); |
107 | } | 109 | } |
108 | #endif | 110 | #endif |
diff --git a/src/daemon/internal.h b/src/daemon/internal.h index 6e8c0c9a..9fc299e2 100644 --- a/src/daemon/internal.h +++ b/src/daemon/internal.h | |||
@@ -66,12 +66,26 @@ void MHD_http_unescape (char *val); | |||
66 | */ | 66 | */ |
67 | struct MHD_HTTP_Header | 67 | struct MHD_HTTP_Header |
68 | { | 68 | { |
69 | /** | ||
70 | * Headers are kept in a linked list. | ||
71 | */ | ||
69 | struct MHD_HTTP_Header *next; | 72 | struct MHD_HTTP_Header *next; |
70 | 73 | ||
74 | /** | ||
75 | * The name of the header (key), without | ||
76 | * the colon. | ||
77 | */ | ||
71 | char *header; | 78 | char *header; |
72 | 79 | ||
80 | /** | ||
81 | * The value of the header. | ||
82 | */ | ||
73 | char *value; | 83 | char *value; |
74 | 84 | ||
85 | /** | ||
86 | * Type of the header (where in the HTTP | ||
87 | * protocol is this header from). | ||
88 | */ | ||
75 | enum MHD_ValueKind kind; | 89 | enum MHD_ValueKind kind; |
76 | 90 | ||
77 | }; | 91 | }; |
@@ -82,68 +96,68 @@ struct MHD_HTTP_Header | |||
82 | struct MHD_Response | 96 | struct MHD_Response |
83 | { | 97 | { |
84 | 98 | ||
85 | /** | 99 | /** |
86 | * Headers to send for the response. Initially | 100 | * Headers to send for the response. Initially |
87 | * the linked list is created in inverse order; | 101 | * the linked list is created in inverse order; |
88 | * the order should be inverted before sending! | 102 | * the order should be inverted before sending! |
89 | */ | 103 | */ |
90 | struct MHD_HTTP_Header *first_header; | 104 | struct MHD_HTTP_Header *first_header; |
91 | 105 | ||
92 | /** | 106 | /** |
93 | * Buffer pointing to data that we are supposed | 107 | * Buffer pointing to data that we are supposed |
94 | * to send as a response. | 108 | * to send as a response. |
95 | */ | 109 | */ |
96 | char *data; | 110 | char *data; |
97 | 111 | ||
98 | /** | 112 | /** |
99 | * Closure to give to the content reader | 113 | * Closure to give to the content reader |
100 | * free callback. | 114 | * free callback. |
101 | */ | 115 | */ |
102 | void *crc_cls; | 116 | void *crc_cls; |
103 | 117 | ||
104 | /** | 118 | /** |
105 | * How do we get more data? NULL if we are | 119 | * How do we get more data? NULL if we are |
106 | * given all of the data up front. | 120 | * given all of the data up front. |
107 | */ | 121 | */ |
108 | MHD_ContentReaderCallback crc; | 122 | MHD_ContentReaderCallback crc; |
109 | 123 | ||
110 | /** | 124 | /** |
111 | * NULL if data must not be freed, otherwise | 125 | * NULL if data must not be freed, otherwise |
112 | * either user-specified callback or "&free". | 126 | * either user-specified callback or "&free". |
113 | */ | 127 | */ |
114 | MHD_ContentReaderFreeCallback crfc; | 128 | MHD_ContentReaderFreeCallback crfc; |
115 | 129 | ||
116 | /** | 130 | /** |
117 | * Mutex to synchronize access to data/size and | 131 | * Mutex to synchronize access to data/size and |
118 | * reference counts. | 132 | * reference counts. |
119 | */ | 133 | */ |
120 | pthread_mutex_t mutex; | 134 | pthread_mutex_t mutex; |
121 | 135 | ||
122 | /** | 136 | /** |
123 | * Reference count for this response. Free | 137 | * Reference count for this response. Free |
124 | * once the counter hits zero. | 138 | * once the counter hits zero. |
125 | */ | 139 | */ |
126 | unsigned int reference_count; | 140 | unsigned int reference_count; |
127 | 141 | ||
128 | /** | 142 | /** |
129 | * Set to -1 if size is not known. | 143 | * Set to -1 if size is not known. |
130 | */ | 144 | */ |
131 | size_t total_size; | 145 | size_t total_size; |
132 | 146 | ||
133 | /** | 147 | /** |
134 | * Size of data. | 148 | * Size of data. |
135 | */ | 149 | */ |
136 | size_t data_size; | 150 | size_t data_size; |
137 | 151 | ||
138 | /** | 152 | /** |
139 | * Size of the data buffer. | 153 | * Size of the data buffer. |
140 | */ | 154 | */ |
141 | size_t data_buffer_size; | 155 | size_t data_buffer_size; |
142 | 156 | ||
143 | /** | 157 | /** |
144 | * At what offset in the stream is the | 158 | * At what offset in the stream is the |
145 | * beginning of data located? | 159 | * beginning of data located? |
146 | */ | 160 | */ |
147 | size_t data_start; | 161 | size_t data_start; |
148 | 162 | ||
149 | }; | 163 | }; |
@@ -164,124 +178,124 @@ struct MHD_Response | |||
164 | */ | 178 | */ |
165 | enum MHD_CONNECTION_STATE | 179 | enum MHD_CONNECTION_STATE |
166 | { | 180 | { |
167 | /** | 181 | /** |
168 | * Connection just started (no headers received). | 182 | * Connection just started (no headers received). |
169 | * Waiting for the line with the request type, URL and version. | 183 | * Waiting for the line with the request type, URL and version. |
170 | */ | 184 | */ |
171 | MHD_CONNECTION_INIT = 0, | 185 | MHD_CONNECTION_INIT = 0, |
172 | 186 | ||
173 | /** | 187 | /** |
174 | * 1: We got the URL (and request type and version). Wait for a header line. | 188 | * 1: We got the URL (and request type and version). Wait for a header line. |
175 | */ | 189 | */ |
176 | MHD_CONNECTION_URL_RECEIVED = MHD_CONNECTION_INIT + 1, | 190 | MHD_CONNECTION_URL_RECEIVED = MHD_CONNECTION_INIT + 1, |
177 | 191 | ||
178 | /** | 192 | /** |
179 | * 2: We got part of a multi-line request header. Wait for the rest. | 193 | * 2: We got part of a multi-line request header. Wait for the rest. |
180 | */ | 194 | */ |
181 | MHD_CONNECTION_HEADER_PART_RECEIVED = MHD_CONNECTION_URL_RECEIVED + 1, | 195 | MHD_CONNECTION_HEADER_PART_RECEIVED = MHD_CONNECTION_URL_RECEIVED + 1, |
182 | 196 | ||
183 | /** | 197 | /** |
184 | * 3: We got the request headers. Process them. | 198 | * 3: We got the request headers. Process them. |
185 | */ | 199 | */ |
186 | MHD_CONNECTION_HEADERS_RECEIVED = MHD_CONNECTION_HEADER_PART_RECEIVED + 1, | 200 | MHD_CONNECTION_HEADERS_RECEIVED = MHD_CONNECTION_HEADER_PART_RECEIVED + 1, |
187 | 201 | ||
188 | /** | 202 | /** |
189 | * 4: We have processed the request headers. Send 100 continue. | 203 | * 4: We have processed the request headers. Send 100 continue. |
190 | */ | 204 | */ |
191 | MHD_CONNECTION_HEADERS_PROCESSED = MHD_CONNECTION_HEADERS_RECEIVED + 1, | 205 | MHD_CONNECTION_HEADERS_PROCESSED = MHD_CONNECTION_HEADERS_RECEIVED + 1, |
192 | 206 | ||
193 | /** | 207 | /** |
194 | * 5: We have processed the headers and need to send 100 CONTINUE. | 208 | * 5: We have processed the headers and need to send 100 CONTINUE. |
195 | */ | 209 | */ |
196 | MHD_CONNECTION_CONTINUE_SENDING = MHD_CONNECTION_HEADERS_PROCESSED + 1, | 210 | MHD_CONNECTION_CONTINUE_SENDING = MHD_CONNECTION_HEADERS_PROCESSED + 1, |
197 | 211 | ||
198 | /** | 212 | /** |
199 | * 6: We have sent 100 CONTINUE (or do not need to). Read the message body. | 213 | * 6: We have sent 100 CONTINUE (or do not need to). Read the message body. |
200 | */ | 214 | */ |
201 | MHD_CONNECTION_CONTINUE_SENT = MHD_CONNECTION_CONTINUE_SENDING + 1, | 215 | MHD_CONNECTION_CONTINUE_SENT = MHD_CONNECTION_CONTINUE_SENDING + 1, |
202 | 216 | ||
203 | /** | 217 | /** |
204 | * 7: We got the request body. Wait for a line of the footer. | 218 | * 7: We got the request body. Wait for a line of the footer. |
205 | */ | 219 | */ |
206 | MHD_CONNECTION_BODY_RECEIVED = MHD_CONNECTION_CONTINUE_SENT + 1, | 220 | MHD_CONNECTION_BODY_RECEIVED = MHD_CONNECTION_CONTINUE_SENT + 1, |
207 | 221 | ||
208 | /** | 222 | /** |
209 | * 8: We got part of a line of the footer. Wait for the | 223 | * 8: We got part of a line of the footer. Wait for the |
210 | * rest. | 224 | * rest. |
211 | */ | 225 | */ |
212 | MHD_CONNECTION_FOOTER_PART_RECEIVED = MHD_CONNECTION_BODY_RECEIVED + 1, | 226 | MHD_CONNECTION_FOOTER_PART_RECEIVED = MHD_CONNECTION_BODY_RECEIVED + 1, |
213 | 227 | ||
214 | /** | 228 | /** |
215 | * 9: We received the entire footer. Wait for a response to be queued | 229 | * 9: We received the entire footer. Wait for a response to be queued |
216 | * and prepare the response headers. | 230 | * and prepare the response headers. |
217 | */ | 231 | */ |
218 | MHD_CONNECTION_FOOTERS_RECEIVED = MHD_CONNECTION_FOOTER_PART_RECEIVED + 1, | 232 | MHD_CONNECTION_FOOTERS_RECEIVED = MHD_CONNECTION_FOOTER_PART_RECEIVED + 1, |
219 | 233 | ||
220 | /** | 234 | /** |
221 | * 10: We have prepared the response headers in the writ buffer. | 235 | * 10: We have prepared the response headers in the writ buffer. |
222 | * Send the response headers. | 236 | * Send the response headers. |
223 | */ | 237 | */ |
224 | MHD_CONNECTION_HEADERS_SENDING = MHD_CONNECTION_FOOTERS_RECEIVED + 1, | 238 | MHD_CONNECTION_HEADERS_SENDING = MHD_CONNECTION_FOOTERS_RECEIVED + 1, |
225 | 239 | ||
226 | /** | 240 | /** |
227 | * 11: We have sent the response headers. Get ready to send the body. | 241 | * 11: We have sent the response headers. Get ready to send the body. |
228 | */ | 242 | */ |
229 | MHD_CONNECTION_HEADERS_SENT = MHD_CONNECTION_HEADERS_SENDING + 1, | 243 | MHD_CONNECTION_HEADERS_SENT = MHD_CONNECTION_HEADERS_SENDING + 1, |
230 | 244 | ||
231 | /** | 245 | /** |
232 | * 12: We are ready to send a part of a non-chunked body. Send it. | 246 | * 12: We are ready to send a part of a non-chunked body. Send it. |
233 | */ | 247 | */ |
234 | MHD_CONNECTION_NORMAL_BODY_READY = MHD_CONNECTION_HEADERS_SENT + 1, | 248 | MHD_CONNECTION_NORMAL_BODY_READY = MHD_CONNECTION_HEADERS_SENT + 1, |
235 | 249 | ||
236 | /** | 250 | /** |
237 | * 13: We are waiting for the client to provide more | 251 | * 13: We are waiting for the client to provide more |
238 | * data of a non-chunked body. | 252 | * data of a non-chunked body. |
239 | */ | 253 | */ |
240 | MHD_CONNECTION_NORMAL_BODY_UNREADY = MHD_CONNECTION_NORMAL_BODY_READY + 1, | 254 | MHD_CONNECTION_NORMAL_BODY_UNREADY = MHD_CONNECTION_NORMAL_BODY_READY + 1, |
241 | 255 | ||
242 | /** | 256 | /** |
243 | * 14: We are ready to send a chunk. | 257 | * 14: We are ready to send a chunk. |
244 | */ | 258 | */ |
245 | MHD_CONNECTION_CHUNKED_BODY_READY = MHD_CONNECTION_NORMAL_BODY_UNREADY + 1, | 259 | MHD_CONNECTION_CHUNKED_BODY_READY = MHD_CONNECTION_NORMAL_BODY_UNREADY + 1, |
246 | 260 | ||
247 | /** | 261 | /** |
248 | * 15: We are waiting for the client to provide a chunk of the body. | 262 | * 15: We are waiting for the client to provide a chunk of the body. |
249 | */ | 263 | */ |
250 | MHD_CONNECTION_CHUNKED_BODY_UNREADY = MHD_CONNECTION_CHUNKED_BODY_READY + 1, | 264 | MHD_CONNECTION_CHUNKED_BODY_UNREADY = MHD_CONNECTION_CHUNKED_BODY_READY + 1, |
251 | 265 | ||
252 | /** | 266 | /** |
253 | * 16: We have sent the response body. Prepare the footers. | 267 | * 16: We have sent the response body. Prepare the footers. |
254 | */ | 268 | */ |
255 | MHD_CONNECTION_BODY_SENT = MHD_CONNECTION_CHUNKED_BODY_UNREADY + 1, | 269 | MHD_CONNECTION_BODY_SENT = MHD_CONNECTION_CHUNKED_BODY_UNREADY + 1, |
256 | 270 | ||
257 | /** | 271 | /** |
258 | * 17: We have prepared the response footer. Send it. | 272 | * 17: We have prepared the response footer. Send it. |
259 | */ | 273 | */ |
260 | MHD_CONNECTION_FOOTERS_SENDING = MHD_CONNECTION_BODY_SENT + 1, | 274 | MHD_CONNECTION_FOOTERS_SENDING = MHD_CONNECTION_BODY_SENT + 1, |
261 | 275 | ||
262 | /** | 276 | /** |
263 | * 18: We have sent the response footer. Shutdown or restart. | 277 | * 18: We have sent the response footer. Shutdown or restart. |
264 | */ | 278 | */ |
265 | MHD_CONNECTION_FOOTERS_SENT = MHD_CONNECTION_FOOTERS_SENDING + 1, | 279 | MHD_CONNECTION_FOOTERS_SENT = MHD_CONNECTION_FOOTERS_SENDING + 1, |
266 | 280 | ||
267 | /** | 281 | /** |
268 | * 19: This connection is closed (no more activity | 282 | * 19: This connection is closed (no more activity |
269 | * allowed). | 283 | * allowed). |
270 | */ | 284 | */ |
271 | MHD_CONNECTION_CLOSED = MHD_CONNECTION_FOOTERS_SENT + 1, | 285 | MHD_CONNECTION_CLOSED = MHD_CONNECTION_FOOTERS_SENT + 1, |
272 | 286 | ||
273 | /* | 287 | /* |
274 | * SSL/TLS connection states | 288 | * SSL/TLS connection states |
275 | */ | 289 | */ |
276 | 290 | ||
277 | /* | 291 | /** |
278 | * The initial connection state for all secure connectoins | 292 | * The initial connection state for all secure connectoins |
279 | * Handshake messages will be processed in this state & while | 293 | * Handshake messages will be processed in this state & while |
280 | * in the 'MHD_TLS_HELLO_REQUEST' state | 294 | * in the 'MHD_TLS_HELLO_REQUEST' state |
281 | */ | 295 | */ |
282 | MHD_TLS_CONNECTION_INIT = MHD_CONNECTION_CLOSED + 1, | 296 | MHD_TLS_CONNECTION_INIT = MHD_CONNECTION_CLOSED + 1, |
283 | 297 | ||
284 | /* | 298 | /** |
285 | * This state indicates the server has send a 'Hello Request' to | 299 | * This state indicates the server has send a 'Hello Request' to |
286 | * the client & a renegotiation of the handshake is in progress. | 300 | * the client & a renegotiation of the handshake is in progress. |
287 | * | 301 | * |
@@ -329,341 +343,416 @@ typedef ssize_t (*TransmitCallback) (struct MHD_Connection * conn, | |||
329 | const void *write_to, size_t max_bytes); | 343 | const void *write_to, size_t max_bytes); |
330 | 344 | ||
331 | 345 | ||
332 | 346 | /** | |
347 | * State kept for each HTTP request. | ||
348 | */ | ||
333 | struct MHD_Connection | 349 | struct MHD_Connection |
334 | { | 350 | { |
335 | 351 | ||
336 | /** | 352 | /** |
337 | * This is a linked list. | 353 | * This is a linked list. |
338 | */ | 354 | */ |
339 | struct MHD_Connection *next; | 355 | struct MHD_Connection *next; |
340 | 356 | ||
341 | /** | 357 | /** |
342 | * Reference to the MHD_Daemon struct. | 358 | * Reference to the MHD_Daemon struct. |
343 | */ | 359 | */ |
344 | struct MHD_Daemon *daemon; | 360 | struct MHD_Daemon *daemon; |
345 | 361 | ||
346 | /** | 362 | /** |
347 | * Linked list of parsed headers. | 363 | * Linked list of parsed headers. |
348 | */ | 364 | */ |
349 | struct MHD_HTTP_Header *headers_received; | 365 | struct MHD_HTTP_Header *headers_received; |
350 | 366 | ||
351 | /** | 367 | /** |
352 | * Response to transmit (initially NULL). | 368 | * Response to transmit (initially NULL). |
353 | */ | 369 | */ |
354 | struct MHD_Response *response; | 370 | struct MHD_Response *response; |
355 | 371 | ||
356 | /** | 372 | /** |
357 | * The memory pool is created whenever we first read | 373 | * The memory pool is created whenever we first read |
358 | * from the TCP stream and destroyed at the end of | 374 | * from the TCP stream and destroyed at the end of |
359 | * each request (and re-created for the next request). | 375 | * each request (and re-created for the next request). |
360 | * In the meantime, this pointer is NULL. The | 376 | * In the meantime, this pointer is NULL. The |
361 | * pool is used for all connection-related data | 377 | * pool is used for all connection-related data |
362 | * except for the response (which maybe shared between | 378 | * except for the response (which maybe shared between |
363 | * connections) and the IP address (which persists | 379 | * connections) and the IP address (which persists |
364 | * across individual requests). | 380 | * across individual requests). |
365 | */ | 381 | */ |
366 | struct MemoryPool *pool; | 382 | struct MemoryPool *pool; |
367 | 383 | ||
368 | /** | 384 | /** |
369 | * We allow the main application to associate some | 385 | * We allow the main application to associate some |
370 | * pointer with the connection. Here is where we | 386 | * pointer with the connection. Here is where we |
371 | * store it. (MHD does not know or care what it | 387 | * store it. (MHD does not know or care what it |
372 | * is). | 388 | * is). |
373 | */ | 389 | */ |
374 | void *client_context; | 390 | void *client_context; |
375 | 391 | ||
376 | /** | 392 | /** |
377 | * Request method. Should be GET/POST/etc. Allocated | 393 | * Request method. Should be GET/POST/etc. Allocated |
378 | * in pool. | 394 | * in pool. |
379 | */ | 395 | */ |
380 | char *method; | 396 | char *method; |
381 | 397 | ||
382 | /** | 398 | /** |
383 | * Requested URL (everything after "GET" only). Allocated | 399 | * Requested URL (everything after "GET" only). Allocated |
384 | * in pool. | 400 | * in pool. |
385 | */ | 401 | */ |
386 | char *url; | 402 | char *url; |
387 | 403 | ||
388 | /** | 404 | /** |
389 | * HTTP version string (i.e. http/1.1). Allocated | 405 | * HTTP version string (i.e. http/1.1). Allocated |
390 | * in pool. | 406 | * in pool. |
391 | */ | 407 | */ |
392 | char *version; | 408 | char *version; |
393 | 409 | ||
394 | /** | 410 | /** |
395 | * Buffer for reading requests. Allocated | 411 | * Buffer for reading requests. Allocated |
396 | * in pool. Actually one byte larger than | 412 | * in pool. Actually one byte larger than |
397 | * read_buffer_size (if non-NULL) to allow for | 413 | * read_buffer_size (if non-NULL) to allow for |
398 | * 0-termination. | 414 | * 0-termination. |
399 | */ | 415 | */ |
400 | char *read_buffer; | 416 | char *read_buffer; |
401 | 417 | ||
402 | /** | 418 | /** |
403 | * Buffer for writing response (headers only). Allocated | 419 | * Buffer for writing response (headers only). Allocated |
404 | * in pool. | 420 | * in pool. |
405 | */ | 421 | */ |
406 | char *write_buffer; | 422 | char *write_buffer; |
407 | 423 | ||
408 | /** | 424 | /** |
409 | * Last incomplete header line during parsing of headers. | 425 | * Last incomplete header line during parsing of headers. |
410 | * Allocated in pool. Only valid if state is | 426 | * Allocated in pool. Only valid if state is |
411 | * either HEADER_PART_RECEIVED or FOOTER_PART_RECEIVED. | 427 | * either HEADER_PART_RECEIVED or FOOTER_PART_RECEIVED. |
412 | */ | 428 | */ |
413 | char *last; | 429 | char *last; |
414 | 430 | ||
415 | /** | 431 | /** |
416 | * Position after the colon on the last incomplete header | 432 | * Position after the colon on the last incomplete header |
417 | * line during parsing of headers. | 433 | * line during parsing of headers. |
418 | * Allocated in pool. Only valid if state is | 434 | * Allocated in pool. Only valid if state is |
419 | * either HEADER_PART_RECEIVED or FOOTER_PART_RECEIVED. | 435 | * either HEADER_PART_RECEIVED or FOOTER_PART_RECEIVED. |
420 | */ | 436 | */ |
421 | char *colon; | 437 | char *colon; |
422 | 438 | ||
423 | /** | 439 | /** |
424 | * Foreign address (of length addr_len). MALLOCED (not | 440 | * Foreign address (of length addr_len). MALLOCED (not |
425 | * in pool!). | 441 | * in pool!). |
426 | */ | 442 | */ |
427 | struct sockaddr_in *addr; | 443 | struct sockaddr_in *addr; |
428 | 444 | ||
429 | /** | 445 | /** |
430 | * Thread for this connection (if we are using | 446 | * Thread for this connection (if we are using |
431 | * one thread per connection). | 447 | * one thread per connection). |
432 | */ | 448 | */ |
433 | pthread_t pid; | 449 | pthread_t pid; |
434 | 450 | ||
435 | /** | 451 | /** |
436 | * Size of read_buffer (in bytes). This value indicates | 452 | * Size of read_buffer (in bytes). This value indicates |
437 | * how many bytes we're willing to read into the buffer; | 453 | * how many bytes we're willing to read into the buffer; |
438 | * the real buffer is one byte longer to allow for | 454 | * the real buffer is one byte longer to allow for |
439 | * adding zero-termination (when needed). | 455 | * adding zero-termination (when needed). |
440 | */ | 456 | */ |
441 | size_t read_buffer_size; | 457 | size_t read_buffer_size; |
442 | 458 | ||
443 | /** | 459 | /** |
444 | * Position where we currently append data in | 460 | * Position where we currently append data in |
445 | * read_buffer (last valid position). | 461 | * read_buffer (last valid position). |
446 | */ | 462 | */ |
447 | size_t read_buffer_offset; | 463 | size_t read_buffer_offset; |
448 | 464 | ||
449 | /** | 465 | /** |
450 | * Size of write_buffer (in bytes). | 466 | * Size of write_buffer (in bytes). |
451 | */ | 467 | */ |
452 | size_t write_buffer_size; | 468 | size_t write_buffer_size; |
453 | 469 | ||
454 | /** | 470 | /** |
455 | * Offset where we are with sending from write_buffer. | 471 | * Offset where we are with sending from write_buffer. |
456 | */ | 472 | */ |
457 | size_t write_buffer_send_offset; | 473 | size_t write_buffer_send_offset; |
458 | 474 | ||
459 | /** | 475 | /** |
460 | * Last valid location in write_buffer (where do we | 476 | * Last valid location in write_buffer (where do we |
461 | * append and up to where is it safe to send?) | 477 | * append and up to where is it safe to send?) |
462 | */ | 478 | */ |
463 | size_t write_buffer_append_offset; | 479 | size_t write_buffer_append_offset; |
464 | 480 | ||
465 | /** | 481 | /** |
466 | * How many more bytes of the body do we expect | 482 | * How many more bytes of the body do we expect |
467 | * to read? "-1" for unknown. | 483 | * to read? "-1" for unknown. |
468 | */ | 484 | */ |
469 | size_t remaining_upload_size; | 485 | size_t remaining_upload_size; |
470 | 486 | ||
471 | /** | 487 | /** |
472 | * Current write position in the actual response | 488 | * Current write position in the actual response |
473 | * (excluding headers, content only; should be 0 | 489 | * (excluding headers, content only; should be 0 |
474 | * while sending headers). | 490 | * while sending headers). |
475 | */ | 491 | */ |
476 | size_t response_write_position; | 492 | size_t response_write_position; |
477 | 493 | ||
478 | /** | 494 | /** |
479 | * Position in the 100 CONTINUE message that | 495 | * Position in the 100 CONTINUE message that |
480 | * we need to send when receiving http 1.1 requests. | 496 | * we need to send when receiving http 1.1 requests. |
481 | */ | 497 | */ |
482 | size_t continue_message_write_offset; | 498 | size_t continue_message_write_offset; |
483 | 499 | ||
484 | /** | 500 | /** |
485 | * Length of the foreign address. | 501 | * Length of the foreign address. |
486 | */ | 502 | */ |
487 | socklen_t addr_len; | 503 | socklen_t addr_len; |
488 | 504 | ||
489 | /** | 505 | /** |
490 | * Last time this connection had any activity | 506 | * Last time this connection had any activity |
491 | * (reading or writing). | 507 | * (reading or writing). |
492 | */ | 508 | */ |
493 | time_t last_activity; | 509 | time_t last_activity; |
494 | 510 | ||
495 | /** | 511 | /** |
496 | * Socket for this connection. Set to -1 if | 512 | * Socket for this connection. Set to -1 if |
497 | * this connection has died (daemon should clean | 513 | * this connection has died (daemon should clean |
498 | * up in that case). | 514 | * up in that case). |
499 | */ | 515 | */ |
500 | int socket_fd; | 516 | int socket_fd; |
501 | 517 | ||
502 | /** | 518 | /** |
503 | * Has this socket been closed for reading (i.e. | 519 | * Has this socket been closed for reading (i.e. |
504 | * other side closed the connection)? If so, | 520 | * other side closed the connection)? If so, |
505 | * we must completely close the connection once | 521 | * we must completely close the connection once |
506 | * we are done sending our response (and stop | 522 | * we are done sending our response (and stop |
507 | * trying to read from this socket). | 523 | * trying to read from this socket). |
508 | */ | 524 | */ |
509 | int read_closed; | 525 | int read_closed; |
510 | 526 | ||
511 | /** | 527 | /** |
512 | * State in the FSM for this connection. | 528 | * State in the FSM for this connection. |
513 | */ | 529 | */ |
514 | enum MHD_CONNECTION_STATE state; | 530 | enum MHD_CONNECTION_STATE state; |
515 | 531 | ||
516 | /** | 532 | /** |
517 | * HTTP response code. Only valid if response object | 533 | * HTTP response code. Only valid if response object |
518 | * is already set. | 534 | * is already set. |
519 | */ | 535 | */ |
520 | unsigned int responseCode; | 536 | unsigned int responseCode; |
521 | 537 | ||
522 | /** | 538 | /** |
523 | * Set to MHD_YES if the response's content reader | 539 | * Set to MHD_YES if the response's content reader |
524 | * callback failed to provide data the last time | 540 | * callback failed to provide data the last time |
525 | * we tried to read from it. In that case, the | 541 | * we tried to read from it. In that case, the |
526 | * write socket should be marked as unready until | 542 | * write socket should be marked as unready until |
527 | * the CRC call succeeds. | 543 | * the CRC call succeeds. |
528 | */ | 544 | */ |
529 | int response_unready; | 545 | int response_unready; |
530 | 546 | ||
531 | /** | 547 | /** |
532 | * Are we sending with chunked encoding? | 548 | * Are we sending with chunked encoding? |
533 | */ | 549 | */ |
534 | int have_chunked_response; | 550 | int have_chunked_response; |
535 | 551 | ||
536 | /** | 552 | /** |
537 | * Are we receiving with chunked encoding? This will be set to | 553 | * Are we receiving with chunked encoding? This will be set to |
538 | * MHD_YES after we parse the headers and are processing the body | 554 | * MHD_YES after we parse the headers and are processing the body |
539 | * with chunks. After we are done with the body and we are | 555 | * with chunks. After we are done with the body and we are |
540 | * processing the footers; once the footers are also done, this will | 556 | * processing the footers; once the footers are also done, this will |
541 | * be set to MHD_NO again (before the final call to the handler). | 557 | * be set to MHD_NO again (before the final call to the handler). |
542 | */ | 558 | */ |
543 | int have_chunked_upload; | 559 | int have_chunked_upload; |
544 | 560 | ||
545 | /** | 561 | /** |
546 | * If we are receiving with chunked encoding, where are we right | 562 | * If we are receiving with chunked encoding, where are we right |
547 | * now? Set to 0 if we are waiting to receive the chunk size; | 563 | * now? Set to 0 if we are waiting to receive the chunk size; |
548 | * otherwise, this is the size of the current chunk. A value of | 564 | * otherwise, this is the size of the current chunk. A value of |
549 | * zero is also used when we're at the end of the chunks. | 565 | * zero is also used when we're at the end of the chunks. |
550 | */ | 566 | */ |
551 | unsigned int current_chunk_size; | 567 | unsigned int current_chunk_size; |
552 | 568 | ||
553 | /** | 569 | /** |
554 | * If we are receiving with chunked encoding, where are we currently | 570 | * If we are receiving with chunked encoding, where are we currently |
555 | * with respect to the current chunk (at what offset / position)? | 571 | * with respect to the current chunk (at what offset / position)? |
556 | */ | 572 | */ |
557 | unsigned int current_chunk_offset; | 573 | unsigned int current_chunk_offset; |
558 | 574 | ||
559 | /* handlers used for processing read, write & idle connection operations */ | 575 | /** |
576 | * Handler used for processing read connection operations | ||
577 | */ | ||
560 | int (*read_handler) (struct MHD_Connection * connection); | 578 | int (*read_handler) (struct MHD_Connection * connection); |
561 | 579 | ||
580 | /** | ||
581 | * Handler used for processing write connection operations | ||
582 | */ | ||
562 | int (*write_handler) (struct MHD_Connection * connection); | 583 | int (*write_handler) (struct MHD_Connection * connection); |
563 | 584 | ||
585 | /** | ||
586 | * Handler used for processing idle connection operations | ||
587 | */ | ||
564 | int (*idle_handler) (struct MHD_Connection * connection); | 588 | int (*idle_handler) (struct MHD_Connection * connection); |
565 | 589 | ||
590 | /** | ||
591 | * Function used for reading HTTP request stream. | ||
592 | */ | ||
566 | ReceiveCallback recv_cls; | 593 | ReceiveCallback recv_cls; |
567 | 594 | ||
595 | /** | ||
596 | * Function used for writing HTTP response stream. | ||
597 | */ | ||
568 | TransmitCallback send_cls; | 598 | TransmitCallback send_cls; |
569 | 599 | ||
570 | #if HTTPS_SUPPORT | 600 | #if HTTPS_SUPPORT |
571 | /* TODO rename as this might be an SSL connection */ | 601 | /** |
602 | * State required for HTTPS/SSL/TLS support. | ||
603 | */ | ||
572 | MHD_gtls_session_t tls_session; | 604 | MHD_gtls_session_t tls_session; |
573 | #endif | 605 | #endif |
574 | }; | 606 | }; |
575 | 607 | ||
608 | /** | ||
609 | * State kept for each MHD daemon. | ||
610 | */ | ||
576 | struct MHD_Daemon | 611 | struct MHD_Daemon |
577 | { | 612 | { |
578 | 613 | ||
579 | /** | 614 | /** |
580 | * Callback function for all requests. | 615 | * Callback function for all requests. |
581 | */ | 616 | */ |
582 | MHD_AccessHandlerCallback default_handler; | 617 | MHD_AccessHandlerCallback default_handler; |
583 | 618 | ||
584 | /** | 619 | /** |
585 | * Closure argument to default_handler. | 620 | * Closure argument to default_handler. |
586 | */ | 621 | */ |
587 | void *default_handler_cls; | 622 | void *default_handler_cls; |
588 | 623 | ||
589 | /** | 624 | /** |
590 | * Linked list of our current connections. | 625 | * Linked list of our current connections. |
591 | */ | 626 | */ |
592 | struct MHD_Connection *connections; | 627 | struct MHD_Connection *connections; |
593 | 628 | ||
629 | /** | ||
630 | * Function to call to check if we should | ||
631 | * accept or reject an incoming request. | ||
632 | * May be NULL. | ||
633 | */ | ||
594 | MHD_AcceptPolicyCallback apc; | 634 | MHD_AcceptPolicyCallback apc; |
595 | 635 | ||
636 | /** | ||
637 | * Closure argument to apc. | ||
638 | */ | ||
596 | void *apc_cls; | 639 | void *apc_cls; |
597 | 640 | ||
641 | /** | ||
642 | * Function to call when we are done processing | ||
643 | * a particular request. May be NULL. | ||
644 | */ | ||
598 | MHD_RequestCompletedCallback notify_completed; | 645 | MHD_RequestCompletedCallback notify_completed; |
599 | 646 | ||
647 | /** | ||
648 | * Closure argument to notify_completed. | ||
649 | */ | ||
600 | void *notify_completed_cls; | 650 | void *notify_completed_cls; |
601 | 651 | ||
652 | /** | ||
653 | * Function to call with the full URI at the | ||
654 | * beginning of request processing. May be NULL. | ||
655 | * <p> | ||
656 | * Returns the initial pointer to internal state | ||
657 | * kept by the client for the request. | ||
658 | */ | ||
602 | void *(*uri_log_callback) (void *cls, const char *uri); | 659 | void *(*uri_log_callback) (void *cls, const char *uri); |
603 | 660 | ||
661 | /** | ||
662 | * Closure argument to uri_log_callback. | ||
663 | */ | ||
604 | void *uri_log_callback_cls; | 664 | void *uri_log_callback_cls; |
605 | 665 | ||
606 | /** | 666 | #if HAVE_MESSAGES |
607 | * PID of the select thread (if we have internal select) | 667 | /** |
608 | */ | 668 | * Function for logging error messages (if we |
669 | * support error reporting). | ||
670 | */ | ||
671 | void (*custom_error_log) (void *cls, const char *fmt, va_list va); | ||
672 | |||
673 | /** | ||
674 | * Closure argument to custom_error_log. | ||
675 | */ | ||
676 | void *custom_error_log_cls; | ||
677 | #endif | ||
678 | |||
679 | /** | ||
680 | * PID of the select thread (if we have internal select) | ||
681 | */ | ||
609 | pthread_t pid; | 682 | pthread_t pid; |
610 | 683 | ||
611 | /** | 684 | /** |
612 | * Listen socket. | 685 | * Listen socket. |
613 | */ | 686 | */ |
614 | int socket_fd; | 687 | int socket_fd; |
615 | 688 | ||
616 | /** | 689 | /** |
617 | * Are we shutting down? | 690 | * Are we shutting down? |
618 | */ | 691 | */ |
619 | int shutdown; | 692 | int shutdown; |
620 | 693 | ||
621 | /** | 694 | /** |
622 | * Size of the per-connection memory pools. | 695 | * Size of the per-connection memory pools. |
623 | */ | 696 | */ |
624 | unsigned int pool_size; | 697 | unsigned int pool_size; |
625 | 698 | ||
626 | /** | 699 | /** |
627 | * Limit on the number of parallel connections. | 700 | * Limit on the number of parallel connections. |
628 | */ | 701 | */ |
629 | unsigned int max_connections; | 702 | unsigned int max_connections; |
630 | 703 | ||
631 | /** | 704 | /** |
632 | * After how many seconds of inactivity should | 705 | * After how many seconds of inactivity should |
633 | * connections time out? Zero for no timeout. | 706 | * connections time out? Zero for no timeout. |
634 | */ | 707 | */ |
635 | unsigned int connection_timeout; | 708 | unsigned int connection_timeout; |
636 | 709 | ||
637 | /** | 710 | /** |
638 | * Maximum number of connections per IP, or 0 for | 711 | * Maximum number of connections per IP, or 0 for |
639 | * unlimited. | 712 | * unlimited. |
640 | */ | 713 | */ |
641 | unsigned int per_ip_connection_limit; | 714 | unsigned int per_ip_connection_limit; |
642 | 715 | ||
643 | /** | 716 | /** |
644 | * Daemon's options. | 717 | * Daemon's options. |
645 | */ | 718 | */ |
646 | enum MHD_OPTION options; | 719 | enum MHD_OPTION options; |
647 | 720 | ||
648 | /** | 721 | /** |
649 | * Listen port. | 722 | * Listen port. |
650 | */ | 723 | */ |
651 | unsigned short port; | 724 | unsigned short port; |
652 | 725 | ||
653 | #if HTTPS_SUPPORT | 726 | #if HTTPS_SUPPORT |
727 | /** | ||
728 | * What kind of credentials are we offering | ||
729 | * for SSL/TLS? | ||
730 | */ | ||
654 | enum MHD_GNUTLS_CredentialsType cred_type; | 731 | enum MHD_GNUTLS_CredentialsType cred_type; |
655 | 732 | ||
656 | /* server x509 credintials */ | 733 | /** |
734 | * Server x509 credentials | ||
735 | */ | ||
657 | MHD_gtls_cert_credentials_t x509_cred; | 736 | MHD_gtls_cert_credentials_t x509_cred; |
658 | 737 | ||
659 | /* cipher priority cache */ | 738 | /** |
739 | * Cipher priority cache | ||
740 | */ | ||
660 | MHD_gnutls_priority_t priority_cache; | 741 | MHD_gnutls_priority_t priority_cache; |
661 | 742 | ||
662 | /* Diffie-Hellman parameters */ | 743 | /** |
744 | * Diffie-Hellman parameters | ||
745 | */ | ||
663 | MHD_gtls_dh_params_t dh_params; | 746 | MHD_gtls_dh_params_t dh_params; |
664 | 747 | ||
748 | /** | ||
749 | * Pointer to our SSL/TLS key (in ASCII) in memory. | ||
750 | */ | ||
665 | const char *https_mem_key; | 751 | const char *https_mem_key; |
666 | 752 | ||
753 | /** | ||
754 | * Pointer to our SSL/TLS certificate (in ASCII) in memory. | ||
755 | */ | ||
667 | const char *https_mem_cert; | 756 | const char *https_mem_cert; |
668 | #endif | 757 | #endif |
669 | }; | 758 | }; |