]> granicus.if.org Git - apache/blob - server/connection.c
Start getting extended status working again.
[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 #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_vhost.h"
72 #include "scoreboard.h"
73 #include "http_log.h"
74 #include "util_filter.h"
75
76 APR_HOOK_STRUCT(
77             APR_HOOK_LINK(pre_connection)
78             APR_HOOK_LINK(process_connection)
79 )
80
81 AP_IMPLEMENT_HOOK_RUN_ALL(int,pre_connection,(conn_rec *c),(c),OK,DECLINED)
82 AP_IMPLEMENT_HOOK_RUN_FIRST(int,process_connection,(conn_rec *c),(c),DECLINED)
83
84 /*
85  * More machine-dependent networking gooo... on some systems,
86  * you've got to be *really* sure that all the packets are acknowledged
87  * before closing the connection, since the client will not be able
88  * to see the last response if their TCP buffer is flushed by a RST
89  * packet from us, which is what the server's TCP stack will send
90  * if it receives any request data after closing the connection.
91  *
92  * In an ideal world, this function would be accomplished by simply
93  * setting the socket option SO_LINGER and handling it within the
94  * server's TCP stack while the process continues on to the next request.
95  * Unfortunately, it seems that most (if not all) operating systems
96  * block the server process on close() when SO_LINGER is used.
97  * For those that don't, see USE_SO_LINGER below.  For the rest,
98  * we have created a home-brew lingering_close.
99  *
100  * Many operating systems tend to block, puke, or otherwise mishandle
101  * calls to shutdown only half of the connection.  You should define
102  * NO_LINGCLOSE in ap_config.h if such is the case for your system.
103  */
104 #ifndef MAX_SECS_TO_LINGER
105 #define MAX_SECS_TO_LINGER 30
106 #endif
107
108 #ifdef USE_SO_LINGER
109 #define NO_LINGCLOSE            /* The two lingering options are exclusive */
110
111 static void sock_enable_linger(int s) 
112 {
113     struct linger li;                 
114
115     li.l_onoff = 1;
116     li.l_linger = MAX_SECS_TO_LINGER;
117
118     if (setsockopt(s, SOL_SOCKET, SO_LINGER, 
119                    (char *) &li, sizeof(struct linger)) < 0) {
120         ap_log_error(APLOG_MARK, APLOG_WARNING, errno, server_conf,
121                     "setsockopt: (SO_LINGER)");
122         /* not a fatal error */
123     }
124 }
125
126 #else
127 #define sock_enable_linger(s)   /* NOOP */
128 #endif /* USE_SO_LINGER */
129
130 AP_CORE_DECLARE(void) ap_flush_conn(conn_rec *c)
131 {
132     apr_bucket_brigade *bb;
133     apr_bucket *b;
134
135     bb = apr_brigade_create(c->pool);
136     b = apr_bucket_flush_create();
137     APR_BRIGADE_INSERT_TAIL(bb, b);
138     ap_pass_brigade(c->output_filters, bb);
139 }
140
141 /* we now proceed to read from the client until we get EOF, or until
142  * MAX_SECS_TO_LINGER has passed.  the reasons for doing this are
143  * documented in a draft:
144  *
145  * http://www.ics.uci.edu/pub/ietf/http/draft-ietf-http-connection-00.txt
146  *
147  * in a nutshell -- if we don't make this effort we risk causing
148  * TCP RST packets to be sent which can tear down a connection before
149  * all the response data has been sent to the client.
150  */
151
152 void ap_lingering_close(conn_rec *c)
153 {
154     char dummybuf[512];
155     apr_time_t start;
156     apr_size_t nbytes;
157     apr_status_t rc;
158     int timeout;
159
160 #ifdef NO_LINGCLOSE
161     ap_flush_conn(c);   /* just close it */
162     apr_socket_close(c->client_socket);
163     return;
164 #endif
165
166     /* Close the connection, being careful to send out whatever is still
167      * in our buffers.  If possible, try to avoid a hard close until the
168      * client has ACKed our FIN and/or has stopped sending us data.
169      */
170
171     /* Send any leftover data to the client, but never try to again */
172     ap_flush_conn(c);
173
174     if (c->aborted) {
175         apr_socket_close(c->client_socket);
176         return;
177     }
178
179     /* Shut down the socket for write, which will send a FIN
180      * to the peer.
181      */
182     
183     if (apr_shutdown(c->client_socket, APR_SHUTDOWN_WRITE) != APR_SUCCESS || 
184         c->aborted) {
185         apr_socket_close(c->client_socket);
186         return;
187     }
188
189     /* Read all data from the peer until we reach "end-of-file" (FIN
190      * from peer) or we've exceeded our overall timeout.
191      */
192     
193     start = apr_time_now();
194     timeout = MAX_SECS_TO_LINGER * APR_USEC_PER_SEC;
195     for (;;) {
196         apr_setsocketopt(c->client_socket, APR_SO_TIMEOUT, timeout);
197         nbytes = sizeof(dummybuf);
198         rc = apr_recv(c->client_socket, dummybuf, &nbytes);
199         if (rc != APR_SUCCESS || nbytes == 0) break;
200
201         /* how much time has elapsed? */
202         timeout = (int)((apr_time_now() - start) / APR_USEC_PER_SEC);
203         if (timeout >= MAX_SECS_TO_LINGER) break;
204
205         /* figure out the new timeout */
206         timeout = (int)((MAX_SECS_TO_LINGER - timeout) * APR_USEC_PER_SEC);
207     }
208
209     apr_socket_close(c->client_socket);
210 }
211
212 AP_CORE_DECLARE(void) ap_process_connection(conn_rec *c)
213 {
214     ap_update_vhost_given_ip(c);
215
216     ap_run_pre_connection(c);
217
218     ap_run_process_connection(c);
219
220 }
221
222 int ap_pre_http_connection(conn_rec *c)
223 {
224     ap_add_input_filter("HTTP_IN", NULL, NULL, c);
225     ap_add_input_filter("CORE_IN", NULL, NULL, c);
226     ap_add_output_filter("CORE", NULL, NULL, c);
227     return OK;
228 }
229
230 AP_CORE_DECLARE_NONSTD(int) ap_process_http_connection(conn_rec *c)
231 {
232     request_rec *r;
233
234     /*
235      * Read and process each request found on our connection
236      * until no requests are left or we decide to close.
237      */
238
239     ap_update_child_status(AP_CHILD_THREAD_FROM_ID(c->id), SERVER_BUSY_READ, NULL);
240     while ((r = ap_read_request(c)) != NULL) {
241
242         /* process the request if it was read without error */
243
244         ap_update_child_status(AP_CHILD_THREAD_FROM_ID(c->id), SERVER_BUSY_WRITE, NULL); 
245         if (r->status == HTTP_OK)
246             ap_process_request(r);
247
248         if (ap_extended_status)
249             ap_increment_counts(AP_CHILD_THREAD_FROM_ID(c->id), r);
250
251         if (!c->keepalive || c->aborted)
252             break;
253
254         ap_update_child_status(AP_CHILD_THREAD_FROM_ID(c->id), SERVER_BUSY_KEEPALIVE, NULL);
255         apr_pool_destroy(r->pool);
256
257         if (ap_graceful_stop_signalled())
258             break;
259     }
260
261     return OK;
262 }
263
264 /* Clearly some of this stuff doesn't belong in a generalised connection
265    structure, but for now...
266 */
267
268 conn_rec *ap_new_connection(apr_pool_t *p, server_rec *server, 
269                             apr_socket_t *inout, long id)
270 {
271     conn_rec *conn = (conn_rec *) apr_pcalloc(p, sizeof(conn_rec));
272     apr_status_t rv;
273
274     (void) ap_update_child_status(AP_CHILD_THREAD_FROM_ID(id), 
275                                   SERVER_BUSY_READ, (request_rec *) NULL);
276
277     /* Got a connection structure, so initialize what fields we can
278      * (the rest are zeroed out by pcalloc).
279      */
280
281     conn->conn_config=ap_create_conn_config(p);
282     conn->notes = apr_table_make(p, 5);
283
284     conn->pool = p;
285     if ((rv = apr_socket_addr_get(&conn->local_addr, APR_LOCAL, inout)) 
286         != APR_SUCCESS) {
287         ap_log_error(APLOG_MARK, APLOG_INFO, rv, server,
288                      "apr_socket_addr_get(APR_LOCAL)");
289         apr_socket_close(inout);
290         return NULL;
291     }
292     apr_sockaddr_ip_get(&conn->local_ip, conn->local_addr);
293     if ((rv = apr_socket_addr_get(&conn->remote_addr, APR_REMOTE, inout))
294         != APR_SUCCESS) {
295         ap_log_error(APLOG_MARK, APLOG_INFO, rv, server,
296                      "apr_socket_addr_get(APR_REMOTE)");
297         apr_socket_close(inout);
298         return NULL;
299     }
300     apr_sockaddr_ip_get(&conn->remote_ip, conn->remote_addr);
301     conn->base_server = server;
302     conn->client_socket = inout;
303
304     conn->id = id;
305
306     return conn;
307 }