aboutsummaryrefslogtreecommitdiff
path: root/src/daemon/daemon.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/daemon/daemon.c')
-rw-r--r--src/daemon/daemon.c108
1 files changed, 54 insertions, 54 deletions
diff --git a/src/daemon/daemon.c b/src/daemon/daemon.c
index 8d5c3459..379e3cf9 100644
--- a/src/daemon/daemon.c
+++ b/src/daemon/daemon.c
@@ -29,26 +29,26 @@
29#include "response.h" 29#include "response.h"
30#include "session.h" 30#include "session.h"
31 31
32#define MHD_MAX_CONNECTIONS FD_SETSIZE -4 32#define MHD_MAX_CONNECTIONS FD_SETSIZE -4
33 33
34 34
35/** 35/**
36 * Register an access handler for all URIs beginning with uri_prefix. 36 * Register an access handler for all URIs beginning with uri_prefix.
37 * 37 *
38 * @param uri_prefix 38 * @param uri_prefix
39 * @return MRI_NO if a handler for this exact prefix 39 * @return MRI_NO if a handler for this exact prefix
40 * already exists 40 * already exists
41 */ 41 */
42int 42int
43MHD_register_handler(struct MHD_Daemon * daemon, 43MHD_register_handler(struct MHD_Daemon * daemon,
44 const char * uri_prefix, 44 const char * uri_prefix,
45 MHD_AccessHandlerCallback dh, 45 MHD_AccessHandlerCallback dh,
46 void * dh_cls) { 46 void * dh_cls) {
47 struct MHD_Access_Handler * ah; 47 struct MHD_Access_Handler * ah;
48 48
49 if ( (daemon == NULL) || 49 if ( (daemon == NULL) ||
50 (uri_prefix == NULL) || 50 (uri_prefix == NULL) ||
51 (dh == NULL) ) 51 (dh == NULL) )
52 return MHD_NO; 52 return MHD_NO;
53 ah = daemon->handlers; 53 ah = daemon->handlers;
54 while (ah != NULL) { 54 while (ah != NULL) {
@@ -71,11 +71,11 @@ MHD_register_handler(struct MHD_Daemon * daemon,
71 * Unregister an access handler for the URIs beginning with 71 * Unregister an access handler for the URIs beginning with
72 * uri_prefix. 72 * uri_prefix.
73 * 73 *
74 * @param uri_prefix 74 * @param uri_prefix
75 * @return MHD_NO if a handler for this exact prefix 75 * @return MHD_NO if a handler for this exact prefix
76 * is not known for this daemon 76 * is not known for this daemon
77 */ 77 */
78int 78int
79MHD_unregister_handler(struct MHD_Daemon * daemon, 79MHD_unregister_handler(struct MHD_Daemon * daemon,
80 const char * uri_prefix, 80 const char * uri_prefix,
81 MHD_AccessHandlerCallback dh, 81 MHD_AccessHandlerCallback dh,
@@ -83,9 +83,9 @@ MHD_unregister_handler(struct MHD_Daemon * daemon,
83 struct MHD_Access_Handler * prev; 83 struct MHD_Access_Handler * prev;
84 struct MHD_Access_Handler * pos; 84 struct MHD_Access_Handler * pos;
85 85
86 if ( (daemon == NULL) || 86 if ( (daemon == NULL) ||
87 (uri_prefix == NULL) || 87 (uri_prefix == NULL) ||
88 (dh == NULL) ) 88 (dh == NULL) )
89 return MHD_NO; 89 return MHD_NO;
90 pos = daemon->handlers; 90 pos = daemon->handlers;
91 prev = NULL; 91 prev = NULL;
@@ -114,7 +114,7 @@ MHD_unregister_handler(struct MHD_Daemon * daemon,
114 * daemon was not started with the right 114 * daemon was not started with the right
115 * options for this call. 115 * options for this call.
116 */ 116 */
117int 117int
118MHD_get_fdset(struct MHD_Daemon * daemon, 118MHD_get_fdset(struct MHD_Daemon * daemon,
119 fd_set * read_fd_set, 119 fd_set * read_fd_set,
120 fd_set * write_fd_set, 120 fd_set * write_fd_set,
@@ -123,13 +123,13 @@ MHD_get_fdset(struct MHD_Daemon * daemon,
123 struct MHD_Session * pos; 123 struct MHD_Session * pos;
124 124
125 if ( (daemon == NULL) || 125 if ( (daemon == NULL) ||
126 (read_fd_set == NULL) || 126 (read_fd_set == NULL) ||
127 (write_fd_set == NULL) || 127 (write_fd_set == NULL) ||
128 (except_fd_set == NULL) || 128 (except_fd_set == NULL) ||
129 (max_fd == NULL) || 129 (max_fd == NULL) ||
130 ( (daemon->options & MHD_USE_THREAD_PER_CONNECTION) != 0) ) 130 ( (daemon->options & MHD_USE_THREAD_PER_CONNECTION) != 0) )
131 return MHD_NO; 131 return MHD_NO;
132 FD_SET(daemon->socket_fd, 132 FD_SET(daemon->socket_fd,
133 read_fd_set); 133 read_fd_set);
134 if ( (*max_fd) < daemon->socket_fd) 134 if ( (*max_fd) < daemon->socket_fd)
135 *max_fd = daemon->socket_fd; 135 *max_fd = daemon->socket_fd;
@@ -159,14 +159,14 @@ MHD_handle_connection(void * data) {
159 fd_set ws; 159 fd_set ws;
160 fd_set es; 160 fd_set es;
161 int max; 161 int max;
162 162
163 if (con == NULL) 163 if (con == NULL)
164 abort(); 164 abort();
165 while ( (! con->daemon->shutdown) && 165 while ( (! con->daemon->shutdown) &&
166 (con->socket_fd != -1) ) { 166 (con->socket_fd != -1) ) {
167 FD_ZERO(&rs); 167 FD_ZERO(&rs);
168 FD_ZERO(&ws); 168 FD_ZERO(&ws);
169 FD_ZERO(&es); 169 FD_ZERO(&es);
170 max = 0; 170 max = 0;
171 MHD_session_get_fdset(con, 171 MHD_session_get_fdset(con,
172 &rs, 172 &rs,
@@ -174,9 +174,9 @@ MHD_handle_connection(void * data) {
174 &es, 174 &es,
175 &max); 175 &max);
176 num_ready = SELECT(max + 1, 176 num_ready = SELECT(max + 1,
177 &rs, 177 &rs,
178 &ws, 178 &ws,
179 &es, 179 &es,
180 NULL); 180 NULL);
181 if (num_ready <= 0) { 181 if (num_ready <= 0) {
182 if (errno == EINTR) 182 if (errno == EINTR)
@@ -192,7 +192,7 @@ MHD_handle_connection(void * data) {
192 if ( (con->headersReceived == 1) && 192 if ( (con->headersReceived == 1) &&
193 (con->response == NULL) ) 193 (con->response == NULL) )
194 MHD_call_session_handler(con); 194 MHD_call_session_handler(con);
195 } 195 }
196 if (con->socket_fd != -1) { 196 if (con->socket_fd != -1) {
197 CLOSE(con->socket_fd); 197 CLOSE(con->socket_fd);
198 con->socket_fd = -1; 198 con->socket_fd = -1;
@@ -214,10 +214,10 @@ MHD_accept_connection(struct MHD_Daemon * daemon) {
214 int s; 214 int s;
215 215
216 addrlen = sizeof(struct sockaddr); 216 addrlen = sizeof(struct sockaddr);
217 memset(&addr, 217 memset(&addr,
218 0, 218 0,
219 sizeof(struct sockaddr)); 219 sizeof(struct sockaddr));
220 s = ACCEPT(daemon->socket_fd, 220 s = ACCEPT(daemon->socket_fd,
221 &addr, 221 &addr,
222 &addrlen); 222 &addrlen);
223 if ( (s < 0) || 223 if ( (s < 0) ||
@@ -246,8 +246,8 @@ MHD_accept_connection(struct MHD_Daemon * daemon) {
246 session->daemon = daemon; 246 session->daemon = daemon;
247 if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION) ) && 247 if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION) ) &&
248 (0 != pthread_create(&session->pid, 248 (0 != pthread_create(&session->pid,
249 NULL, 249 NULL,
250 &MHD_handle_connection, 250 &MHD_handle_connection,
251 session)) ) { 251 session)) ) {
252 MHD_DLOG(daemon, 252 MHD_DLOG(daemon,
253 "Failed to create a thread: %s\n", 253 "Failed to create a thread: %s\n",
@@ -273,7 +273,7 @@ MHD_accept_connection(struct MHD_Daemon * daemon) {
273 * calling the application again with upload data when 273 * calling the application again with upload data when
274 * the upload data buffer is full). 274 * the upload data buffer is full).
275 */ 275 */
276static void 276static void
277MHD_cleanup_sessions(struct MHD_Daemon * daemon) { 277MHD_cleanup_sessions(struct MHD_Daemon * daemon) {
278 struct MHD_Session * pos; 278 struct MHD_Session * pos;
279 struct MHD_Session * prev; 279 struct MHD_Session * prev;
@@ -316,14 +316,14 @@ MHD_cleanup_sessions(struct MHD_Daemon * daemon) {
316 else 316 else
317 pos = prev->next; 317 pos = prev->next;
318 continue; 318 continue;
319 } 319 }
320 320
321 if ( (pos->headersReceived == 1) && 321 if ( (pos->headersReceived == 1) &&
322 (pos->response == NULL) ) 322 (pos->response == NULL) )
323 MHD_call_session_handler(pos); 323 MHD_call_session_handler(pos);
324 324
325 prev = pos; 325 prev = pos;
326 pos = pos->next; 326 pos = pos->next;
327 } 327 }
328} 328}
329 329
@@ -345,14 +345,14 @@ MHD_select(struct MHD_Daemon * daemon,
345 int max; 345 int max;
346 struct timeval timeout; 346 struct timeval timeout;
347 int ds; 347 int ds;
348 348
349 timeout.tv_sec = 0; 349 timeout.tv_sec = 0;
350 timeout.tv_usec = 0; 350 timeout.tv_usec = 0;
351 if(daemon == NULL) 351 if(daemon == NULL)
352 abort(); 352 abort();
353 FD_ZERO(&rs); 353 FD_ZERO(&rs);
354 FD_ZERO(&ws); 354 FD_ZERO(&ws);
355 FD_ZERO(&es); 355 FD_ZERO(&es);
356 max = 0; 356 max = 0;
357 357
358 if (0 == (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) { 358 if (0 == (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) {
@@ -372,21 +372,21 @@ MHD_select(struct MHD_Daemon * daemon,
372 &rs, 372 &rs,
373 &ws, 373 &ws,
374 &es, 374 &es,
375 may_block == MHD_NO ? &timeout : NULL); 375 may_block == MHD_NO ? &timeout : NULL);
376 if (num_ready < 0) { 376 if (num_ready < 0) {
377 if (errno == EINTR) 377 if (errno == EINTR)
378 return MHD_YES; 378 return MHD_YES;
379 MHD_DLOG(daemon, 379 MHD_DLOG(daemon,
380 "Select failed: %s\n", 380 "Select failed: %s\n",
381 STRERROR(errno)); 381 STRERROR(errno));
382 return MHD_NO; 382 return MHD_NO;
383 } 383 }
384 ds = daemon->socket_fd; 384 ds = daemon->socket_fd;
385 if (ds == -1) 385 if (ds == -1)
386 return MHD_YES; 386 return MHD_YES;
387 if (FD_ISSET(ds, 387 if (FD_ISSET(ds,
388 &rs)) 388 &rs))
389 MHD_accept_connection(daemon); 389 MHD_accept_connection(daemon);
390 if (0 == (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) { 390 if (0 == (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) {
391 /* do not have a thread per connection, process all connections now */ 391 /* do not have a thread per connection, process all connections now */
392 pos = daemon->connections; 392 pos = daemon->connections;
@@ -396,7 +396,7 @@ MHD_select(struct MHD_Daemon * daemon,
396 pos = pos->next; 396 pos = pos->next;
397 continue; 397 continue;
398 } 398 }
399 if (FD_ISSET(ds, &rs)) 399 if (FD_ISSET(ds, &rs))
400 MHD_session_handle_read(pos); 400 MHD_session_handle_read(pos);
401 if (FD_ISSET(ds, &ws)) 401 if (FD_ISSET(ds, &ws))
402 MHD_session_handle_write(pos); 402 MHD_session_handle_write(pos);
@@ -411,7 +411,7 @@ MHD_select(struct MHD_Daemon * daemon,
411 * Run webserver operations (without blocking unless 411 * Run webserver operations (without blocking unless
412 * in client callbacks). This method should be called 412 * in client callbacks). This method should be called
413 * by clients in combination with MHD_get_fdset 413 * by clients in combination with MHD_get_fdset
414 * if the client-controlled select method is used. 414 * if the client-controlled select method is used.
415 * 415 *
416 * @return MHD_YES on success, MHD_NO if this 416 * @return MHD_YES on success, MHD_NO if this
417 * daemon was not started with the right 417 * daemon was not started with the right
@@ -466,12 +466,12 @@ MHD_start_daemon(unsigned int options,
466 int socket_fd; 466 int socket_fd;
467 struct sockaddr_in servaddr; 467 struct sockaddr_in servaddr;
468 468
469 if ((options & MHD_USE_SSL) != 0) 469 if ((options & MHD_USE_SSL) != 0)
470 return NULL; 470 return NULL;
471 if ((options & MHD_USE_IPv6) != 0) 471 if ((options & MHD_USE_IPv6) != 0)
472 return NULL; 472 return NULL;
473 if ((options & MHD_USE_IPv4) == 0) 473 if ((options & MHD_USE_IPv4) == 0)
474 return NULL; 474 return NULL;
475 if ( (port == 0) || 475 if ( (port == 0) ||
476 (dh == NULL) ) 476 (dh == NULL) )
477 return NULL; 477 return NULL;
@@ -486,11 +486,11 @@ MHD_start_daemon(unsigned int options,
486 /* FIXME: setsockopt: SO_REUSEADDR? */ 486 /* FIXME: setsockopt: SO_REUSEADDR? */
487 memset(&servaddr, 487 memset(&servaddr,
488 0, 488 0,
489 sizeof(struct sockaddr_in)); 489 sizeof(struct sockaddr_in));
490 servaddr.sin_family = AF_INET; 490 servaddr.sin_family = AF_INET;
491 servaddr.sin_port = htons(port); 491 servaddr.sin_port = htons(port);
492 if (BIND(socket_fd, 492 if (BIND(socket_fd,
493 (struct sockaddr *)&servaddr, 493 (struct sockaddr *)&servaddr,
494 sizeof(struct sockaddr_in)) < 0) { 494 sizeof(struct sockaddr_in)) < 0) {
495 if ( (options & MHD_USE_DEBUG) != 0) 495 if ( (options & MHD_USE_DEBUG) != 0)
496 fprintf(stderr, 496 fprintf(stderr,
@@ -506,7 +506,7 @@ MHD_start_daemon(unsigned int options,
506 "Failed to listen for connections: %s\n", 506 "Failed to listen for connections: %s\n",
507 STRERROR(errno)); 507 STRERROR(errno));
508 CLOSE(socket_fd); 508 CLOSE(socket_fd);
509 return NULL; 509 return NULL;
510 } 510 }
511 retVal = malloc(sizeof(struct MHD_Daemon)); 511 retVal = malloc(sizeof(struct MHD_Daemon));
512 memset(retVal, 512 memset(retVal,
@@ -524,15 +524,15 @@ MHD_start_daemon(unsigned int options,
524 if ( ( (0 != (options & MHD_USE_THREAD_PER_CONNECTION)) || 524 if ( ( (0 != (options & MHD_USE_THREAD_PER_CONNECTION)) ||
525 (0 != (options & MHD_USE_SELECT_INTERNALLY)) ) && 525 (0 != (options & MHD_USE_SELECT_INTERNALLY)) ) &&
526 (0 != pthread_create(&retVal->pid, 526 (0 != pthread_create(&retVal->pid,
527 NULL, 527 NULL,
528 &MHD_select_thread, 528 &MHD_select_thread,
529 retVal)) ) { 529 retVal)) ) {
530 MHD_DLOG(retVal, 530 MHD_DLOG(retVal,
531 "Failed to create listen thread: %s\n", 531 "Failed to create listen thread: %s\n",
532 STRERROR(errno)); 532 STRERROR(errno));
533 free(retVal); 533 free(retVal);
534 CLOSE(socket_fd); 534 CLOSE(socket_fd);
535 return NULL; 535 return NULL;
536 } 536 }
537 return retVal; 537 return retVal;
538} 538}
@@ -544,12 +544,12 @@ void
544MHD_stop_daemon(struct MHD_Daemon * daemon) { 544MHD_stop_daemon(struct MHD_Daemon * daemon) {
545 void * unused; 545 void * unused;
546 546
547 if (daemon == NULL) 547 if (daemon == NULL)
548 return; 548 return;
549 daemon->shutdown = 1; 549 daemon->shutdown = 1;
550 CLOSE(daemon->socket_fd); 550 CLOSE(daemon->socket_fd);
551 daemon->socket_fd = -1; 551 daemon->socket_fd = -1;
552 if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) || 552 if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) ||
553 (0 != (daemon->options & MHD_USE_SELECT_INTERNALLY)) ) { 553 (0 != (daemon->options & MHD_USE_SELECT_INTERNALLY)) ) {
554 pthread_kill(daemon->pid, SIGALRM); 554 pthread_kill(daemon->pid, SIGALRM);
555 pthread_join(daemon->pid, &unused); 555 pthread_join(daemon->pid, &unused);