upload http
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / modules / proxy / proxy_connect.c
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /* CONNECT method for Apache proxy */
18
19 #define CORE_PRIVATE
20
21 #include "mod_proxy.h"
22 #include "apr_poll.h"
23
24 module AP_MODULE_DECLARE_DATA proxy_connect_module;
25
26 int ap_proxy_connect_canon(request_rec *r, char *url);
27 int ap_proxy_connect_handler(request_rec *r, proxy_server_conf *conf, 
28                              char *url, const char *proxyname, 
29                              apr_port_t proxyport);
30
31 /*  
32  * This handles Netscape CONNECT method secure proxy requests.
33  * A connection is opened to the specified host and data is
34  * passed through between the WWW site and the browser.
35  *
36  * This code is based on the INTERNET-DRAFT document
37  * "Tunneling SSL Through a WWW Proxy" currently at
38  * http://www.mcom.com/newsref/std/tunneling_ssl.html.
39  *
40  * If proxyhost and proxyport are set, we send a CONNECT to 
41  * the specified proxy..  
42  *
43  * FIXME: this doesn't log the number of bytes sent, but
44  *        that may be okay, since the data is supposed to
45  *        be transparent. In fact, this doesn't log at all
46  *        yet. 8^)
47  * FIXME: doesn't check any headers initally sent from the
48  *        client.
49  * FIXME: should allow authentication, but hopefully the
50  *        generic proxy authentication is good enough.
51  * FIXME: no check for r->assbackwards, whatever that is.
52  */
53
54 static int
55 allowed_port(proxy_server_conf *conf, int port)
56 {
57     int i;
58     int *list = (int *) conf->allowed_connect_ports->elts;
59
60     for(i = 0; i < conf->allowed_connect_ports->nelts; i++) {
61         if(port == list[i])
62             return 1;
63     }
64     return 0;
65 }
66
67 /* canonicalise CONNECT URLs. */
68 int ap_proxy_connect_canon(request_rec *r, char *url)
69 {
70
71     if (r->method_number != M_CONNECT) {
72         return DECLINED;
73     }
74     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
75                  "proxy: CONNECT: canonicalising URL %s", url);
76
77     return OK;
78 }
79
80 /* CONNECT handler */
81 int ap_proxy_connect_handler(request_rec *r, proxy_server_conf *conf, 
82                              char *url, const char *proxyname, 
83                              apr_port_t proxyport)
84 {
85     apr_pool_t *p = r->pool;
86     apr_socket_t *sock;
87     apr_status_t err, rv;
88     apr_size_t i, o, nbytes;
89     char buffer[HUGE_STRING_LEN];
90     apr_socket_t *client_socket = ap_get_module_config(r->connection->conn_config, &core_module);
91     int failed;
92     apr_pollfd_t *pollfd;
93     apr_int32_t pollcnt;
94     apr_int16_t pollevent;
95     apr_sockaddr_t *uri_addr, *connect_addr;
96
97     apr_uri_t uri;
98     const char *connectname;
99     int connectport = 0;
100
101     /* is this for us? */
102     if (r->method_number != M_CONNECT) {
103         ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
104                      "proxy: CONNECT: declining URL %s", url);
105         return DECLINED;
106     }
107     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
108                  "proxy: CONNECT: serving URL %s", url);
109
110
111     /*
112      * Step One: Determine Who To Connect To
113      *
114      * Break up the URL to determine the host to connect to
115      */
116
117     /* we break the URL into host, port, uri */
118     if (APR_SUCCESS != apr_uri_parse_hostinfo(p, url, &uri)) {
119         return ap_proxyerror(r, HTTP_BAD_REQUEST,
120                              apr_pstrcat(p, "URI cannot be parsed: ", url, NULL));
121     }
122
123     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
124                  "proxy: CONNECT: connecting %s to %s:%d", url, uri.hostname, uri.port);
125
126     /* do a DNS lookup for the destination host */
127     err = apr_sockaddr_info_get(&uri_addr, uri.hostname, APR_UNSPEC, uri.port, 0, p);
128
129     /* are we connecting directly, or via a proxy? */
130     if (proxyname) {
131         connectname = proxyname;
132         connectport = proxyport;
133         err = apr_sockaddr_info_get(&connect_addr, proxyname, APR_UNSPEC, proxyport, 0, p);
134     }
135     else {
136         connectname = uri.hostname;
137         connectport = uri.port;
138         connect_addr = uri_addr;
139     }
140     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
141                  "proxy: CONNECT: connecting to remote proxy %s on port %d", connectname, connectport);
142
143     /* check if ProxyBlock directive on this host */
144     if (OK != ap_proxy_checkproxyblock(r, conf, uri_addr)) {
145         return ap_proxyerror(r, HTTP_FORBIDDEN,
146                              "Connect to remote machine blocked");
147     }
148
149     /* Check if it is an allowed port */
150     if (conf->allowed_connect_ports->nelts == 0) {
151         /* Default setting if not overridden by AllowCONNECT */
152         switch (uri.port) {
153             case APR_URI_HTTPS_DEFAULT_PORT:
154             case APR_URI_SNEWS_DEFAULT_PORT:
155                 break;
156             default:
157                 /* XXX can we call ap_proxyerror() here to get a nice log message? */
158                 return HTTP_FORBIDDEN;
159         }
160     } else if(!allowed_port(conf, uri.port)) {
161         /* XXX can we call ap_proxyerror() here to get a nice log message? */
162         return HTTP_FORBIDDEN;
163     }
164
165     /*
166      * Step Two: Make the Connection
167      *
168      * We have determined who to connect to. Now make the connection.
169      */
170
171     /* get all the possible IP addresses for the destname and loop through them
172      * until we get a successful connection
173      */
174     if (APR_SUCCESS != err) {
175         return ap_proxyerror(r, HTTP_BAD_GATEWAY, apr_pstrcat(p,
176                              "DNS lookup failure for: ",
177                              connectname, NULL));
178     }
179
180     /*
181      * At this point we have a list of one or more IP addresses of
182      * the machine to connect to. If configured, reorder this
183      * list so that the "best candidate" is first try. "best
184      * candidate" could mean the least loaded server, the fastest
185      * responding server, whatever.
186      *
187      * For now we do nothing, ie we get DNS round robin.
188      * XXX FIXME
189      */
190     failed = ap_proxy_connect_to_backend(&sock, "CONNECT", connect_addr,
191                                          connectname, conf, r->server,
192                                          r->pool);
193
194     /* handle a permanent error from the above loop */
195     if (failed) {
196         if (proxyname) {
197             return DECLINED;
198         }
199         else {
200             return HTTP_BAD_GATEWAY;
201         }
202     }
203
204     /*
205      * Step Three: Send the Request
206      *
207      * Send the HTTP/1.1 CONNECT request to the remote server
208      */
209
210     /* we are acting as a tunnel - the output filter stack should
211      * be completely empty, because when we are done here we are done completely.
212      * We add the NULL filter to the stack to do this...
213      */
214     r->output_filters = NULL;
215     r->connection->output_filters = NULL;
216
217
218     /* If we are connecting through a remote proxy, we need to pass
219      * the CONNECT request on to it.
220      */
221     if (proxyport) {
222         /* FIXME: Error checking ignored.
223          */
224         ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
225                      "proxy: CONNECT: sending the CONNECT request to the remote proxy");
226         nbytes = apr_snprintf(buffer, sizeof(buffer),
227                               "CONNECT %s HTTP/1.0" CRLF, r->uri);
228         apr_send(sock, buffer, &nbytes);
229         nbytes = apr_snprintf(buffer, sizeof(buffer),
230                               "Proxy-agent: %s" CRLF CRLF, ap_get_server_version());
231         apr_send(sock, buffer, &nbytes);
232     }
233     else {
234         ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
235                      "proxy: CONNECT: Returning 200 OK Status");
236         nbytes = apr_snprintf(buffer, sizeof(buffer),
237                               "HTTP/1.0 200 Connection Established" CRLF);
238         ap_xlate_proto_to_ascii(buffer, nbytes);
239         apr_send(client_socket, buffer, &nbytes);
240         nbytes = apr_snprintf(buffer, sizeof(buffer),
241                               "Proxy-agent: %s" CRLF CRLF, ap_get_server_version());
242         ap_xlate_proto_to_ascii(buffer, nbytes);
243         apr_send(client_socket, buffer, &nbytes);
244 #if 0
245         /* This is safer code, but it doesn't work yet.  I'm leaving it 
246          * here so that I can fix it later.
247          */
248         r->status = HTTP_OK;
249         r->header_only = 1;
250         apr_table_set(r->headers_out, "Proxy-agent: %s", ap_get_server_version());
251         ap_rflush(r);
252 #endif
253     }
254
255     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
256                  "proxy: CONNECT: setting up poll()");
257
258     /*
259      * Step Four: Handle Data Transfer
260      *
261      * Handle two way transfer of data over the socket (this is a tunnel).
262      */
263
264 /*    r->sent_bodyct = 1;*/
265
266     if((rv = apr_poll_setup(&pollfd, 2, r->pool)) != APR_SUCCESS)
267     {
268         apr_socket_close(sock);
269         ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
270             "proxy: CONNECT: error apr_poll_setup()");
271         return HTTP_INTERNAL_SERVER_ERROR;
272     }
273
274     /* Add client side to the poll */
275     apr_poll_socket_add(pollfd, client_socket, APR_POLLIN);
276
277     /* Add the server side to the poll */
278     apr_poll_socket_add(pollfd, sock, APR_POLLIN);
279
280     while (1) { /* Infinite loop until error (one side closes the connection) */
281 /*      ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server, "proxy: CONNECT: going to sleep (poll)");*/
282         if ((rv = apr_poll(pollfd, 2, &pollcnt, -1)) != APR_SUCCESS)
283         {
284             apr_socket_close(sock);
285             ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, "proxy: CONNECT: error apr_poll()");
286             return HTTP_INTERNAL_SERVER_ERROR;
287         }
288 /*      ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
289                      "proxy: CONNECT: woke from select(), i=%d", pollcnt);*/
290
291         if (pollcnt) {
292             apr_poll_revents_get(&pollevent, sock, pollfd);
293             if (pollevent & APR_POLLIN) {
294 /*              ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
295                              "proxy: CONNECT: sock was set");*/
296                 nbytes = sizeof(buffer);
297                 if (apr_recv(sock, buffer, &nbytes) == APR_SUCCESS) {
298                     o = 0;
299                     i = nbytes;
300                     while(i > 0)
301                     {
302                         nbytes = i;
303     /* This is just plain wrong.  No module should ever write directly
304      * to the client.  For now, this works, but this is high on my list of
305      * things to fix.  The correct line is:
306      * if ((nbytes = ap_rwrite(buffer + o, nbytes, r)) < 0)
307      * rbb
308      */
309                         if (apr_send(client_socket, buffer + o, &nbytes) != APR_SUCCESS)
310                             break;
311                         o += nbytes;
312                         i -= nbytes;
313                     }
314                 }
315                 else
316                     break;
317             }
318             else if ((pollevent & APR_POLLERR) || (pollevent & APR_POLLHUP))
319                 break;
320
321
322             apr_poll_revents_get(&pollevent, client_socket, pollfd);
323             if (pollevent & APR_POLLIN) {
324 /*              ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
325                              "proxy: CONNECT: client was set");*/
326                 nbytes = sizeof(buffer);
327                 if (apr_recv(client_socket, buffer, &nbytes) == APR_SUCCESS) {
328                     o = 0;
329                     i = nbytes;
330                     while(i > 0)
331                     {
332                         nbytes = i;
333                         if (apr_send(sock, buffer + o, &nbytes) != APR_SUCCESS)
334                             break;
335                         o += nbytes;
336                         i -= nbytes;
337                     }
338                 }
339                 else
340                     break;
341             }
342             else if ((pollevent & APR_POLLERR) || (pollevent & APR_POLLHUP))
343                 break;
344         }
345         else
346             break;
347     }
348
349     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
350                  "proxy: CONNECT: finished with poll() - cleaning up");
351
352     /*
353      * Step Five: Clean Up
354      *
355      * Close the socket and clean up
356      */
357
358     apr_socket_close(sock);
359
360     return OK;
361 }
362
363 static void ap_proxy_connect_register_hook(apr_pool_t *p)
364 {
365     proxy_hook_scheme_handler(ap_proxy_connect_handler, NULL, NULL, APR_HOOK_MIDDLE);
366     proxy_hook_canon_handler(ap_proxy_connect_canon, NULL, NULL, APR_HOOK_MIDDLE);
367 }
368
369 module AP_MODULE_DECLARE_DATA proxy_connect_module = {
370     STANDARD20_MODULE_STUFF,
371     NULL,               /* create per-directory config structure */
372     NULL,               /* merge per-directory config structures */
373     NULL,               /* create per-server config structure */
374     NULL,               /* merge per-server config structures */
375     NULL,               /* command apr_table_t */
376     ap_proxy_connect_register_hook      /* register hooks */
377 };