View | Details | Raw Unified | Return to bug 29744
Collapse All | Expand All

(-)mod_proxy_connect.orig (-95 / +130 lines)
Lines 21-26 Link Here
21
#include "mod_proxy.h"
21
#include "mod_proxy.h"
22
#include "apr_poll.h"
22
#include "apr_poll.h"
23
23
24
#define CONN_BLKSZ AP_IOBUFSIZE
25
24
module AP_MODULE_DECLARE_DATA proxy_connect_module;
26
module AP_MODULE_DECLARE_DATA proxy_connect_module;
25
27
26
/*
28
/*
Lines 71-76 Link Here
71
    return OK;
73
    return OK;
72
}
74
}
73
75
76
/* read available data (in blocks of CONN_BLKSZ) from c_i and copy to c_o */
77
static int proxy_connect_transfer(request_rec *r, conn_rec *c_i, conn_rec *c_o,
78
				  apr_bucket_brigade *bb, char *name)
79
{
80
    int rv;
81
#ifdef DEBUGGING
82
    apr_off_t len;
83
#endif
84
85
    do {
86
	apr_brigade_cleanup(bb);
87
	rv = ap_get_brigade(c_i->input_filters, bb, AP_MODE_READBYTES,
88
				APR_NONBLOCK_READ, CONN_BLKSZ);
89
	if (rv == APR_SUCCESS) {
90
		if (APR_BRIGADE_EMPTY(bb))
91
		break;
92
#ifdef DEBUGGING
93
		len = -1;
94
		apr_brigade_length(bb, 0, &len);
95
		ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
96
			  "proxy: CONNECT: read %" APR_OFF_T_FMT
97
			  " bytes from %s", len, name);
98
#endif
99
		rv = ap_pass_brigade(c_o->output_filters, bb);
100
		if (rv == APR_SUCCESS) {
101
		ap_fflush(c_o->output_filters, bb);
102
		} else {
103
		ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
104
				  "proxy: CONNECT: error on %s - ap_pass_brigade",
105
				  name);
106
		}
107
	} else if (!APR_STATUS_IS_EAGAIN(rv)) {
108
		ap_log_rerror(APLOG_MARK, APLOG_DEBUG, rv, r,
109
			  "proxy: CONNECT: error on %s - ap_get_brigade",
110
			  name);
111
	}
112
    } while (rv == APR_SUCCESS);
113
114
    if (APR_STATUS_IS_EAGAIN(rv)) {
115
	rv = APR_SUCCESS;
116
    }
117
    return rv;
118
}
119
74
/* CONNECT handler */
120
/* CONNECT handler */
75
static int proxy_connect_handler(request_rec *r, proxy_worker *worker,
121
static int proxy_connect_handler(request_rec *r, proxy_worker *worker,
76
                                 proxy_server_conf *conf,
122
                                 proxy_server_conf *conf,
Lines 79-89 Link Here
79
{
125
{
80
    apr_pool_t *p = r->pool;
126
    apr_pool_t *p = r->pool;
81
    apr_socket_t *sock;
127
    apr_socket_t *sock;
128
    conn_rec *c = r->connection;
129
    conn_rec *backconn;
130
131
    apr_bucket_brigade *bb = apr_brigade_create(p, c->bucket_alloc);
82
    apr_status_t err, rv;
132
    apr_status_t err, rv;
83
    apr_size_t i, o, nbytes;
133
    apr_size_t i, o, nbytes;
84
    char buffer[HUGE_STRING_LEN];
134
    char buffer[HUGE_STRING_LEN];
85
    apr_socket_t *client_socket = ap_get_module_config(r->connection->conn_config, &core_module);
135
    apr_socket_t *client_socket = ap_get_module_config(c->conn_config, &core_module);
86
    int failed;
136
    int failed, rc;
87
    apr_pollset_t *pollset;
137
    apr_pollset_t *pollset;
88
    apr_pollfd_t pollfd;
138
    apr_pollfd_t pollfd;
89
    const apr_pollfd_t *signalled;
139
    const apr_pollfd_t *signalled;
Lines 158-169 Link Here
158
            case APR_URI_SNEWS_DEFAULT_PORT:
208
            case APR_URI_SNEWS_DEFAULT_PORT:
159
                break;
209
                break;
160
            default:
210
            default:
161
                /* XXX can we call ap_proxyerror() here to get a nice log message? */
211
                return ap_proxyerror(r, HTTP_FORBIDDEN, "Connect to remote machine blocked");
162
                return HTTP_FORBIDDEN;
163
        }
212
        }
164
    } else if(!allowed_port(conf, uri.port)) {
213
    } else if(!allowed_port(conf, uri.port)) {
165
        /* XXX can we call ap_proxyerror() here to get a nice log message? */
214
        return ap_proxyerror(r, HTTP_FORBIDDEN, "Connect to remote machine blocked");
166
        return HTTP_FORBIDDEN;
167
    }
215
    }
