proxy_util.c revision f66d52ac5f701eed7986e23d12960344f8bf6b61
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele/* Licensed to the Apache Software Foundation (ASF) under one or more
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * contributor license agreements. See the NOTICE file distributed with
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * this work for additional information regarding copyright ownership.
5f5d1b4cc970b7f06ff8ef6526128e9a27303d88nd * The ASF licenses this file to You under the Apache License, Version 2.0
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * (the "License"); you may not use this file except in compliance with
1aa933455fcd538b1ee573f4566e1a78a89fce77nd * the License. You may obtain a copy of the License at
db479b48bd4d75423ed4a45e15b75089d1a8ad72fielding * Unless required by applicable law or agreed to in writing, software
db479b48bd4d75423ed4a45e15b75089d1a8ad72fielding * distributed under the License is distributed on an "AS IS" BASIS,
db479b48bd4d75423ed4a45e15b75089d1a8ad72fielding * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1aa933455fcd538b1ee573f4566e1a78a89fce77nd * See the License for the specific language governing permissions and
1aa933455fcd538b1ee573f4566e1a78a89fce77nd * limitations under the License.
1aa933455fcd538b1ee573f4566e1a78a89fce77nd/* Utility routines for Apache proxy */
5d7e5de2da57434c8e68c8fa49cbf6d70ee0f817slive * Opaque structure containing target server info when
5d7e5de2da57434c8e68c8fa49cbf6d70ee0f817slive * using a forward proxy.
5d7e5de2da57434c8e68c8fa49cbf6d70ee0f817slive * Up to now only used in combination with HTTP CONNECT.
5d7e5de2da57434c8e68c8fa49cbf6d70ee0f817slivetypedef struct {
5d7e5de2da57434c8e68c8fa49cbf6d70ee0f817slive int use_http_connect; /* Use SSL Tunneling via HTTP CONNECT */
5d7e5de2da57434c8e68c8fa49cbf6d70ee0f817slive/* Keep synced with mod_proxy.h! */
5d7e5de2da57434c8e68c8fa49cbf6d70ee0f817slivestatic struct wstat {
5d7e5de2da57434c8e68c8fa49cbf6d70ee0f817slive unsigned int bit;
5d7e5de2da57434c8e68c8fa49cbf6d70ee0f817slive const char *name;
5d7e5de2da57434c8e68c8fa49cbf6d70ee0f817slive {PROXY_WORKER_INITIALIZED, PROXY_WORKER_INITIALIZED_FLAG, "Init "},
5d7e5de2da57434c8e68c8fa49cbf6d70ee0f817slive {PROXY_WORKER_IGNORE_ERRORS, PROXY_WORKER_IGNORE_ERRORS_FLAG, "Ign "},
aa8cf57195dfb7fa3d0baedf81f8be377946cea8slive {PROXY_WORKER_DRAIN, PROXY_WORKER_DRAIN_FLAG, "Drn "},
aa8cf57195dfb7fa3d0baedf81f8be377946cea8slive {PROXY_WORKER_IN_SHUTDOWN, PROXY_WORKER_IN_SHUTDOWN_FLAG, "Shut "},
aa8cf57195dfb7fa3d0baedf81f8be377946cea8slive {PROXY_WORKER_DISABLED, PROXY_WORKER_DISABLED_FLAG, "Dis "},
aa8cf57195dfb7fa3d0baedf81f8be377946cea8slive {PROXY_WORKER_STOPPED, PROXY_WORKER_STOPPED_FLAG, "Stop "},
06d77ae37da42a6f8bbea25b7d7f8b6629245629slive {PROXY_WORKER_IN_ERROR, PROXY_WORKER_IN_ERROR_FLAG, "Err "},
aa8cf57195dfb7fa3d0baedf81f8be377946cea8slive {PROXY_WORKER_HOT_STANDBY, PROXY_WORKER_HOT_STANDBY_FLAG, "Stby "},
c6f41bc69d643835804e7e831776d3d46c6f5962slive/* Global balancer counter */
c6f41bc69d643835804e7e831776d3d46c6f5962slivestatic int lb_workers_limit = 0;
c6f41bc69d643835804e7e831776d3d46c6f5962sliveconst apr_strmatch_pattern PROXY_DECLARE_DATA *ap_proxy_strmatch_path;
d7604f90897d9b08b227c127ff5392393178911crpluemconst apr_strmatch_pattern PROXY_DECLARE_DATA *ap_proxy_strmatch_domain;
aa8cf57195dfb7fa3d0baedf81f8be377946cea8slivestatic int proxy_match_ipaddr(struct dirconn_entry *This, request_rec *r);
aa8cf57195dfb7fa3d0baedf81f8be377946cea8slivestatic int proxy_match_domainname(struct dirconn_entry *This, request_rec *r);
aa8cf57195dfb7fa3d0baedf81f8be377946cea8slivestatic int proxy_match_hostname(struct dirconn_entry *This, request_rec *r);
aa8cf57195dfb7fa3d0baedf81f8be377946cea8slivestatic int proxy_match_word(struct dirconn_entry *This, request_rec *r);
aa8cf57195dfb7fa3d0baedf81f8be377946cea8sliveAPR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(proxy, PROXY, int, create_req,
d7604f90897d9b08b227c127ff5392393178911crpluemPROXY_DECLARE(apr_status_t) ap_proxy_strncpy(char *dst, const char *src, size_t dlen)
d7604f90897d9b08b227c127ff5392393178911crpluem /* APR_ENOSPACE would be better */
c6f41bc69d643835804e7e831776d3d46c6f5962slive/* already called in the knowledge that the characters are hex digits */
c6f41bc69d643835804e7e831776d3d46c6f5962slive int ch = x[0];
d7604f90897d9b08b227c127ff5392393178911crpluem#else /*APR_CHARSET_EBCDIC*/
aa8cf57195dfb7fa3d0baedf81f8be377946cea8slive * we assume that the hex value refers to an ASCII character
aa8cf57195dfb7fa3d0baedf81f8be377946cea8slive * so convert to EBCDIC so that it makes sense locally;
aa8cf57195dfb7fa3d0baedf81f8be377946cea8slive * client specifies %20 in URL to refer to a space char;
aa8cf57195dfb7fa3d0baedf81f8be377946cea8slive * at this point we're called with EBCDIC "20"; after turning
aa8cf57195dfb7fa3d0baedf81f8be377946cea8slive * EBCDIC "20" into binary 0x20, we then need to assume that 0x20
aa8cf57195dfb7fa3d0baedf81f8be377946cea8slive * represents an ASCII char and convert 0x20 to EBCDIC, yielding
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele#endif /*APR_CHARSET_EBCDIC*/
4ab980a06412fd86f52a6d054fb7e26de155c530erikabelePROXY_DECLARE(void) ap_proxy_c2hex(int ch, char *x)
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele if (i >= 10) {
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele if (i >= 10) {
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele#else /*APR_CHARSET_EBCDIC*/
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele static const char ntoa[] = { "0123456789ABCDEF" };
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele#endif /*APR_CHARSET_EBCDIC*/
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * canonicalise a URL-encoded string
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * Convert a URL-encoded string to canonical form.
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * It decodes characters which need not be encoded,
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * and encodes those which must be encoded, and does not touch
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * those which must not be touched.
4ab980a06412fd86f52a6d054fb7e26de155c530erikabelePROXY_DECLARE(char *)ap_proxy_canonenc(apr_pool_t *p, const char *x, int len,
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele char *allowed; /* characters which should not be encoded */
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele char *reserved; /* characters which much not be en/de-coded */
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * N.B. in addition to :@&=, this allows ';' in an http path
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * and '?' in an ftp path -- this may be revised
10a304fc5348d394375b98ae10ca9b137fd10cafkess * Also, it makes a '+' character in a search string reserved, as
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * it may be form-encoded. (Although RFC 1738 doesn't allow this -
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * it only permits ; / ? : @ = & as reserved chars.)
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele else if (t == enc_search) {
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele else if (t == enc_user) {
10a304fc5348d394375b98ae10ca9b137fd10cafkess else if (t == enc_fpath) {
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele else { /* if (t == enc_parm) */
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele else if (t == enc_search) {
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele for (i = 0, j = 0; i < len; i++, j++) {
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele/* always handle '/' first */
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * decode it if not already done. do not decode reverse proxied URLs
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * unless specifically forced
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele if ((forcedec || (proxyreq && proxyreq != PROXYREQ_REVERSE)) && ch == '%') {
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele if (!apr_isxdigit(x[i + 1]) || !apr_isxdigit(x[i + 2])) {
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele if (ch != 0 && strchr(reserved, ch)) { /* keep it encoded */
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele/* recode it, if necessary */
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele y[j] = '\0';
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * Parses network-location.
a3388213b2b4d46b356be205e38204e67b4304d8rbowen * urlp on input the URL; on output the path, after the leading /
a3388213b2b4d46b356be205e38204e67b4304d8rbowen * user NULL if no user/password permitted
a3388213b2b4d46b356be205e38204e67b4304d8rbowen * password holder for password
a3388213b2b4d46b356be205e38204e67b4304d8rbowen * host holder for host
a3388213b2b4d46b356be205e38204e67b4304d8rbowen * port port number; only set if one is supplied.
a3388213b2b4d46b356be205e38204e67b4304d8rbowen * Returns an error string.
d94fadc8231669a4e710869f290ed57a8232a61drbowen ap_proxy_canon_netloc(apr_pool_t *p, char **const urlp, char **userp,
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele return "Malformed URL";
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele /* find _last_ '@' since it might occur in user/password part */
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele/* find password */
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele password = ap_proxy_canonenc(p, strp + 1, strlen(strp + 1), enc_user, 1, 0);
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele return "Bad %-escape in URL (password)";
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele user = ap_proxy_canonenc(p, user, strlen(user), enc_user, 1, 0);
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele return "Bad %-escape in URL (username)";
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * Parse the host string to separate host portion from optional port.
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * Perform range checking on port.
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele rv = apr_parse_addr_port(&addr, &scope_id, &tmp_port, host, p);
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele if (rv != APR_SUCCESS || addr == NULL || scope_id != NULL) {
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele return "Invalid host/port";
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele if (tmp_port != 0) { /* only update caller's port if port was specified */
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele ap_str_tolower(addr); /* DNS names are case-insensitive */
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * If the date is a valid RFC 850 date or asctime() date, then it
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * is converted to the RFC 1123 format.
4ab980a06412fd86f52a6d054fb7e26de155c530erikabelePROXY_DECLARE(request_rec *)ap_proxy_make_fake_req(conn_rec *c, request_rec *r)
e8811b6d38f756b325446ded5d96857d13856511takashi rp->request_config = ap_create_request_config(pool);
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * list is a comma-separated list of case-insensitive tokens, with
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * optional whitespace around the tokens.
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele * The return returns 1 if the token val is found in the list, or 0
0ed10f46135a337e6779d6895d80b43a3471dc70pquerna * otherwise.
4ab980a06412fd86f52a6d054fb7e26de155c530erikabelePROXY_DECLARE(int) ap_proxy_liststr(const char *list, const char *val)
4ab980a06412fd86f52a6d054fb7e26de155c530erikabele const char *p;
if (p != NULL) {
i = p - list;
} while (apr_isspace(*p));
list = p;
int i, ch;
ch = x[i];
int i, ch;
NULL));
r->uri);
return statuscode;
return r->hostname;
return NULL;
url = apr_pstrdup(r->pool, &url[1]); /* make it point to "//", which is what proxy_canon_netloc expects */
int i, quads;
long bits;
char *tmp;
char *tmp;
++addr;
--quads;
/* "IP Address should be given in dotted-quad form, optionally followed by a netmask (e.g., 192.168.111.0/24)"; */
#if DEBUGGING
#if DEBUGGING
!= APR_SUCCESS) {
#if DEBUGGING
while (reqaddr) {
#if DEBUGGING
#if DEBUGGING
--d_len;
--h_len;
int h2_len;
int h1_len;
while (addr) {
--h2_len;
--h1_len;
return HTTP_FORBIDDEN;
while (conf_addr) {
while (uri_addr) {
char *conf_ip;
char *uri_ip;
uri_ip);
return HTTP_FORBIDDEN;
return OK;
return OK;
* ap_rgetline() in protocol.c. Deprecate this function and use ap_rgetline()
apr_bucket *e;
char *response;
int found = 0;
buff[0] = 0;
*eos = 0;
while (!found) {
return rv;
while (!found) {
return APR_ECONNABORTED;
if (APR_BUCKET_IS_EOS(e)) {
(const char **)&response,
&len,
APR_BLOCK_READ))) {
return rv;
if (len > 0) {
return APR_SUCCESS;
if (!initial) {
count++;
return url;
int n, l3 = 0;
if (urlpart) {
* BalancerMember balancer://alias http://example.com/foo
* translate url http://example.com/foo/bar/that to /bash/that
if (urlpart) {
--l2;
NULL);
worker++;
if (part) {
if (part) {
return url;
&proxy_module);
const char *pathp;
const char *domainp;
int ddiff = 0;
int pdiff = 0;
char *ret;
return str;
if (newpath) {
if (newdomain) {
if (newdomain) {
return ret;
const char *url,
int care)
return NULL;
return balancer;
balancer++;
return NULL;
const char *url)
return NULL;
const char *url,
const char *alias,
int do_malloc)
const char *sname;
if (!lbmethod) {
if (do_malloc)
&sname);
return APR_EINVAL;
if (lbmethod)
return APR_SUCCESS;
PROXY_DECLARE(apr_status_t) ap_proxy_initialize_balancer(proxy_balancer *balancer, server_rec *s, apr_pool_t *p)
unsigned int num;
if (!storage) {
return APR_EGENERAL;
return APR_EGENERAL;
return rv;
return APR_EGENERAL;
return rv;
return APR_SUCCESS;
return APR_SUCCESS;
return APR_SUCCESS;
if (conn->r) {
return APR_SUCCESS;
apr_pool_clear(p);
return APR_SUCCESS;
request_rec *r)
return APR_SUCCESS;
return APR_SUCCESS;
return APR_SUCCESS;
const char *url)
int max_match = 0;
int url_length;
int min_match;
int worker_name_length;
char *url_copy;
return NULL;
char *pathstart;
if (balancer) {
return max_worker;
const char *url,
int do_malloc)
int rv;
char *ptr;
* ProxyPass / http://www.example.com
if (balancer) {
} else if (conf) {
if (do_malloc)
return NULL;
return APR_EINVAL;
return APR_SUCCESS;
PROXY_DECLARE(apr_status_t) ap_proxy_initialize_worker(proxy_worker *worker, server_rec *s, apr_pool_t *p)
int mpm_threads;
return rv;
return APR_EGENERAL;
void *conn;
return rv;
server_rec *s)
return OK;
return DECLINED;
return OK;
request_rec *r,
int access_status;
if (*worker) {
return access_status;
request_rec *r,
if (balancer) {
return access_status;
const char *proxy_function,
const char *backend_name,
request_rec *r)
int connected = 0;
int loglevel;
server_rec *s)
return HTTP_SERVICE_UNAVAILABLE;
return HTTP_SERVICE_UNAVAILABLE;
return OK;
server_rec *s)
return OK;
PROXY_DECLARE(int)
char **url,
const char *proxyname,
char *server_portstr,
int server_portstr_size)
int server_port;
NULL));
if (!proxyname) {
if (proxyname) {
const char *proxy_auth;
return HTTP_INTERNAL_SERVER_ERROR;
return OK;
server_rec *s)
int status;
int complete = 0;
int len = 0;
if (!complete) {
buffer);
code_str);
return(status);
server_rec *s)
int connected = 0;
int loglevel;
sizeof(apr_sockaddr_t));
conn_rec *c,
server_rec *s)
int rc;
return OK;
0, NULL,
return HTTP_INTERNAL_SERVER_ERROR;
return HTTP_INTERNAL_SERVER_ERROR;
return rc;
return OK;
int ap_proxy_lb_workers(void)
if (!lb_workers_limit)
return lb_workers_limit;
apr_bucket *e;
if (r->main)
c->bucket_alloc);
apr_bucket *e;
const char *data;
e = APR_BUCKET_NEXT(e)) {
if (!APR_BUCKET_IS_METADATA(e)) {
else if (APR_BUCKET_IS_FLUSH(e)) {
else if (APR_BUCKET_IS_EOS(e)) {
return rv;
PROXY_DECLARE(unsigned int)
unsigned int hash;
return hash;
unsigned int hash;
return hash;
if (set)
return APR_SUCCESS;
pwt++;
return APR_EINVAL;
pwt++;
if (PROXY_WORKER_IS_USABLE(w))
return ret;
int index;
return APR_SUCCESS;
if (lbmethod) {
int found;
return APR_EGENERAL;
found = 0;
if (!found) {
return rv;
if (b->s->need_reset) {
b->s->need_reset = 0;
return APR_SUCCESS;