connection.c revision ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dc
57221209d11b05aa0373cc3892d5df89ba96ebf9Christian Maeder/* Copyright 2000-2004 Apache Software Foundation
53bd0c89aa4743dc41a6394db5a90717c1ca4517Liam O'Reilly *
53bd0c89aa4743dc41a6394db5a90717c1ca4517Liam O'Reilly * Licensed under the Apache License, Version 2.0 (the "License");
53bd0c89aa4743dc41a6394db5a90717c1ca4517Liam O'Reilly * you may not use this file except in compliance with the License.
53bd0c89aa4743dc41a6394db5a90717c1ca4517Liam O'Reilly * You may obtain a copy of the License at
98890889ffb2e8f6f722b00e265a211f13b5a861Corneliu-Claudiu Prodescu *
53bd0c89aa4743dc41a6394db5a90717c1ca4517Liam O'Reilly * http://www.apache.org/licenses/LICENSE-2.0
53bd0c89aa4743dc41a6394db5a90717c1ca4517Liam O'Reilly *
53bd0c89aa4743dc41a6394db5a90717c1ca4517Liam O'Reilly * Unless required by applicable law or agreed to in writing, software
53bd0c89aa4743dc41a6394db5a90717c1ca4517Liam O'Reilly * distributed under the License is distributed on an "AS IS" BASIS,
53bd0c89aa4743dc41a6394db5a90717c1ca4517Liam O'Reilly * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly * See the License for the specific language governing permissions and
53bd0c89aa4743dc41a6394db5a90717c1ca4517Liam O'Reilly * limitations under the License.
53bd0c89aa4743dc41a6394db5a90717c1ca4517Liam O'Reilly */
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly#include "apr.h"
57221209d11b05aa0373cc3892d5df89ba96ebf9Christian Maeder#include "apr_strings.h"
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly
56899f6457976a2ee20f6a23f088cb5655b15715Liam O'Reilly#define CORE_PRIVATE
7d41a06add784ca68e6507ce832e621a1d2abf49Christian Maeder#include "ap_config.h"
43e4005c54870e481df3a9be0f041caefe0c6eb5Christian Maeder#include "httpd.h"
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly#include "http_connection.h"
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly#include "http_request.h"
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly#include "http_protocol.h"
ace03c3051e5c5144e43ae78cae73f6a29dde6d5Christian Maeder#include "ap_mpm.h"
c0833539c8cf577dd3f2497792fbdd818442744cChristian Maeder#include "mpm_default.h"
036ecbd8f721096321f47cf6a354a9d1bf3d032fChristian Maeder#include "http_config.h"
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly#include "http_core.h"
53bd0c89aa4743dc41a6394db5a90717c1ca4517Liam O'Reilly#include "http_vhost.h"
57221209d11b05aa0373cc3892d5df89ba96ebf9Christian Maeder#include "scoreboard.h"
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly#include "http_log.h"
c0833539c8cf577dd3f2497792fbdd818442744cChristian Maeder#include "util_filter.h"
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly
33bdce26495121cdbce30331ef90a1969126a840Liam O'ReillyAPR_HOOK_STRUCT(
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly APR_HOOK_LINK(create_connection)
036ecbd8f721096321f47cf6a354a9d1bf3d032fChristian Maeder APR_HOOK_LINK(process_connection)
036ecbd8f721096321f47cf6a354a9d1bf3d032fChristian Maeder APR_HOOK_LINK(pre_connection)
8e9c3881fb6e710b1e08bf5ac8ff9d393df2e74eChristian Maeder)
33bdce26495121cdbce30331ef90a1969126a840Liam O'ReillyAP_IMPLEMENT_HOOK_RUN_FIRST(conn_rec *,create_connection,
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly (apr_pool_t *p, server_rec *server, apr_socket_t *csd, long conn_id, void *sbh, apr_bucket_alloc_t *alloc),
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly (p, server, csd, conn_id, sbh, alloc), NULL)
33bdce26495121cdbce30331ef90a1969126a840Liam O'ReillyAP_IMPLEMENT_HOOK_RUN_FIRST(int,process_connection,(conn_rec *c),(c),DECLINED)
53bd0c89aa4743dc41a6394db5a90717c1ca4517Liam O'ReillyAP_IMPLEMENT_HOOK_RUN_ALL(int,pre_connection,(conn_rec *c, void *csd),(c, csd),OK,DECLINED)
caf021dd48c90ff6b26117f13e1d8c0ef1ca618aChristian Maeder/*
53bd0c89aa4743dc41a6394db5a90717c1ca4517Liam O'Reilly * More machine-dependent networking gooo... on some systems,
53bd0c89aa4743dc41a6394db5a90717c1ca4517Liam O'Reilly * you've got to be *really* sure that all the packets are acknowledged
53bd0c89aa4743dc41a6394db5a90717c1ca4517Liam O'Reilly * before closing the connection, since the client will not be able
53bd0c89aa4743dc41a6394db5a90717c1ca4517Liam O'Reilly * to see the last response if their TCP buffer is flushed by a RST
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly * packet from us, which is what the server's TCP stack will send
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly * if it receives any request data after closing the connection.
4770d29adda064baa5dbfcca03f600d7608806f7Liam O'Reilly *
caf021dd48c90ff6b26117f13e1d8c0ef1ca618aChristian Maeder * In an ideal world, this function would be accomplished by simply
4770d29adda064baa5dbfcca03f600d7608806f7Liam O'Reilly * setting the socket option SO_LINGER and handling it within the
4770d29adda064baa5dbfcca03f600d7608806f7Liam O'Reilly * server's TCP stack while the process continues on to the next request.
caf021dd48c90ff6b26117f13e1d8c0ef1ca618aChristian Maeder * Unfortunately, it seems that most (if not all) operating systems
4770d29adda064baa5dbfcca03f600d7608806f7Liam O'Reilly * block the server process on close() when SO_LINGER is used.
53bd0c89aa4743dc41a6394db5a90717c1ca4517Liam O'Reilly * For those that don't, see USE_SO_LINGER below. For the rest,
ace03c3051e5c5144e43ae78cae73f6a29dde6d5Christian Maeder * we have created a home-brew lingering_close.
ace03c3051e5c5144e43ae78cae73f6a29dde6d5Christian Maeder *
ace03c3051e5c5144e43ae78cae73f6a29dde6d5Christian Maeder * Many operating systems tend to block, puke, or otherwise mishandle
caf021dd48c90ff6b26117f13e1d8c0ef1ca618aChristian Maeder * calls to shutdown only half of the connection. You should define
53bd0c89aa4743dc41a6394db5a90717c1ca4517Liam O'Reilly * NO_LINGCLOSE in ap_config.h if such is the case for your system.
4770d29adda064baa5dbfcca03f600d7608806f7Liam O'Reilly */
53bd0c89aa4743dc41a6394db5a90717c1ca4517Liam O'Reilly#ifndef MAX_SECS_TO_LINGER
4770d29adda064baa5dbfcca03f600d7608806f7Liam O'Reilly#define MAX_SECS_TO_LINGER 30
ace03c3051e5c5144e43ae78cae73f6a29dde6d5Christian Maeder#endif
4770d29adda064baa5dbfcca03f600d7608806f7Liam O'Reilly
33bdce26495121cdbce30331ef90a1969126a840Liam O'ReillyAP_CORE_DECLARE(void) ap_flush_conn(conn_rec *c)
4770d29adda064baa5dbfcca03f600d7608806f7Liam O'Reilly{
4770d29adda064baa5dbfcca03f600d7608806f7Liam O'Reilly apr_bucket_brigade *bb;
4770d29adda064baa5dbfcca03f600d7608806f7Liam O'Reilly apr_bucket *b;
4770d29adda064baa5dbfcca03f600d7608806f7Liam O'Reilly
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly bb = apr_brigade_create(c->pool, c->bucket_alloc);
4770d29adda064baa5dbfcca03f600d7608806f7Liam O'Reilly b = apr_bucket_flush_create(c->bucket_alloc);
4770d29adda064baa5dbfcca03f600d7608806f7Liam O'Reilly APR_BRIGADE_INSERT_TAIL(bb, b);
4770d29adda064baa5dbfcca03f600d7608806f7Liam O'Reilly ap_pass_brigade(c->output_filters, bb);
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly}
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly/* we now proceed to read from the client until we get EOF, or until
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly * MAX_SECS_TO_LINGER has passed. the reasons for doing this are
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly * documented in a draft:
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly *
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly * http://www.ics.uci.edu/pub/ietf/http/draft-ietf-http-connection-00.txt
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly *
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly * in a nutshell -- if we don't make this effort we risk causing
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly * TCP RST packets to be sent which can tear down a connection before
3c8d067accf18572352351ec42ff905c7297a8a5Christian Maeder * all the response data has been sent to the client.
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly */
3c8d067accf18572352351ec42ff905c7297a8a5Christian Maeder#define SECONDS_TO_LINGER 2
3c8d067accf18572352351ec42ff905c7297a8a5Christian MaederAP_DECLARE(void) ap_lingering_close(conn_rec *c)
3c8d067accf18572352351ec42ff905c7297a8a5Christian Maeder{
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly char dummybuf[512];
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly apr_size_t nbytes = sizeof(dummybuf);
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly apr_status_t rc;
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly apr_int32_t timeout;
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly apr_int32_t total_linger_time = 0;
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly apr_socket_t *csd = ap_get_module_config(c->conn_config, &core_module);
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly if (!csd) {
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly return;
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly }
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly ap_update_child_status(c->sbh, SERVER_CLOSING, NULL);
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly#ifdef NO_LINGCLOSE
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly ap_flush_conn(c); /* just close it */
57221209d11b05aa0373cc3892d5df89ba96ebf9Christian Maeder apr_socket_close(csd);
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly return;
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly#endif
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly /* Close the connection, being careful to send out whatever is still
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly * in our buffers. If possible, try to avoid a hard close until the
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly * client has ACKed our FIN and/or has stopped sending us data.
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly */
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly /* Send any leftover data to the client, but never try to again */
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly ap_flush_conn(c);
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly if (c->aborted) {
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly apr_socket_close(csd);
56899f6457976a2ee20f6a23f088cb5655b15715Liam O'Reilly return;
57221209d11b05aa0373cc3892d5df89ba96ebf9Christian Maeder }
56899f6457976a2ee20f6a23f088cb5655b15715Liam O'Reilly
56899f6457976a2ee20f6a23f088cb5655b15715Liam O'Reilly /* Shut down the socket for write, which will send a FIN
56899f6457976a2ee20f6a23f088cb5655b15715Liam O'Reilly * to the peer.
56899f6457976a2ee20f6a23f088cb5655b15715Liam O'Reilly */
56899f6457976a2ee20f6a23f088cb5655b15715Liam O'Reilly if (apr_socket_shutdown(csd, APR_SHUTDOWN_WRITE) != APR_SUCCESS
56899f6457976a2ee20f6a23f088cb5655b15715Liam O'Reilly || c->aborted) {
57221209d11b05aa0373cc3892d5df89ba96ebf9Christian Maeder apr_socket_close(csd);
57221209d11b05aa0373cc3892d5df89ba96ebf9Christian Maeder return;
57221209d11b05aa0373cc3892d5df89ba96ebf9Christian Maeder }
57221209d11b05aa0373cc3892d5df89ba96ebf9Christian Maeder
57221209d11b05aa0373cc3892d5df89ba96ebf9Christian Maeder /* Read all data from the peer until we reach "end-of-file" (FIN
57221209d11b05aa0373cc3892d5df89ba96ebf9Christian Maeder * from peer) or we've exceeded our overall timeout. If the client does
57221209d11b05aa0373cc3892d5df89ba96ebf9Christian Maeder * not send us bytes within 2 seconds (a value pulled from Apache 1.3
57221209d11b05aa0373cc3892d5df89ba96ebf9Christian Maeder * which seems to work well), close the connection.
57221209d11b05aa0373cc3892d5df89ba96ebf9Christian Maeder */
57221209d11b05aa0373cc3892d5df89ba96ebf9Christian Maeder timeout = apr_time_from_sec(SECONDS_TO_LINGER);
57221209d11b05aa0373cc3892d5df89ba96ebf9Christian Maeder apr_socket_timeout_set(csd, timeout);
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly apr_socket_opt_set(csd, APR_INCOMPLETE_READ, 1);
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly while (1) {
8e9c3881fb6e710b1e08bf5ac8ff9d393df2e74eChristian Maeder nbytes = sizeof(dummybuf);
8e9c3881fb6e710b1e08bf5ac8ff9d393df2e74eChristian Maeder rc = apr_socket_recv(csd, dummybuf, &nbytes);
8e9c3881fb6e710b1e08bf5ac8ff9d393df2e74eChristian Maeder if (rc != APR_SUCCESS || nbytes == 0)
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly break;
ace03c3051e5c5144e43ae78cae73f6a29dde6d5Christian Maeder
ace03c3051e5c5144e43ae78cae73f6a29dde6d5Christian Maeder total_linger_time += SECONDS_TO_LINGER;
ace03c3051e5c5144e43ae78cae73f6a29dde6d5Christian Maeder if (total_linger_time >= MAX_SECS_TO_LINGER) {
ace03c3051e5c5144e43ae78cae73f6a29dde6d5Christian Maeder break;
ace03c3051e5c5144e43ae78cae73f6a29dde6d5Christian Maeder }
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly }
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly apr_socket_close(csd);
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly return;
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly}
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly
8e9c3881fb6e710b1e08bf5ac8ff9d393df2e74eChristian MaederAP_CORE_DECLARE(void) ap_process_connection(conn_rec *c, void *csd)
8e9c3881fb6e710b1e08bf5ac8ff9d393df2e74eChristian Maeder{
43e4005c54870e481df3a9be0f041caefe0c6eb5Christian Maeder int rc;
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly ap_update_vhost_given_ip(c);
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly rc = ap_run_pre_connection(c, csd);
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly if (rc != OK && rc != DONE) {
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly c->aborted = 1;
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly }
57221209d11b05aa0373cc3892d5df89ba96ebf9Christian Maeder
57221209d11b05aa0373cc3892d5df89ba96ebf9Christian Maeder if (!c->aborted) {
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly ap_run_process_connection(c);
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly }
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly}
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly
33bdce26495121cdbce30331ef90a1969126a840Liam O'Reilly