168
216
169
    /*
217
    /*
Lines 205-223 Link Here
205
        }
253
        }
206
    }
254
    }
207
255
256
    /* setup polling for connection */
257
    ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
258
         "proxy: CONNECT: setting up poll()");
259
260
    if ((rv = apr_pollset_create(&pollset, 2, r->pool, 0)) != APR_SUCCESS) {
261
    apr_socket_close(sock);
262
        ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
263
            "proxy: CONNECT: error apr_pollset_create()");
264
        return HTTP_INTERNAL_SERVER_ERROR;
265
    }
266
267
    /* Add client side to the poll */
268
    pollfd.p = r->pool;
269
    pollfd.desc_type = APR_POLL_SOCKET;
270
    pollfd.reqevents = APR_POLLIN;
271
    pollfd.desc.s = client_socket;
272
    pollfd.client_data = NULL;
273
    apr_pollset_add(pollset, &pollfd);
274
275
    /* Add the server side to the poll */
276
    pollfd.desc.s = sock;
277
    apr_pollset_add(pollset, &pollfd);
278
208
    /*
279
    /*
209
     * Step Three: Send the Request
280
     * Step Three: Send the Request
210
     *
281
     *
211
     * Send the HTTP/1.1 CONNECT request to the remote server
282
     * Send the HTTP/1.1 CONNECT request to the remote server
212
     */
283
     */
213
284
214
    /* we are acting as a tunnel - the output filter stack should
285
    backconn = ap_run_create_connection(c->pool, r->server, sock,
215
     * be completely empty, because when we are done here we are done completely.
286
                   c->id, c->sbh, c->bucket_alloc);
216
     * We add the NULL filter to the stack to do this...
287
    if (!backconn) {
217
     */
288
    /* peer reset */
218
    r->output_filters = NULL;
289
    ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
219
    r->connection->output_filters = NULL;
290
             "proxy: an error occurred creating a new connection "
220
291
             "to %pI (%s)", connect_addr, connectname);
292
    apr_socket_close(sock);
293
    return HTTP_INTERNAL_SERVER_ERROR;
294
     }
295
     ap_proxy_ssl_disable(backconn);
296
     rc = ap_run_pre_connection(backconn, sock);
297
     if (rc != OK && rc != DONE) {
298
    backconn->aborted = 1;
299
    ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
300
              "proxy: CONNECT: pre_connection setup failed (%d)", rc);
301
    return HTTP_INTERNAL_SERVER_ERROR;
302
    }
303
 
304
     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
305
          "proxy: CONNECT: connection complete to %pI (%s)",
306
          connect_addr, connectname);
221
307
222
    /* If we are connecting through a remote proxy, we need to pass
308
    /* If we are connecting through a remote proxy, we need to pass
223
     * the CONNECT request on to it.
309
     * the CONNECT request on to it.
Lines 227-238 Link Here
227
     */
313
     */
228
        ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
314
        ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
229
             "proxy: CONNECT: sending the CONNECT request to the remote proxy");
315
             "proxy: CONNECT: sending the CONNECT request to the remote proxy");
230
        nbytes = apr_snprintf(buffer, sizeof(buffer),
316
        ap_fprintf(backconn->output_filters, bb,
231
                  "CONNECT %s HTTP/1.0" CRLF, r->uri);
317
                  "CONNECT %s HTTP/1.0" CRLF, r->uri);
232
        apr_socket_send(sock, buffer, &nbytes);
318
        ap_fprintf(backconn->output_filters, bb,
233
        nbytes = apr_snprintf(buffer, sizeof(buffer),
319
            "Proxy-agent: %s" CRLF CRLF, ap_get_server_version());
234
                  "Proxy-agent: %s" CRLF CRLF, ap_get_server_banner());
