diff options
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/Makefile.am | 1 | ||||
-rw-r--r-- | src/lib/connection_add.c | 23 | ||||
-rw-r--r-- | src/lib/connection_call_handlers.c | 146 | ||||
-rw-r--r-- | src/lib/connection_call_handlers.h | 48 | ||||
-rw-r--r-- | src/lib/daemon_epoll.c | 13 | ||||
-rw-r--r-- | src/lib/daemon_poll.c | 13 | ||||
-rw-r--r-- | src/lib/daemon_select.c | 15 |
7 files changed, 229 insertions, 30 deletions
diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index 3c5b6afd..320287d3 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am | |||
@@ -59,6 +59,7 @@ libmicrohttpd_la_SOURCES = \ | |||
59 | action_process_upload.c \ | 59 | action_process_upload.c \ |
60 | action_suspend.c \ | 60 | action_suspend.c \ |
61 | connection_add.c connection_add.h \ | 61 | connection_add.c connection_add.h \ |
62 | connection_call_handlers.c connection_call_handlers.h \ | ||
62 | connection_cleanup.c connection_cleanup.h \ | 63 | connection_cleanup.c connection_cleanup.h \ |
63 | connection_close.c connection_close.h \ | 64 | connection_close.c connection_close.h \ |
64 | connection_finish_forward.c connection_finish_forward.h \ | 65 | connection_finish_forward.c connection_finish_forward.h \ |
diff --git a/src/lib/connection_add.c b/src/lib/connection_add.c index 178ad929..c4150b1e 100644 --- a/src/lib/connection_add.c +++ b/src/lib/connection_add.c | |||
@@ -23,6 +23,7 @@ | |||
23 | */ | 23 | */ |
24 | #include "internal.h" | 24 | #include "internal.h" |
25 | #include "connection_add.h" | 25 | #include "connection_add.h" |
26 | #include "connection_call_handlers.h" | ||
26 | #include "connection_close.h" | 27 | #include "connection_close.h" |
27 | #include "connection_finish_forward.h" | 28 | #include "connection_finish_forward.h" |
28 | #include "connection_update_last_activity.h" | 29 | #include "connection_update_last_activity.h" |
@@ -317,13 +318,13 @@ thread_main_handle_connection (void *data) | |||
317 | MHD_itc_clear_ (daemon->itc); | 318 | MHD_itc_clear_ (daemon->itc); |
318 | #endif | 319 | #endif |
319 | if (MHD_NO == | 320 | if (MHD_NO == |
320 | call_handlers (con, | 321 | MHD_connection_call_handlers_ (con, |
321 | FD_ISSET (con->socket_fd, | 322 | FD_ISSET (con->socket_fd, |
322 | &rs), | 323 | &rs), |
323 | FD_ISSET (con->socket_fd, | 324 | FD_ISSET (con->socket_fd, |
324 | &ws), | 325 | &ws), |
325 | FD_ISSET (con->socket_fd, | 326 | FD_ISSET (con->socket_fd, |
326 | &es)) ) | 327 | &es)) ) |
327 | goto exit; | 328 | goto exit; |
328 | } | 329 | } |
329 | #ifdef HAVE_POLL | 330 | #ifdef HAVE_POLL |
@@ -385,10 +386,10 @@ thread_main_handle_connection (void *data) | |||
385 | MHD_itc_clear_ (daemon->itc); | 386 | MHD_itc_clear_ (daemon->itc); |
386 | #endif | 387 | #endif |
387 | if (MHD_NO == | 388 | if (MHD_NO == |
388 | call_handlers (con, | 389 | MHD_connection_call_handlers_ (con, |
389 | 0 != (p[0].revents & POLLIN), | 390 | 0 != (p[0].revents & POLLIN), |
390 | 0 != (p[0].revents & POLLOUT), | 391 | 0 != (p[0].revents & POLLOUT), |
391 | 0 != (p[0].revents & (POLLERR | MHD_POLL_REVENTS_ERR_DISC)))) | 392 | 0 != (p[0].revents & (POLLERR | MHD_POLL_REVENTS_ERR_DISC)))) |
392 | goto exit; | 393 | goto exit; |
393 | } | 394 | } |
394 | #endif | 395 | #endif |
diff --git a/src/lib/connection_call_handlers.c b/src/lib/connection_call_handlers.c new file mode 100644 index 00000000..3ded0ebd --- /dev/null +++ b/src/lib/connection_call_handlers.c | |||
@@ -0,0 +1,146 @@ | |||
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 | * @file lib/connection_call_handlers.c | ||
21 | * @brief call the connection's handlers based on the event trigger | ||
22 | * @author Christian Grothoff | ||
23 | */ | ||
24 | #include "internal.h" | ||
25 | #include "connection_call_handlers.h" | ||
26 | #include "connection_close.h" | ||
27 | |||
28 | |||
29 | /** | ||
30 | * Call the handlers for a connection in the appropriate order based | ||
31 | * on the readiness as detected by the event loop. | ||
32 | * | ||
33 | * @param con connection to handle | ||
34 | * @param read_ready set if the socket is ready for reading | ||
35 | * @param write_ready set if the socket is ready for writing | ||
36 | * @param force_close set if a hard error was detected on the socket; | ||
37 | * if this information is not available, simply pass #MHD_NO | ||
38 | * @return #MHD_YES to continue normally, | ||
39 | * #MHD_NO if a serious error was encountered and the | ||
40 | * connection is to be closed. | ||
41 | */ | ||
42 | // FIXME: rename connection->request? | ||
43 | int | ||
44 | MHD_connection_call_handlers_ (struct MHD_Connection *con, | ||
45 | bool read_ready, | ||
46 | bool write_ready, | ||
47 | bool force_close) | ||
48 | { | ||
49 | struct MHD_Daemon *daemon = con->daemon; | ||
50 | int ret; | ||
51 | bool states_info_processed = false; | ||
52 | /* Fast track flag */ | ||
53 | bool on_fasttrack = (con->request.state == MHD_REQUEST_INIT); | ||
54 | |||
55 | #ifdef HTTPS_SUPPORT | ||
56 | if (con->tls_read_ready) | ||
57 | read_ready = true; | ||
58 | #endif /* HTTPS_SUPPORT */ | ||
59 | if (!force_close) | ||
60 | { | ||
61 | if ( (MHD_EVENT_LOOP_INFO_READ == | ||
62 | con->request.event_loop_info) && | ||
63 | read_ready) | ||
64 | { | ||
65 | MHD_connection_handle_read (con); | ||
66 | ret = MHD_connection_handle_idle (con); | ||
67 | states_info_processed = true; | ||
68 | } | ||
69 | /* No need to check value of 'ret' here as closed connection | ||
70 | * cannot be in MHD_EVENT_LOOP_INFO_WRITE state. */ | ||
71 | if ( (MHD_EVENT_LOOP_INFO_WRITE == | ||
72 | con->request.event_loop_info) && | ||
73 | write_ready) | ||
74 | { | ||
75 | MHD_connection_handle_write (con); | ||
76 | ret = MHD_connection_handle_idle (con); | ||
77 | states_info_processed = true; | ||
78 | } | ||
79 | } | ||
80 | else | ||
81 | { | ||
82 | MHD_connection_close_ (con, | ||
83 | MHD_REQUEST_TERMINATED_WITH_ERROR); | ||
84 | return MHD_connection_handle_idle (con); | ||
85 | } | ||
86 | |||
87 | if (! states_info_processed) | ||
88 | { /* Connection is not read or write ready, but external conditions | ||
89 | * may be changed and need to be processed. */ | ||
90 | ret = MHD_connection_handle_idle (con); | ||
91 | } | ||
92 | /* Fast track for fast connections. */ | ||
93 | /* If full request was read by single read_handler() invocation | ||
94 | and headers were completely prepared by single MHD_connection_handle_idle() | ||
95 | then try not to wait for next sockets polling and send response | ||
96 | immediately. | ||
97 | As writeability of socket was not checked and it may have | ||
98 | some data pending in system buffers, use this optimization | ||
99 | only for non-blocking sockets. */ | ||
100 | /* No need to check 'ret' as connection is always in | ||
101 | * MHD_CONNECTION_CLOSED state if 'ret' is equal 'MHD_NO'. */ | ||
102 | else if (on_fasttrack && | ||
103 | con->sk_nonblck) | ||
104 | { | ||
105 | if (MHD_REQUEST_HEADERS_SENDING == con->request.state) | ||
106 | { | ||
107 | MHD_connection_handle_write (con); | ||
108 | /* Always call 'MHD_connection_handle_idle()' after each read/write. */ | ||
109 | ret = MHD_connection_handle_idle (con); | ||
110 | } | ||
111 | /* If all headers were sent by single write_handler() and | ||
112 | * response body is prepared by single MHD_connection_handle_idle() | ||
113 | * call - continue. */ | ||
114 | if ((MHD_REQUEST_NORMAL_BODY_READY == con->request.state) || | ||
115 | (MHD_REQUEST_CHUNKED_BODY_READY == con->request.state)) | ||
116 | { | ||
117 | MHD_connection_handle_write (con); | ||
118 | ret = MHD_connection_handle_idle (con); | ||
119 | } | ||
120 | } | ||
121 | |||
122 | /* All connection's data and states are processed for this turn. | ||
123 | * If connection already has more data to be processed - use | ||
124 | * zero timeout for next select()/poll(). */ | ||
125 | /* Thread-per-connection do not need global zero timeout as | ||
126 | * connections are processed individually. */ | ||
127 | /* Note: no need to check for read buffer availability for | ||
128 | * TLS read-ready connection in 'read info' state as connection | ||
129 | * without space in read buffer will be market as 'info block'. */ | ||
130 | if ( (! daemon->data_already_pending) && | ||
131 | (MHD_TM_THREAD_PER_CONNECTION != daemon->threading_model) ) | ||
132 | { | ||
133 | if (MHD_EVENT_LOOP_INFO_BLOCK == | ||
134 | con->request.event_loop_info) | ||
135 | daemon->data_already_pending = true; | ||
136 | #ifdef HTTPS_SUPPORT | ||
137 | else if ( (con->tls_read_ready) && | ||
138 | (MHD_EVENT_LOOP_INFO_READ == | ||
139 | con->request.event_loop_info) ) | ||
140 | daemon->data_already_pending = true; | ||
141 | #endif /* HTTPS_SUPPORT */ | ||
142 | } | ||
143 | return ret; | ||
144 | } | ||
145 | |||
146 | /* end of connection_call_handlers.c */ | ||
diff --git a/src/lib/connection_call_handlers.h b/src/lib/connection_call_handlers.h new file mode 100644 index 00000000..1bd74710 --- /dev/null +++ b/src/lib/connection_call_handlers.h | |||
@@ -0,0 +1,48 @@ | |||
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 | * @file lib/connection_call_handlers.h | ||
21 | * @brief function to call event handlers based on event mask | ||
22 | * @author Christian Grothoff | ||
23 | */ | ||
24 | |||
25 | #ifndef CONNECTION_CALL_HANDLERS_H | ||
26 | #define CONNECTION_CALL_HANDLERS_H | ||
27 | |||
28 | /** | ||
29 | * Call the handlers for a connection in the appropriate order based | ||
30 | * on the readiness as detected by the event loop. | ||
31 | * | ||
32 | * @param con connection to handle | ||
33 | * @param read_ready set if the socket is ready for reading | ||
34 | * @param write_ready set if the socket is ready for writing | ||
35 | * @param force_close set if a hard error was detected on the socket; | ||
36 | * if this information is not available, simply pass #MHD_NO | ||
37 | * @return #MHD_YES to continue normally, | ||
38 | * #MHD_NO if a serious error was encountered and the | ||
39 | * connection is to be closed. | ||
40 | */ | ||
41 | int | ||
42 | MHD_connection_call_handlers_ (struct MHD_Connection *con, | ||
43 | bool read_ready, | ||
44 | bool write_ready, | ||
45 | bool force_close) | ||
46 | MHD_NONNULL (1); | ||
47 | |||
48 | #endif | ||
diff --git a/src/lib/daemon_epoll.c b/src/lib/daemon_epoll.c index cc2b68ec..74d44523 100644 --- a/src/lib/daemon_epoll.c +++ b/src/lib/daemon_epoll.c | |||
@@ -23,11 +23,12 @@ | |||
23 | * @author Christian Grothoff | 23 | * @author Christian Grothoff |
24 | */ | 24 | */ |
25 | #include "internal.h" | 25 | #include "internal.h" |
26 | #include "connection_add.h" | ||
27 | #include "connection_call_handlers.h" | ||
28 | #include "connection_finish_forward.h" | ||
26 | #include "daemon_epoll.h" | 29 | #include "daemon_epoll.h" |
27 | #include "upgrade_process.h" | 30 | #include "upgrade_process.h" |
28 | #include "request_resume.h" | 31 | #include "request_resume.h" |
29 | #include "connection_add.h" | ||
30 | #include "connection_finish_forward.h" | ||
31 | 32 | ||
32 | #ifdef EPOLL_SUPPORT | 33 | #ifdef EPOLL_SUPPORT |
33 | 34 | ||
@@ -454,10 +455,10 @@ MHD_daemon_epoll_ (struct MHD_Daemon *daemon, | |||
454 | while (NULL != (pos = prev)) | 455 | while (NULL != (pos = prev)) |
455 | { | 456 | { |
456 | prev = pos->prevE; | 457 | prev = pos->prevE; |
457 | call_handlers (pos, | 458 | MHD_connection_call_handlers_ (pos, |
458 | 0 != (pos->epoll_state & MHD_EPOLL_STATE_READ_READY), | 459 | 0 != (pos->epoll_state & MHD_EPOLL_STATE_READ_READY), |
459 | 0 != (pos->epoll_state & MHD_EPOLL_STATE_WRITE_READY), | 460 | 0 != (pos->epoll_state & MHD_EPOLL_STATE_WRITE_READY), |
460 | 0 != (pos->epoll_state & MHD_EPOLL_STATE_ERROR)); | 461 | 0 != (pos->epoll_state & MHD_EPOLL_STATE_ERROR)); |
461 | if (MHD_EPOLL_STATE_IN_EREADY_EDLL == | 462 | if (MHD_EPOLL_STATE_IN_EREADY_EDLL == |
462 | (pos->epoll_state & (MHD_EPOLL_STATE_SUSPENDED | MHD_EPOLL_STATE_IN_EREADY_EDLL))) | 463 | (pos->epoll_state & (MHD_EPOLL_STATE_SUSPENDED | MHD_EPOLL_STATE_IN_EREADY_EDLL))) |
463 | { | 464 | { |
diff --git a/src/lib/daemon_poll.c b/src/lib/daemon_poll.c index 39d22ccf..28256f1f 100644 --- a/src/lib/daemon_poll.c +++ b/src/lib/daemon_poll.c | |||
@@ -22,11 +22,12 @@ | |||
22 | * @author Christian Grothoff | 22 | * @author Christian Grothoff |
23 | */ | 23 | */ |
24 | #include "internal.h" | 24 | #include "internal.h" |
25 | #include "connection_add.h" | ||
26 | #include "connection_call_handlers.h" | ||
27 | #include "connection_finish_forward.h" | ||
25 | #include "daemon_poll.h" | 28 | #include "daemon_poll.h" |
26 | #include "upgrade_process.h" | 29 | #include "upgrade_process.h" |
27 | #include "request_resume.h" | 30 | #include "request_resume.h" |
28 | #include "connection_add.h" | ||
29 | #include "connection_finish_forward.h" | ||
30 | 31 | ||
31 | 32 | ||
32 | #ifdef HAVE_POLL | 33 | #ifdef HAVE_POLL |
@@ -294,10 +295,10 @@ MHD_daemon_poll_all_ (struct MHD_Daemon *daemon, | |||
294 | break; /* connection list changed somehow, retry later ... */ | 295 | break; /* connection list changed somehow, retry later ... */ |
295 | if (p[poll_server+i].fd != pos->socket_fd) | 296 | if (p[poll_server+i].fd != pos->socket_fd) |
296 | continue; /* fd mismatch, something else happened, retry later ... */ | 297 | continue; /* fd mismatch, something else happened, retry later ... */ |
297 | call_handlers (pos, | 298 | MHD_connection_call_handlers_ (pos, |
298 | 0 != (p[poll_server+i].revents & POLLIN), | 299 | 0 != (p[poll_server+i].revents & POLLIN), |
299 | 0 != (p[poll_server+i].revents & POLLOUT), | 300 | 0 != (p[poll_server+i].revents & POLLOUT), |
300 | 0 != (p[poll_server+i].revents & MHD_POLL_REVENTS_ERR_DISC)); | 301 | 0 != (p[poll_server+i].revents & MHD_POLL_REVENTS_ERR_DISC)); |
301 | i++; | 302 | i++; |
302 | } | 303 | } |
303 | #if defined(HTTPS_SUPPORT) && defined(UPGRADE_SUPPORT) | 304 | #if defined(HTTPS_SUPPORT) && defined(UPGRADE_SUPPORT) |
diff --git a/src/lib/daemon_select.c b/src/lib/daemon_select.c index 3c0360ba..d2740b7e 100644 --- a/src/lib/daemon_select.c +++ b/src/lib/daemon_select.c | |||
@@ -24,6 +24,7 @@ | |||
24 | */ | 24 | */ |
25 | #include "internal.h" | 25 | #include "internal.h" |
26 | #include "connection_add.h" | 26 | #include "connection_add.h" |
27 | #include "connection_call_handlers.h" | ||
27 | #include "connection_cleanup.h" | 28 | #include "connection_cleanup.h" |
28 | #include "connection_finish_forward.h" | 29 | #include "connection_finish_forward.h" |
29 | #include "daemon_select.h" | 30 | #include "daemon_select.h" |
@@ -444,13 +445,13 @@ internal_run_from_select (struct MHD_Daemon *daemon, | |||
444 | ds = pos->socket_fd; | 445 | ds = pos->socket_fd; |
445 | if (MHD_INVALID_SOCKET == ds) | 446 | if (MHD_INVALID_SOCKET == ds) |
446 | continue; | 447 | continue; |
447 | call_handlers (pos, | 448 | MHD_connection_call_handlers_ (pos, |
448 | FD_ISSET (ds, | 449 | FD_ISSET (ds, |
449 | read_fd_set), | 450 | read_fd_set), |
450 | FD_ISSET (ds, | 451 | FD_ISSET (ds, |
451 | write_fd_set), | 452 | write_fd_set), |
452 | FD_ISSET (ds, | 453 | FD_ISSET (ds, |
453 | except_fd_set)); | 454 | except_fd_set)); |
454 | } | 455 | } |
455 | } | 456 | } |
456 | 457 | ||