📄 mod_proxy_http.c
字号:
/* Copyright 1999-2005 The Apache Software Foundation or its licensors, as * applicable. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *//* HTTP routines for Apache proxy */#include "mod_proxy.h"module AP_MODULE_DECLARE_DATA proxy_http_module;static apr_status_t ap_proxy_http_cleanup(const char *scheme, request_rec *r, proxy_conn_rec *backend);/* * Canonicalise http-like URLs. * scheme is the scheme for the URL * url is the URL starting with the first '/' * def_port is the default port for this scheme. */static int proxy_http_canon(request_rec *r, char *url){ char *host, *path, *search, sport[7]; const char *err; const char *scheme; apr_port_t port, def_port; /* ap_port_of_scheme() */ if (strncasecmp(url, "http:", 5) == 0) { url += 5; scheme = "http"; } else if (strncasecmp(url, "https:", 6) == 0) { url += 6; scheme = "https"; } else { return DECLINED; } def_port = apr_uri_port_of_scheme(scheme); ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server, "proxy: HTTP: canonicalising URL %s", url); /* do syntatic check. * We break the URL into host, port, path, search */ port = def_port; err = ap_proxy_canon_netloc(r->pool, &url, NULL, NULL, &host, &port); if (err) { ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "error parsing URL %s: %s", url, err); return HTTP_BAD_REQUEST; } /* now parse path/search args, according to rfc1738 */ /* N.B. if this isn't a true proxy request, then the URL _path_ * has already been decoded. True proxy requests have r->uri * == r->unparsed_uri, and no others have that property. */ if (r->uri == r->unparsed_uri) { search = strchr(url, '?'); if (search != NULL) *(search++) = '\0'; } else search = r->args; /* process path */ path = ap_proxy_canonenc(r->pool, url, strlen(url), enc_path, 0, r->proxyreq); if (path == NULL) return HTTP_BAD_REQUEST; if (port != def_port) apr_snprintf(sport, sizeof(sport), ":%d", port); else sport[0] = '\0'; if (ap_strchr_c(host, ':')) { /* if literal IPv6 address */ host = apr_pstrcat(r->pool, "[", host, "]", NULL); } r->filename = apr_pstrcat(r->pool, "proxy:", scheme, "://", host, sport, "/", path, (search) ? "?" : "", (search) ? search : "", NULL); return OK;}/* 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");}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_conn_rec *conn, conn_rec *origin, apr_bucket_brigade *bb, int flush){ apr_status_t status; apr_off_t transferred; if (flush) { apr_bucket *e = apr_bucket_flush_create(bucket_alloc); APR_BRIGADE_INSERT_TAIL(bb, e); } apr_brigade_length(bb, 0, &transferred); if (transferred != -1) conn->worker->s->transferred += transferred; 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)", conn->addr, conn->hostname); return status; } apr_brigade_cleanup(bb); return APR_SUCCESS;}#define MAX_MEM_SPOOL 16384static apr_status_t stream_reqbody_chunked(apr_pool_t *p, request_rec *r, proxy_conn_rec *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); while (!APR_BUCKET_IS_EOS(APR_BRIGADE_FIRST(input_brigade))) { char chunk_hdr[20]; /* must be here due to transient bucket. */ /* If this brigade contains EOS, either stop or remove it. */ if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(input_brigade))) { seen_eos = 1; /* We can't pass this EOS to the output_filters. */ e = APR_BRIGADE_LAST(input_brigade); apr_bucket_delete(e); } apr_brigade_length(input_brigade, 1, &bytes); hdr_len = apr_snprintf(chunk_hdr, sizeof(chunk_hdr), "%" APR_UINT64_T_HEX_FMT CRLF, (apr_uint64_t)bytes); ap_xlate_proto_to_ascii(chunk_hdr, hdr_len); e = apr_bucket_transient_create(chunk_hdr, hdr_len, bucket_alloc); APR_BRIGADE_INSERT_HEAD(input_brigade, e); /* * Append the end-of-chunk CRLF */ e = apr_bucket_immortal_create(ASCII_CRLF, 2, bucket_alloc); APR_BRIGADE_INSERT_TAIL(input_brigade, e); if (header_brigade) { /* we never sent the header brigade, so go ahead and * take care of that now */ bb = header_brigade; /* * Save input_brigade in bb brigade. (At least) in the SSL case * input_brigade contains transient buckets whose data would get * overwritten during the next call of ap_get_brigade in the loop. * ap_save_brigade ensures these buckets to be set aside. * Calling ap_save_brigade with NULL as filter is OK, because * bb brigade already has been created and does not need to get * created by ap_save_brigade. */ status = ap_save_brigade(NULL, &bb, &input_brigade, p); if (status != APR_SUCCESS) { return status; } header_brigade = NULL; } else { bb = input_brigade; } /* The request is flushed below this loop with chunk EOS header */ status = pass_brigade(bucket_alloc, r, p_conn, origin, bb, 0); if (status != APR_SUCCESS) { return status; } if (seen_eos) { break; } status = ap_get_brigade(r->input_filters, input_brigade, AP_MODE_READBYTES, APR_BLOCK_READ, HUGE_STRING_LEN); if (status != APR_SUCCESS) { return status; } } if (header_brigade) { /* we never sent the header brigade because there was no request body; * send it now */ bb = header_brigade; } else { if (!APR_BRIGADE_EMPTY(input_brigade)) { /* input brigade still has an EOS which we can't pass to the output_filters. */ e = APR_BRIGADE_LAST(input_brigade); AP_DEBUG_ASSERT(APR_BUCKET_IS_EOS(e)); apr_bucket_delete(e); } bb = input_brigade; } e = apr_bucket_immortal_create(ASCII_ZERO ASCII_CRLF /* <trailers> */ ASCII_CRLF, 5, bucket_alloc); APR_BRIGADE_INSERT_TAIL(bb, e); /* Now we have headers-only, or the chunk EOS mark; flush it */ status = pass_brigade(bucket_alloc, r, p_conn, origin, bb, 1); return status;}static apr_status_t stream_reqbody_cl(apr_pool_t *p, request_rec *r, proxy_conn_rec *p_conn, conn_rec *origin, apr_bucket_brigade *header_brigade, apr_bucket_brigade *input_brigade, const char *old_cl_val){ int seen_eos = 0; apr_status_t status = APR_SUCCESS; apr_bucket_alloc_t *bucket_alloc = r->connection->bucket_alloc; apr_bucket_brigade *bb; apr_bucket *e; apr_off_t cl_val = 0; apr_off_t bytes; apr_off_t bytes_streamed = 0; if (old_cl_val) { add_cl(p, bucket_alloc, header_brigade, old_cl_val);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -