connection.c revision ec020ca384efb30d501a5af796ddc3bb237d7b8f
0797faae937515a5225a36db4a1ec79480d2555cjorton/* ====================================================================
0797faae937515a5225a36db4a1ec79480d2555cjorton * The Apache Software License, Version 1.1
0797faae937515a5225a36db4a1ec79480d2555cjorton * Copyright (c) 2000-2001 The Apache Software Foundation. All rights
0797faae937515a5225a36db4a1ec79480d2555cjorton * reserved.
0797faae937515a5225a36db4a1ec79480d2555cjorton * Redistribution and use in source and binary forms, with or without
0797faae937515a5225a36db4a1ec79480d2555cjorton * modification, are permitted provided that the following conditions
0797faae937515a5225a36db4a1ec79480d2555cjorton * 1. Redistributions of source code must retain the above copyright
0797faae937515a5225a36db4a1ec79480d2555cjorton * notice, this list of conditions and the following disclaimer.
0797faae937515a5225a36db4a1ec79480d2555cjorton * 2. Redistributions in binary form must reproduce the above copyright
0797faae937515a5225a36db4a1ec79480d2555cjorton * notice, this list of conditions and the following disclaimer in
0797faae937515a5225a36db4a1ec79480d2555cjorton * the documentation and/or other materials provided with the
0797faae937515a5225a36db4a1ec79480d2555cjorton * distribution.
0797faae937515a5225a36db4a1ec79480d2555cjorton * 3. The end-user documentation included with the redistribution,
0797faae937515a5225a36db4a1ec79480d2555cjorton * if any, must include the following acknowledgment:
0797faae937515a5225a36db4a1ec79480d2555cjorton * "This product includes software developed by the
0797faae937515a5225a36db4a1ec79480d2555cjorton * Apache Software Foundation (http://www.apache.org/)."
0797faae937515a5225a36db4a1ec79480d2555cjorton * Alternately, this acknowledgment may appear in the software itself,
0797faae937515a5225a36db4a1ec79480d2555cjorton * if and wherever such third-party acknowledgments normally appear.
0797faae937515a5225a36db4a1ec79480d2555cjorton * 4. The names "Apache" and "Apache Software Foundation" must
0797faae937515a5225a36db4a1ec79480d2555cjorton * not be used to endorse or promote products derived from this
0797faae937515a5225a36db4a1ec79480d2555cjorton * software without prior written permission. For written
0797faae937515a5225a36db4a1ec79480d2555cjorton * permission, please contact apache@apache.org.
0797faae937515a5225a36db4a1ec79480d2555cjorton * 5. Products derived from this software may not be called "Apache",
0797faae937515a5225a36db4a1ec79480d2555cjorton * nor may "Apache" appear in their name, without prior written
0797faae937515a5225a36db4a1ec79480d2555cjorton * permission of the Apache Software Foundation.
0797faae937515a5225a36db4a1ec79480d2555cjorton * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
0797faae937515a5225a36db4a1ec79480d2555cjorton * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
dca6643570b2f028bc7af76329bd7e333bf7493cpquerna * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
dca6643570b2f028bc7af76329bd7e333bf7493cpquerna * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
dca6643570b2f028bc7af76329bd7e333bf7493cpquerna * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
dca6643570b2f028bc7af76329bd7e333bf7493cpquerna * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0797faae937515a5225a36db4a1ec79480d2555cjorton * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
0797faae937515a5225a36db4a1ec79480d2555cjorton * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
2685f3814b77577ef7b2523442dab1ca88df1e41jorton * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
0797faae937515a5225a36db4a1ec79480d2555cjorton * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
0797faae937515a5225a36db4a1ec79480d2555cjorton * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
0797faae937515a5225a36db4a1ec79480d2555cjorton * SUCH DAMAGE.
0797faae937515a5225a36db4a1ec79480d2555cjorton * ====================================================================
0797faae937515a5225a36db4a1ec79480d2555cjorton * This software consists of voluntary contributions made by many
0797faae937515a5225a36db4a1ec79480d2555cjorton * individuals on behalf of the Apache Software Foundation. For more
0797faae937515a5225a36db4a1ec79480d2555cjorton * information on the Apache Software Foundation, please see
0797faae937515a5225a36db4a1ec79480d2555cjorton * Portions of this software are based upon public domain software
0797faae937515a5225a36db4a1ec79480d2555cjorton * originally written at the National Center for Supercomputing Applications,
0797faae937515a5225a36db4a1ec79480d2555cjorton * University of Illinois, Urbana-Champaign.
0797faae937515a5225a36db4a1ec79480d2555cjortonAP_IMPLEMENT_HOOK_RUN_ALL(int,pre_connection,(conn_rec *c),(c),OK,DECLINED)
0797faae937515a5225a36db4a1ec79480d2555cjortonAP_IMPLEMENT_HOOK_RUN_FIRST(int,process_connection,(conn_rec *c),(c),DECLINED)
0797faae937515a5225a36db4a1ec79480d2555cjorton * More machine-dependent networking gooo... on some systems,
0797faae937515a5225a36db4a1ec79480d2555cjorton * you've got to be *really* sure that all the packets are acknowledged
0797faae937515a5225a36db4a1ec79480d2555cjorton * before closing the connection, since the client will not be able
fca945cb6bed035dcc6bbced5e327bbd4d8420abjorton * to see the last response if their TCP buffer is flushed by a RST
0797faae937515a5225a36db4a1ec79480d2555cjorton * packet from us, which is what the server's TCP stack will send
0797faae937515a5225a36db4a1ec79480d2555cjorton * if it receives any request data after closing the connection.
0797faae937515a5225a36db4a1ec79480d2555cjorton * In an ideal world, this function would be accomplished by simply
0797faae937515a5225a36db4a1ec79480d2555cjorton * setting the socket option SO_LINGER and handling it within the
2685f3814b77577ef7b2523442dab1ca88df1e41jorton * server's TCP stack while the process continues on to the next request.
fca945cb6bed035dcc6bbced5e327bbd4d8420abjorton * Unfortunately, it seems that most (if not all) operating systems
fca945cb6bed035dcc6bbced5e327bbd4d8420abjorton * block the server process on close() when SO_LINGER is used.
2685f3814b77577ef7b2523442dab1ca88df1e41jorton * For those that don't, see USE_SO_LINGER below. For the rest,
0797faae937515a5225a36db4a1ec79480d2555cjorton * we have created a home-brew lingering_close.
0797faae937515a5225a36db4a1ec79480d2555cjorton * Many operating systems tend to block, puke, or otherwise mishandle
0797faae937515a5225a36db4a1ec79480d2555cjorton * calls to shutdown only half of the connection. You should define
0797faae937515a5225a36db4a1ec79480d2555cjorton * NO_LINGCLOSE in ap_config.h if such is the case for your system.
0797faae937515a5225a36db4a1ec79480d2555cjorton#define NO_LINGCLOSE /* The two lingering options are exclusive */
0797faae937515a5225a36db4a1ec79480d2555cjortonstatic void sock_enable_linger(int s)
0797faae937515a5225a36db4a1ec79480d2555cjorton ap_log_error(APLOG_MARK, APLOG_WARNING, errno, server_conf,
fca945cb6bed035dcc6bbced5e327bbd4d8420abjorton "setsockopt: (SO_LINGER)");
fca945cb6bed035dcc6bbced5e327bbd4d8420abjorton /* not a fatal error */
0797faae937515a5225a36db4a1ec79480d2555cjorton#endif /* USE_SO_LINGER */
0797faae937515a5225a36db4a1ec79480d2555cjorton/* we now proceed to read from the client until we get EOF, or until
a91a59d0b0ceed7cd5621fe8757eda5ff6a043a8pquerna * MAX_SECS_TO_LINGER has passed. the reasons for doing this are
0797faae937515a5225a36db4a1ec79480d2555cjorton * documented in a draft:
0797faae937515a5225a36db4a1ec79480d2555cjorton * http://www.ics.uci.edu/pub/ietf/http/draft-ietf-http-connection-00.txt
0797faae937515a5225a36db4a1ec79480d2555cjorton * in a nutshell -- if we don't make this effort we risk causing
0797faae937515a5225a36db4a1ec79480d2555cjorton * TCP RST packets to be sent which can tear down a connection before
0797faae937515a5225a36db4a1ec79480d2555cjorton * all the response data has been sent to the client.
0797faae937515a5225a36db4a1ec79480d2555cjorton ap_update_child_status(AP_CHILD_THREAD_FROM_ID(c->id), SERVER_CLOSING, NULL);
072f7e449a76d28b580de6e89a1723713ab9adb1jorton /* Close the connection, being careful to send out whatever is still
072f7e449a76d28b580de6e89a1723713ab9adb1jorton * in our buffers. If possible, try to avoid a hard close until the
072f7e449a76d28b580de6e89a1723713ab9adb1jorton * client has ACKed our FIN and/or has stopped sending us data.
0797faae937515a5225a36db4a1ec79480d2555cjorton /* Send any leftover data to the client, but never try to again */
0797faae937515a5225a36db4a1ec79480d2555cjorton /* Shut down the socket for write, which will send a FIN
0797faae937515a5225a36db4a1ec79480d2555cjorton * to the peer.
0797faae937515a5225a36db4a1ec79480d2555cjorton if (apr_shutdown(c->client_socket, APR_SHUTDOWN_WRITE) != APR_SUCCESS ||
0797faae937515a5225a36db4a1ec79480d2555cjorton /* Read all data from the peer until we reach "end-of-file" (FIN
0797faae937515a5225a36db4a1ec79480d2555cjorton * from peer) or we've exceeded our overall timeout. If the client does
0797faae937515a5225a36db4a1ec79480d2555cjorton * not send us bytes within 2 seconds (a value pulled from Apache 1.3
0797faae937515a5225a36db4a1ec79480d2555cjorton * which seems to work well), close the connection.
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_setsocketopt(c->client_socket, APR_SO_TIMEOUT, timeout);
0797faae937515a5225a36db4a1ec79480d2555cjortonAP_CORE_DECLARE(void) ap_process_connection(conn_rec *c)
0797faae937515a5225a36db4a1ec79480d2555cjorton/* Clearly some of this stuff doesn't belong in a generalised connection
0797faae937515a5225a36db4a1ec79480d2555cjorton structure, but for now...
0797faae937515a5225a36db4a1ec79480d2555cjortonAP_CORE_DECLARE(conn_rec *)ap_new_connection(apr_pool_t *p, server_rec *server,
0797faae937515a5225a36db4a1ec79480d2555cjorton conn_rec *conn = (conn_rec *) apr_pcalloc(p, sizeof(conn_rec));
0797faae937515a5225a36db4a1ec79480d2555cjorton (void) ap_update_child_status(AP_CHILD_THREAD_FROM_ID(id),
0797faae937515a5225a36db4a1ec79480d2555cjorton /* Got a connection structure, so initialize what fields we can
0797faae937515a5225a36db4a1ec79480d2555cjorton * (the rest are zeroed out by pcalloc).
072f7e449a76d28b580de6e89a1723713ab9adb1jorton if ((rv = apr_socket_addr_get(&conn->local_addr, APR_LOCAL, inout))
0797faae937515a5225a36db4a1ec79480d2555cjorton "apr_socket_addr_get(APR_LOCAL)");
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_sockaddr_ip_get(&conn->local_ip, conn->local_addr);
0797faae937515a5225a36db4a1ec79480d2555cjorton if ((rv = apr_socket_addr_get(&conn->remote_addr, APR_REMOTE, inout))
0797faae937515a5225a36db4a1ec79480d2555cjorton "apr_socket_addr_get(APR_REMOTE)");