1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "ap_listen.h"
20 #include "simple_types.h"
21 #include "simple_io.h"
22 #include "simple_event.h"
24 #include "http_connection.h"
25 #include "util_filter.h"
26 #include "http_main.h"
27 #include "scoreboard.h"
28 #include "http_vhost.h"
30 APLOG_USE_MODULE(mpm_simple);
32 static void simple_io_timeout_cb(simple_core_t * sc, void *baton)
35 simple_conn_t *scon = (simple_conn_t *) baton;
36 /* pqXXXXX: handle timeouts. */
37 conn_rec *c = scon->c;
38 conn_state_t *cs = c->cs;
43 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, ap_server_conf,
44 "io timeout hit (?)");
47 static apr_status_t simple_io_process(simple_conn_t * scon)
54 if (scon->c->clogging_input_filters && !scon->c->aborted) {
55 /* Since we have an input filter which 'cloggs' the input stream,
56 * like mod_ssl, lets just do the normal read from input filters,
57 * like the Worker MPM does.
59 ap_run_process_connection(scon->c);
60 if (scon->c->cs->state != CONN_STATE_SUSPENDED) {
61 scon->c->cs->state = CONN_STATE_LINGER;
71 if (cs->pfd.reqevents != 0) {
72 rv = apr_pollcb_remove(sc->pollcb, &cs->pfd);
74 ap_log_error(APLOG_MARK, APLOG_ERR, rv, ap_server_conf,
75 "simple_io_process: apr_pollcb_remove failure");
76 /*AP_DEBUG_ASSERT(rv == APR_SUCCESS);*/
78 cs->pfd.reqevents = 0;
81 if (cs->state == CONN_STATE_READ_REQUEST_LINE) {
83 ap_run_process_connection(c);
84 /* state will be updated upon return
85 * fall thru to either wait for readability/timeout or
90 cs->state = CONN_STATE_LINGER;
94 if (cs->state == CONN_STATE_WRITE_COMPLETION) {
95 ap_filter_t *output_filter = c->output_filters;
96 while (output_filter->next != NULL) {
97 output_filter = output_filter->next;
100 rv = output_filter->frec->filter_func.out_func(output_filter,
103 if (rv != APR_SUCCESS) {
104 ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf,
105 "network write failure in core output filter");
106 cs->state = CONN_STATE_LINGER;
108 else if (c->data_in_output_filters) {
109 /* Still in WRITE_COMPLETION_STATE:
110 * Set a write timeout for this connection, and let the
111 * event thread poll for writeability.
114 simple_register_timer(scon->sc,
115 simple_io_timeout_cb,
117 scon->c->base_server !=
118 NULL ? scon->c->base_server->
119 timeout : ap_server_conf->timeout,
122 cs->pfd.reqevents = APR_POLLOUT | APR_POLLHUP | APR_POLLERR;
124 rv = apr_pollcb_add(sc->pollcb, &cs->pfd);
126 if (rv != APR_SUCCESS) {
127 ap_log_error(APLOG_MARK, APLOG_WARNING, rv,
129 "apr_pollcb_add: failed in write completion");
130 AP_DEBUG_ASSERT(rv == APR_SUCCESS);
134 else if (c->keepalive != AP_CONN_KEEPALIVE || c->aborted) {
135 c->cs->state = CONN_STATE_LINGER;
137 else if (c->data_in_input_filters) {
138 cs->state = CONN_STATE_READ_REQUEST_LINE;
141 cs->state = CONN_STATE_CHECK_REQUEST_LINE_READABLE;
145 if (cs->state == CONN_STATE_LINGER) {
146 ap_lingering_close(c);
147 apr_pool_destroy(scon->pool);
151 if (cs->state == CONN_STATE_CHECK_REQUEST_LINE_READABLE) {
152 simple_register_timer(scon->sc,
153 simple_io_timeout_cb,
155 scon->c->base_server !=
156 NULL ? scon->c->base_server->
157 timeout : ap_server_conf->timeout,
160 cs->pfd.reqevents = APR_POLLIN;
162 rv = apr_pollcb_add(sc->pollcb, &cs->pfd);
165 ap_log_error(APLOG_MARK, APLOG_ERR, rv, ap_server_conf,
166 "process_socket: apr_pollcb_add failure in read request line");
167 AP_DEBUG_ASSERT(rv == APR_SUCCESS);
174 ap_lingering_close(c);
175 apr_pool_destroy(scon->pool);
179 static void *simple_io_invoke(apr_thread_t * thread, void *baton)
181 simple_sb_t *sb = (simple_sb_t *) baton;
182 simple_conn_t *scon = (simple_conn_t *) sb->baton;
185 scon->c->current_thread = thread;
187 rv = simple_io_process(scon);
190 ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, ap_server_conf,
191 "simple_io_invoke: simple_io_process failed (?)");
197 static void *simple_io_setup_conn(apr_thread_t * thread, void *baton)
204 simple_conn_t *scon = (simple_conn_t *) baton;
206 /* pqXXXXX: remove this. */
207 ap_create_sb_handle(&sbh, scon->pool, 0, 0);
209 scon->ba = apr_bucket_alloc_create(scon->pool);
211 scon->c = ap_run_create_connection(scon->pool, ap_server_conf, scon->sock,
212 conn_id, sbh, scon->ba);
213 /* XXX: handle failure */
215 scon->c->cs = apr_pcalloc(scon->pool, sizeof(conn_state_t));
217 sb = apr_pcalloc(scon->pool, sizeof(simple_sb_t));
219 scon->c->current_thread = thread;
221 cs->pfd.p = scon->pool;
222 cs->pfd.desc_type = APR_POLL_SOCKET;
223 cs->pfd.desc.s = scon->sock;
224 cs->pfd.reqevents = APR_POLLIN;
226 sb->type = SIMPLE_PT_CORE_IO;
228 cs->pfd.client_data = sb;
230 ap_update_vhost_given_ip(scon->c);
232 rv = ap_run_pre_connection(scon->c, scon->sock);
233 if (rv != OK && rv != DONE) {
234 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf,
235 "simple_io_setup_conn: connection aborted");
236 scon->c->aborted = 1;
239 scon->c->cs->state = CONN_STATE_READ_REQUEST_LINE;
241 rv = simple_io_process(scon);
244 ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, ap_server_conf,
245 "simple_io_setup_conn: simple_io_process failed (?)");
251 apr_status_t simple_io_accept(simple_core_t * sc, simple_sb_t * sb)
255 apr_socket_t *socket;
256 ap_listen_rec *lr = (ap_listen_rec *) sb->baton;
258 /* pqXXXXXX: Consider doing pool recycling like the event/worker MPMs do. */
259 apr_pool_create(&ptrans, NULL);
261 apr_pool_tag(ptrans, "transaction");
263 rv = apr_socket_accept(&socket, lr->sd, ptrans);
265 /* pqXXXXXX: unixd.c has _tons_ of custom handling on return values
266 * from accept, but it seems really crazy, it either worked, or didn't,
267 * but taking this approach of swallowing the error it is possible we have a
268 * fatal error on our listening socket, but we don't notice.
270 * Need to discuss this on dev@
272 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
273 "simple_io_accept: apr_socket_accept failed");
277 simple_conn_t *scon = apr_pcalloc(ptrans, sizeof(simple_conn_t));
282 return apr_thread_pool_push(sc->workers,
283 simple_io_setup_conn,
285 APR_THREAD_TASK_PRIORITY_NORMAL, NULL);
291 apr_status_t simple_io_event_process(simple_core_t * sc, simple_sb_t * sb)
293 /* pqXXXXX: In theory, if we have non-blocking operations on the connection
294 * we can do them here, before pushing to another thread, thats just
295 * not implemented right now.
297 return apr_thread_pool_push(sc->workers,
299 sb, APR_THREAD_TASK_PRIORITY_NORMAL, NULL);