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