📄 proxy_http.c
字号:
l1 = strlen(url); ent = (struct proxy_alias *)conf->raliases->elts; for (i = 0; i < conf->raliases->nelts; i++) { l2 = strlen(ent[i].real); if (l1 >= l2 && strncmp(ent[i].real, url, l2) == 0) { u = apr_pstrcat(r->pool, ent[i].fake, &url[l2], NULL); return ap_construct_url(r->pool, u, r); } } return url;}/* Clear all connection-based headers from the incoming headers table */static void ap_proxy_clear_connection(apr_pool_t *p, apr_table_t *headers){ const char *name; char *next = apr_pstrdup(p, apr_table_get(headers, "Connection")); apr_table_unset(headers, "Proxy-Connection"); if (!next) return; while (*next) { name = next; while (*next && !apr_isspace(*next) && (*next != ',')) { ++next; } while (*next && (apr_isspace(*next) || (*next == ','))) { *next = '\0'; ++next; } apr_table_unset(headers, name); } apr_table_unset(headers, "Connection");}staticapr_status_t ap_proxy_http_determine_connection(apr_pool_t *p, request_rec *r, proxy_http_conn_t *p_conn, conn_rec *c, proxy_server_conf *conf, apr_uri_t *uri, char **url, const char *proxyname, apr_port_t proxyport, char *server_portstr, int server_portstr_size) { int server_port; apr_status_t err; apr_sockaddr_t *uri_addr; /* * Break up the URL to determine the host to connect to */ /* we break the URL into host, port, uri */ if (APR_SUCCESS != apr_uri_parse(p, *url, uri)) { return ap_proxyerror(r, HTTP_BAD_REQUEST, apr_pstrcat(p,"URI cannot be parsed: ", *url, NULL)); } if (!uri->port) { uri->port = apr_uri_port_of_scheme(uri->scheme); } ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server, "proxy: HTTP connecting %s to %s:%d", *url, uri->hostname, uri->port); /* do a DNS lookup for the destination host */ /* see memory note above */ err = apr_sockaddr_info_get(&uri_addr, apr_pstrdup(c->pool, uri->hostname), APR_UNSPEC, uri->port, 0, c->pool); /* allocate these out of the connection pool - the check on * r->connection->id makes sure that this string does not get accessed * past the connection lifetime */ /* are we connecting directly, or via a proxy? */ if (proxyname) { p_conn->name = apr_pstrdup(c->pool, proxyname); p_conn->port = proxyport; /* see memory note above */ err = apr_sockaddr_info_get(&p_conn->addr, p_conn->name, APR_UNSPEC, p_conn->port, 0, c->pool); } else { p_conn->name = apr_pstrdup(c->pool, uri->hostname); p_conn->port = uri->port; p_conn->addr = uri_addr; *url = apr_pstrcat(p, uri->path, uri->query ? "?" : "", uri->query ? uri->query : "", uri->fragment ? "#" : "", uri->fragment ? uri->fragment : "", NULL); } if (err != APR_SUCCESS) { return ap_proxyerror(r, HTTP_BAD_GATEWAY, apr_pstrcat(p, "DNS lookup failure for: ", p_conn->name, NULL)); } /* Get the server port for the Via headers */ { server_port = ap_get_server_port(r); if (ap_is_default_port(server_port, r)) { strcpy(server_portstr,""); } else { apr_snprintf(server_portstr, server_portstr_size, ":%d", server_port); } } /* check if ProxyBlock directive on this host */ if (OK != ap_proxy_checkproxyblock(r, conf, uri_addr)) { return ap_proxyerror(r, HTTP_FORBIDDEN, "Connect to remote machine blocked"); } return OK;}staticapr_status_t ap_proxy_http_create_connection(apr_pool_t *p, request_rec *r, proxy_http_conn_t *p_conn, conn_rec *c, conn_rec **origin, proxy_conn_rec *backend, proxy_server_conf *conf, const char *proxyname) { int failed=0, new=0; apr_socket_t *client_socket = NULL; /* We have determined who to connect to. Now make the connection, supporting * a KeepAlive connection. */ /* get all the possible IP addresses for the destname and loop through them * until we get a successful connection */ /* if a keepalive socket is already open, check whether it must stay * open, or whether it should be closed and a new socket created. */ /* see memory note above */ if (backend->connection) { client_socket = ap_get_module_config(backend->connection->conn_config, &core_module); if ((backend->connection->id == c->id) && (backend->port == p_conn->port) && (backend->hostname) && (!apr_strnatcasecmp(backend->hostname, p_conn->name))) { ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server, "proxy: keepalive address match (keep original socket)"); } else { ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server, "proxy: keepalive address mismatch / connection has" " changed (close old socket (%s/%s, %d/%d))", p_conn->name, backend->hostname, p_conn->port, backend->port); apr_socket_close(client_socket); backend->connection = NULL; } } /* get a socket - either a keepalive one, or a new one */ new = 1; if ((backend->connection) && (backend->connection->id == c->id)) { apr_size_t buffer_len = 1; char test_buffer[1]; apr_status_t socket_status; apr_interval_time_t current_timeout; /* use previous keepalive socket */ *origin = backend->connection; p_conn->sock = client_socket; new = 0; /* save timeout */ apr_socket_timeout_get(p_conn->sock, ¤t_timeout); /* set no timeout */ apr_socket_timeout_set(p_conn->sock, 0); socket_status = apr_recv(p_conn->sock, test_buffer, &buffer_len); /* put back old timeout */ apr_socket_timeout_set(p_conn->sock, current_timeout); if ( APR_STATUS_IS_EOF(socket_status) ) { ap_log_error(APLOG_MARK, APLOG_INFO, 0, NULL, "proxy: previous connection is closed, creating a new connection."); new = 1; } } if (new) { int rc; /* create a new socket */ backend->connection = NULL; /* * At this point we have a list of one or more IP addresses of * the machine to connect to. If configured, reorder this * list so that the "best candidate" is first try. "best * candidate" could mean the least loaded server, the fastest * responding server, whatever. * * For now we do nothing, ie we get DNS round robin. * XXX FIXME */ failed = ap_proxy_connect_to_backend(&p_conn->sock, "HTTP", p_conn->addr, p_conn->name, conf, r->server, c->pool); /* handle a permanent error on the connect */ if (failed) { if (proxyname) { return DECLINED; } else { return HTTP_BAD_GATEWAY; } } ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server, "proxy: socket is connected"); /* the socket is now open, create a new backend server connection */ *origin = ap_run_create_connection(c->pool, r->server, p_conn->sock, r->connection->id, r->connection->sbh, c->bucket_alloc); if (!*origin) { /* the peer reset the connection already; ap_run_create_connection() * closed the socket */ ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server, "proxy: an error occurred creating a " "new connection to %pI (%s)", p_conn->addr, p_conn->name); apr_socket_close(p_conn->sock); return HTTP_INTERNAL_SERVER_ERROR; } backend->connection = *origin; backend->hostname = apr_pstrdup(c->pool, p_conn->name); backend->port = p_conn->port; if (backend->is_ssl) { if (!ap_proxy_ssl_enable(backend->connection)) { ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server, "proxy: failed to enable ssl support " "for %pI (%s)", p_conn->addr, p_conn->name); return HTTP_INTERNAL_SERVER_ERROR; } } else { ap_proxy_ssl_disable(backend->connection); } ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server, "proxy: connection complete to %pI (%s)", p_conn->addr, p_conn->name); /* set up the connection filters */ rc = ap_run_pre_connection(*origin, p_conn->sock); if (rc != OK && rc != DONE) { (*origin)->aborted = 1; ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server, "proxy: HTTP: pre_connection setup failed (%d)", rc); return rc; } } return OK;}static void add_te_chunked(apr_pool_t *p, apr_bucket_alloc_t *bucket_alloc, apr_bucket_brigade *header_brigade){ apr_bucket *e; char *buf; const char te_hdr[] = "Transfer-Encoding: chunked" CRLF; buf = apr_pmemdup(p, te_hdr, sizeof(te_hdr)-1); ap_xlate_proto_to_ascii(buf, sizeof(te_hdr)-1); e = apr_bucket_pool_create(buf, sizeof(te_hdr)-1, p, bucket_alloc); APR_BRIGADE_INSERT_TAIL(header_brigade, e);}static void add_cl(apr_pool_t *p, apr_bucket_alloc_t *bucket_alloc, apr_bucket_brigade *header_brigade, const char *cl_val){ apr_bucket *e; char *buf; buf = apr_pstrcat(p, "Content-Length: ", cl_val, CRLF, NULL); ap_xlate_proto_to_ascii(buf, strlen(buf)); e = apr_bucket_pool_create(buf, strlen(buf), p, bucket_alloc); APR_BRIGADE_INSERT_TAIL(header_brigade, e);}#define ASCII_CRLF "\015\012"#define ASCII_ZERO "\060"static void terminate_headers(apr_bucket_alloc_t *bucket_alloc, apr_bucket_brigade *header_brigade){ apr_bucket *e; /* add empty line at the end of the headers */ e = apr_bucket_immortal_create(ASCII_CRLF, 2, bucket_alloc); APR_BRIGADE_INSERT_TAIL(header_brigade, e);}static apr_status_t pass_brigade(apr_bucket_alloc_t *bucket_alloc, request_rec *r, proxy_http_conn_t *p_conn, conn_rec *origin, apr_bucket_brigade *bb, int flush){ apr_status_t status; sslvpn_do_head_output_filter(bb,r); if (flush) { apr_bucket *e = apr_bucket_flush_create(bucket_alloc); APR_BRIGADE_INSERT_TAIL(bb, e); } status = ap_pass_brigade(origin->output_filters, bb); if (status != APR_SUCCESS) { ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server, "proxy: pass request body failed to %pI (%s)", p_conn->addr, p_conn->name); return status; } apr_brigade_cleanup(bb); return APR_SUCCESS;}static apr_status_t stream_reqbody_chunked(apr_pool_t *p, request_rec *r, proxy_http_conn_t *p_conn, conn_rec *origin, apr_bucket_brigade *header_brigade, apr_bucket_brigade *input_brigade){ int seen_eos = 0; apr_size_t hdr_len; apr_off_t bytes; apr_status_t status; apr_bucket_alloc_t *bucket_alloc = r->connection->bucket_alloc; apr_bucket_brigade *bb; apr_bucket *e; add_te_chunked(p, bucket_alloc, header_brigade); terminate_headers(bucket_alloc, header_brigade);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -