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.c169
1 files changed, 23 insertions, 146 deletions
diff --git a/src/daemon/daemon.c b/src/daemon/daemon.c
index 19c614a1..b6f1e9f3 100644
--- a/src/daemon/daemon.c
+++ b/src/daemon/daemon.c
@@ -52,85 +52,6 @@
52 */ 52 */
53#define DEBUG_CONNECT MHD_NO 53#define DEBUG_CONNECT MHD_NO
54 54
55/**
56 * Register an access handler for all URIs beginning with uri_prefix.
57 *
58 * @param uri_prefix
59 * @return MRI_NO if a handler for this exact prefix
60 * already exists
61 */
62int
63MHD_register_handler (struct MHD_Daemon *daemon,
64 const char *uri_prefix,
65 MHD_AccessHandlerCallback dh, void *dh_cls)
66{
67 struct MHD_Access_Handler *ah;
68
69 if ((daemon == NULL) || (uri_prefix == NULL) || (dh == NULL))
70 return MHD_NO;
71 ah = daemon->handlers;
72 while (ah != NULL)
73 {
74 if (0 == strcmp (uri_prefix, ah->uri_prefix))
75 return MHD_NO;
76 ah = ah->next;
77 }
78 ah = malloc (sizeof (struct MHD_Access_Handler));
79 if (ah == NULL)
80 {
81#if HAVE_MESSAGES
82 MHD_DLOG (daemon, "Error allocating memory: %s\n", STRERROR (errno));
83#endif
84 return MHD_NO;
85 }
86
87 ah->next = daemon->handlers;
88 ah->uri_prefix = strdup (uri_prefix);
89 ah->dh = dh;
90 ah->dh_cls = dh_cls;
91 daemon->handlers = ah;
92 return MHD_YES;
93}
94
95
96/**
97 * Unregister an access handler for the URIs beginning with
98 * uri_prefix.
99 *
100 * @param uri_prefix
101 * @return MHD_NO if a handler for this exact prefix
102 * is not known for this daemon
103 */
104int
105MHD_unregister_handler (struct MHD_Daemon *daemon,
106 const char *uri_prefix,
107 MHD_AccessHandlerCallback dh, void *dh_cls)
108{
109 struct MHD_Access_Handler *prev;
110 struct MHD_Access_Handler *pos;
111
112 if ((daemon == NULL) || (uri_prefix == NULL) || (dh == NULL))
113 return MHD_NO;
114 pos = daemon->handlers;
115 prev = NULL;
116 while (pos != NULL)
117 {
118 if ((dh == pos->dh) &&
119 (dh_cls == pos->dh_cls) &&
120 (0 == strcmp (uri_prefix, pos->uri_prefix)))
121 {
122 if (prev == NULL)
123 daemon->handlers = pos->next;
124 else
125 prev->next = pos->next;
126 free (pos);
127 return MHD_YES;
128 }
129 prev = pos;
130 pos = pos->next;
131 }
132 return MHD_NO;
133}
134 55
135/** 56/**
136 * Obtain the select sets for this daemon. 57 * Obtain the select sets for this daemon.
@@ -175,7 +96,6 @@ MHD_get_fdset (struct MHD_Daemon *daemon,
175 return MHD_YES; 96 return MHD_YES;
176} 97}
177 98
178
179/** 99/**
180 * Main function of the thread that handles an individual 100 * Main function of the thread that handles an individual
181 * connection. 101 * connection.
@@ -191,26 +111,27 @@ MHD_handle_connection (void *data)
191 int max; 111 int max;
192 struct timeval tv; 112 struct timeval tv;
193 unsigned int timeout; 113 unsigned int timeout;
194 time_t now; 114 unsigned int now;
195 115
196 if (con == NULL) 116 if (con == NULL)
197 abort (); 117 abort ();
198 timeout = con->daemon->connection_timeout; 118 timeout = con->daemon->connection_timeout;
199 now = time (NULL); 119 while ( (!con->daemon->shutdown) &&
200 while ((!con->daemon->shutdown) && 120 (con->socket_fd != -1) )
201 (con->socket_fd != -1) &&
202 ((timeout == 0) || (now - timeout > con->last_activity)))
203 { 121 {
204 FD_ZERO (&rs); 122 FD_ZERO (&rs);
205 FD_ZERO (&ws); 123 FD_ZERO (&ws);
206 FD_ZERO (&es); 124 FD_ZERO (&es);
207 max = 0; 125 max = 0;
208 MHD_connection_get_fdset (con, &rs, &ws, &es, &max); 126 MHD_connection_get_fdset (con, &rs, &ws, &es, &max);
127 now = time(NULL);
209 tv.tv_usec = 0; 128 tv.tv_usec = 0;
210 tv.tv_sec = timeout - (now - con->last_activity); 129 if ( timeout > (now - con->last_activity) )
130 tv.tv_sec = timeout - (now - con->last_activity);
131 else
132 tv.tv_sec = 0;
211 num_ready = SELECT (max + 1, 133 num_ready = SELECT (max + 1,
212 &rs, &ws, &es, (timeout != 0) ? &tv : NULL); 134 &rs, &ws, &es, (timeout != 0) ? &tv : NULL);
213 now = time (NULL);
214 if (num_ready < 0) 135 if (num_ready < 0)
215 { 136 {
216 if (errno == EINTR) 137 if (errno == EINTR)
@@ -221,18 +142,13 @@ MHD_handle_connection (void *data)
221#endif 142#endif
222 break; 143 break;
223 } 144 }
224 if (((FD_ISSET (con->socket_fd, &rs)) && 145 if (FD_ISSET (con->socket_fd, &rs))
225 (MHD_YES != MHD_connection_handle_read (con))) || 146 MHD_connection_handle_read (con);
226 ((con->socket_fd != -1) &&
227 (FD_ISSET (con->socket_fd, &ws)) &&
228 (MHD_YES != MHD_connection_handle_write (con))))
229 break;
230 if ((con->have_received_headers == MHD_YES) && (con->response == NULL))
231 MHD_call_connection_handler (con);
232 if ((con->socket_fd != -1) && 147 if ((con->socket_fd != -1) &&
233 ((FD_ISSET (con->socket_fd, &rs)) || 148 (FD_ISSET (con->socket_fd, &ws)) )
234 (FD_ISSET (con->socket_fd, &ws)))) 149 MHD_connection_handle_write (con);
235 con->last_activity = now; 150 if (con->socket_fd != -1)
151 MHD_connection_handle_idle (con);
236 } 152 }
237 if (con->socket_fd != -1) 153 if (con->socket_fd != -1)
238 { 154 {
@@ -370,11 +286,6 @@ MHD_accept_connection (struct MHD_Daemon *daemon)
370 * Free resources associated with all closed connections. 286 * Free resources associated with all closed connections.
371 * (destroy responses, free buffers, etc.). A connection 287 * (destroy responses, free buffers, etc.). A connection
372 * is known to be closed if the socket_fd is -1. 288 * is known to be closed if the socket_fd is -1.
373 *
374 * Also performs connection actions that need to be run
375 * even if the connection is not selectable (such as
376 * calling the application again with upload data when
377 * the upload data buffer is full).
378 */ 289 */
379static void 290static void
380MHD_cleanup_connections (struct MHD_Daemon *daemon) 291MHD_cleanup_connections (struct MHD_Daemon *daemon)
@@ -382,33 +293,11 @@ MHD_cleanup_connections (struct MHD_Daemon *daemon)
382 struct MHD_Connection *pos; 293 struct MHD_Connection *pos;
383 struct MHD_Connection *prev; 294 struct MHD_Connection *prev;
384 void *unused; 295 void *unused;
385 time_t timeout;
386 296
387 timeout = time (NULL);
388 if (daemon->connection_timeout != 0)
389 timeout -= daemon->connection_timeout;
390 else
391 timeout = 0;
392 pos = daemon->connections; 297 pos = daemon->connections;
393 prev = NULL; 298 prev = NULL;
394 while (pos != NULL) 299 while (pos != NULL)
395 { 300 {
396 if ((pos->last_activity < timeout) && (pos->socket_fd != -1))
397 {
398#if DEBUG_CLOSE
399#if HAVE_MESSAGES
400 MHD_DLOG (daemon, "Connection timed out, closing connection\n");
401#endif
402#endif
403 SHUTDOWN (pos->socket_fd, SHUT_RDWR);
404 CLOSE (pos->socket_fd);
405 pos->socket_fd = -1;
406 if (pos->daemon->notify_completed != NULL)
407 pos->daemon->notify_completed (pos->daemon->notify_completed_cls,
408 pos,
409 &pos->client_context,
410 MHD_REQUEST_TERMINATED_TIMEOUT_REACHED);
411 }
412 if (pos->socket_fd == -1) 301 if (pos->socket_fd == -1)
413 { 302 {
414 if (prev == NULL) 303 if (prev == NULL)
@@ -420,8 +309,7 @@ MHD_cleanup_connections (struct MHD_Daemon *daemon)
420 pthread_kill (pos->pid, SIGALRM); 309 pthread_kill (pos->pid, SIGALRM);
421 pthread_join (pos->pid, &unused); 310 pthread_join (pos->pid, &unused);
422 } 311 }
423 if (pos->response != NULL) 312 MHD_destroy_response (pos->response);
424 MHD_destroy_response (pos->response);
425 MHD_pool_destroy (pos->pool); 313 MHD_pool_destroy (pos->pool);
426 free (pos->addr); 314 free (pos->addr);
427 free (pos); 315 free (pos);
@@ -432,12 +320,6 @@ MHD_cleanup_connections (struct MHD_Daemon *daemon)
432 pos = prev->next; 320 pos = prev->next;
433 continue; 321 continue;
434 } 322 }
435
436 if ((0 == (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) &&
437 ((pos->have_received_headers == MHD_YES)
438 && (pos->response == NULL)))
439 MHD_call_connection_handler (pos);
440
441 prev = pos; 323 prev = pos;
442 pos = pos->next; 324 pos = pos->next;
443 } 325 }
@@ -573,15 +455,12 @@ MHD_select (struct MHD_Daemon *daemon, int may_block)
573 if (ds != -1) 455 if (ds != -1)
574 { 456 {
575 if (FD_ISSET (ds, &rs)) 457 if (FD_ISSET (ds, &rs))
576 { 458 MHD_connection_handle_read (pos);
577 pos->last_activity = now; 459 if ( (pos->socket_fd != -1) &&
578 MHD_connection_handle_read (pos); 460 (FD_ISSET (ds, &ws)) )
579 } 461 MHD_connection_handle_write (pos);
580 if (FD_ISSET (ds, &ws)) 462 if (pos->socket_fd != -1)
581 { 463 MHD_connection_handle_idle(pos);
582 pos->last_activity = now;
583 MHD_connection_handle_write (pos);
584 }
585 } 464 }
586 pos = pos->next; 465 pos = pos->next;
587 } 466 }
@@ -726,10 +605,8 @@ MHD_start_daemon (unsigned int options,
726 retVal->apc = apc; 605 retVal->apc = apc;
727 retVal->apc_cls = apc_cls; 606 retVal->apc_cls = apc_cls;
728 retVal->socket_fd = socket_fd; 607 retVal->socket_fd = socket_fd;
729 retVal->default_handler.dh = dh; 608 retVal->default_handler = dh;
730 retVal->default_handler.dh_cls = dh_cls; 609 retVal->default_handler_cls = dh_cls;
731 retVal->default_handler.uri_prefix = "";
732 retVal->default_handler.next = NULL;
733 retVal->max_connections = MHD_MAX_CONNECTIONS_DEFAULT; 610 retVal->max_connections = MHD_MAX_CONNECTIONS_DEFAULT;
734 retVal->pool_size = MHD_POOL_SIZE_DEFAULT; 611 retVal->pool_size = MHD_POOL_SIZE_DEFAULT;
735 retVal->connection_timeout = 0; /* no timeout */ 612 retVal->connection_timeout = 0; /* no timeout */