proxy_util.c revision fc72c3c272c85ce40db4115c071b37828535b499
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd/* Licensed to the Apache Software Foundation (ASF) under one or more
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * contributor license agreements. See the NOTICE file distributed with
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * this work for additional information regarding copyright ownership.
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * The ASF licenses this file to You under the Apache License, Version 2.0
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * (the "License"); you may not use this file except in compliance with
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * the License. You may obtain a copy of the License at
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * Unless required by applicable law or agreed to in writing, software
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen * distributed under the License is distributed on an "AS IS" BASIS,
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen * See the License for the specific language governing permissions and
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen * limitations under the License.
d229f940abfb2490dee17979e9a5ff31b7012eb5rbowen/* Utility routines for Apache proxy */
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * Opaque structure containing target server info when
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * using a forward proxy.
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * Up to now only used in combination with HTTP CONNECT.
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34ndtypedef struct {
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd int use_http_connect; /* Use SSL Tunneling via HTTP CONNECT */
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd/* Keep synced with mod_proxy.h! */
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34ndstatic struct wstat {
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh unsigned int bit;
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd const char *name;
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd {PROXY_WORKER_INITIALIZED, PROXY_WORKER_INITIALIZED_FLAG, "Init "},
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd {PROXY_WORKER_IGNORE_ERRORS, PROXY_WORKER_IGNORE_ERRORS_FLAG, "Ign "},
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd {PROXY_WORKER_IN_SHUTDOWN, PROXY_WORKER_IN_SHUTDOWN_FLAG, "Shut "},
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd {PROXY_WORKER_DISABLED, PROXY_WORKER_DISABLED_FLAG, "Dis "},
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd {PROXY_WORKER_STOPPED, PROXY_WORKER_STOPPED_FLAG, "Stop "},
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd {PROXY_WORKER_IN_ERROR, PROXY_WORKER_IN_ERROR_FLAG, "Err "},
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd {PROXY_WORKER_HOT_STANDBY, PROXY_WORKER_HOT_STANDBY_FLAG, "Stby "},
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd/* Global balancer counter */
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34ndstatic int lb_workers_limit = 0;
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34ndstatic int proxy_match_ipaddr(struct dirconn_entry *This, request_rec *r);
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34ndstatic int proxy_match_domainname(struct dirconn_entry *This, request_rec *r);
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34ndstatic int proxy_match_hostname(struct dirconn_entry *This, request_rec *r);
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34ndstatic int proxy_match_word(struct dirconn_entry *This, request_rec *r);
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34ndAPR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(proxy, PROXY, int, create_req,
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd/* already called in the knowledge that the characters are hex digits */
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd int ch = x[0];
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd#else /*APR_CHARSET_EBCDIC*/
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * we assume that the hex value refers to an ASCII character
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * so convert to EBCDIC so that it makes sense locally;
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * example:
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * client specifies %20 in URL to refer to a space char;
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * at this point we're called with EBCDIC "20"; after turning
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * EBCDIC "20" into binary 0x20, we then need to assume that 0x20
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * represents an ASCII char and convert 0x20 to EBCDIC, yielding
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd return buf[0];
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd#endif /*APR_CHARSET_EBCDIC*/
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd x[0] = '%';
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd if (i >= 10) {
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd if (i >= 10) {
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd#else /*APR_CHARSET_EBCDIC*/
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd x[0] = '%';
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd#endif /*APR_CHARSET_EBCDIC*/
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * canonicalise a URL-encoded string
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * Convert a URL-encoded string to canonical form.
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * It decodes characters which need not be encoded,
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * and encodes those which must be encoded, and does not touch
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * those which must not be touched.
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34ndPROXY_DECLARE(char *)ap_proxy_canonenc(apr_pool_t *p, const char *x, int len,
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd int i, j, ch;
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd char *allowed; /* characters which should not be encoded */
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd char *reserved; /* characters which much not be en/de-coded */
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * N.B. in addition to :@&=, this allows ';' in an http path
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * and '?' in an ftp path -- this may be revised
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * Also, it makes a '+' character in a search string reserved, as
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * it may be form-encoded. (Although RFC 1738 doesn't allow this -
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * it only permits ; / ? : @ = & as reserved chars.)
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd if (t == enc_path) {
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd else if (t == enc_search) {
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd else if (t == enc_user) {
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd else if (t == enc_fpath) {
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd else { /* if (t == enc_parm) */
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd if (t == enc_path) {
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd else if (t == enc_search) {
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd for (i = 0, j = 0; i < len; i++, j++) {
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd/* always handle '/' first */
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * decode it if not already done. do not decode reverse proxied URLs
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * unless specifically forced
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd if ((forcedec || (proxyreq && proxyreq != PROXYREQ_REVERSE)) && ch == '%') {
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd if (!apr_isxdigit(x[i + 1]) || !apr_isxdigit(x[i + 2])) {
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd if (ch != 0 && strchr(reserved, ch)) { /* keep it encoded */
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd/* recode it, if necessary */
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd y[j] = '\0';
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * Parses network-location.
9472e4d3c410be3b3f1addbf3b1db1769f64e765nd * urlp on input the URL; on output the path, after the leading /
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * user NULL if no user/password permitted
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * password holder for password
7f5b59ccc63c0c0e3e678a168f09ee6a2f51f9d0nd * host holder for host
97a3ecc40b65d5f8e865bbe0b1c9325d8c016e34nd * port port number; only set if one is supplied.
f086b4b402fa9a2fefc7dda85de2a3cc1cd0a654rjung * Returns an error string.
0d0ba3a410038e179b695446bb149cce6264e0abnd ap_proxy_canon_netloc(apr_pool_t *p, char **const urlp, char **userp,
0d0ba3a410038e179b695446bb149cce6264e0abnd return "Malformed URL";
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd /* find _last_ '@' since it might occur in user/password part */
return NULL;
PROXY_DECLARE(const char *)
char* ndate;
if (!time) {
return date;
return date;
return ndate;
return rp;
int len, i;
if (p != NULL) {
i = p - list;
} while (apr_isspace(*p));
list = p;
int len, i;
if (p != NULL) {
i = p - list;
} while (apr_isspace(*p));
if (new) {
list = p;
return new;
int i, ch;
ch = x[i];
int i, ch;
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;
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)
return NULL;
return balancer;
balancer++;
return NULL;
const char *url,
int do_malloc)
if (!lbmethod) {
if (do_malloc)
return NULL;
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;
* 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) {
*url);
*url);
*url);
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;
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;