320
        ap_fflush(backconn->output_filters, bb);
235
        apr_socket_send(sock, buffer, &nbytes);
236
    }
321
    }
237
    else {
322
    else {
238
        ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
323
        ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
Lines 240-250 Link Here
240
        nbytes = apr_snprintf(buffer, sizeof(buffer),
325
        nbytes = apr_snprintf(buffer, sizeof(buffer),
241
                  "HTTP/1.0 200 Connection Established" CRLF);
326
                  "HTTP/1.0 200 Connection Established" CRLF);
242
        ap_xlate_proto_to_ascii(buffer, nbytes);
327
        ap_xlate_proto_to_ascii(buffer, nbytes);
243
        apr_socket_send(client_socket, buffer, &nbytes);
328
        ap_fwrite(c->output_filters, bb, buffer, nbytes);
244
        nbytes = apr_snprintf(buffer, sizeof(buffer),
329
        nbytes = apr_snprintf(buffer, sizeof(buffer),
245
                  "Proxy-agent: %s" CRLF CRLF, ap_get_server_banner());
330
                  "Proxy-agent: %s" CRLF CRLF, ap_get_server_banner());
246
        ap_xlate_proto_to_ascii(buffer, nbytes);
331
        ap_xlate_proto_to_ascii(buffer, nbytes);
247
        apr_socket_send(client_socket, buffer, &nbytes);
332
        ap_fwrite(c->output_filters, bb, buffer, nbytes);
333
        ap_fflush(c->output_filters, bb);
248
#if 0
334
#if 0
249
        /* This is safer code, but it doesn't work yet.  I'm leaving it
335
        /* This is safer code, but it doesn't work yet.  I'm leaving it
250
         * here so that I can fix it later.
336
         * here so that I can fix it later.
Lines 264-291 Link Here
264
     *
350
     *
265
     * Handle two way transfer of data over the socket (this is a tunnel).
351
     * Handle two way transfer of data over the socket (this is a tunnel).
266
     */
352
     */
353
     /* we are now acting as a tunnel - the input/output filter stacks should
354
      * not contain any non-connection filters.
355
      */
356
     r->output_filters = c->output_filters;
357
     r->proto_output_filters = c->output_filters;
358
     r->input_filters = c->input_filters;
359
     r->proto_input_filters = c->input_filters;
267
360
268
/*    r->sent_bodyct = 1;*/
361
/*    r->sent_bodyct = 1;*/
269
362
270
    if ((rv = apr_pollset_create(&pollset, 2, r->pool, 0)) != APR_SUCCESS) {
271
        apr_socket_close(sock);
272
        ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
273
            "proxy: CONNECT: error apr_pollset_create()");
274
        return HTTP_INTERNAL_SERVER_ERROR;
275
    }
276
277
    /* Add client side to the poll */
278
    pollfd.p = r->pool;
279
    pollfd.desc_type = APR_POLL_SOCKET;
280
    pollfd.reqevents = APR_POLLIN;
281
    pollfd.desc.s = client_socket;
282
    pollfd.client_data = NULL;
283
    apr_pollset_add(pollset, &pollfd);
284
285
    /* Add the server side to the poll */
286
    pollfd.desc.s = sock;
287
    apr_pollset_add(pollset, &pollfd);
288
289
    while (1) { /* Infinite loop until error (one side closes the connection) */
363
    while (1) { /* Infinite loop until error (one side closes the connection) */
290
        if ((rv = apr_pollset_poll(pollset, -1, &pollcnt, &signalled)) != APR_SUCCESS) {
364
        if ((rv = apr_pollset_poll(pollset, -1, &pollcnt, &signalled)) != APR_SUCCESS) {
291
            apr_socket_close(sock);
365
            apr_socket_close(sock);
Lines 294-300 Link Here
294
        }
368
        }
295
#ifdef DEBUGGING
369
#ifdef DEBUGGING
296
        ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
370
        ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
297
                     "proxy: CONNECT: woke from select(), i=%d", pollcnt);
371
                     "proxy: CONNECT: woke from poll(), i=%d", pollcnt);
298
#endif
372
#endif
299
373
300
        for (pi = 0; pi < pollcnt; pi++) {
374
        for (pi = 0; pi < pollcnt; pi++) {
Lines 304-375 Link Here
304
                pollevent = cur->rtnevents;
378
                pollevent = cur->rtnevents;
305
                if (pollevent & APR_POLLIN) {
379
                if (pollevent & APR_POLLIN) {
306
#ifdef DEBUGGING
380
#ifdef DEBUGGING
307
                    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
381
                    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r,
308
                                 "proxy: CONNECT: sock was set");
382
                                 "proxy: CONNECT: sock was readable");
309
#endif
383
#endif
310
                    nbytes = sizeof(buffer);
384
                    rv = proxy_connect_transfer(r, backconn, c, bb, "sock");
311
                    rv = apr_socket_recv(sock, buffer, &nbytes);
312
                    if (rv == APR_SUCCESS) {
313
                        o = 0;
314
                        i = nbytes;
315
                        while(i > 0)
316
                        {
317
                            nbytes = i;
318
    /* This is just plain wrong.  No module should ever write directly
319
     * to the client.  For now, this works, but this is high on my list of
320
     * things to fix.  The correct line is:
321
     * if ((nbytes = ap_rwrite(buffer + o, nbytes, r)) < 0)
322
     * rbb
323
     */
324
                            rv = apr_socket_send(client_socket, buffer + o, &nbytes);
325
                            if (rv != APR_SUCCESS)
326
                                break;
327
                            o += nbytes;
328
                            i -= nbytes;
329
                        }
330
                    }
385
                    }
331
                    else
386
                    else if ((pollevent & APR_POLLERR) || (pollevent & APR_POLLHUP)) {
332
                        break;
387
                        rv = APR_EPIPE;
388
                        ap_log_rerror(APLOG_MARK, APLOG_NOTICE, 0, r, "proxy: CONNECT: err/hup on backconn");
333
                }
389
                }
334
                else if ((pollevent & APR_POLLERR) || (pollevent & APR_POLLHUP))
335
                    break;
336
            }
