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"
28 #include "scoreboard.h"
29 #include "http_vhost.h"
32 simple_io_timeout_cb(simple_core_t *sc,
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;
42 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, ap_server_conf,
43 "io timeout hit (?)");
47 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;
70 if (cs->state == CONN_STATE_READ_REQUEST_LINE) {
72 ap_run_process_connection(c);
73 /* state will be updated upon return
74 * fall thru to either wait for readability/timeout or
79 cs->state = CONN_STATE_LINGER;
83 if (cs->state == CONN_STATE_WRITE_COMPLETION) {
84 ap_filter_t *output_filter = c->output_filters;
85 while (output_filter->next != NULL) {
86 output_filter = output_filter->next;
89 rv = output_filter->frec->filter_func.out_func(output_filter, NULL);
91 if (rv != APR_SUCCESS) {
92 ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf,
93 "network write failure in core output filter");
94 cs->state = CONN_STATE_LINGER;
96 else if (c->data_in_output_filters) {
97 /* Still in WRITE_COMPLETION_STATE:
98 * Set a write timeout for this connection, and let the
99 * event thread poll for writeability.
102 simple_register_timer(scon->sc,
103 simple_io_timeout_cb,
105 scon->c->base_server != NULL ? scon->c->base_server->timeout : ap_server_conf->timeout);
107 cs->pfd.reqevents = APR_POLLOUT | APR_POLLHUP | APR_POLLERR;
109 rv = apr_pollcb_add(sc->pollcb, &cs->pfd);
111 if (rv != APR_SUCCESS) {
112 ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf,
113 "apr_pollcb_add: failed in write completion");
114 AP_DEBUG_ASSERT(rv == APR_SUCCESS);
118 else if (c->keepalive != AP_CONN_KEEPALIVE || c->aborted) {
119 c->cs->state = CONN_STATE_LINGER;
121 else if (c->data_in_input_filters) {
122 cs->state = CONN_STATE_READ_REQUEST_LINE;
125 cs->state = CONN_STATE_CHECK_REQUEST_LINE_READABLE;
129 if (cs->state == CONN_STATE_LINGER) {
130 ap_lingering_close(c);
131 apr_pool_destroy(scon->pool);
135 if (cs->state == CONN_STATE_CHECK_REQUEST_LINE_READABLE) {
136 simple_register_timer(scon->sc,
137 simple_io_timeout_cb,
139 scon->c->base_server != NULL ? scon->c->base_server->timeout : ap_server_conf->timeout);
141 cs->pfd.reqevents = APR_POLLIN;
143 rv = apr_pollcb_add(sc->pollcb, &cs->pfd);
146 ap_log_error(APLOG_MARK, APLOG_ERR, rv, ap_server_conf,
147 "process_socket: apr_pollset_add failure in read request line");
148 AP_DEBUG_ASSERT(rv == APR_SUCCESS);
155 ap_lingering_close(c);
156 apr_pool_destroy(scon->pool);
161 simple_io_invoke(apr_thread_t* thread, void *baton)
163 simple_sb_t *sb = (simple_sb_t *)baton;
164 simple_conn_t *scon = (simple_conn_t *)sb->baton;
167 rv = simple_io_process(scon);
170 ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, ap_server_conf,
171 "simple_io_invoke: simple_io_process failed (?)");
178 simple_io_setup_conn(apr_thread_t* thread, void *baton)
185 simple_conn_t *scon = (simple_conn_t *)baton;
187 /* pqXXXXX: remove this. */
188 ap_create_sb_handle(&sbh, scon->pool, 0, 0);
190 scon->ba = apr_bucket_alloc_create(scon->pool);
192 scon->c = ap_run_create_connection(scon->pool, ap_server_conf, scon->sock,
193 conn_id, sbh, scon->ba);
195 scon->c->cs = apr_pcalloc(scon->pool, sizeof(conn_state_t));
197 sb = apr_pcalloc(scon->pool, sizeof(simple_sb_t));
199 cs->pfd.p = scon->pool;
200 cs->pfd.desc_type = APR_POLL_SOCKET;
201 cs->pfd.desc.s = scon->sock;
202 cs->pfd.reqevents = APR_POLLIN;
204 sb->type = SIMPLE_PT_CORE_IO;
206 cs->pfd.client_data = sb;
208 ap_update_vhost_given_ip(scon->c);
210 rv = ap_run_pre_connection(scon->c, scon->sock);
211 if (rv != OK && rv != DONE) {
212 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf,
213 "simple_io_setup_conn: connection aborted");
214 scon->c->aborted = 1;
217 scon->c->cs->state = CONN_STATE_READ_REQUEST_LINE;
219 rv = simple_io_process(scon);
222 ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, ap_server_conf,
223 "simple_io_setup_conn: simple_io_process failed (?)");
230 simple_io_accept(simple_core_t *sc, simple_sb_t *sb)
234 apr_socket_t *socket;
235 ap_listen_rec *lr = (ap_listen_rec *)sb->baton;
237 /* pqXXXXXX: Consider doing pool recycling like the event/worker MPMs do. */
238 apr_pool_create(&ptrans, NULL);
240 apr_pool_tag(ptrans, "transaction");
242 rv = apr_socket_accept(&socket, lr->sd, ptrans);
244 /* pqXXXXXX: unixd.c has _tons_ of custom handling on return values
245 * from accept, but it seems really crazy, it either worked, or didn't,
246 * but taking this approach of swallowing the error it is possible we have a
247 * fatal error on our listening socket, but we don't notice.
249 * Need to discuss this on dev@
251 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
252 "simple_io_accept: apr_socket_accept failed");
256 simple_conn_t *scon = apr_pcalloc(ptrans, sizeof(simple_conn_t));
261 return apr_thread_pool_push(sc->workers,
262 simple_io_setup_conn,
264 APR_THREAD_TASK_PRIORITY_NORMAL,
272 simple_io_event_process(simple_core_t *sc, simple_sb_t *sb)
274 /* pqXXXXX: In theory, if we have non-blocking operations on the connection
275 * we can do them here, before pushing to another thread, thats just
276 * not implemented right now.
278 return apr_thread_pool_push(sc->workers,
281 APR_THREAD_TASK_PRIORITY_NORMAL,