]> granicus.if.org Git - apache/blob - server/connection.c
New API for I/O layering, and dependency updates.
[apache] / server / connection.c
1 /* ====================================================================
2  * Copyright (c) 1995-1999 The Apache Group.  All rights reserved.
3  * 
4  * Redistribution and use in source and binary forms, with or without 
5  * modification, are permitted provided that the following conditions 
6  * are met: 
7  * 
8  * 1. Redistributions of source code must retain the above copyright 
9  *    notice, this list of conditions and the following disclaimer.  
10  * 
11  * 2. Redistributions in binary form must reproduce the above copyright 
12  *    notice, this list of conditions and the following disclaimer in 
13  *    the documentation and/or other materials provided with the 
14  *    distribution. 
15  * 
16  * 3. All advertising materials mentioning features or use of this 
17  *    software must display the following acknowledgment: 
18  *    "This product includes software developed by the Apache Group 
19  *    for use in the Apache HTTP server project (http://www.apache.org/)." 
20  * 
21  * 4. The names "Apache Server" and "Apache Group" must not be used to 
22  *    endorse or promote products derived from this software without 
23  *    prior written permission. For written permission, please contact 
24  *    apache@apache.org. 
25  * 
26  * 5. Products derived from this software may not be called "Apache" 
27  *    nor may "Apache" appear in their names without prior written 
28  *    permission of the Apache Group. 
29  * 
30  * 6. Redistributions of any form whatsoever must retain the following 
31  *    acknowledgment: 
32  *    "This product includes software developed by the Apache Group 
33  *    for use in the Apache HTTP server project (http://www.apache.org/)." 
34  * 
35  * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY 
36  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
37  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
38  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR 
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
41  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
42  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
43  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
44  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
45  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
46  * OF THE POSSIBILITY OF SUCH DAMAGE. 
47  * ==================================================================== 
48  * 
49  * This software consists of voluntary contributions made by many 
50  * individuals on behalf of the Apache Group and was originally based 
51  * on public domain software written at the National Center for 
52  * Supercomputing Applications, University of Illinois, Urbana-Champaign. 
53  * For more information on the Apache Group and the Apache HTTP server 
54  * project, please see <http://www.apache.org/>. 
55  * 
56  */ 
57  
58 #define CORE_PRIVATE
59 #include "httpd.h"
60 #include "http_connection.h"
61 #include "http_request.h"
62 #include "http_protocol.h"
63 #include "ap_mpm.h"
64 #include "http_config.h"
65 #include "http_vhost.h"
66
67 /* TODO: re-implement the lingering close stuff */
68 #define NO_LINGCLOSE
69
70 /*
71  * More machine-dependent networking gooo... on some systems,
72  * you've got to be *really* sure that all the packets are acknowledged
73  * before closing the connection, since the client will not be able
74  * to see the last response if their TCP buffer is flushed by a RST
75  * packet from us, which is what the server's TCP stack will send
76  * if it receives any request data after closing the connection.
77  *
78  * In an ideal world, this function would be accomplished by simply
79  * setting the socket option SO_LINGER and handling it within the
80  * server's TCP stack while the process continues on to the next request.
81  * Unfortunately, it seems that most (if not all) operating systems
82  * block the server process on close() when SO_LINGER is used.
83  * For those that don't, see USE_SO_LINGER below.  For the rest,
84  * we have created a home-brew lingering_close.
85  *
86  * Many operating systems tend to block, puke, or otherwise mishandle
87  * calls to shutdown only half of the connection.  You should define
88  * NO_LINGCLOSE in ap_config.h if such is the case for your system.
89  */
90 #ifndef MAX_SECS_TO_LINGER
91 #define MAX_SECS_TO_LINGER 30
92 #endif
93
94 #ifdef USE_SO_LINGER
95 #define NO_LINGCLOSE            /* The two lingering options are exclusive */
96
97 static void sock_enable_linger(int s) // ZZZZZ abstract the socket, s
98 {
99     struct linger li;                 // ZZZZZ SocketOptions...
100
101     li.l_onoff = 1;
102     li.l_linger = MAX_SECS_TO_LINGER;
103
104     if (setsockopt(s, SOL_SOCKET, SO_LINGER, // ZZZZZ abstract, return SUCCESS or not
105                    (char *) &li, sizeof(struct linger)) < 0) {
106         ap_log_error(APLOG_MARK, APLOG_WARNING, server_conf,
107                     "setsockopt: (SO_LINGER)");
108         /* not a fatal error */
109     }
110 }
111
112 #else
113 #define sock_enable_linger(s)   /* NOOP */
114 #endif /* USE_SO_LINGER */
115
116 #ifndef NO_LINGCLOSE
117
118 /* Since many clients will abort a connection instead of closing it,
119  * attempting to log an error message from this routine will only
120  * confuse the webmaster.  There doesn't seem to be any portable way to
121  * distinguish between a dropped connection and something that might be
122  * worth logging.
123  */
124 /*ZZZ this routine needs to be adapted for use with poll()*/
125 static void lingering_close(request_rec *r)     
126 {
127   /*ZZZ remove the hardwired 512. This is an IO Buffer Size */
128     char dummybuf[512];    
129     struct pollfd pd;
130     int lsd;
131     int max_wait;
132
133     /* Prevent a slow-drip client from holding us here indefinitely */
134
135     max_wait = 30;
136     ap_bsetopt(r->connection->client, BO_TIMEOUT, &max_wait);
137
138     /* Send any leftover data to the client, but never try to again */
139
140     if (ap_bflush(r->connection->client) == -1) {
141         ap_bclose(r->connection->client);
142         return;
143     }
144     ap_bsetflag(r->connection->client, B_EOUT, 1);
145
146     /* Close our half of the connection --- send the client a FIN */
147
148     lsd = r->connection->client->fd;
149
150     if ((shutdown(lsd, 1) != 0)  /* ZZZ abstract shutdown */
151         || ap_is_aborted(r->connection)) {
152         ap_bclose(r->connection->client);
153         return;
154     }
155
156     /* Set up to wait for readable data on socket... */
157     pd.fd = lsd;
158     pd.events = POLLIN;
159
160     /* Wait for readable data or error condition on socket;
161      * slurp up any data that arrives...  We exit when we go for an
162      * interval of tv length without getting any more data, get an error
163      * from poll(), get an error or EOF on a read, or the timer expires.
164      */
165     /* We use a 2 second timeout because current (Feb 97) browsers
166      * fail to close a connection after the server closes it.  Thus,
167      * to avoid keeping the child busy, we are only lingering long enough
168      * for a client that is actively sending data on a connection.
169      * This should be sufficient unless the connection is massively
170      * losing packets, in which case we might have missed the RST anyway.
171      * These parameters are reset on each pass, since they might be
172      * changed by poll.
173      */
174     do {
175         pd.revents = 0;
176     } while ((poll(&pd, 1, 2) == 1)   
177              && read(lsd, dummybuf, sizeof(dummybuf)));
178       /* && (time() = epoch) < max_wait); */    /* ZZZZ time function is not good... */
179
180     /* Should now have seen final ack.  Safe to finally kill socket */
181     ap_bclose(r->connection->client);
182 }
183 #endif /* ndef NO_LINGCLOSE */
184
185 CORE_EXPORT(void) ap_process_connection(conn_rec *c)
186 {
187     request_rec *r;
188
189     ap_update_vhost_given_ip(c);
190
191     ap_run_pre_connection(c);
192
193     /*
194      * Read and process each request found on our connection
195      * until no requests are left or we decide to close.
196      */
197
198     while ((r = ap_read_request(c)) != NULL) {
199
200         /* process the request if it was read without error */
201
202         if (r->status == HTTP_OK)
203             ap_process_request(r);
204
205         if (!c->keepalive || c->aborted)
206             break;
207
208         ap_destroy_pool(r->pool);
209
210         if (ap_graceful_stop_signalled()) {
211             /* XXX: hey wait, this should do a lingering_close! */
212             ap_bclose(c->client);
213             return;
214         }
215     }
216
217     /*
218      * Close the connection, being careful to send out whatever is still
219      * in our buffers.  If possible, try to avoid a hard close until the
220      * client has ACKed our FIN and/or has stopped sending us data.
221      */
222
223 #ifdef NO_LINGCLOSE
224     ap_bclose(c->client);       /* just close it */
225 #else
226     if (r && r->connection
227         && !r->connection->aborted
228         && r->connection->client
229         && (r->connection->client->fd >= 0)) {
230
231         lingering_close(r);
232     }
233     else {
234         ap_bsetflag(c->client, B_EOUT, 1);
235         ap_bclose(c->client);
236     }
237 #endif
238 }
239
240 /* Clearly some of this stuff doesn't belong in a generalised connection
241    structure, but for now...
242 */
243
244 conn_rec *ap_new_connection(pool *p, server_rec *server, BUFF *inout,
245                             const struct sockaddr_in *remaddr,
246                             const struct sockaddr_in *saddr,
247                             int child_num)
248 {
249     conn_rec *conn = (conn_rec *) ap_pcalloc(p, sizeof(conn_rec));
250
251     /* Got a connection structure, so initialize what fields we can
252      * (the rest are zeroed out by pcalloc).
253      */
254
255     conn->conn_config=ap_create_conn_config(p);
256
257     conn->child_num = child_num;
258
259     conn->pool = p;
260     conn->local_addr = *saddr;
261     conn->base_server = server;
262     conn->client = inout;
263
264     conn->remote_addr = *remaddr;
265     conn->remote_ip = ap_pstrdup(conn->pool,
266                               inet_ntoa(conn->remote_addr.sin_addr));
267
268     return conn;
269 }