390
            }
337
            else if (cur->desc.s == client_socket) {
391
            else if (cur->desc.s == client_socket) {
338
                pollevent = cur->rtnevents;
392
                pollevent = cur->rtnevents;
339
                if (pollevent & APR_POLLIN) {
393
                if (pollevent & APR_POLLIN) {
340
#ifdef DEBUGGING
394
#ifdef DEBUGGING
341
                    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
395
                        ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r,
342
                                 "proxy: CONNECT: client was set");
396
                                     "proxy: CONNECT: client was readable");
343
#endif
344
                    nbytes = sizeof(buffer);
345
                    rv = apr_socket_recv(client_socket, buffer, &nbytes);
346
                    if (rv == APR_SUCCESS) {
347
                        o = 0;
348
                        i = nbytes;
349
#ifdef DEBUGGING
350
                        ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
351
                                     "proxy: CONNECT: read %d from client", i);
352
#endif
397
#endif
353
                        while(i > 0)
398
                    rv = proxy_connect_transfer(r, c, backconn, bb, "client");
354
                        {
355
                            nbytes = i;
356
                            rv = apr_socket_send(sock, buffer + o, &nbytes);
357
                            if (rv != APR_SUCCESS)
358
                                break;
359
                            o += nbytes;
360
                            i -= nbytes;
361
                        }
362
                    }
363
                    else
364
                        break;
365
                }
399
                }
366
                else if ((pollevent & APR_POLLERR) || (pollevent & APR_POLLHUP)) {
367
                    rv = APR_EOF;
368
                    break;
369
                }
400
                }
401
                else {
402
                    rv = APR_EBADF;
403
                    ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
404
                     "proxy: CONNECT: unknown socket in pollset");
370
            }
405
            }
371
            else
372
                break;
373
        }
406
        }
374
        if (rv != APR_SUCCESS) {
407
        if (rv != APR_SUCCESS) {
375
            break;
408
            break;
Lines 385-391 Link Here
385
     * Close the socket and clean up
418
     * Close the socket and clean up
386
     */
419
     */
387
420
388
    apr_socket_close(sock);
421
    ap_lingering_close(backconn);
422
423
    c->aborted = 1;
389
424
390
    return OK;
425
    return OK;
391
}
426
}

Return to bug 29744