]> granicus.if.org Git - apache/blob - server/connection.c
Fix a buglet in the APR-ization of ap_lingering_close() -- use APR_SO_TIMEOUT
[apache] / server / connection.c
1 /* ====================================================================
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2000 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 #define CORE_PRIVATE
60 #include "ap_config.h"
61 #include "apr_strings.h"
62 #include "httpd.h"
63 #include "http_connection.h"
64 #include "http_request.h"
65 #include "http_protocol.h"
66 #include "ap_mpm.h"
67 #include "mpm_status.h"
68 #include "http_config.h"
69 #include "http_vhost.h"
70 #include "util_filter.h"
71
72 #ifdef HAVE_NETINET_IN_H
73 #include <netinet/in.h>
74 #endif
75 #ifdef HAVE_ARPA_INET_H
76 #include <arpa/inet.h>
77 #endif
78
79 AP_HOOK_STRUCT(
80             AP_HOOK_LINK(pre_connection)
81             AP_HOOK_LINK(process_connection)
82 )
83
84 AP_IMPLEMENT_HOOK_RUN_ALL(int,pre_connection,(conn_rec *c),(c),OK,DECLINED)
85 AP_IMPLEMENT_HOOK_RUN_FIRST(int,process_connection,(conn_rec *c),(c),DECLINED)
86
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 #ifdef USE_SO_LINGER
112 #define NO_LINGCLOSE            /* The two lingering options are exclusive */
113
114 static void sock_enable_linger(int s) 
115 {
116     struct linger li;                 
117
118     li.l_onoff = 1;
119     li.l_linger = MAX_SECS_TO_LINGER;
120
121     if (setsockopt(s, SOL_SOCKET, SO_LINGER, 
122                    (char *) &li, sizeof(struct linger)) < 0) {
123         ap_log_error(APLOG_MARK, APLOG_WARNING, errno, server_conf,
124                     "setsockopt: (SO_LINGER)");
125         /* not a fatal error */
126     }
127 }
128
129 #else
130 #define sock_enable_linger(s)   /* NOOP */
131 #endif /* USE_SO_LINGER */
132
133 AP_CORE_DECLARE(void) ap_flush_conn(conn_rec *c)
134 {
135     ap_bucket_brigade *bb;
136     ap_bucket *b;
137
138     bb = ap_brigade_create(c->pool);
139     b = ap_bucket_create_flush();
140     AP_BRIGADE_INSERT_TAIL(bb, b);
141     ap_pass_brigade(c->output_filters, bb);
142 }
143
144 /* we now proceed to read from the client until we get EOF, or until
145  * MAX_SECS_TO_LINGER has passed.  the reasons for doing this are
146  * documented in a draft:
147  *
148  * http://www.ics.uci.edu/pub/ietf/http/draft-ietf-http-connection-00.txt
149  *
150  * in a nutshell -- if we don't make this effort we risk causing
151  * TCP RST packets to be sent which can tear down a connection before
152  * all the response data has been sent to the client.
153  */
154
155 void ap_lingering_close(conn_rec *c)
156 {
157     char dummybuf[512];
158     apr_time_t start;
159     apr_ssize_t nbytes;
160     apr_status_t rc;
161     int timeout;
162
163 #ifdef NO_LINGCLOSE
164     ap_flush_conn(c);   /* just close it */
165     apr_close_socket(c->client_socket);
166     return;
167 #endif
168
169     /* Close the connection, being careful to send out whatever is still
170      * in our buffers.  If possible, try to avoid a hard close until the
171      * client has ACKed our FIN and/or has stopped sending us data.
172      */
173
174     if (c->aborted) {
175         ap_flush_conn(c);
176         apr_close_socket(c->client_socket);
177         return;
178     }
179
180     /* Send any leftover data to the client, but never try to again */
181
182     ap_flush_conn(c);
183
184     /* Shut down the socket for write, which will send a FIN
185      * to the peer.
186      */
187     
188     if (apr_shutdown(c->client_socket, APR_SHUTDOWN_WRITE) != APR_SUCCESS || 
189         c->aborted) {
190         apr_close_socket(c->client_socket);
191         return;
192     }
193
194     /* Read all data from the peer until we reach "end-of-file" (FIN
195      * from peer) or we've exceeded our overall timeout.
196      */
197     
198     start = apr_now();
199     timeout = MAX_SECS_TO_LINGER * APR_USEC_PER_SEC;
200     for (;;) {
201         apr_setsocketopt(c->client_socket, APR_SO_TIMEOUT, timeout);
202         nbytes = sizeof(dummybuf);
203         rc = apr_recv(c->client_socket, dummybuf, &nbytes);
204         if (rc != APR_SUCCESS || nbytes == 0) break;
205
206         /* how much time has elapsed? */
207         timeout = (int)((apr_now() - start) / APR_USEC_PER_SEC);
208         if (timeout >= MAX_SECS_TO_LINGER) break;
209
210         /* figure out the new timeout */
211         timeout = (MAX_SECS_TO_LINGER - timeout) * APR_USEC_PER_SEC;
212     }
213
214     apr_close_socket(c->client_socket);
215 }
216
217 AP_CORE_DECLARE(void) ap_process_connection(conn_rec *c)
218 {
219     ap_update_vhost_given_ip(c);
220
221     ap_run_pre_connection(c);
222
223     ap_run_process_connection(c);
224
225 }
226
227 int ap_pre_http_connection(conn_rec *c)
228 {
229     ap_add_input_filter("HTTP_IN", NULL, NULL, c);
230     ap_add_input_filter("CORE_IN", NULL, NULL, c);
231     ap_add_output_filter("CORE", NULL, NULL, c);
232     return OK;
233 }
234
235 int ap_process_http_connection(conn_rec *c)
236 {
237     request_rec *r;
238
239     /*
240      * Read and process each request found on our connection
241      * until no requests are left or we decide to close.
242      */
243
244     ap_update_connection_status(c->id, "Status", "Reading");
245     while ((r = ap_read_request(c)) != NULL) {
246
247         /* process the request if it was read without error */
248
249         ap_update_connection_status(c->id, "Status", "Writing");
250         if (r->status == HTTP_OK)
251             ap_process_request(r);
252
253         if (!c->keepalive || c->aborted)
254             break;
255
256         ap_update_connection_status(c->id, "Status", "Keepalive");
257         apr_destroy_pool(r->pool);
258
259         if (ap_graceful_stop_signalled())
260             break;
261     }
262
263     ap_reset_connection_status(c->id);
264     return OK;
265 }
266
267 /* Clearly some of this stuff doesn't belong in a generalised connection
268    structure, but for now...
269 */
270
271 conn_rec *ap_new_connection(apr_pool_t *p, server_rec *server, 
272                             apr_socket_t *inout,
273                             const struct sockaddr_in *remaddr,
274                             const struct sockaddr_in *saddr, long id)
275 {
276     conn_rec *conn = (conn_rec *) apr_pcalloc(p, sizeof(conn_rec));
277     BUFF *conn_io = ap_bcreate(p, B_RDWR);
278     ap_bpush_socket(conn_io, inout);
279
280     /* Got a connection structure, so initialize what fields we can
281      * (the rest are zeroed out by pcalloc).
282      */
283
284     conn->conn_config=ap_create_conn_config(p);
285     conn->notes = apr_make_table(p, 5);
286
287     conn->pool = p;
288     conn->local_addr = *saddr;
289     conn->local_ip = apr_pstrdup(conn->pool,
290                                 inet_ntoa(conn->local_addr.sin_addr));
291     conn->base_server = server;
292     conn->client = conn_io;
293     conn->client_socket = inout;
294
295     conn->remote_addr = *remaddr;
296     conn->remote_ip = apr_pstrdup(conn->pool,
297                               inet_ntoa(conn->remote_addr.sin_addr));
298     
299     conn->id = id;
300
301     return conn;
302 }
303
304
305
306 conn_rec *ap_new_apr_connection(apr_pool_t *p, server_rec *server, 
307                                 apr_socket_t *conn_socket, long id)
308 {
309     struct sockaddr_in *sa_local, *sa_remote;
310
311     apr_get_local_name(&sa_local, conn_socket);
312     apr_get_remote_name(&sa_remote, conn_socket);
313     return ap_new_connection(p, server, conn_socket, sa_remote, sa_local, id);
314 }