1 /* ====================================================================
2 * Copyright (c) 1995-1999 The Apache Group. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
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
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/)."
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
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.
30 * 6. Redistributions of any form whatsoever must retain the following
32 * "This product includes software developed by the Apache Group
33 * for use in the Apache HTTP server project (http://www.apache.org/)."
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 * ====================================================================
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/>.
60 #include "http_connection.h"
61 #include "http_request.h"
62 #include "http_protocol.h"
64 #include "http_config.h"
65 #include "http_vhost.h"
67 /* TODO: re-implement the lingering close stuff */
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.
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.
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.
90 #ifndef MAX_SECS_TO_LINGER
91 #define MAX_SECS_TO_LINGER 30
95 #define NO_LINGCLOSE /* The two lingering options are exclusive */
97 static void sock_enable_linger(int s) // ZZZZZ abstract the socket, s
99 struct linger li; // ZZZZZ SocketOptions...
102 li.l_linger = MAX_SECS_TO_LINGER;
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 */
113 #define sock_enable_linger(s) /* NOOP */
114 #endif /* USE_SO_LINGER */
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
124 /*ZZZ this routine needs to be adapted for use with poll()*/
125 static void lingering_close(request_rec *r)
127 /*ZZZ remove the hardwired 512. This is an IO Buffer Size */
133 /* Prevent a slow-drip client from holding us here indefinitely */
136 ap_bsetopt(r->connection->client, BO_TIMEOUT, &max_wait);
138 /* Send any leftover data to the client, but never try to again */
140 if (ap_bflush(r->connection->client) == -1) {
141 ap_bclose(r->connection->client);
144 ap_bsetflag(r->connection->client, B_EOUT, 1);
146 /* Close our half of the connection --- send the client a FIN */
148 lsd = r->connection->client->fd;
150 if ((shutdown(lsd, 1) != 0) /* ZZZ abstract shutdown */
151 || ap_is_aborted(r->connection)) {
152 ap_bclose(r->connection->client);
156 /* Set up to wait for readable data on socket... */
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.
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
176 } while ((poll(&pd, 1, 2) == 1)
177 && read(lsd, dummybuf, sizeof(dummybuf)));
178 /* && (time() = epoch) < max_wait); */ /* ZZZZ time function is not good... */
180 /* Should now have seen final ack. Safe to finally kill socket */
181 ap_bclose(r->connection->client);
183 #endif /* ndef NO_LINGCLOSE */
185 CORE_EXPORT(void) ap_process_connection(conn_rec *c)
189 ap_update_vhost_given_ip(c);
191 ap_run_pre_connection(c);
194 * Read and process each request found on our connection
195 * until no requests are left or we decide to close.
198 while ((r = ap_read_request(c)) != NULL) {
200 /* process the request if it was read without error */
202 if (r->status == HTTP_OK)
203 ap_process_request(r);
205 if (!c->keepalive || c->aborted)
208 ap_destroy_pool(r->pool);
210 if (ap_graceful_stop_signalled()) {
211 /* XXX: hey wait, this should do a lingering_close! */
212 ap_bclose(c->client);
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.
224 ap_bclose(c->client); /* just close it */
226 if (r && r->connection
227 && !r->connection->aborted
228 && r->connection->client
229 && (r->connection->client->fd >= 0)) {
234 ap_bsetflag(c->client, B_EOUT, 1);
235 ap_bclose(c->client);
240 /* Clearly some of this stuff doesn't belong in a generalised connection
241 structure, but for now...
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,
249 conn_rec *conn = (conn_rec *) ap_pcalloc(p, sizeof(conn_rec));
251 /* Got a connection structure, so initialize what fields we can
252 * (the rest are zeroed out by pcalloc).
255 conn->conn_config=ap_create_conn_config(p);
257 conn->child_num = child_num;
260 conn->local_addr = *saddr;
261 conn->base_server = server;
262 conn->client = inout;
264 conn->remote_addr = *remaddr;
265 conn->remote_ip = ap_pstrdup(conn->pool,
266 inet_ntoa(conn->remote_addr.sin_addr));