--- httpd-2.0.52-orig/modules/proxy/proxy_connect.c 2004-02-09 12:53:19.000000000 -0800 +++ httpd-2.0.52-orig/modules/proxy/proxy_connect.c 2004-10-20 16:09:26.000000000 -0700 @@ -83,10 +83,14 @@ { apr_pool_t *p = r->pool; apr_socket_t *sock; + conn_rec *c = r->connection; + conn_rec *backconn; + apr_bucket_brigade *bb = apr_brigade_create(p, c->bucket_alloc); + apr_status_t err, rv; apr_size_t i, o, nbytes; char buffer[HUGE_STRING_LEN]; - apr_socket_t *client_socket = ap_get_module_config(r->connection->conn_config, &core_module); + apr_socket_t *client_socket = ap_get_module_config(c->conn_config, &core_module); int failed; apr_pollfd_t *pollfd; apr_int32_t pollcnt; @@ -211,7 +215,23 @@ * We add the NULL filter to the stack to do this... */ r->output_filters = NULL; - r->connection->output_filters = NULL; + + backconn = ap_run_create_connection(c->pool, r->server, sock, + c->id, c->sbh, c->bucket_alloc); + if(!backconn) { + /* peer reset */ + ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server, + "proxy: an error occurred creating a new connection " + "to %pI (%s)", connect_addr, connectname); + apr_socket_close(sock); + return HTTP_INTERNAL_SERVER_ERROR; + } + ap_proxy_ssl_disable(backconn); + ap_run_pre_connection(backconn, sock); + + ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server, + "proxy: connection complete to %pI (%s)", + connect_addr, connectname); /* If we are connecting through a remote proxy, we need to pass @@ -222,12 +242,12 @@ */ ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server, "proxy: CONNECT: sending the CONNECT request to the remote proxy"); - nbytes = apr_snprintf(buffer, sizeof(buffer), + + ap_fprintf(backconn->output_filters, bb, "CONNECT %s HTTP/1.0" CRLF, r->uri); - apr_send(sock, buffer, &nbytes); - nbytes = apr_snprintf(buffer, sizeof(buffer), - "Proxy-agent: %s" CRLF CRLF, ap_get_server_version()); - apr_send(sock, buffer, &nbytes); + ap_fprintf(backconn->output_filters, bb, + "Proxy-agent: %s" CRLF CRLF, ap_get_server_version()); + ap_fflush(backconn->output_filters, bb); } else { ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server, @@ -235,11 +255,12 @@ nbytes = apr_snprintf(buffer, sizeof(buffer), "HTTP/1.0 200 Connection Established" CRLF); ap_xlate_proto_to_ascii(buffer, nbytes); - apr_send(client_socket, buffer, &nbytes); + ap_fwrite(c->output_filters, bb, buffer, nbytes); nbytes = apr_snprintf(buffer, sizeof(buffer), "Proxy-agent: %s" CRLF CRLF, ap_get_server_version()); ap_xlate_proto_to_ascii(buffer, nbytes); - apr_send(client_socket, buffer, &nbytes); + ap_fwrite(c->output_filters, bb, buffer, nbytes); + ap_fflush(c->output_filters, bb); #if 0 /* This is safer code, but it doesn't work yet. I'm leaving it * here so that I can fix it later. @@ -293,23 +314,13 @@ /* ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server, "proxy: CONNECT: sock was set");*/ nbytes = sizeof(buffer); - if (apr_recv(sock, buffer, &nbytes) == APR_SUCCESS) { - o = 0; - i = nbytes; - while(i > 0) - { - nbytes = i; - /* This is just plain wrong. No module should ever write directly - * to the client. For now, this works, but this is high on my list of - * things to fix. The correct line is: - * if ((nbytes = ap_rwrite(buffer + o, nbytes, r)) < 0) - * rbb - */ - if (apr_send(client_socket, buffer + o, &nbytes) != APR_SUCCESS) - break; - o += nbytes; - i -= nbytes; - } + apr_brigade_cleanup(bb); + if (ap_get_brigade(backconn->input_filters, bb, + AP_MODE_READBYTES, APR_NONBLOCK_READ, + nbytes) == APR_SUCCESS) { + if (ap_pass_brigade(c->output_filters, bb) != APR_SUCCESS) + break; + ap_fflush(c->output_filters, bb); } else break; @@ -323,17 +334,13 @@ /* ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server, "proxy: CONNECT: client was set");*/ nbytes = sizeof(buffer); - if (apr_recv(client_socket, buffer, &nbytes) == APR_SUCCESS) { - o = 0; - i = nbytes; - while(i > 0) - { - nbytes = i; - if (apr_send(sock, buffer + o, &nbytes) != APR_SUCCESS) - break; - o += nbytes; - i -= nbytes; - } + apr_brigade_cleanup(bb); + if (ap_get_brigade(c->input_filters, bb, AP_MODE_READBYTES, + APR_NONBLOCK_READ, nbytes) == APR_SUCCESS) { + if (ap_pass_brigade(backconn->output_filters, bb) + != APR_SUCCESS) + break; + ap_fflush(backconn->output_filters, bb); } else break;