]> granicus.if.org Git - apache/blob - server/connection.c
Clean up a big chunk of ap_config.h. This basically stops ap_config.h from
[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 "httpd.h"
62 #include "http_connection.h"
63 #include "http_request.h"
64 #include "http_protocol.h"
65 #include "ap_mpm.h"
66 #include "mpm_status.h"
67 #include "http_config.h"
68 #include "http_vhost.h"
69
70 #include <netinet/in.h>
71 #include <arpa/inet.h>
72
73 AP_HOOK_STRUCT(
74             AP_HOOK_LINK(pre_connection)
75             AP_HOOK_LINK(process_connection)
76 )
77
78 AP_IMPLEMENT_HOOK_RUN_ALL(int,pre_connection,(conn_rec *c),(c),OK,DECLINED)
79 AP_IMPLEMENT_HOOK_RUN_FIRST(int,process_connection,(conn_rec *c),(c),DECLINED)
80
81 /*
82  * More machine-dependent networking gooo... on some systems,
83  * you've got to be *really* sure that all the packets are acknowledged
84  * before closing the connection, since the client will not be able
85  * to see the last response if their TCP buffer is flushed by a RST
86  * packet from us, which is what the server's TCP stack will send
87  * if it receives any request data after closing the connection.
88  *
89  * In an ideal world, this function would be accomplished by simply
90  * setting the socket option SO_LINGER and handling it within the
91  * server's TCP stack while the process continues on to the next request.
92  * Unfortunately, it seems that most (if not all) operating systems
93  * block the server process on close() when SO_LINGER is used.
94  * For those that don't, see USE_SO_LINGER below.  For the rest,
95  * we have created a home-brew lingering_close.
96  *
97  * Many operating systems tend to block, puke, or otherwise mishandle
98  * calls to shutdown only half of the connection.  You should define
99  * NO_LINGCLOSE in ap_config.h if such is the case for your system.
100  */
101 #ifndef MAX_SECS_TO_LINGER
102 #define MAX_SECS_TO_LINGER 30
103 #endif
104
105 #ifdef USE_SO_LINGER
106 #define NO_LINGCLOSE            /* The two lingering options are exclusive */
107
108 static void sock_enable_linger(int s) 
109 {
110     struct linger li;                 
111
112     li.l_onoff = 1;
113     li.l_linger = MAX_SECS_TO_LINGER;
114
115     if (setsockopt(s, SOL_SOCKET, SO_LINGER, 
116                    (char *) &li, sizeof(struct linger)) < 0) {
117         ap_log_error(APLOG_MARK, APLOG_WARNING, server_conf,
118                     "setsockopt: (SO_LINGER)");
119         /* not a fatal error */
120     }
121 }
122
123 #else
124 #define sock_enable_linger(s)   /* NOOP */
125 #endif /* USE_SO_LINGER */
126
127 /* we now proceed to read from the client until we get EOF, or until
128  * MAX_SECS_TO_LINGER has passed.  the reasons for doing this are
129  * documented in a draft:
130  *
131  * http://www.ics.uci.edu/pub/ietf/http/draft-ietf-http-connection-00.txt
132  *
133  * in a nutshell -- if we don't make this effort we risk causing
134  * TCP RST packets to be sent which can tear down a connection before
135  * all the response data has been sent to the client.
136  */
137
138 void ap_lingering_close(conn_rec *c)
139 {
140     char dummybuf[512];
141     ap_time_t start;
142     ap_ssize_t nbytes;
143     ap_status_t rc;
144     int timeout;
145
146 #ifdef NO_LINGCLOSE
147     ap_bclose(c->client);       /* just close it */
148     return;
149 #endif
150
151     /* Close the connection, being careful to send out whatever is still
152      * in our buffers.  If possible, try to avoid a hard close until the
153      * client has ACKed our FIN and/or has stopped sending us data.
154      */
155
156     if (c->aborted || !(c->client)) {
157         ap_bsetflag(c->client, B_EOUT, 1);
158         ap_bclose(c->client);
159         return;
160     }
161
162     /* Send any leftover data to the client, but never try to again */
163
164     if (ap_bflush(c->client) != APR_SUCCESS) {
165         ap_bclose(c->client);
166         return;
167     }
168
169     /* Shut down the socket for write, which will send a FIN
170      * to the peer.
171      */
172     
173     if (ap_bshutdown(c->client, 1) != APR_SUCCESS
174         || ap_is_aborted(c)) {
175         ap_bclose(c->client);
176         return;
177     }
178
179     /* Read all data from the peer until we reach "end-of-file" (FIN
180      * from peer) or we've exceeded our overall timeout.
181      */
182     
183     start = ap_now();
184     timeout = MAX_SECS_TO_LINGER;
185     for (;;) {
186         ap_bsetopt(c->client, BO_TIMEOUT, &timeout);
187         rc = ap_bread(c->client, dummybuf, sizeof(dummybuf),
188                       &nbytes);
189         if (rc != APR_SUCCESS || nbytes == 0) break;
190
191         /* how much time has elapsed? */
192         timeout = (int)((ap_now() - start) / AP_USEC_PER_SEC);
193         if (timeout >= MAX_SECS_TO_LINGER) break;
194
195         /* figure out the new timeout */
196         timeout = MAX_SECS_TO_LINGER - timeout;
197     }
198
199     ap_bclose(c->client);
200 }
201
202 CORE_EXPORT(void) ap_process_connection(conn_rec *c)
203 {
204     ap_update_vhost_given_ip(c);
205
206     ap_run_pre_connection(c);
207
208     ap_run_process_connection(c);
209
210 }
211
212 int ap_process_http_connection(conn_rec *c)
213 {
214     request_rec *r;
215
216     /*
217      * Read and process each request found on our connection
218      * until no requests are left or we decide to close.
219      */
220
221     ap_update_connection_status(c->id, "Status", "Reading");
222     while ((r = ap_read_request(c)) != NULL) {
223
224         /* process the request if it was read without error */
225
226         ap_update_connection_status(c->id, "Status", "Writing");
227         if (r->status == HTTP_OK)
228             ap_process_request(r);
229
230         if (!c->keepalive || c->aborted)
231             break;
232
233         ap_update_connection_status(c->id, "Status", "Keepalive");
234         ap_destroy_pool(r->pool);
235
236         if (ap_graceful_stop_signalled())
237             break;
238     }
239
240     ap_reset_connection_status(c->id);
241     return OK;
242 }
243
244 /* Clearly some of this stuff doesn't belong in a generalised connection
245    structure, but for now...
246 */
247
248 conn_rec *ap_new_connection(ap_pool_t *p, server_rec *server, BUFF *inout,
249                             const struct sockaddr_in *remaddr,
250                             const struct sockaddr_in *saddr, long id)
251 {
252     conn_rec *conn = (conn_rec *) ap_pcalloc(p, sizeof(conn_rec));
253
254     /* Got a connection structure, so initialize what fields we can
255      * (the rest are zeroed out by pcalloc).
256      */
257
258     conn->conn_config=ap_create_conn_config(p);
259
260     conn->pool = p;
261     conn->local_addr = *saddr;
262     conn->local_ip = ap_pstrdup(conn->pool,
263                                 inet_ntoa(conn->local_addr.sin_addr));
264     conn->base_server = server;
265     conn->client = inout;
266
267     conn->remote_addr = *remaddr;
268     conn->remote_ip = ap_pstrdup(conn->pool,
269                               inet_ntoa(conn->remote_addr.sin_addr));
270     
271     conn->id = id;
272
273     return conn;
274 }
275
276
277
278 conn_rec *ap_new_apr_connection(ap_pool_t *p, server_rec *server, BUFF *inout,
279                             const ap_socket_t *conn_socket, long id)
280 {
281     struct sockaddr_in *sa_local, *sa_remote;
282
283     ap_get_local_name(&sa_local, conn_socket);
284     ap_get_remote_name(&sa_remote, conn_socket);
285     return ap_new_connection(p, server, inout, sa_remote, sa_local, id);
286 }