proxy_util.c revision 1d492700aafec672c1363b7694fc3b9007270641
1d614fee64a868146fde4ed70edccc1d60535fa1nd/* Licensed to the Apache Software Foundation (ASF) under one or more
1d614fee64a868146fde4ed70edccc1d60535fa1nd * contributor license agreements. See the NOTICE file distributed with
1d614fee64a868146fde4ed70edccc1d60535fa1nd * this work for additional information regarding copyright ownership.
5f5d1b4cc970b7f06ff8ef6526128e9a27303d88nd * The ASF licenses this file to You under the Apache License, Version 2.0
6d9c7d0283713aa8ae15fef7e9bfe2c7bfa73872nd * (the "License"); you may not use this file except in compliance with
6d9c7d0283713aa8ae15fef7e9bfe2c7bfa73872nd * the License. You may obtain a copy of the License at
031b91a62d25106ae69d4693475c79618dd5e884fielding * Unless required by applicable law or agreed to in writing, software
031b91a62d25106ae69d4693475c79618dd5e884fielding * distributed under the License is distributed on an "AS IS" BASIS,
031b91a62d25106ae69d4693475c79618dd5e884fielding * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
6d9c7d0283713aa8ae15fef7e9bfe2c7bfa73872nd * See the License for the specific language governing permissions and
6d9c7d0283713aa8ae15fef7e9bfe2c7bfa73872nd * limitations under the License.
6d9c7d0283713aa8ae15fef7e9bfe2c7bfa73872nd/* Utility routines for Apache proxy */
1d614fee64a868146fde4ed70edccc1d60535fa1nd * Opaque structure containing target server info when
a1ef40892ffa2b44fc249423c5b6c42a74a84c68nd * using a forward proxy.
1d614fee64a868146fde4ed70edccc1d60535fa1nd * Up to now only used in combination with HTTP CONNECT.
1d614fee64a868146fde4ed70edccc1d60535fa1ndtypedef struct {
1d614fee64a868146fde4ed70edccc1d60535fa1nd int use_http_connect; /* Use SSL Tunneling via HTTP CONNECT */
1d614fee64a868146fde4ed70edccc1d60535fa1nd/* Global balancer counter */
1d614fee64a868146fde4ed70edccc1d60535fa1ndstatic int lb_workers_limit = 0;
1d614fee64a868146fde4ed70edccc1d60535fa1ndstatic int proxy_match_ipaddr(struct dirconn_entry *This, request_rec *r);
1d614fee64a868146fde4ed70edccc1d60535fa1ndstatic int proxy_match_domainname(struct dirconn_entry *This, request_rec *r);
1d614fee64a868146fde4ed70edccc1d60535fa1ndstatic int proxy_match_hostname(struct dirconn_entry *This, request_rec *r);
1d614fee64a868146fde4ed70edccc1d60535fa1ndstatic int proxy_match_word(struct dirconn_entry *This, request_rec *r);
1d614fee64a868146fde4ed70edccc1d60535fa1ndAPR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(proxy, PROXY, int, create_req,
1d614fee64a868146fde4ed70edccc1d60535fa1nd/* already called in the knowledge that the characters are hex digits */
1d614fee64a868146fde4ed70edccc1d60535fa1nd int ch = x[0];
1d614fee64a868146fde4ed70edccc1d60535fa1nd#else /*APR_CHARSET_EBCDIC*/
1d614fee64a868146fde4ed70edccc1d60535fa1nd * we assume that the hex value refers to an ASCII character
1d614fee64a868146fde4ed70edccc1d60535fa1nd * so convert to EBCDIC so that it makes sense locally;
1d614fee64a868146fde4ed70edccc1d60535fa1nd * example:
1d614fee64a868146fde4ed70edccc1d60535fa1nd * client specifies %20 in URL to refer to a space char;
1d614fee64a868146fde4ed70edccc1d60535fa1nd * at this point we're called with EBCDIC "20"; after turning
1d614fee64a868146fde4ed70edccc1d60535fa1nd * EBCDIC "20" into binary 0x20, we then need to assume that 0x20
1d614fee64a868146fde4ed70edccc1d60535fa1nd * represents an ASCII char and convert 0x20 to EBCDIC, yielding
1d614fee64a868146fde4ed70edccc1d60535fa1nd return buf[0];
1d614fee64a868146fde4ed70edccc1d60535fa1nd#endif /*APR_CHARSET_EBCDIC*/
1d614fee64a868146fde4ed70edccc1d60535fa1nd x[0] = '%';
1d614fee64a868146fde4ed70edccc1d60535fa1nd if (i >= 10) {
1d614fee64a868146fde4ed70edccc1d60535fa1nd if (i >= 10) {
860b4efe27e7c1c9a2bf5c872b29c90f76849b51jim#else /*APR_CHARSET_EBCDIC*/
1d614fee64a868146fde4ed70edccc1d60535fa1nd x[0] = '%';
1d614fee64a868146fde4ed70edccc1d60535fa1nd#endif /*APR_CHARSET_EBCDIC*/
1d614fee64a868146fde4ed70edccc1d60535fa1nd * canonicalise a URL-encoded string
1d614fee64a868146fde4ed70edccc1d60535fa1nd * Convert a URL-encoded string to canonical form.
1d614fee64a868146fde4ed70edccc1d60535fa1nd * It decodes characters which need not be encoded,
1d614fee64a868146fde4ed70edccc1d60535fa1nd * and encodes those which must be encoded, and does not touch
1d614fee64a868146fde4ed70edccc1d60535fa1nd * those which must not be touched.
1d614fee64a868146fde4ed70edccc1d60535fa1ndPROXY_DECLARE(char *)ap_proxy_canonenc(apr_pool_t *p, const char *x, int len,
1d614fee64a868146fde4ed70edccc1d60535fa1nd int i, j, ch;
1d614fee64a868146fde4ed70edccc1d60535fa1nd char *allowed; /* characters which should not be encoded */
1d614fee64a868146fde4ed70edccc1d60535fa1nd char *reserved; /* characters which much not be en/de-coded */
1d614fee64a868146fde4ed70edccc1d60535fa1nd * N.B. in addition to :@&=, this allows ';' in an http path
1d614fee64a868146fde4ed70edccc1d60535fa1nd * and '?' in an ftp path -- this may be revised
1d614fee64a868146fde4ed70edccc1d60535fa1nd * Also, it makes a '+' character in a search string reserved, as
1d614fee64a868146fde4ed70edccc1d60535fa1nd * it may be form-encoded. (Although RFC 1738 doesn't allow this -
1d614fee64a868146fde4ed70edccc1d60535fa1nd * it only permits ; / ? : @ = & as reserved chars.)
1d614fee64a868146fde4ed70edccc1d60535fa1nd if (t == enc_path) {
1d614fee64a868146fde4ed70edccc1d60535fa1nd else if (t == enc_search) {
1d614fee64a868146fde4ed70edccc1d60535fa1nd else if (t == enc_user) {
1d614fee64a868146fde4ed70edccc1d60535fa1nd else if (t == enc_fpath) {
1d614fee64a868146fde4ed70edccc1d60535fa1nd else { /* if (t == enc_parm) */
1d614fee64a868146fde4ed70edccc1d60535fa1nd if (t == enc_path) {
1d614fee64a868146fde4ed70edccc1d60535fa1nd else if (t == enc_search) {
1d614fee64a868146fde4ed70edccc1d60535fa1nd for (i = 0, j = 0; i < len; i++, j++) {
1d614fee64a868146fde4ed70edccc1d60535fa1nd/* always handle '/' first */
11433bae0a0a1aab84e1c903980d7afa1aaa61e5covener * decode it if not already done. do not decode reverse proxied URLs
11433bae0a0a1aab84e1c903980d7afa1aaa61e5covener * unless specifically forced
1d614fee64a868146fde4ed70edccc1d60535fa1nd if ((forcedec || (proxyreq && proxyreq != PROXYREQ_REVERSE)) && ch == '%') {
1d614fee64a868146fde4ed70edccc1d60535fa1nd if (!apr_isxdigit(x[i + 1]) || !apr_isxdigit(x[i + 2])) {
1d614fee64a868146fde4ed70edccc1d60535fa1nd if (ch != 0 && strchr(reserved, ch)) { /* keep it encoded */
1d614fee64a868146fde4ed70edccc1d60535fa1nd/* recode it, if necessary */
1d614fee64a868146fde4ed70edccc1d60535fa1nd y[j] = '\0';
1d614fee64a868146fde4ed70edccc1d60535fa1nd * Parses network-location.
1d614fee64a868146fde4ed70edccc1d60535fa1nd * urlp on input the URL; on output the path, after the leading /
1d614fee64a868146fde4ed70edccc1d60535fa1nd * user NULL if no user/password permitted
1d614fee64a868146fde4ed70edccc1d60535fa1nd * password holder for password
1d614fee64a868146fde4ed70edccc1d60535fa1nd * host holder for host
1d614fee64a868146fde4ed70edccc1d60535fa1nd * port port number; only set if one is supplied.
1d614fee64a868146fde4ed70edccc1d60535fa1nd * Returns an error string.
1d614fee64a868146fde4ed70edccc1d60535fa1nd ap_proxy_canon_netloc(apr_pool_t *p, char **const urlp, char **userp,
1d614fee64a868146fde4ed70edccc1d60535fa1nd return "Malformed URL";
1d614fee64a868146fde4ed70edccc1d60535fa1nd /* find _last_ '@' since it might occur in user/password part */
1d614fee64a868146fde4ed70edccc1d60535fa1nd/* find password */
1d614fee64a868146fde4ed70edccc1d60535fa1nd password = ap_proxy_canonenc(p, strp + 1, strlen(strp + 1), enc_user, 1, 0);
1d614fee64a868146fde4ed70edccc1d60535fa1nd return "Bad %-escape in URL (password)";
1d614fee64a868146fde4ed70edccc1d60535fa1nd user = ap_proxy_canonenc(p, user, strlen(user), enc_user, 1, 0);
1d614fee64a868146fde4ed70edccc1d60535fa1nd return "Bad %-escape in URL (username)";
1d614fee64a868146fde4ed70edccc1d60535fa1nd * Parse the host string to separate host portion from optional port.
1d614fee64a868146fde4ed70edccc1d60535fa1nd * Perform range checking on port.
1d614fee64a868146fde4ed70edccc1d60535fa1nd rv = apr_parse_addr_port(&addr, &scope_id, &tmp_port, host, p);
1d614fee64a868146fde4ed70edccc1d60535fa1nd if (rv != APR_SUCCESS || addr == NULL || scope_id != NULL) {
1d614fee64a868146fde4ed70edccc1d60535fa1nd return "Invalid host/port";
1d614fee64a868146fde4ed70edccc1d60535fa1nd if (tmp_port != 0) { /* only update caller's port if port was specified */
c4f2f3a5c66fbad199e0d25d077e161cc31b5561jorton ap_str_tolower(addr); /* DNS names are case-insensitive */
1d614fee64a868146fde4ed70edccc1d60535fa1nd * If the date is a valid RFC 850 date or asctime() date, then it
1d614fee64a868146fde4ed70edccc1d60535fa1nd * is converted to the RFC 1123 format.
1d614fee64a868146fde4ed70edccc1d60535fa1ndPROXY_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;
apr_pool_t *p,
const char *url)
if (!lbmethod) {
#if APR_HAS_THREADS
return NULL;
const char *url)
int max_match = 0;
int url_length;
int min_match;
int worker_name_length;
char *url_copy;
return NULL;
char *pathstart;
worker++;
return max_worker;
#if APR_HAS_THREADS
return APR_SUCCESS;
apr_pool_t *p,
const char *url,
int id)
int rv;
if (id < 0) {
return NULL;
apr_pool_t *p,
const char *url)
if (id < 0) {
return worker;
PROXY_DECLARE(void)
if (id < 0) {
PROXY_DECLARE(void)
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;
return APR_SUCCESS;
#if APR_HAS_THREADS
return APR_SUCCESS;
apr_pool_clear(p);
#if APR_HAS_THREADS
return APR_SUCCESS;
request_rec *r)
return APR_SUCCESS;
#if APR_HAS_THREADS
return APR_SUCCESS;
return APR_SUCCESS;
server_rec *s)
if (!worker->s) {
if (ap_scoreboard_image) {
if (!score) {
if (!score) {
PROXY_DECLARE(apr_status_t) ap_proxy_initialize_worker(proxy_worker *worker, server_rec *s, apr_pool_t *p)
#if APR_HAS_THREADS
int mpm_threads;
return APR_SUCCESS;
return APR_EGENERAL;
#if APR_HAS_THREADS
return rv;
#if (APR_MAJOR_VERSION > 0)
void *conn;
return rv;
server_rec *s)
return OK;
return DECLINED;
return OK;
server_rec *s)
return HTTP_SERVICE_UNAVAILABLE;
#if APR_HAS_THREADS
return HTTP_SERVICE_UNAVAILABLE;
#if APR_HAS_THREADS
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;