]> granicus.if.org Git - apache/blob - server/connection.c
finished that boring job:
[apache] / server / connection.c
1 /* ====================================================================
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2000-2003 The Apache Software Foundation.  All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  *
19  * 3. The end-user documentation included with the redistribution,
20  *    if any, must include the following acknowledgment:
21  *       "This product includes software developed by the
22  *        Apache Software Foundation (http://www.apache.org/)."
23  *    Alternately, this acknowledgment may appear in the software itself,
24  *    if and wherever such third-party acknowledgments normally appear.
25  *
26  * 4. The names "Apache" and "Apache Software Foundation" must
27  *    not be used to endorse or promote products derived from this
28  *    software without prior written permission. For written
29  *    permission, please contact apache@apache.org.
30  *
31  * 5. Products derived from this software may not be called "Apache",
32  *    nor may "Apache" appear in their name, without prior written
33  *    permission of the Apache Software Foundation.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Apache Software Foundation.  For more
51  * information on the Apache Software Foundation, please see
52  * <http://www.apache.org/>.
53  *
54  * Portions of this software are based upon public domain software
55  * originally written at the National Center for Supercomputing Applications,
56  * University of Illinois, Urbana-Champaign.
57  */
58
59 #include "apr.h"
60 #include "apr_strings.h"
61
62 #define CORE_PRIVATE
63 #include "ap_config.h"
64 #include "httpd.h"
65 #include "http_connection.h"
66 #include "http_request.h"
67 #include "http_protocol.h"
68 #include "ap_mpm.h"
69 #include "mpm_default.h"
70 #include "http_config.h"
71 #include "http_core.h"
72 #include "http_vhost.h"
73 #include "scoreboard.h"
74 #include "http_log.h"
75 #include "util_filter.h"
76
77 APR_HOOK_STRUCT(
78             APR_HOOK_LINK(create_connection)
79             APR_HOOK_LINK(process_connection)
80             APR_HOOK_LINK(pre_connection)
81 )
82 AP_IMPLEMENT_HOOK_RUN_FIRST(conn_rec *,create_connection,
83                             (apr_pool_t *p, server_rec *server, apr_socket_t *csd, long conn_id, void *sbh, apr_bucket_alloc_t *alloc),
84                             (p, server, csd, conn_id, sbh, alloc), NULL)
85 AP_IMPLEMENT_HOOK_RUN_FIRST(int,process_connection,(conn_rec *c),(c),DECLINED)
86 AP_IMPLEMENT_HOOK_RUN_ALL(int,pre_connection,(conn_rec *c, void *csd),(c, csd),OK,DECLINED)
87 /*
88  * More machine-dependent networking gooo... on some systems,
89  * you've got to be *really* sure that all the packets are acknowledged
90  * before closing the connection, since the client will not be able
91  * to see the last response if their TCP buffer is flushed by a RST
92  * packet from us, which is what the server's TCP stack will send
93  * if it receives any request data after closing the connection.
94  *
95  * In an ideal world, this function would be accomplished by simply
96  * setting the socket option SO_LINGER and handling it within the
97  * server's TCP stack while the process continues on to the next request.
98  * Unfortunately, it seems that most (if not all) operating systems
99  * block the server process on close() when SO_LINGER is used.
100  * For those that don't, see USE_SO_LINGER below.  For the rest,
101  * we have created a home-brew lingering_close.
102  *
103  * Many operating systems tend to block, puke, or otherwise mishandle
104  * calls to shutdown only half of the connection.  You should define
105  * NO_LINGCLOSE in ap_config.h if such is the case for your system.
106  */
107 #ifndef MAX_SECS_TO_LINGER
108 #define MAX_SECS_TO_LINGER 30
109 #endif
110
111 AP_CORE_DECLARE(void) ap_flush_conn(conn_rec *c)
112 {
113     apr_bucket_brigade *bb;
114     apr_bucket *b;
115
116     bb = apr_brigade_create(c->pool, c->bucket_alloc);
117     b = apr_bucket_flush_create(c->bucket_alloc);
118     APR_BRIGADE_INSERT_TAIL(bb, b);
119     ap_pass_brigade(c->output_filters, bb);
120 }
121
122 /* we now proceed to read from the client until we get EOF, or until
123  * MAX_SECS_TO_LINGER has passed.  the reasons for doing this are
124  * documented in a draft:
125  *
126  * http://www.ics.uci.edu/pub/ietf/http/draft-ietf-http-connection-00.txt
127  *
128  * in a nutshell -- if we don't make this effort we risk causing
129  * TCP RST packets to be sent which can tear down a connection before
130  * all the response data has been sent to the client.
131  */
132 #define SECONDS_TO_LINGER  2
133 AP_DECLARE(void) ap_lingering_close(conn_rec *c)
134 {
135     char dummybuf[512];
136     apr_size_t nbytes = sizeof(dummybuf);
137     apr_status_t rc;
138     apr_int32_t timeout;
139     apr_int32_t total_linger_time = 0;
140     apr_socket_t *csd = ap_get_module_config(c->conn_config, &core_module);
141
142     if (!csd) {
143         return;
144     }
145
146     ap_update_child_status(c->sbh, SERVER_CLOSING, NULL);
147
148 #ifdef NO_LINGCLOSE
149     ap_flush_conn(c); /* just close it */
150     apr_socket_close(csd);
151     return;
152 #endif
153
154     /* Close the connection, being careful to send out whatever is still
155      * in our buffers.  If possible, try to avoid a hard close until the
156      * client has ACKed our FIN and/or has stopped sending us data.
157      */
158
159     /* Send any leftover data to the client, but never try to again */
160     ap_flush_conn(c);
161
162     if (c->aborted) {
163         apr_socket_close(csd);
164         return;
165     }
166
167     /* Shut down the socket for write, which will send a FIN
168      * to the peer.
169      */
170     if (apr_shutdown(csd, APR_SHUTDOWN_WRITE) != APR_SUCCESS
171         || c->aborted) {
172         apr_socket_close(csd);
173         return;
174     }
175
176     /* Read all data from the peer until we reach "end-of-file" (FIN
177      * from peer) or we've exceeded our overall timeout. If the client does
178      * not send us bytes within 2 seconds (a value pulled from Apache 1.3
179      * which seems to work well), close the connection.
180      */
181     timeout = apr_time_from_sec(SECONDS_TO_LINGER);
182     apr_socket_timeout_set(csd, timeout);
183     apr_socket_opt_set(csd, APR_INCOMPLETE_READ, 1);
184     while (1) {
185         nbytes = sizeof(dummybuf);
186         rc = apr_recv(csd, dummybuf, &nbytes);
187         if (rc != APR_SUCCESS || nbytes == 0)
188             break;
189
190         total_linger_time += SECONDS_TO_LINGER;
191         if (total_linger_time >= MAX_SECS_TO_LINGER) {
192             break;
193         }
194     }
195
196     apr_socket_close(csd);
197     return;
198 }
199
200 AP_CORE_DECLARE(void) ap_process_connection(conn_rec *c, void *csd)
201 {
202     ap_update_vhost_given_ip(c);
203
204     ap_run_pre_connection(c, csd);
205
206     if (!c->aborted) {
207         ap_run_process_connection(c);
208     }
209 }
210