proxy_util.c revision 0361488d59792d052a9f8024c0e5a1ef909252e6
842ae4bd224140319ae7feec1872b93dfd491143fielding/* Licensed to the Apache Software Foundation (ASF) under one or more
842ae4bd224140319ae7feec1872b93dfd491143fielding * contributor license agreements. See the NOTICE file distributed with
842ae4bd224140319ae7feec1872b93dfd491143fielding * this work for additional information regarding copyright ownership.
842ae4bd224140319ae7feec1872b93dfd491143fielding * The ASF licenses this file to You under the Apache License, Version 2.0
842ae4bd224140319ae7feec1872b93dfd491143fielding * (the "License"); you may not use this file except in compliance with
842ae4bd224140319ae7feec1872b93dfd491143fielding * the License. You may obtain a copy of the License at
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * Unless required by applicable law or agreed to in writing, software
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * distributed under the License is distributed on an "AS IS" BASIS,
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * See the License for the specific language governing permissions and
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * limitations under the License.
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh/* Utility routines for Apache proxy */
2d2eda71267231c2526be701fe655db125852c1ffielding * Opaque structure containing target server info when
2d2eda71267231c2526be701fe655db125852c1ffielding * using a forward proxy.
2d2eda71267231c2526be701fe655db125852c1ffielding * Up to now only used in combination with HTTP CONNECT.
2d2eda71267231c2526be701fe655db125852c1ffieldingtypedef struct {
2d2eda71267231c2526be701fe655db125852c1ffielding int use_http_connect; /* Use SSL Tunneling via HTTP CONNECT */
7cd1840a91b1a17b40e879e1ab381b4123caa941rbb/* Keep synced with mod_proxy.h! */
7cd1840a91b1a17b40e879e1ab381b4123caa941rbbstatic struct wstat {
f47c690c6fa01fa4334306be872da72fb9a3cdadsctemme unsigned int bit;
3d96ee83babeec32482c9082c9426340cee8c44dwrowe const char *name;
7cd1840a91b1a17b40e879e1ab381b4123caa941rbb {PROXY_WORKER_INITIALIZED, PROXY_WORKER_INITIALIZED_FLAG, "Init "},
7cd1840a91b1a17b40e879e1ab381b4123caa941rbb {PROXY_WORKER_IGNORE_ERRORS, PROXY_WORKER_IGNORE_ERRORS_FLAG, "Ign "},
7cd1840a91b1a17b40e879e1ab381b4123caa941rbb {PROXY_WORKER_IN_SHUTDOWN, PROXY_WORKER_IN_SHUTDOWN_FLAG, "Shut "},
7cd1840a91b1a17b40e879e1ab381b4123caa941rbb {PROXY_WORKER_DISABLED, PROXY_WORKER_DISABLED_FLAG, "Dis "},
7cd1840a91b1a17b40e879e1ab381b4123caa941rbb {PROXY_WORKER_STOPPED, PROXY_WORKER_STOPPED_FLAG, "Stop "},
7cd1840a91b1a17b40e879e1ab381b4123caa941rbb {PROXY_WORKER_IN_ERROR, PROXY_WORKER_IN_ERROR_FLAG, "Err "},
f47c690c6fa01fa4334306be872da72fb9a3cdadsctemme {PROXY_WORKER_HOT_STANDBY, PROXY_WORKER_HOT_STANDBY_FLAG, "Stby "},
742318b93e89c311f66b55f426c4d9cf2c14628bjim/* Global balancer counter */
7cd1840a91b1a17b40e879e1ab381b4123caa941rbbstatic int lb_workers_limit = 0;
f47c690c6fa01fa4334306be872da72fb9a3cdadsctemmeconst apr_strmatch_pattern PROXY_DECLARE_DATA *ap_proxy_strmatch_path;
7cd1840a91b1a17b40e879e1ab381b4123caa941rbbconst apr_strmatch_pattern PROXY_DECLARE_DATA *ap_proxy_strmatch_domain;
7cd1840a91b1a17b40e879e1ab381b4123caa941rbbstatic int proxy_match_ipaddr(struct dirconn_entry *This, request_rec *r);
7cd1840a91b1a17b40e879e1ab381b4123caa941rbbstatic int proxy_match_domainname(struct dirconn_entry *This, request_rec *r);
7cd1840a91b1a17b40e879e1ab381b4123caa941rbbstatic int proxy_match_hostname(struct dirconn_entry *This, request_rec *r);
7cd1840a91b1a17b40e879e1ab381b4123caa941rbbstatic int proxy_match_word(struct dirconn_entry *This, request_rec *r);
7cd1840a91b1a17b40e879e1ab381b4123caa941rbbAPR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(proxy, PROXY, int, create_req,
7cd1840a91b1a17b40e879e1ab381b4123caa941rbbPROXY_DECLARE(apr_status_t) ap_proxy_strncpy(char *dst, const char *src,
f47c690c6fa01fa4334306be872da72fb9a3cdadsctemme /* Assume the typical case is smaller copying into bigger
742318b93e89c311f66b55f426c4d9cf2c14628bjim so we have a fast return */
533d85911f7e4914ee5f9d5c99a2421f4ab4208asf /* XXX: APR_ENOSPACE would be better */
533d85911f7e4914ee5f9d5c99a2421f4ab4208asf/* already called in the knowledge that the characters are hex digits */
533d85911f7e4914ee5f9d5c99a2421f4ab4208asf int ch = x[0];
533d85911f7e4914ee5f9d5c99a2421f4ab4208asf#else /*APR_CHARSET_EBCDIC*/
533d85911f7e4914ee5f9d5c99a2421f4ab4208asf * we assume that the hex value refers to an ASCII character
533d85911f7e4914ee5f9d5c99a2421f4ab4208asf * so convert to EBCDIC so that it makes sense locally;
533d85911f7e4914ee5f9d5c99a2421f4ab4208asf * example:
533d85911f7e4914ee5f9d5c99a2421f4ab4208asf * client specifies %20 in URL to refer to a space char;
533d85911f7e4914ee5f9d5c99a2421f4ab4208asf * at this point we're called with EBCDIC "20"; after turning
533d85911f7e4914ee5f9d5c99a2421f4ab4208asf * EBCDIC "20" into binary 0x20, we then need to assume that 0x20
533d85911f7e4914ee5f9d5c99a2421f4ab4208asf * represents an ASCII char and convert 0x20 to EBCDIC, yielding
597d160db0361084da89b9786a2f1b213385cc24wrowe return buf[0];
c779aea0c9b7c47c2e4fd258e3f54835a849625dpoirier#endif /*APR_CHARSET_EBCDIC*/
597d160db0361084da89b9786a2f1b213385cc24wrowe x[0] = '%';
533d85911f7e4914ee5f9d5c99a2421f4ab4208asf if (i >= 10) {
533d85911f7e4914ee5f9d5c99a2421f4ab4208asf if (i >= 10) {
533d85911f7e4914ee5f9d5c99a2421f4ab4208asf#else /*APR_CHARSET_EBCDIC*/
7cd1840a91b1a17b40e879e1ab381b4123caa941rbb x[0] = '%';
7cd1840a91b1a17b40e879e1ab381b4123caa941rbb#endif /*APR_CHARSET_EBCDIC*/
7cd1840a91b1a17b40e879e1ab381b4123caa941rbb * canonicalise a URL-encoded string
3d96ee83babeec32482c9082c9426340cee8c44dwrowe * Convert a URL-encoded string to canonical form.
11e076839c8d5a82d55e710194d0daac51390dbdsf * It decodes characters which need not be encoded,
11e076839c8d5a82d55e710194d0daac51390dbdsf * and encodes those which must be encoded, and does not touch
7cd1840a91b1a17b40e879e1ab381b4123caa941rbb * those which must not be touched.
533d85911f7e4914ee5f9d5c99a2421f4ab4208asfPROXY_DECLARE(char *)ap_proxy_canonenc(apr_pool_t *p, const char *x, int len,
533d85911f7e4914ee5f9d5c99a2421f4ab4208asf int i, j, ch;
533d85911f7e4914ee5f9d5c99a2421f4ab4208asf char *allowed; /* characters which should not be encoded */
533d85911f7e4914ee5f9d5c99a2421f4ab4208asf char *reserved; /* characters which much not be en/de-coded */
533d85911f7e4914ee5f9d5c99a2421f4ab4208asf * N.B. in addition to :@&=, this allows ';' in an http path
533d85911f7e4914ee5f9d5c99a2421f4ab4208asf * and '?' in an ftp path -- this may be revised
533d85911f7e4914ee5f9d5c99a2421f4ab4208asf * Also, it makes a '+' character in a search string reserved, as
533d85911f7e4914ee5f9d5c99a2421f4ab4208asf * it may be form-encoded. (Although RFC 1738 doesn't allow this -
533d85911f7e4914ee5f9d5c99a2421f4ab4208asf * it only permits ; / ? : @ = & as reserved chars.)
a061049790af16ffc2f0ef2885d4b145ebe2bde2trawick else if (t == enc_search) {
a061049790af16ffc2f0ef2885d4b145ebe2bde2trawick else if (t == enc_user) {
cb1e20df951447249fd1344ae04a790219a16be5pquerna else if (t == enc_fpath) {
2d2eda71267231c2526be701fe655db125852c1ffielding else { /* if (t == enc_parm) */
if (t == enc_path) {
else if (t == enc_search) {
for (i = 0, j = 0; i < len; i++, j++) {
ch = x[i];
y[j] = ch;
return NULL;
y[j] = ch;
PROXY_DECLARE(char *)
return NULL;
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;
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);
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;