proxy_util.c revision 2df9fb666776a6f6c7a84391609c4e41d87c350b
842ae4bd224140319ae7feec1872b93dfd491143fielding/* Copyright 1999-2006 The Apache Software Foundation or its licensors, as
842ae4bd224140319ae7feec1872b93dfd491143fielding * applicable.
842ae4bd224140319ae7feec1872b93dfd491143fielding *
842ae4bd224140319ae7feec1872b93dfd491143fielding * Licensed under the Apache License, Version 2.0 (the "License");
842ae4bd224140319ae7feec1872b93dfd491143fielding * you may not use this file except in compliance with the License.
842ae4bd224140319ae7feec1872b93dfd491143fielding * You may obtain a copy of the License at
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * http://www.apache.org/licenses/LICENSE-2.0
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Unless required by applicable law or agreed to in writing, software
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * distributed under the License is distributed on an "AS IS" BASIS,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * See the License for the specific language governing permissions and
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * limitations under the License.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* Utility routines for Apache proxy */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#include "mod_proxy.h"
e8f95a682820a599fe41b22977010636be5c2717jim#include "ap_mpm.h"
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#include "scoreboard.h"
e8f95a682820a599fe41b22977010636be5c2717jim#include "apr_version.h"
1747d30b98aa1bdbc43994c02cd46ab4cb9319e4fielding
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#if APR_HAVE_UNISTD_H
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#include <unistd.h> /* for getpid() */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#endif
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#if (APR_MAJOR_VERSION < 1)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#undef apr_socket_create
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#define apr_socket_create apr_socket_create_ex
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#endif
11f2c481e1d57bedb3f758565307501e9a2730ddtrawick
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* Global balancer counter */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesint PROXY_DECLARE_DATA proxy_lb_workers = 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesstatic int lb_workers_limit = 0;
5c0419d51818eb02045cf923a9fe456127a44c60wrowe
5c0419d51818eb02045cf923a9fe456127a44c60wrowestatic int proxy_match_ipaddr(struct dirconn_entry *This, request_rec *r);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesstatic int proxy_match_domainname(struct dirconn_entry *This, request_rec *r);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesstatic int proxy_match_hostname(struct dirconn_entry *This, request_rec *r);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesstatic int proxy_match_word(struct dirconn_entry *This, request_rec *r);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesAPR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(proxy, PROXY, int, create_req,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes (request_rec *r, request_rec *pr), (r, pr),
d266c3777146d36a4c23c17aad6f153aebea1bf4jorton OK, DECLINED)
d266c3777146d36a4c23c17aad6f153aebea1bf4jorton
22f8da8087791fcb95b836c8a81937c5a9bba202bnicholes/* already called in the knowledge that the characters are hex digits */
22f8da8087791fcb95b836c8a81937c5a9bba202bnicholesPROXY_DECLARE(int) ap_proxy_hex2c(const char *x)
22f8da8087791fcb95b836c8a81937c5a9bba202bnicholes{
22f8da8087791fcb95b836c8a81937c5a9bba202bnicholes int i, ch;
22f8da8087791fcb95b836c8a81937c5a9bba202bnicholes
22f8da8087791fcb95b836c8a81937c5a9bba202bnicholes#if !APR_CHARSET_EBCDIC
22f8da8087791fcb95b836c8a81937c5a9bba202bnicholes ch = x[0];
cd3bbd6d2df78d6c75e5d159a81ef8bdd5f70df9trawick if (apr_isdigit(ch))
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes i = ch - '0';
0f60998368b493f90120180a93fc2e1e74490872covener else if (apr_isupper(ch))
0f60998368b493f90120180a93fc2e1e74490872covener i = ch - ('A' - 10);
0f60998368b493f90120180a93fc2e1e74490872covener else
0f60998368b493f90120180a93fc2e1e74490872covener i = ch - ('a' - 10);
0f60998368b493f90120180a93fc2e1e74490872covener i <<= 4;
0f60998368b493f90120180a93fc2e1e74490872covener
0f60998368b493f90120180a93fc2e1e74490872covener ch = x[1];
0f60998368b493f90120180a93fc2e1e74490872covener if (apr_isdigit(ch))
87587593f1a53030e840acc0dec6cc881022ea40covener i += ch - '0';
87587593f1a53030e840acc0dec6cc881022ea40covener else if (apr_isupper(ch))
87587593f1a53030e840acc0dec6cc881022ea40covener i += ch - ('A' - 10);
87587593f1a53030e840acc0dec6cc881022ea40covener else
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovener i += ch - ('a' - 10);
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovener return i;
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovener#else /*APR_CHARSET_EBCDIC*/
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /*
43997561b2302d13dee973998e77743a3ddd2374trawick * we assume that the hex value refers to an ASCII character
fa123db15501821e36e513afa78e839775ad2800covener * so convert to EBCDIC so that it makes sense locally;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *
0568280364eb026393be492ebc732795c4934643jorton * example:
0568280364eb026393be492ebc732795c4934643jorton *
0568280364eb026393be492ebc732795c4934643jorton * client specifies %20 in URL to refer to a space char;
0568280364eb026393be492ebc732795c4934643jorton * at this point we're called with EBCDIC "20"; after turning
0568280364eb026393be492ebc732795c4934643jorton * EBCDIC "20" into binary 0x20, we then need to assume that 0x20
0568280364eb026393be492ebc732795c4934643jorton * represents an ASCII char and convert 0x20 to EBCDIC, yielding
0568280364eb026393be492ebc732795c4934643jorton * 0x40
0568280364eb026393be492ebc732795c4934643jorton */
0568280364eb026393be492ebc732795c4934643jorton char buf[1];
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (1 == sscanf(x, "%2x", &i)) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes buf[0] = i & 0xFF;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_xlate_proto_from_ascii(buf, 1);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return buf[0];
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
796e4a7141265d8ed7036e4628161c6eafb2a789jorton#endif /*APR_CHARSET_EBCDIC*/
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesPROXY_DECLARE(void) ap_proxy_c2hex(int ch, char *x)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#if !APR_CHARSET_EBCDIC
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes int i;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes x[0] = '%';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes i = (ch & 0xF0) >> 4;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (i >= 10)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes x[1] = ('A' - 10) + i;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes x[1] = '0' + i;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes i = ch & 0x0F;
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe if (i >= 10)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes x[2] = ('A' - 10) + i;
95b6fe1346805e1731e6e97c15d569c73be22cf7minfrin else
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes x[2] = '0' + i;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#else /*APR_CHARSET_EBCDIC*/
95b6fe1346805e1731e6e97c15d569c73be22cf7minfrin static const char ntoa[] = { "0123456789ABCDEF" };
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char buf[1];
95b6fe1346805e1731e6e97c15d569c73be22cf7minfrin
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ch &= 0xFF;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes buf[0] = ch;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_xlate_proto_to_ascii(buf, 1);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
95b6fe1346805e1731e6e97c15d569c73be22cf7minfrin x[0] = '%';
95b6fe1346805e1731e6e97c15d569c73be22cf7minfrin x[1] = ntoa[(buf[0] >> 4) & 0x0F];
95b6fe1346805e1731e6e97c15d569c73be22cf7minfrin x[2] = ntoa[buf[0] & 0x0F];
a1790fb35c4b352dab721370985c623a9f8f5062rpluem x[3] = '\0';
713a2b68bac4aeb1e9c48785006c0732451039depquerna#endif /*APR_CHARSET_EBCDIC*/
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * canonicalise a URL-encoded string
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe/*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Convert a URL-encoded string to canonical form.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * It decodes characters which need not be encoded,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * and encodes those which must be encoded, and does not touch
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * those which must not be touched.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesPROXY_DECLARE(char *)ap_proxy_canonenc(apr_pool_t *p, const char *x, int len, enum enctype t,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes int forcedec, int proxyreq)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener int i, j, ch;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char *y;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char *allowed; /* characters which should not be encoded */
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes char *reserved; /* characters which much not be en/de-coded */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/*
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe * N.B. in addition to :@&=, this allows ';' in an http path
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * and '?' in an ftp path -- this may be revised
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *
fa123db15501821e36e513afa78e839775ad2800covener * Also, it makes a '+' character in a search string reserved, as
fa123db15501821e36e513afa78e839775ad2800covener * it may be form-encoded. (Although RFC 1738 doesn't allow this -
fa123db15501821e36e513afa78e839775ad2800covener * it only permits ; / ? : @ = & as reserved chars.)
fa123db15501821e36e513afa78e839775ad2800covener */
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener if (t == enc_path)
fa123db15501821e36e513afa78e839775ad2800covener allowed = "$-_.+!*'(),;:@&=";
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener else if (t == enc_search)
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener allowed = "$-_.!*'(),;:@&=";
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener else if (t == enc_user)
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovener allowed = "$-_.+!*'(),;@&=";
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else if (t == enc_fpath)
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener allowed = "$-_.+!*'(),?:@&=";
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else /* if (t == enc_parm) */
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener allowed = "$-_.+!*'(),?/:@&=";
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (t == enc_path)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes reserved = "/";
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else if (t == enc_search)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes reserved = "+";
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener reserved = "";
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe y = apr_palloc(p, 3 * len + 1);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (i = 0, j = 0; i < len; i++, j++) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* always handle '/' first */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ch = x[i];
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (strchr(reserved, ch)) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes y[j] = ch;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes continue;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * decode it if not already done. do not decode reverse proxied URLs
fa123db15501821e36e513afa78e839775ad2800covener * unless specifically forced
cceddc0b6c0fdaed0c73abda39975bb1d388243acovener */
cceddc0b6c0fdaed0c73abda39975bb1d388243acovener if ((forcedec || (proxyreq && proxyreq != PROXYREQ_REVERSE)) && ch == '%') {
fa123db15501821e36e513afa78e839775ad2800covener if (!apr_isxdigit(x[i + 1]) || !apr_isxdigit(x[i + 2]))
cceddc0b6c0fdaed0c73abda39975bb1d388243acovener return NULL;
f2be127030aa4190033084f0a6add531c9bc41desf ch = ap_proxy_hex2c(&x[i + 1]);
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener i += 2;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener if (ch != 0 && strchr(reserved, ch)) { /* keep it encoded */
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener ap_proxy_c2hex(ch, &y[j]);
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener j += 2;
60215f303c7e1ce8b6d272acb5bfa5b3d99dfd34covener continue;
60215f303c7e1ce8b6d272acb5bfa5b3d99dfd34covener }
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener }
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener/* recode it, if necessary */
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener if (!apr_isalnum(ch) && !strchr(allowed, ch)) {
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener ap_proxy_c2hex(ch, &y[j]);
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener j += 2;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener }
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener else
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener y[j] = ch;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener }
6683642c1e0032eeeed5f99e8c14880692ef84c5sf y[j] = '\0';
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener return y;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener}
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener
6683642c1e0032eeeed5f99e8c14880692ef84c5sf/*
6683642c1e0032eeeed5f99e8c14880692ef84c5sf * Parses network-location.
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener * urlp on input the URL; on output the path, after the leading /
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener * user NULL if no user/password permitted
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener * password holder for password
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener * host holder for host
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener * port port number; only set if one is supplied.
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener *
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener * Returns an error string.
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener */
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovenerPROXY_DECLARE(char *)
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener ap_proxy_canon_netloc(apr_pool_t *p, char **const urlp, char **userp,
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener char **passwordp, char **hostp, apr_port_t *port)
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener{
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener char *addr, *scope_id, *strp, *host, *url = *urlp;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener char *user = NULL, *password = NULL;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener apr_port_t tmp_port;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener apr_status_t rv;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener if (url[0] != '/' || url[1] != '/')
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return "Malformed URL";
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes host = url + 2;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener url = strchr(host, '/');
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener if (url == NULL)
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener url = "";
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener else
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener *(url++) = '\0'; /* skip seperating '/' */
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener /* find _last_ '@' since it might occur in user/password part */
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener strp = strrchr(host, '@');
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener if (strp != NULL) {
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener *strp = '\0';
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener user = host;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener host = strp + 1;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener/* find password */
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener strp = strchr(user, ':');
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (strp != NULL) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *strp = '\0';
60215f303c7e1ce8b6d272acb5bfa5b3d99dfd34covener password = ap_proxy_canonenc(p, strp + 1, strlen(strp + 1), enc_user, 1, 0);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (password == NULL)
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes return "Bad %-escape in URL (password)";
0e05808dc59a321566303084c84b9826a4353cefrederpj }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
b08925593f214f621161742925dcf074a8047e0acovener user = ap_proxy_canonenc(p, user, strlen(user), enc_user, 1, 0);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (user == NULL)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return "Bad %-escape in URL (username)";
465bb68501690d7a47bfd2a6129580047d76d8f1rederpj }
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf if (userp != NULL) {
465bb68501690d7a47bfd2a6129580047d76d8f1rederpj *userp = user;
e8f95a682820a599fe41b22977010636be5c2717jim }
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholes if (passwordp != NULL) {
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholes *passwordp = password;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin }
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes /*
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes * Parse the host string to separate host portion from optional port.
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes * Perform range checking on port.
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes rv = apr_parse_addr_port(&addr, &scope_id, &tmp_port, host, p);
ebe5305f8b22507374358f32b74d12fb50c05a25covener if (rv != APR_SUCCESS || addr == NULL || scope_id != NULL) {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes return "Invalid host/port";
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes }
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes if (tmp_port != 0) { /* only update caller's port if port was specified */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes *port = tmp_port;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
513b324e774c559b579896df131fd7c8471ed529rederpj
513b324e774c559b579896df131fd7c8471ed529rederpj ap_str_tolower(addr); /* DNS names are case-insensitive */
513b324e774c559b579896df131fd7c8471ed529rederpj
513b324e774c559b579896df131fd7c8471ed529rederpj *urlp = url;
513b324e774c559b579896df131fd7c8471ed529rederpj *hostp = addr;
513b324e774c559b579896df131fd7c8471ed529rederpj
513b324e774c559b579896df131fd7c8471ed529rederpj return NULL;
513b324e774c559b579896df131fd7c8471ed529rederpj}
513b324e774c559b579896df131fd7c8471ed529rederpj
513b324e774c559b579896df131fd7c8471ed529rederpjstatic const char * const lwday[7] =
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
02fd88c85a9850109753b87612955ad372de1575sf
02fd88c85a9850109753b87612955ad372de1575sf/*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * If the date is a valid RFC 850 date or asctime() date, then it
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * is converted to the RFC 1123 format, otherwise it is not modified.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * This routine is not very fast at doing conversions, as it uses
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * sscanf and sprintf. However, if the date is already correctly
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes * formatted, then it exits very quickly.
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholesPROXY_DECLARE(const char *)
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes ap_proxy_date_canon(apr_pool_t *p, const char *x1)
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char *x = apr_pstrdup(p, x1);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes int wk, mday, year, hour, min, sec, mon;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes char *q, month[4], zone[4], week[4];
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes q = strchr(x, ',');
707f6d077f73cc948deead8df5b40ea42c1eaa78covener /* check for RFC 850 date */
707f6d077f73cc948deead8df5b40ea42c1eaa78covener if (q != NULL && q - x > 3 && q[1] == ' ') {
fa123db15501821e36e513afa78e839775ad2800covener *q = '\0';
707f6d077f73cc948deead8df5b40ea42c1eaa78covener for (wk = 0; wk < 7; wk++)
707f6d077f73cc948deead8df5b40ea42c1eaa78covener if (strcmp(x, lwday[wk]) == 0)
707f6d077f73cc948deead8df5b40ea42c1eaa78covener break;
707f6d077f73cc948deead8df5b40ea42c1eaa78covener *q = ',';
707f6d077f73cc948deead8df5b40ea42c1eaa78covener if (wk == 7)
707f6d077f73cc948deead8df5b40ea42c1eaa78covener return x; /* not a valid date */
707f6d077f73cc948deead8df5b40ea42c1eaa78covener if (q[4] != '-' || q[8] != '-' || q[11] != ' ' || q[14] != ':' ||
9ad7b260be233be7d7b5576979825cac72e15498rederpj q[17] != ':' || strcmp(&q[20], " GMT") != 0)
9ad7b260be233be7d7b5576979825cac72e15498rederpj return x;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes if (sscanf(q + 2, "%u-%3s-%u %u:%u:%u %3s", &mday, month, &year,
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes &hour, &min, &sec, zone) != 7)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return x;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes if (year < 70)
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes year += 2000;
ebe5305f8b22507374358f32b74d12fb50c05a25covener else
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes year += 1900;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes }
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes else {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes/* check for acstime() date */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes if (x[3] != ' ' || x[7] != ' ' || x[10] != ' ' || x[13] != ':' ||
54d22ed1c429b903b029bbd62621f11a9e286137minfrin x[16] != ':' || x[19] != ' ' || x[24] != '\0')
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes return x;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if (sscanf(x, "%3s %3s %u %u:%u:%u %u", week, month, &mday, &hour,
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes &min, &sec, &year) != 7)
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes return x;
ebe5305f8b22507374358f32b74d12fb50c05a25covener for (wk = 0; wk < 7; wk++)
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes if (strcmp(week, apr_day_snames[wk]) == 0)
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes break;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes if (wk == 7)
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes return x;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes/* check date */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (mon = 0; mon < 12; mon++)
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes if (strcmp(month, apr_month_snames[mon]) == 0)
b08925593f214f621161742925dcf074a8047e0acovener break;
b08925593f214f621161742925dcf074a8047e0acovener if (mon == 12)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return x;
707f6d077f73cc948deead8df5b40ea42c1eaa78covener
707f6d077f73cc948deead8df5b40ea42c1eaa78covener q = apr_palloc(p, 30);
9ad7b260be233be7d7b5576979825cac72e15498rederpj apr_snprintf(q, 30, "%s, %.2d %s %d %.2d:%.2d:%.2d GMT", apr_day_snames[wk],
707f6d077f73cc948deead8df5b40ea42c1eaa78covener mday, apr_month_snames[mon], year, hour, min, sec);
707f6d077f73cc948deead8df5b40ea42c1eaa78covener return q;
9ad7b260be233be7d7b5576979825cac72e15498rederpj}
707f6d077f73cc948deead8df5b40ea42c1eaa78covener
707f6d077f73cc948deead8df5b40ea42c1eaa78covenerPROXY_DECLARE(request_rec *)ap_proxy_make_fake_req(conn_rec *c, request_rec *r)
707f6d077f73cc948deead8df5b40ea42c1eaa78covener{
707f6d077f73cc948deead8df5b40ea42c1eaa78covener request_rec *rp = apr_pcalloc(c->pool, sizeof(*r));
9ad7b260be233be7d7b5576979825cac72e15498rederpj
707f6d077f73cc948deead8df5b40ea42c1eaa78covener rp->pool = c->pool;
707f6d077f73cc948deead8df5b40ea42c1eaa78covener rp->status = HTTP_OK;
707f6d077f73cc948deead8df5b40ea42c1eaa78covener
707f6d077f73cc948deead8df5b40ea42c1eaa78covener rp->headers_in = apr_table_make(c->pool, 50);
707f6d077f73cc948deead8df5b40ea42c1eaa78covener rp->subprocess_env = apr_table_make(c->pool, 50);
707f6d077f73cc948deead8df5b40ea42c1eaa78covener rp->headers_out = apr_table_make(c->pool, 12);
707f6d077f73cc948deead8df5b40ea42c1eaa78covener rp->err_headers_out = apr_table_make(c->pool, 5);
707f6d077f73cc948deead8df5b40ea42c1eaa78covener rp->notes = apr_table_make(c->pool, 5);
707f6d077f73cc948deead8df5b40ea42c1eaa78covener
707f6d077f73cc948deead8df5b40ea42c1eaa78covener rp->server = r->server;
707f6d077f73cc948deead8df5b40ea42c1eaa78covener rp->proxyreq = r->proxyreq;
707f6d077f73cc948deead8df5b40ea42c1eaa78covener rp->request_time = r->request_time;
707f6d077f73cc948deead8df5b40ea42c1eaa78covener rp->connection = c;
707f6d077f73cc948deead8df5b40ea42c1eaa78covener rp->output_filters = c->output_filters;
707f6d077f73cc948deead8df5b40ea42c1eaa78covener rp->input_filters = c->input_filters;
707f6d077f73cc948deead8df5b40ea42c1eaa78covener rp->proto_output_filters = c->output_filters;
707f6d077f73cc948deead8df5b40ea42c1eaa78covener rp->proto_input_filters = c->input_filters;
707f6d077f73cc948deead8df5b40ea42c1eaa78covener
707f6d077f73cc948deead8df5b40ea42c1eaa78covener rp->request_config = ap_create_request_config(c->pool);
707f6d077f73cc948deead8df5b40ea42c1eaa78covener proxy_run_create_req(r, rp);
707f6d077f73cc948deead8df5b40ea42c1eaa78covener
707f6d077f73cc948deead8df5b40ea42c1eaa78covener return rp;
707f6d077f73cc948deead8df5b40ea42c1eaa78covener}
707f6d077f73cc948deead8df5b40ea42c1eaa78covener
707f6d077f73cc948deead8df5b40ea42c1eaa78covener
707f6d077f73cc948deead8df5b40ea42c1eaa78covener/*
707f6d077f73cc948deead8df5b40ea42c1eaa78covener * list is a comma-separated list of case-insensitive tokens, with
707f6d077f73cc948deead8df5b40ea42c1eaa78covener * optional whitespace around the tokens.
707f6d077f73cc948deead8df5b40ea42c1eaa78covener * The return returns 1 if the token val is found in the list, or 0
9ad7b260be233be7d7b5576979825cac72e15498rederpj * otherwise.
9ad7b260be233be7d7b5576979825cac72e15498rederpj */
9ad7b260be233be7d7b5576979825cac72e15498rederpjPROXY_DECLARE(int) ap_proxy_liststr(const char *list, const char *val)
7add8f7fb048534390571801b7794f71cd9e127abnicholes{
7add8f7fb048534390571801b7794f71cd9e127abnicholes int len, i;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem const char *p;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem
7add8f7fb048534390571801b7794f71cd9e127abnicholes len = strlen(val);
7add8f7fb048534390571801b7794f71cd9e127abnicholes
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes while (list != NULL) {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes p = ap_strchr_c(list, ',');
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes if (p != NULL) {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes i = p - list;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes do
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes p++;
7add8f7fb048534390571801b7794f71cd9e127abnicholes while (apr_isspace(*p));
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes }
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes else
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes i = strlen(list);
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes while (i > 0 && apr_isspace(list[i - 1]))
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes i--;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes if (i == len && strncasecmp(list, val, len) == 0)
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes return 1;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes list = p;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes }
7add8f7fb048534390571801b7794f71cd9e127abnicholes return 0;
7add8f7fb048534390571801b7794f71cd9e127abnicholes}
141e1368614dc7564e1627671361b01b4869b491bnicholes
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholes/*
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes * list is a comma-separated list of case-insensitive tokens, with
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf * optional whitespace around the tokens.
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes * if val appears on the list of tokens, it is removed from the list,
e8f95a682820a599fe41b22977010636be5c2717jim * and the new list is returned.
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes */
ebe5305f8b22507374358f32b74d12fb50c05a25covenerPROXY_DECLARE(char *)ap_proxy_removestr(apr_pool_t *pool, const char *list, const char *val)
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf{
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes int len, i;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes const char *p;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes char *new = NULL;
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholes
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes len = strlen(val);
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholes
3dfeb02cfb853d8717ca0cc259b59fea610173f5bnicholes while (list != NULL) {
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf p = ap_strchr_c(list, ',');
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf if (p != NULL) {
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf i = p - list;
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf do
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf p++;
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf while (apr_isspace(*p));
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf }
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf else
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf i = strlen(list);
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf while (i > 0 && apr_isspace(list[i - 1]))
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf i--;
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf if (i == len && strncasecmp(list, val, len) == 0) {
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf /* do nothing */
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf }
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf else {
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf if (new)
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf new = apr_pstrcat(pool, new, ",", apr_pstrndup(pool, list, i), NULL);
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf else
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes new = apr_pstrndup(pool, list, i);
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes }
e8f95a682820a599fe41b22977010636be5c2717jim list = p;
7dbf29be626018bc389ef94c1846aeac4b72633bsf }
7dbf29be626018bc389ef94c1846aeac4b72633bsf return new;
7dbf29be626018bc389ef94c1846aeac4b72633bsf}
7dbf29be626018bc389ef94c1846aeac4b72633bsf
7dbf29be626018bc389ef94c1846aeac4b72633bsf/*
7dbf29be626018bc389ef94c1846aeac4b72633bsf * Converts 8 hex digits to a time integer
7dbf29be626018bc389ef94c1846aeac4b72633bsf */
7dbf29be626018bc389ef94c1846aeac4b72633bsfPROXY_DECLARE(int) ap_proxy_hex2sec(const char *x)
7dbf29be626018bc389ef94c1846aeac4b72633bsf{
7dbf29be626018bc389ef94c1846aeac4b72633bsf int i, ch;
7dbf29be626018bc389ef94c1846aeac4b72633bsf unsigned int j;
7dbf29be626018bc389ef94c1846aeac4b72633bsf
7dbf29be626018bc389ef94c1846aeac4b72633bsf for (i = 0, j = 0; i < 8; i++) {
7dbf29be626018bc389ef94c1846aeac4b72633bsf ch = x[i];
7dbf29be626018bc389ef94c1846aeac4b72633bsf j <<= 4;
7dbf29be626018bc389ef94c1846aeac4b72633bsf if (apr_isdigit(ch))
7dbf29be626018bc389ef94c1846aeac4b72633bsf j |= ch - '0';
7dbf29be626018bc389ef94c1846aeac4b72633bsf else if (apr_isupper(ch))
7dbf29be626018bc389ef94c1846aeac4b72633bsf j |= ch - ('A' - 10);
7dbf29be626018bc389ef94c1846aeac4b72633bsf else
7dbf29be626018bc389ef94c1846aeac4b72633bsf j |= ch - ('a' - 10);
7dbf29be626018bc389ef94c1846aeac4b72633bsf }
7dbf29be626018bc389ef94c1846aeac4b72633bsf if (j == 0xffffffff)
7dbf29be626018bc389ef94c1846aeac4b72633bsf return -1; /* so that it works with 8-byte ints */
7dbf29be626018bc389ef94c1846aeac4b72633bsf else
7dbf29be626018bc389ef94c1846aeac4b72633bsf return j;
7dbf29be626018bc389ef94c1846aeac4b72633bsf}
7dbf29be626018bc389ef94c1846aeac4b72633bsf
7dbf29be626018bc389ef94c1846aeac4b72633bsf/*
7dbf29be626018bc389ef94c1846aeac4b72633bsf * Converts a time integer to 8 hex digits
7dbf29be626018bc389ef94c1846aeac4b72633bsf */
7dbf29be626018bc389ef94c1846aeac4b72633bsfPROXY_DECLARE(void) ap_proxy_sec2hex(int t, char *y)
7dbf29be626018bc389ef94c1846aeac4b72633bsf{
7dbf29be626018bc389ef94c1846aeac4b72633bsf int i, ch;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes unsigned int j = t;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes for (i = 7; i >= 0; i--) {
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes ch = j & 0xF;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes j >>= 4;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes if (ch >= 10)
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes y[i] = ch + ('A' - 10);
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes else
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes y[i] = ch + '0';
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes }
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes y[8] = '\0';
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf}
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholesPROXY_DECLARE(int) ap_proxyerror(request_rec *r, int statuscode, const char *message)
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes{
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes apr_table_setn(r->notes, "error-notes",
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes apr_pstrcat(r->pool,
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes "The proxy server could not handle the request "
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes "<em><a href=\"", ap_escape_uri(r->pool, r->uri),
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes "\">", ap_escape_html(r->pool, r->method),
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes "&nbsp;",
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes ap_escape_html(r->pool, r->uri), "</a></em>.<p>\n"
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes "Reason: <strong>",
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes ap_escape_html(r->pool, message),
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes "</strong></p>", NULL));
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes /* Allow "error-notes" string to be printed by ap_send_error_response() */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes apr_table_setn(r->notes, "verbose-error-to", apr_pstrdup(r->pool, "*"));
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes r->status_line = apr_psprintf(r->pool, "%3.3u Proxy Error", statuscode);
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf "proxy: %s returned by %s", message, r->uri);
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes return statuscode;
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes}
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholesstatic const char *
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes proxy_get_host_of_request(request_rec *r)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char *url, *user = NULL, *password = NULL, *err, *host;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_port_t port;
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf if (r->hostname != NULL)
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf return r->hostname;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf /* Set url to the first char after "scheme://" */
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf if ((url = strchr(r->uri, ':')) == NULL
7dbf29be626018bc389ef94c1846aeac4b72633bsf || url[1] != '/' || url[2] != '/')
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf return NULL;
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes url = apr_pstrdup(r->pool, &url[1]); /* make it point to "//", which is what proxy_canon_netloc expects */
e8f95a682820a599fe41b22977010636be5c2717jim
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes err = ap_proxy_canon_netloc(r->pool, &url, &user, &password, &host, &port);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (err != NULL)
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "%s", err);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
7dbf29be626018bc389ef94c1846aeac4b72633bsf r->hostname = host;
7dbf29be626018bc389ef94c1846aeac4b72633bsf
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf return host; /* ought to return the port, too */
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf}
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf
9c63a05713cb83a44a1590b4af33edeebf39f118sf/* Return TRUE if addr represents an IP address (or an IP network address) */
9c63a05713cb83a44a1590b4af33edeebf39f118sfPROXY_DECLARE(int) ap_proxy_is_ipaddr(struct dirconn_entry *This, apr_pool_t *p)
9c63a05713cb83a44a1590b4af33edeebf39f118sf{
9c63a05713cb83a44a1590b4af33edeebf39f118sf const char *addr = This->name;
9c63a05713cb83a44a1590b4af33edeebf39f118sf long ip_addr[4];
9c63a05713cb83a44a1590b4af33edeebf39f118sf int i, quads;
9c63a05713cb83a44a1590b4af33edeebf39f118sf long bits;
9c63a05713cb83a44a1590b4af33edeebf39f118sf
9c63a05713cb83a44a1590b4af33edeebf39f118sf /*
9c63a05713cb83a44a1590b4af33edeebf39f118sf * if the address is given with an explicit netmask, use that
9c63a05713cb83a44a1590b4af33edeebf39f118sf * Due to a deficiency in apr_inet_addr(), it is impossible to parse
9c63a05713cb83a44a1590b4af33edeebf39f118sf * "partial" addresses (with less than 4 quads) correctly, i.e.
9c63a05713cb83a44a1590b4af33edeebf39f118sf * 192.168.123 is parsed as 192.168.0.123, which is not what I want.
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf * I therefore have to parse the IP address manually:
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf * if (proxy_readmask(This->name, &This->addr.s_addr, &This->mask.s_addr) == 0)
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf * addr and mask were set by proxy_readmask()
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf * return 1;
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf */
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf /*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Parse IP addr manually, optionally allowing
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * abbreviated net addresses like 192.168.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
54d22ed1c429b903b029bbd62621f11a9e286137minfrin /* Iterate over up to 4 (dotted) quads. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (quads = 0; quads < 4 && *addr != '\0'; ++quads) {
9c63a05713cb83a44a1590b4af33edeebf39f118sf char *tmp;
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (*addr == '/' && quads > 0) /* netmask starts here. */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes break;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (!apr_isdigit(*addr))
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return 0; /* no digit at start of quad */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
54d22ed1c429b903b029bbd62621f11a9e286137minfrin ip_addr[quads] = strtol(addr, &tmp, 0);
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if (tmp == addr) /* expected a digit, found something else */
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return 0;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if (ip_addr[quads] < 0 || ip_addr[quads] > 255) {
54d22ed1c429b903b029bbd62621f11a9e286137minfrin /* invalid octet */
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return 0;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin }
e8f95a682820a599fe41b22977010636be5c2717jim
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe addr = tmp;
8a03cd420b800a2428f49f4617293de9b2387b20jorton
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if (*addr == '.' && quads != 3)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin ++addr; /* after the 4th quad, a dot would be illegal */
54d22ed1c429b903b029bbd62621f11a9e286137minfrin }
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin for (This->addr.s_addr = 0, i = 0; i < quads; ++i)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin This->addr.s_addr |= htonl(ip_addr[i] << (24 - 8 * i));
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if (addr[0] == '/' && apr_isdigit(addr[1])) { /* net mask follows: */
54d22ed1c429b903b029bbd62621f11a9e286137minfrin char *tmp;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin ++addr;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin bits = strtol(addr, &tmp, 0);
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if (tmp == addr) /* expected a digit, found something else */
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return 0;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
6999a76d8eb5ef6b4b295e51df0b2fb6064bd373covener addr = tmp;
6999a76d8eb5ef6b4b295e51df0b2fb6064bd373covener
6999a76d8eb5ef6b4b295e51df0b2fb6064bd373covener if (bits < 0 || bits > 32) /* netmask must be between 0 and 32 */
6999a76d8eb5ef6b4b295e51df0b2fb6064bd373covener return 0;
6999a76d8eb5ef6b4b295e51df0b2fb6064bd373covener
6999a76d8eb5ef6b4b295e51df0b2fb6064bd373covener }
54d22ed1c429b903b029bbd62621f11a9e286137minfrin else {
54d22ed1c429b903b029bbd62621f11a9e286137minfrin /*
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * Determine (i.e., "guess") netmask by counting the
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * number of trailing .0's; reduce #quads appropriately
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * (so that 192.168.0.0 is equivalent to 192.168.)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin */
54d22ed1c429b903b029bbd62621f11a9e286137minfrin while (quads > 0 && ip_addr[quads - 1] == 0)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes --quads;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* "IP Address should be given in dotted-quad form, optionally followed by a netmask (e.g., 192.168.111.0/24)"; */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (quads < 1)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* every zero-byte counts as 8 zero-bits */
e8f95a682820a599fe41b22977010636be5c2717jim bits = 8 * quads;
e8f95a682820a599fe41b22977010636be5c2717jim
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe if (bits != 32) /* no warning for fully qualified IP address */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
e8f95a682820a599fe41b22977010636be5c2717jim "Warning: NetMask not supplied with IP-Addr; guessing: %s/%ld",
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe inet_ntoa(This->addr), bits);
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe }
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe
f0f6f1b90ab582896f8a7d56d85bd62a55e57d90covener This->mask.s_addr = htonl(APR_INADDR_NONE << (32 - bits));
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if (*addr == '\0' && (This->addr.s_addr & ~This->mask.s_addr) != 0) {
560fd0658902ab57754616c172d8953e69fc4722bnicholes ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovener "Warning: NetMask and IP-Addr disagree in %s/%ld",
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes inet_ntoa(This->addr), bits);
e8f95a682820a599fe41b22977010636be5c2717jim This->addr.s_addr &= This->mask.s_addr;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes " Set to %s/%ld",
9ad7b260be233be7d7b5576979825cac72e15498rederpj inet_ntoa(This->addr), bits);
9ad7b260be233be7d7b5576979825cac72e15498rederpj }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (*addr == '\0') {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes This->matcher = proxy_match_ipaddr;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return 1;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else
560fd0658902ab57754616c172d8953e69fc4722bnicholes return (*addr == '\0'); /* okay iff we've parsed the whole string */
560fd0658902ab57754616c172d8953e69fc4722bnicholes}
560fd0658902ab57754616c172d8953e69fc4722bnicholes
560fd0658902ab57754616c172d8953e69fc4722bnicholes/* Return TRUE if addr represents an IP address (or an IP network address) */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesstatic int proxy_match_ipaddr(struct dirconn_entry *This, request_rec *r)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes int i, ip_addr[4];
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes struct in_addr addr, *ip;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const char *host = proxy_get_host_of_request(r);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (host == NULL) /* oops! */
e8f95a682820a599fe41b22977010636be5c2717jim return 0;
e8f95a682820a599fe41b22977010636be5c2717jim
e8f95a682820a599fe41b22977010636be5c2717jim memset(&addr, '\0', sizeof addr);
e8f95a682820a599fe41b22977010636be5c2717jim memset(ip_addr, '\0', sizeof ip_addr);
e8f95a682820a599fe41b22977010636be5c2717jim
e8f95a682820a599fe41b22977010636be5c2717jim if (4 == sscanf(host, "%d.%d.%d.%d", &ip_addr[0], &ip_addr[1], &ip_addr[2], &ip_addr[3])) {
ae1981fc94adf2b231e2d0e15d2f895b2138c969covener for (addr.s_addr = 0, i = 0; i < 4; ++i)
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe addr.s_addr |= htonl(ip_addr[i] << (24 - 8 * i));
7a55c294da84865fe13262ed66ffd0c5841a9da5covener
7a55c294da84865fe13262ed66ffd0c5841a9da5covener if (This->addr.s_addr == (addr.s_addr & This->mask.s_addr)) {
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovener#if DEBUGGING
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovener ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovener "1)IP-Match: %s[%s] <-> ", host, inet_ntoa(addr));
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovener ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
54091ac5c596337658fc568231ca1a900abdc5fecovener "%s/", inet_ntoa(This->addr));
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovener ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovener "%s", inet_ntoa(This->mask));
54091ac5c596337658fc568231ca1a900abdc5fecovener#endif
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return 1;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#if DEBUGGING
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "1)IP-NoMatch: %s[%s] <-> ", host, inet_ntoa(addr));
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "%s/", inet_ntoa(This->addr));
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "%s", inet_ntoa(This->mask));
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#endif
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes struct apr_sockaddr_t *reqaddr;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (apr_sockaddr_info_get(&reqaddr, host, APR_UNSPEC, 0, 0, r->pool)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes != APR_SUCCESS) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#if DEBUGGING
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
e8f95a682820a599fe41b22977010636be5c2717jim "2)IP-NoMatch: hostname=%s msg=Host not found",
560fd0658902ab57754616c172d8953e69fc4722bnicholes host);
ae1981fc94adf2b231e2d0e15d2f895b2138c969covener#endif
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe return 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
7a55c294da84865fe13262ed66ffd0c5841a9da5covener
fa123db15501821e36e513afa78e839775ad2800covener /* Try to deal with multiple IP addr's for a host */
fa123db15501821e36e513afa78e839775ad2800covener /* FIXME: This needs to be able to deal with IPv6 */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while (reqaddr) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ip = (struct in_addr *) reqaddr->ipaddr_ptr;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (This->addr.s_addr == (ip->s_addr & This->mask.s_addr)) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#if DEBUGGING
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "3)IP-Match: %s[%s] <-> ", host,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes inet_ntoa(*ip));
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "%s/", inet_ntoa(This->addr));
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "%s", inet_ntoa(This->mask));
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#endif
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return 1;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#if DEBUGGING
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "3)IP-NoMatch: %s[%s] <-> ", host,
0e05808dc59a321566303084c84b9826a4353cefrederpj inet_ntoa(*ip));
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "%s/", inet_ntoa(This->addr));
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener "%s", inet_ntoa(This->mask));
0e05808dc59a321566303084c84b9826a4353cefrederpj }
ebe5305f8b22507374358f32b74d12fb50c05a25covener#endif
ebe5305f8b22507374358f32b74d12fb50c05a25covener reqaddr = reqaddr->next;
ebe5305f8b22507374358f32b74d12fb50c05a25covener }
ebe5305f8b22507374358f32b74d12fb50c05a25covener }
ebe5305f8b22507374358f32b74d12fb50c05a25covener
ebe5305f8b22507374358f32b74d12fb50c05a25covener return 0;
ebe5305f8b22507374358f32b74d12fb50c05a25covener}
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener/* Return TRUE if addr represents a domain name */
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovenerPROXY_DECLARE(int) ap_proxy_is_domainname(struct dirconn_entry *This, apr_pool_t *p)
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener{
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener char *addr = This->name;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener int i;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener /* Domain name must start with a '.' */
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener if (addr[0] != '.')
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener return 0;
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener
0e05808dc59a321566303084c84b9826a4353cefrederpj /* rfc1035 says DNS names must consist of "[-a-zA-Z0-9]" and '.' */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (i = 0; apr_isalnum(addr[i]) || addr[i] == '-' || addr[i] == '.'; ++i)
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener continue;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#if 0
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (addr[i] == ':') {
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "@@@@ handle optional port in proxy_is_domainname()");
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* @@@@ handle optional port */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#endif
9ad7b260be233be7d7b5576979825cac72e15498rederpj
9ad7b260be233be7d7b5576979825cac72e15498rederpj if (addr[i] != '\0')
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return 0;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin /* Strip trailing dots */
54d22ed1c429b903b029bbd62621f11a9e286137minfrin for (i = strlen(addr) - 1; i > 0 && addr[i] == '.'; --i)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin addr[i] = '\0';
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
560fd0658902ab57754616c172d8953e69fc4722bnicholes This->matcher = proxy_match_domainname;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return 1;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin}
ae1981fc94adf2b231e2d0e15d2f895b2138c969covener
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* Return TRUE if host "host" is in domain "domain" */
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovenerstatic int proxy_match_domainname(struct dirconn_entry *This, request_rec *r)
7a55c294da84865fe13262ed66ffd0c5841a9da5covener{
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener const char *host = proxy_get_host_of_request(r);
fa123db15501821e36e513afa78e839775ad2800covener int d_len = strlen(This->name), h_len;
fa123db15501821e36e513afa78e839775ad2800covener
fa123db15501821e36e513afa78e839775ad2800covener if (host == NULL) /* some error was logged already */
fa123db15501821e36e513afa78e839775ad2800covener return 0;
fa123db15501821e36e513afa78e839775ad2800covener
fa123db15501821e36e513afa78e839775ad2800covener h_len = strlen(host);
fa123db15501821e36e513afa78e839775ad2800covener
fa123db15501821e36e513afa78e839775ad2800covener /* @@@ do this within the setup? */
fa123db15501821e36e513afa78e839775ad2800covener /* Ignore trailing dots in domain comparison: */
fa123db15501821e36e513afa78e839775ad2800covener while (d_len > 0 && This->name[d_len - 1] == '.')
fa123db15501821e36e513afa78e839775ad2800covener --d_len;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while (h_len > 0 && host[h_len - 1] == '.')
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes --h_len;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return h_len > d_len
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes && strncasecmp(&host[h_len - d_len], This->name, d_len) == 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* Return TRUE if host represents a host name */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesPROXY_DECLARE(int) ap_proxy_is_hostname(struct dirconn_entry *This, apr_pool_t *p)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes struct apr_sockaddr_t *addr;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char *host = This->name;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes int i;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* Host names must not start with a '.' */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (host[0] == '.')
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe /* rfc1035 says DNS names must consist of "[-a-zA-Z0-9]" and '.' */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe for (i = 0; apr_isalnum(host[i]) || host[i] == '-' || host[i] == '.'; ++i);
e8f95a682820a599fe41b22977010636be5c2717jim
e8f95a682820a599fe41b22977010636be5c2717jim if (host[i] != '\0' || apr_sockaddr_info_get(&addr, host, APR_UNSPEC, 0, 0, p) != APR_SUCCESS)
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe return 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes This->hostaddr = addr;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
e8f95a682820a599fe41b22977010636be5c2717jim /* Strip trailing dots */
e8f95a682820a599fe41b22977010636be5c2717jim for (i = strlen(host) - 1; i > 0 && host[i] == '.'; --i)
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe host[i] = '\0';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes This->matcher = proxy_match_hostname;
e8f95a682820a599fe41b22977010636be5c2717jim return 1;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* Return TRUE if host "host" is equal to host2 "host2" */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesstatic int proxy_match_hostname(struct dirconn_entry *This, request_rec *r)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char *host = This->name;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const char *host2 = proxy_get_host_of_request(r);
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe int h2_len;
e8f95a682820a599fe41b22977010636be5c2717jim int h1_len;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (host == NULL || host2 == NULL)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return 0; /* oops! */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes h2_len = strlen(host2);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes h1_len = strlen(host);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#if 0
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes struct apr_sockaddr_t *addr = *This->hostaddr;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* Try to deal with multiple IP addr's for a host */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while (addr) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (addr->ipaddr_ptr == ? ? ? ? ? ? ? ? ? ? ? ? ?)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return 1;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes addr = addr->next;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#endif
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* Ignore trailing dots in host2 comparison: */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while (h2_len > 0 && host2[h2_len - 1] == '.')
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes --h2_len;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while (h1_len > 0 && host[h1_len - 1] == '.')
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes --h1_len;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return h1_len == h2_len
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes && strncasecmp(host, host2, h1_len) == 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* Return TRUE if addr is to be matched as a word */
e8f95a682820a599fe41b22977010636be5c2717jimPROXY_DECLARE(int) ap_proxy_is_word(struct dirconn_entry *This, apr_pool_t *p)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes This->matcher = proxy_match_word;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return 1;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* Return TRUE if string "str2" occurs literally in "str1" */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesstatic int proxy_match_word(struct dirconn_entry *This, request_rec *r)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const char *host = proxy_get_host_of_request(r);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return host != NULL && ap_strstr_c(host, This->name) != NULL;
e8f95a682820a599fe41b22977010636be5c2717jim}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* checks whether a host in uri_addr matches proxyblock */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesPROXY_DECLARE(int) ap_proxy_checkproxyblock(request_rec *r, proxy_server_conf *conf,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_sockaddr_t *uri_addr)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes int j;
43c3e6a4b559b76b750c245ee95e2782c15b4296jim apr_sockaddr_t * src_uri_addr = uri_addr;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* XXX FIXME: conf->noproxies->elts is part of an opaque structure */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (j = 0; j < conf->noproxies->nelts; j++) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes struct noproxy_entry *npent = (struct noproxy_entry *) conf->noproxies->elts;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes struct apr_sockaddr_t *conf_addr = npent[j].addr;
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes uri_addr = src_uri_addr;
43c3e6a4b559b76b750c245ee95e2782c15b4296jim ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "proxy: checking remote machine [%s] against [%s]", uri_addr->hostname, npent[j].name);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if ((npent[j].name && ap_strstr_c(uri_addr->hostname, npent[j].name))
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes || npent[j].name[0] == '*') {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_WARNING, 0, r->server,
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener "proxy: connect to remote machine %s blocked: name %s matched", uri_addr->hostname, npent[j].name);
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener return HTTP_FORBIDDEN;
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf }
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener while (conf_addr) {
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe while (uri_addr) {
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe char *conf_ip;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe char *uri_ip;
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes apr_sockaddr_ip_get(&conf_ip, conf_addr);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_sockaddr_ip_get(&uri_ip, uri_addr);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf "proxy: ProxyBlock comparing %s and %s", conf_ip, uri_ip);
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf if (!apr_strnatcasecmp(conf_ip, uri_ip)) {
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf ap_log_error(APLOG_MARK, APLOG_WARNING, 0, r->server,
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf "proxy: connect to remote machine %s blocked: IP %s matched", uri_addr->hostname, conf_ip);
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf return HTTP_FORBIDDEN;
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf }
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf uri_addr = uri_addr->next;
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf }
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf conf_addr = conf_addr->next;
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return OK;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* set up the minimal filter set */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesPROXY_DECLARE(int) ap_proxy_pre_http_request(conn_rec *c, request_rec *r)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_add_input_filter("HTTP_IN", NULL, r, c);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return OK;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * converts a series of buckets into a string
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * XXX: BillS says this function performs essentially the same function as
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * ap_rgetline() in protocol.c. Deprecate this function and use ap_rgetline()
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * instead? I think ap_proxy_string_read() will not work properly on non ASCII
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * (EBCDIC) machines either.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesPROXY_DECLARE(apr_status_t) ap_proxy_string_read(conn_rec *c, apr_bucket_brigade *bb,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char *buff, apr_size_t bufflen, int *eos)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
e8f95a682820a599fe41b22977010636be5c2717jim apr_bucket *e;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_status_t rv;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe char *pos = buff;
e8f95a682820a599fe41b22977010636be5c2717jim char *response;
e8f95a682820a599fe41b22977010636be5c2717jim int found = 0;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe apr_size_t len;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* start with an empty string */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes buff[0] = 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *eos = 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* loop through each brigade */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while (!found) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* get brigade from network one line at a time */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (APR_SUCCESS != (rv = ap_get_brigade(c->input_filters, bb,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes AP_MODE_GETLINE,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes APR_BLOCK_READ,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes 0))) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return rv;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
e8f95a682820a599fe41b22977010636be5c2717jim /* loop through each bucket */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem while (!found) {
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem if (*eos || APR_BRIGADE_EMPTY(bb)) {
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem /* The connection aborted or timed out */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return APR_ECONNABORTED;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe e = APR_BRIGADE_FIRST(bb);
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe if (APR_BUCKET_IS_EOS(e)) {
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe *eos = 1;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else {
e8f95a682820a599fe41b22977010636be5c2717jim if (APR_SUCCESS != apr_bucket_read(e, (const char **)&response, &len, APR_BLOCK_READ)) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return rv;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * is string LF terminated?
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * XXX: This check can be made more efficient by simply checking
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * if the last character in the 'response' buffer is an ASCII_LF.
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe * See ap_rgetline() for an example.
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe if (memchr(response, APR_ASCII_LF, len)) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes found = 1;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* concat strings until buff is full - then throw the data away */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (len > ((bufflen-1)-(pos-buff))) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes len = (bufflen-1)-(pos-buff);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (len > 0) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes pos = apr_cpystrn(pos, response, len);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes APR_BUCKET_REMOVE(e);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_bucket_destroy(e);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
e8f95a682820a599fe41b22977010636be5c2717jim
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return APR_SUCCESS;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* unmerge an element in the table */
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpjPROXY_DECLARE(void) ap_proxy_table_unmerge(apr_pool_t *p, apr_table_t *t, char *key)
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj{
e8f95a682820a599fe41b22977010636be5c2717jim apr_off_t offset = 0;
e8f95a682820a599fe41b22977010636be5c2717jim apr_off_t count = 0;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe char *value = NULL;
e8f95a682820a599fe41b22977010636be5c2717jim
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* get the value to unmerge */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const char *initial = apr_table_get(t, key);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (!initial) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes value = apr_pstrdup(p, initial);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* remove the value from the headers */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_table_unset(t, key);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* find each comma */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes while (value[count]) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (value[count] == ',') {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes value[count] = 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_table_add(t, key, value + offset);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes offset = count + 1;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes count++;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_table_add(t, key, value + offset);
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj}
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpjPROXY_DECLARE(const char *) ap_proxy_location_reverse_map(request_rec *r,
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj proxy_dir_conf *conf, const char *url)
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj{
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj struct proxy_alias *ent;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes int i, l1, l2;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char *u;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * XXX FIXME: Make sure this handled the ambiguous case of the :<PORT>
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * after the hostname
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes l1 = strlen(url);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ent = (struct proxy_alias *)conf->raliases->elts;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (i = 0; i < conf->raliases->nelts; i++) {
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj l2 = strlen(ent[i].real);
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes if (l1 >= l2 && strncasecmp(ent[i].real, url, l2) == 0) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes u = apr_pstrcat(r->pool, ent[i].fake, &url[l2], NULL);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return ap_construct_url(r->pool, u, r);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener return url;
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener}
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener/*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Cookies are a bit trickier to match: we've got two substrings to worry
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * about, and we can't just find them with strstr 'cos of case. Regexp
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes * matching would be an easy fix, but for better consistency with all the
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * other matches we'll refrain and use apr_strmatch to find path=/domain=
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * and stick to plain strings for the config values.
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf */
4be9c459920a7c1cfe62d654327dae5c4bb6b284sfPROXY_DECLARE(const char *) ap_proxy_cookie_reverse_map(request_rec *r,
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf proxy_dir_conf *conf, const char *str)
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf{
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf struct proxy_alias *ent;
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf size_t len = strlen(str);
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf const char *newpath = NULL;
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf const char *newdomain = NULL;
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf const char *pathp;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const char *domainp;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const char *pathe = NULL;
e8f95a682820a599fe41b22977010636be5c2717jim const char *domaine = NULL;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes size_t l1, l2, poffs = 0, doffs = 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes int i;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes int ddiff = 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes int pdiff = 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes char *ret;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /*
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj * Find the match and replacement, but save replacing until we've done
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj * both path and domain so we know the new strlen
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe if ((pathp = apr_strmatch(conf->cookie_path_str, str, len)) != NULL) {
e8f95a682820a599fe41b22977010636be5c2717jim pathp += 5;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe poffs = pathp - str;
e8f95a682820a599fe41b22977010636be5c2717jim pathe = ap_strchr_c(pathp, ';');
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe l1 = pathe ? (pathe - pathp) : strlen(pathp);
e8f95a682820a599fe41b22977010636be5c2717jim pathe = pathp + l1 ;
e8f95a682820a599fe41b22977010636be5c2717jim ent = (struct proxy_alias *)conf->cookie_paths->elts;
e8f95a682820a599fe41b22977010636be5c2717jim for (i = 0; i < conf->cookie_paths->nelts; i++) {
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe l2 = strlen(ent[i].fake);
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe if (l1 >= l2 && strncmp(ent[i].fake, pathp, l2) == 0) {
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj newpath = ent[i].real;
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj pdiff = strlen(newpath) - l1;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem break;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
9c63a05713cb83a44a1590b4af33edeebf39f118sf }
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj if ((domainp = apr_strmatch(conf->cookie_domain_str, str, len)) != NULL) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes domainp += 7;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes doffs = domainp - str;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes domaine = ap_strchr_c(domainp, ';');
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes l1 = domaine ? (domaine - domainp) : strlen(domainp);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes domaine = domainp + l1;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ent = (struct proxy_alias *)conf->cookie_domains->elts;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes for (i = 0; i < conf->cookie_domains->nelts; i++) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes l2 = strlen(ent[i].fake);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (l1 >= l2 && strncasecmp(ent[i].fake, domainp, l2) == 0) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes newdomain = ent[i].real;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ddiff = strlen(newdomain) - l1;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes break;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (newpath) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ret = apr_palloc(r->pool, len + pdiff + ddiff + 1);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes l1 = strlen(newpath);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (newdomain) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes l2 = strlen(newdomain);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (doffs > poffs) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes memcpy(ret, str, poffs);
8869662bb1a4078297020e94ae5e928626d877c6rederpj memcpy(ret + poffs, newpath, l1);
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem memcpy(ret + poffs + l1, pathe, domainp - pathe);
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem memcpy(ret + doffs + pdiff, newdomain, l2);
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem strcpy(ret + doffs + pdiff + l2, domaine);
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem else {
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem memcpy(ret, str, doffs) ;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem memcpy(ret + doffs, newdomain, l2);
8869662bb1a4078297020e94ae5e928626d877c6rederpj memcpy(ret + doffs + l2, domaine, pathp - domaine);
8869662bb1a4078297020e94ae5e928626d877c6rederpj memcpy(ret + poffs + ddiff, newpath, l1);
8869662bb1a4078297020e94ae5e928626d877c6rederpj strcpy(ret + poffs + ddiff + l1, pathe);
8869662bb1a4078297020e94ae5e928626d877c6rederpj }
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
8869662bb1a4078297020e94ae5e928626d877c6rederpj else {
8869662bb1a4078297020e94ae5e928626d877c6rederpj memcpy(ret, str, poffs);
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem memcpy(ret + poffs, newpath, l1);
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem strcpy(ret + poffs + l1, pathe);
8869662bb1a4078297020e94ae5e928626d877c6rederpj }
8869662bb1a4078297020e94ae5e928626d877c6rederpj }
8869662bb1a4078297020e94ae5e928626d877c6rederpj else {
8869662bb1a4078297020e94ae5e928626d877c6rederpj if (newdomain) {
8869662bb1a4078297020e94ae5e928626d877c6rederpj ret = apr_palloc(r->pool, len + pdiff + ddiff + 1);
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem l2 = strlen(newdomain);
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem memcpy(ret, str, doffs);
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem memcpy(ret + doffs, newdomain, l2);
8869662bb1a4078297020e94ae5e928626d877c6rederpj strcpy(ret + doffs+l2, domaine);
8869662bb1a4078297020e94ae5e928626d877c6rederpj }
8869662bb1a4078297020e94ae5e928626d877c6rederpj else {
8869662bb1a4078297020e94ae5e928626d877c6rederpj ret = (char *)str; /* no change */
8869662bb1a4078297020e94ae5e928626d877c6rederpj }
8869662bb1a4078297020e94ae5e928626d877c6rederpj }
8869662bb1a4078297020e94ae5e928626d877c6rederpj
8869662bb1a4078297020e94ae5e928626d877c6rederpj return ret;
8869662bb1a4078297020e94ae5e928626d877c6rederpj}
8869662bb1a4078297020e94ae5e928626d877c6rederpj
8869662bb1a4078297020e94ae5e928626d877c6rederpjPROXY_DECLARE(proxy_balancer *) ap_proxy_get_balancer(apr_pool_t *p,
8869662bb1a4078297020e94ae5e928626d877c6rederpj proxy_server_conf *conf,
8869662bb1a4078297020e94ae5e928626d877c6rederpj const char *url)
8869662bb1a4078297020e94ae5e928626d877c6rederpj{
6733d943c9e8d0f27dd077a04037e8c49eb090ffcovener proxy_balancer *balancer;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem char *c, *uri = apr_pstrdup(p, url);
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem int i;
8869662bb1a4078297020e94ae5e928626d877c6rederpj
8869662bb1a4078297020e94ae5e928626d877c6rederpj c = strchr(uri, ':');
8869662bb1a4078297020e94ae5e928626d877c6rederpj if (c == NULL || c[1] != '/' || c[2] != '/' || c[3] == '\0')
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf return NULL;
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf /* remove path from uri */
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf if ((c = strchr(c + 3, '/')))
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf *c = '\0';
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf balancer = (proxy_balancer *)conf->balancers->elts;
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf for (i = 0; i < conf->balancers->nelts; i++) {
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf if (strcasecmp(balancer->name, uri) == 0)
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf return balancer;
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf balancer++;
8869662bb1a4078297020e94ae5e928626d877c6rederpj }
8869662bb1a4078297020e94ae5e928626d877c6rederpj return NULL;
8869662bb1a4078297020e94ae5e928626d877c6rederpj}
8869662bb1a4078297020e94ae5e928626d877c6rederpj
8869662bb1a4078297020e94ae5e928626d877c6rederpjPROXY_DECLARE(const char *) ap_proxy_add_balancer(proxy_balancer **balancer,
8869662bb1a4078297020e94ae5e928626d877c6rederpj apr_pool_t *p,
8869662bb1a4078297020e94ae5e928626d877c6rederpj proxy_server_conf *conf,
8869662bb1a4078297020e94ae5e928626d877c6rederpj const char *url)
8869662bb1a4078297020e94ae5e928626d877c6rederpj{
8869662bb1a4078297020e94ae5e928626d877c6rederpj char *c, *q, *uri = apr_pstrdup(p, url);
8869662bb1a4078297020e94ae5e928626d877c6rederpj proxy_balancer_method *lbmethod;
8869662bb1a4078297020e94ae5e928626d877c6rederpj
8869662bb1a4078297020e94ae5e928626d877c6rederpj c = strchr(uri, ':');
8869662bb1a4078297020e94ae5e928626d877c6rederpj if (c == NULL || c[1] != '/' || c[2] != '/' || c[3] == '\0')
8869662bb1a4078297020e94ae5e928626d877c6rederpj return "Bad syntax for a balancer name";
8869662bb1a4078297020e94ae5e928626d877c6rederpj /* remove path from uri */
8869662bb1a4078297020e94ae5e928626d877c6rederpj if ((q = strchr(c + 3, '/')))
8869662bb1a4078297020e94ae5e928626d877c6rederpj *q = '\0';
8869662bb1a4078297020e94ae5e928626d877c6rederpj
8869662bb1a4078297020e94ae5e928626d877c6rederpj ap_str_tolower(uri);
8869662bb1a4078297020e94ae5e928626d877c6rederpj *balancer = apr_array_push(conf->balancers);
8869662bb1a4078297020e94ae5e928626d877c6rederpj memset(*balancer, 0, sizeof(proxy_balancer));
8869662bb1a4078297020e94ae5e928626d877c6rederpj
8869662bb1a4078297020e94ae5e928626d877c6rederpj /*
8869662bb1a4078297020e94ae5e928626d877c6rederpj * NOTE: The default method is byrequests, which we assume
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem * exists!
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem lbmethod = ap_lookup_provider(PROXY_LBMETHOD, "byrequests", "0");
8869662bb1a4078297020e94ae5e928626d877c6rederpj if (!lbmethod) {
8869662bb1a4078297020e94ae5e928626d877c6rederpj return "Can't find 'byrequests' lb method";
8869662bb1a4078297020e94ae5e928626d877c6rederpj }
8869662bb1a4078297020e94ae5e928626d877c6rederpj
8869662bb1a4078297020e94ae5e928626d877c6rederpj (*balancer)->name = uri;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem (*balancer)->lbmethod = lbmethod;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem (*balancer)->workers = apr_array_make(p, 5, sizeof(proxy_worker));
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem /* XXX Is this a right place to create mutex */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem#if APR_HAS_THREADS
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem if (apr_thread_mutex_create(&((*balancer)->mutex),
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem APR_THREAD_MUTEX_DEFAULT, p) != APR_SUCCESS) {
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem /* XXX: Do we need to log something here */
8869662bb1a4078297020e94ae5e928626d877c6rederpj return "can not create thread mutex";
8869662bb1a4078297020e94ae5e928626d877c6rederpj }
8869662bb1a4078297020e94ae5e928626d877c6rederpj#endif
8869662bb1a4078297020e94ae5e928626d877c6rederpj
8869662bb1a4078297020e94ae5e928626d877c6rederpj return NULL;
8869662bb1a4078297020e94ae5e928626d877c6rederpj}
8869662bb1a4078297020e94ae5e928626d877c6rederpj
8869662bb1a4078297020e94ae5e928626d877c6rederpjPROXY_DECLARE(proxy_worker *) ap_proxy_get_worker(apr_pool_t *p,
8869662bb1a4078297020e94ae5e928626d877c6rederpj proxy_server_conf *conf,
8869662bb1a4078297020e94ae5e928626d877c6rederpj const char *url)
8869662bb1a4078297020e94ae5e928626d877c6rederpj{
8869662bb1a4078297020e94ae5e928626d877c6rederpj proxy_worker *worker;
8869662bb1a4078297020e94ae5e928626d877c6rederpj proxy_worker *max_worker = NULL;
8869662bb1a4078297020e94ae5e928626d877c6rederpj int max_match = 0;
8869662bb1a4078297020e94ae5e928626d877c6rederpj int url_length;
8869662bb1a4078297020e94ae5e928626d877c6rederpj int worker_name_length;
8869662bb1a4078297020e94ae5e928626d877c6rederpj const char *c;
8869662bb1a4078297020e94ae5e928626d877c6rederpj char *url_copy;
8869662bb1a4078297020e94ae5e928626d877c6rederpj int i;
8869662bb1a4078297020e94ae5e928626d877c6rederpj
8869662bb1a4078297020e94ae5e928626d877c6rederpj c = ap_strchr_c(url, ':');
8869662bb1a4078297020e94ae5e928626d877c6rederpj if (c == NULL || c[1] != '/' || c[2] != '/' || c[3] == '\0')
8869662bb1a4078297020e94ae5e928626d877c6rederpj return NULL;
8869662bb1a4078297020e94ae5e928626d877c6rederpj
8869662bb1a4078297020e94ae5e928626d877c6rederpj url_copy = apr_pstrdup(p, url);
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem url_length = strlen(url);
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem
8869662bb1a4078297020e94ae5e928626d877c6rederpj /*
8869662bb1a4078297020e94ae5e928626d877c6rederpj * We need to find the start of the path and
8869662bb1a4078297020e94ae5e928626d877c6rederpj * therefore we know the length of the scheme://hostname/
8869662bb1a4078297020e94ae5e928626d877c6rederpj * part to we can force-lowercase everything up to
8869662bb1a4078297020e94ae5e928626d877c6rederpj * the start of the path.
8869662bb1a4078297020e94ae5e928626d877c6rederpj */
8869662bb1a4078297020e94ae5e928626d877c6rederpj c = ap_strchr_c(c+3, '/');
8869662bb1a4078297020e94ae5e928626d877c6rederpj if (c) {
8869662bb1a4078297020e94ae5e928626d877c6rederpj char *pathstart;
8869662bb1a4078297020e94ae5e928626d877c6rederpj pathstart = url_copy + (c - url);
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj *pathstart = '\0';
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem ap_str_tolower(url_copy);
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem *pathstart = '/';
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem } else {
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem ap_str_tolower(url_copy);
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj worker = (proxy_worker *)conf->workers->elts;
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem /*
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem * Do a "longest match" on the worker name to find the worker that
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem * fits best to the URL.
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem for (i = 0; i < conf->workers->nelts; i++) {
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem if ( ((worker_name_length = strlen(worker->name)) <= url_length)
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem && (worker_name_length > max_match)
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem && (strncmp(url_copy, worker->name, worker_name_length) == 0) ) {
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem max_worker = worker;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem max_match = worker_name_length;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem worker++;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem return max_worker;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem}
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem#if APR_HAS_THREADS
8445dae5cc606ba8ba04efc341cc1e081d95920drpluemstatic apr_status_t conn_pool_cleanup(void *theworker)
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem{
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem proxy_worker *worker = (proxy_worker *)theworker;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem if (worker->cp->res) {
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem worker->cp->pool = NULL;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem apr_reslist_destroy(worker->cp->res);
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem return APR_SUCCESS;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem}
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem#endif
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem
8445dae5cc606ba8ba04efc341cc1e081d95920drpluemstatic void init_conn_pool(apr_pool_t *p, proxy_worker *worker)
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj{
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj apr_pool_t *pool;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem proxy_conn_pool *cp;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj /*
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj * Create a connection pool's subpool.
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem * This pool is used for connection recycling.
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem * Once the worker is added it is never removed but
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem * it can be disabled.
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem */
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj apr_pool_create(&pool, p);
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj /*
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj * Alloc from the same pool as worker.
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj * proxy_conn_pool is permanently attached to the worker.
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj */
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj cp = (proxy_conn_pool *)apr_pcalloc(p, sizeof(proxy_conn_pool));
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj cp->pool = pool;
0e05808dc59a321566303084c84b9826a4353cefrederpj worker->cp = cp;
a9c4332dc6241dc11dd104826bd179d42ccc0f12fuankg}
a9c4332dc6241dc11dd104826bd179d42ccc0f12fuankg
11ca38a20ab9b2d00258f745620e2724838e7e21rederpjPROXY_DECLARE(const char *) ap_proxy_add_worker(proxy_worker **worker,
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj apr_pool_t *p,
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj proxy_server_conf *conf,
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj const char *url)
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem{
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem int rv;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem apr_uri_t uri;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem rv = apr_uri_parse(p, url, &uri);
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem if (rv != APR_SUCCESS) {
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem return "Unable to parse URL";
11ca38a20ab9b2d00258f745620e2724838e7e21rederpj }
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj if (!uri.hostname || !uri.scheme) {
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj return "URL must be absolute!";
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj }
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem ap_str_tolower(uri.hostname);
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem ap_str_tolower(uri.scheme);
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem *worker = apr_array_push(conf->workers);
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem memset(*worker, 0, sizeof(proxy_worker));
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem (*worker)->name = apr_uri_unparse(p, &uri, APR_URI_UNP_REVEALPASSWORD);
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem (*worker)->scheme = uri.scheme;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem (*worker)->hostname = uri.hostname;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem (*worker)->port = uri.port;
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj (*worker)->id = proxy_lb_workers;
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj (*worker)->flush_packets = flush_off;
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj (*worker)->flush_wait = PROXY_FLUSH_WAIT;
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj /* Increase the total worker count */
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj proxy_lb_workers++;
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj init_conn_pool(p, *worker);
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj#if APR_HAS_THREADS
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj if (apr_thread_mutex_create(&((*worker)->mutex),
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj APR_THREAD_MUTEX_DEFAULT, p) != APR_SUCCESS) {
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj /* XXX: Do we need to log something here */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem return "can not create thread mutex";
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem#endif
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj return NULL;
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj}
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpjPROXY_DECLARE(proxy_worker *) ap_proxy_create_worker(apr_pool_t *p)
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj{
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj proxy_worker *worker;
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj worker = (proxy_worker *)apr_pcalloc(p, sizeof(proxy_worker));
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj worker->id = proxy_lb_workers;
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj /* Increase the total worker count */
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj proxy_lb_workers++;
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj init_conn_pool(p, worker);
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj return worker;
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj}
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj
8445dae5cc606ba8ba04efc341cc1e081d95920drpluemPROXY_DECLARE(void)
8445dae5cc606ba8ba04efc341cc1e081d95920drpluemap_proxy_add_worker_to_balancer(apr_pool_t *pool, proxy_balancer *balancer,
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj proxy_worker *worker)
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj{
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem proxy_worker *runtime;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem runtime = apr_array_push(balancer->workers);
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem memcpy(runtime, worker, sizeof(proxy_worker));
0e05808dc59a321566303084c84b9826a4353cefrederpj runtime->id = proxy_lb_workers;
0e05808dc59a321566303084c84b9826a4353cefrederpj /* Increase the total runtime count */
0e05808dc59a321566303084c84b9826a4353cefrederpj proxy_lb_workers++;
0e05808dc59a321566303084c84b9826a4353cefrederpj
0e05808dc59a321566303084c84b9826a4353cefrederpj}
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf
0e05808dc59a321566303084c84b9826a4353cefrederpjPROXY_DECLARE(int) ap_proxy_pre_request(proxy_worker **worker,
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf proxy_balancer **balancer,
0e05808dc59a321566303084c84b9826a4353cefrederpj request_rec *r,
0e05808dc59a321566303084c84b9826a4353cefrederpj proxy_server_conf *conf, char **url)
0e05808dc59a321566303084c84b9826a4353cefrederpj{
0e05808dc59a321566303084c84b9826a4353cefrederpj int access_status;
0e05808dc59a321566303084c84b9826a4353cefrederpj
0e05808dc59a321566303084c84b9826a4353cefrederpj access_status = proxy_run_pre_request(worker, balancer, r, conf, url);
0e05808dc59a321566303084c84b9826a4353cefrederpj if (access_status == DECLINED && *balancer == NULL) {
0e05808dc59a321566303084c84b9826a4353cefrederpj *worker = ap_proxy_get_worker(r->pool, conf, *url);
0e05808dc59a321566303084c84b9826a4353cefrederpj if (*worker) {
0e05808dc59a321566303084c84b9826a4353cefrederpj ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
0e05808dc59a321566303084c84b9826a4353cefrederpj "proxy: %s: found worker %s for %s",
0e05808dc59a321566303084c84b9826a4353cefrederpj (*worker)->scheme, (*worker)->name, *url);
0e05808dc59a321566303084c84b9826a4353cefrederpj
0e05808dc59a321566303084c84b9826a4353cefrederpj *balancer = NULL;
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj access_status = OK;
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj }
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj else if (r->proxyreq == PROXYREQ_PROXY) {
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj if (conf->forward) {
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj "proxy: *: found forward proxy worker for %s",
0e05808dc59a321566303084c84b9826a4353cefrederpj *url);
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj *balancer = NULL;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem *worker = conf->forward;
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf access_status = OK;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj else if (r->proxyreq == PROXYREQ_REVERSE) {
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj if (conf->reverse) {
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf "proxy: *: found reverse proxy worker for %s",
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj *url);
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj *balancer = NULL;
0e05808dc59a321566303084c84b9826a4353cefrederpj *worker = conf->reverse;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem access_status = OK;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj }
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj else if (access_status == DECLINED && *balancer != NULL) {
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj /* All the workers are busy */
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj "proxy: all workers are busy. Unable to serve %s",
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj *url);
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj access_status = HTTP_SERVICE_UNAVAILABLE;
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj }
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj return access_status;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem}
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpjPROXY_DECLARE(int) ap_proxy_post_request(proxy_worker *worker,
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj proxy_balancer *balancer,
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem request_rec *r,
0e05808dc59a321566303084c84b9826a4353cefrederpj proxy_server_conf *conf)
0e05808dc59a321566303084c84b9826a4353cefrederpj{
0e05808dc59a321566303084c84b9826a4353cefrederpj int access_status;
0e05808dc59a321566303084c84b9826a4353cefrederpj if (balancer)
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem access_status = proxy_run_post_request(worker, balancer, r, conf);
0e05808dc59a321566303084c84b9826a4353cefrederpj else {
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf
0e05808dc59a321566303084c84b9826a4353cefrederpj
0e05808dc59a321566303084c84b9826a4353cefrederpj access_status = OK;
0e05808dc59a321566303084c84b9826a4353cefrederpj }
0e05808dc59a321566303084c84b9826a4353cefrederpj
0e05808dc59a321566303084c84b9826a4353cefrederpj return access_status;
0e05808dc59a321566303084c84b9826a4353cefrederpj}
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf/* DEPRECATED */
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sfPROXY_DECLARE(int) ap_proxy_connect_to_backend(apr_socket_t **newsock,
0e05808dc59a321566303084c84b9826a4353cefrederpj const char *proxy_function,
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj apr_sockaddr_t *backend_addr,
0e05808dc59a321566303084c84b9826a4353cefrederpj const char *backend_name,
40a1aee60a66f7c8dbd0835fdd4f09334e12fc15rpluem proxy_server_conf *conf,
0e05808dc59a321566303084c84b9826a4353cefrederpj server_rec *s,
0e05808dc59a321566303084c84b9826a4353cefrederpj apr_pool_t *p)
40a1aee60a66f7c8dbd0835fdd4f09334e12fc15rpluem{
0e05808dc59a321566303084c84b9826a4353cefrederpj apr_status_t rv;
0e05808dc59a321566303084c84b9826a4353cefrederpj int connected = 0;
0e05808dc59a321566303084c84b9826a4353cefrederpj int loglevel;
0e05808dc59a321566303084c84b9826a4353cefrederpj
0e05808dc59a321566303084c84b9826a4353cefrederpj while (backend_addr && !connected) {
0e05808dc59a321566303084c84b9826a4353cefrederpj if ((rv = apr_socket_create(newsock, backend_addr->family,
0e05808dc59a321566303084c84b9826a4353cefrederpj SOCK_STREAM, 0, p)) != APR_SUCCESS) {
0e05808dc59a321566303084c84b9826a4353cefrederpj loglevel = backend_addr->next ? APLOG_DEBUG : APLOG_ERR;
0e05808dc59a321566303084c84b9826a4353cefrederpj ap_log_error(APLOG_MARK, loglevel, rv, s,
0e05808dc59a321566303084c84b9826a4353cefrederpj "proxy: %s: error creating fam %d socket for target %s",
0e05808dc59a321566303084c84b9826a4353cefrederpj proxy_function,
0e05808dc59a321566303084c84b9826a4353cefrederpj backend_addr->family,
0e05808dc59a321566303084c84b9826a4353cefrederpj backend_name);
0e05808dc59a321566303084c84b9826a4353cefrederpj /*
0e05808dc59a321566303084c84b9826a4353cefrederpj * this could be an IPv6 address from the DNS but the
0e05808dc59a321566303084c84b9826a4353cefrederpj * local machine won't give us an IPv6 socket; hopefully the
0e05808dc59a321566303084c84b9826a4353cefrederpj * DNS returned an additional address to try
0e05808dc59a321566303084c84b9826a4353cefrederpj */
0e05808dc59a321566303084c84b9826a4353cefrederpj backend_addr = backend_addr->next;
0e05808dc59a321566303084c84b9826a4353cefrederpj continue;
0e05808dc59a321566303084c84b9826a4353cefrederpj }
0e05808dc59a321566303084c84b9826a4353cefrederpj
0e05808dc59a321566303084c84b9826a4353cefrederpj#if !defined(TPF) && !defined(BEOS)
0e05808dc59a321566303084c84b9826a4353cefrederpj if (conf->recv_buffer_size > 0 &&
0e05808dc59a321566303084c84b9826a4353cefrederpj (rv = apr_socket_opt_set(*newsock, APR_SO_RCVBUF,
0e05808dc59a321566303084c84b9826a4353cefrederpj conf->recv_buffer_size))) {
0e05808dc59a321566303084c84b9826a4353cefrederpj ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj "apr_socket_opt_set(SO_RCVBUF): Failed to set "
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj "ProxyReceiveBufferSize, using default");
0e05808dc59a321566303084c84b9826a4353cefrederpj }
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj#endif
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem /* Set a timeout on the socket */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem if (conf->timeout_set == 1) {
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem apr_socket_timeout_set(*newsock, conf->timeout);
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem }
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem else {
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj apr_socket_timeout_set(*newsock, s->timeout);
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj }
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj "proxy: %s: fam %d socket created to connect to %s",
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj proxy_function, backend_addr->family, backend_name);
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj /* make the connection out of the socket */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem rv = apr_socket_connect(*newsock, backend_addr);
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem /* if an error occurred, loop round and try again */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem if (rv != APR_SUCCESS) {
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj apr_socket_close(*newsock);
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj loglevel = backend_addr->next ? APLOG_DEBUG : APLOG_ERR;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem ap_log_error(APLOG_MARK, loglevel, rv, s,
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem "proxy: %s: attempt to connect to %pI (%s) failed",
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem proxy_function,
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem backend_addr,
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf backend_name);
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf backend_addr = backend_addr->next;
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf continue;
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf }
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj connected = 1;
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj }
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem return connected ? 0 : 1;
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem}
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem
8445dae5cc606ba8ba04efc341cc1e081d95920drpluemstatic apr_status_t connection_cleanup(void *theconn)
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf{
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf proxy_conn_rec *conn = (proxy_conn_rec *)theconn;
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf proxy_worker *worker = conn->worker;
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj /*
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem * If the connection pool is NULL the worker
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem * cleanup has been run. Just return.
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem */
8445dae5cc606ba8ba04efc341cc1e081d95920drpluem if (!worker->cp)
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj return APR_SUCCESS;
85da6b76d07b7af570cbbec208a87697ba9c44f5rederpj
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj#if APR_HAS_THREADS
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj /* Sanity check: Did we already return the pooled connection? */
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj if (conn->inreslist) {
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj ap_log_perror(APLOG_MARK, APLOG_ERR, 0, conn->pool,
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj "proxy: Pooled connection 0x%pp for worker %s has been"
3f5585f7f4a7d74f2f94ec729ea8c1879d419e35rederpj " already returned to the connection pool.", conn,
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe worker->name);
e8f95a682820a599fe41b22977010636be5c2717jim return APR_SUCCESS;
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe }
e8f95a682820a599fe41b22977010636be5c2717jim#endif
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* determine if the connection need to be closed */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (conn->close_on_recycle || conn->close) {
8bdea88407c848c1c2693655e2f8b23abde12307bnicholes apr_pool_t *p = conn->pool;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_pool_clear(conn->pool);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes memset(conn, 0, sizeof(proxy_conn_rec));
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes conn->pool = p;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes conn->worker = worker;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
f05787953018140838ad51456c86c965d6a86267jim#if APR_HAS_THREADS
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (worker->hmax && worker->cp->res) {
f05787953018140838ad51456c86c965d6a86267jim conn->inreslist = 1;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_reslist_release(worker->cp->res, (void *)conn);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else
e8f95a682820a599fe41b22977010636be5c2717jim#endif
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes worker->cp->conn = conn;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* Allways return the SUCCESS */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return APR_SUCCESS;
e8f95a682820a599fe41b22977010636be5c2717jim}
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* reslist constructor */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesstatic apr_status_t connection_constructor(void **resource, void *params,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_pool_t *pool)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_pool_t *ctx;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes proxy_conn_rec *conn;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes proxy_worker *worker = (proxy_worker *)params;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
e8f95a682820a599fe41b22977010636be5c2717jim /*
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe * Create the subpool for each connection
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes * This keeps the memory consumption constant
e8f95a682820a599fe41b22977010636be5c2717jim * when disconnecting from backend.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_pool_create(&ctx, pool);
e8f95a682820a599fe41b22977010636be5c2717jim conn = apr_pcalloc(pool, sizeof(proxy_conn_rec));
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
e8f95a682820a599fe41b22977010636be5c2717jim conn->pool = ctx;
e8f95a682820a599fe41b22977010636be5c2717jim conn->worker = worker;
e8f95a682820a599fe41b22977010636be5c2717jim#if APR_HAS_THREADS
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe conn->inreslist = 1;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#endif
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *resource = conn;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return APR_SUCCESS;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
e8f95a682820a599fe41b22977010636be5c2717jim
e8f95a682820a599fe41b22977010636be5c2717jim#if APR_HAS_THREADS /* only needed when threads are used */
e8f95a682820a599fe41b22977010636be5c2717jim/* reslist destructor */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowestatic apr_status_t connection_destructor(void *resource, void *params,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_pool_t *pool)
c4f16f709c79bb7e2ddffb532bc7708eab9a9691covener{
c4f16f709c79bb7e2ddffb532bc7708eab9a9691covener proxy_conn_rec *conn = (proxy_conn_rec *)resource;
d64dd2fd4516c2b1b664c5e59c0628d9aff26984covener
d64dd2fd4516c2b1b664c5e59c0628d9aff26984covener /* Destroy the pool only if not called from reslist_destroy */
d64dd2fd4516c2b1b664c5e59c0628d9aff26984covener if (conn->worker->cp->pool)
d64dd2fd4516c2b1b664c5e59c0628d9aff26984covener apr_pool_destroy(conn->pool);
c4f16f709c79bb7e2ddffb532bc7708eab9a9691covener
c4f16f709c79bb7e2ddffb532bc7708eab9a9691covener return APR_SUCCESS;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#endif
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * ap_proxy_initialize_worker_share() concerns itself
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * with initializing those parts of worker which
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * are, or could be, shared. Basically worker->s
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesPROXY_DECLARE(void) ap_proxy_initialize_worker_share(proxy_server_conf *conf,
e8f95a682820a599fe41b22977010636be5c2717jim proxy_worker *worker,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes server_rec *s)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#if PROXY_HAS_SCOREBOARD
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes lb_score *score = NULL;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe#else
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes void *score = NULL;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#endif
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (worker->s && PROXY_WORKER_IS_INITIALIZED(worker)) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* The worker share is already initialized */
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "proxy: worker %s already initialized",
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes worker->name);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener#if PROXY_HAS_SCOREBOARD
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener /* Get scoreboard slot */
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener if (ap_scoreboard_image) {
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf score = ap_get_scoreboard_lb(worker->id);
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener if (!score) {
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "proxy: ap_get_scoreboard_lb(%d) failed in child %" APR_PID_T_FMT " for worker %s",
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes worker->id, getpid(), worker->name);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "proxy: grabbed scoreboard slot %d in child %" APR_PID_T_FMT " for worker %s",
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes worker->id, getpid(), worker->name);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#endif
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (!score) {
e8f95a682820a599fe41b22977010636be5c2717jim score = apr_pcalloc(conf->pool, sizeof(proxy_worker_stat));
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "proxy: initialized plain memory in child %" APR_PID_T_FMT " for worker %s",
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes getpid(), worker->name);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
e8f95a682820a599fe41b22977010636be5c2717jim worker->s = (proxy_worker_stat *)score;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * recheck to see if we've already been here. Possible
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * if proxy is using scoreboard to hold shared stats
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe if (PROXY_WORKER_IS_INITIALIZED(worker)) {
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe /* The worker share is already initialized */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "proxy: worker %s already initialized",
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes worker->name);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (worker->route)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes strcpy(worker->s->route, worker->route);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *worker->s->route = '\0';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (worker->redirect)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes strcpy(worker->s->redirect, worker->redirect);
e8f95a682820a599fe41b22977010636be5c2717jim else
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes *worker->s->redirect = '\0';
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes worker->s->status |= (worker->status | PROXY_WORKER_INITIALIZED);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholesPROXY_DECLARE(apr_status_t) ap_proxy_initialize_worker(proxy_worker *worker, server_rec *s)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_status_t rv;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#if APR_HAS_THREADS
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes int mpm_threads;
e8f95a682820a599fe41b22977010636be5c2717jim#endif
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (PROXY_WORKER_IS_INITIALIZED(worker)) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* The worker is already initialized */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return APR_SUCCESS;
7dbf29be626018bc389ef94c1846aeac4b72633bsf }
7dbf29be626018bc389ef94c1846aeac4b72633bsf
7dbf29be626018bc389ef94c1846aeac4b72633bsf /* Set default parameters */
7dbf29be626018bc389ef94c1846aeac4b72633bsf if (!worker->retry)
7dbf29be626018bc389ef94c1846aeac4b72633bsf worker->retry = apr_time_from_sec(PROXY_WORKER_DEFAULT_RETRY);
7dbf29be626018bc389ef94c1846aeac4b72633bsf /* By default address is reusable */
7dbf29be626018bc389ef94c1846aeac4b72633bsf worker->is_address_reusable = 1;
7dbf29be626018bc389ef94c1846aeac4b72633bsf
7dbf29be626018bc389ef94c1846aeac4b72633bsf#if APR_HAS_THREADS
7dbf29be626018bc389ef94c1846aeac4b72633bsf ap_mpm_query(AP_MPMQ_MAX_THREADS, &mpm_threads);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (mpm_threads > 1) {
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes /* Set hard max to no more then mpm_threads */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (worker->hmax == 0 || worker->hmax > mpm_threads)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes worker->hmax = mpm_threads;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (worker->smax == 0 || worker->smax > worker->hmax)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes worker->smax = worker->hmax;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* Set min to be lower then smax */
7dbf29be626018bc389ef94c1846aeac4b72633bsf if (worker->min > worker->smax)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes worker->min = worker->smax;
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* This will supress the apr_reslist creation */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes worker->min = worker->smax = worker->hmax = 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (worker->hmax) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes rv = apr_reslist_create(&(worker->cp->res),
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes worker->min, worker->smax,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes worker->hmax, worker->ttl,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes connection_constructor, connection_destructor,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes worker, worker->cp->pool);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_pool_cleanup_register(worker->cp->pool, (void *)worker,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes conn_pool_cleanup,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_pool_cleanup_null);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "proxy: initialized worker %d in child %" APR_PID_T_FMT " for (%s) min=%d max=%d smax=%d",
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes worker->id, getpid(), worker->hostname, worker->min,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes worker->hmax, worker->smax);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
8bdea88407c848c1c2693655e2f8b23abde12307bnicholes#if (APR_MAJOR_VERSION > 0)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* Set the acquire timeout */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (rv == APR_SUCCESS && worker->acquire_set)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_reslist_timeout_set(worker->cp->res, worker->acquire);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#endif
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#endif
e8f95a682820a599fe41b22977010636be5c2717jim {
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe void *conn;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes rv = connection_constructor(&conn, worker, worker->cp->pool);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes worker->cp->conn = conn;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "proxy: initialized single connection worker %d in child %" APR_PID_T_FMT " for (%s)",
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes worker->id, getpid(), worker->hostname);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (rv == APR_SUCCESS) {
e8f95a682820a599fe41b22977010636be5c2717jim worker->status |= (PROXY_WORKER_INITIALIZED);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return rv;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesPROXY_DECLARE(int) ap_proxy_retry_worker(const char *proxy_function,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes proxy_worker *worker,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes server_rec *s)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (worker->s->status & PROXY_WORKER_IN_ERROR) {
8bdea88407c848c1c2693655e2f8b23abde12307bnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "proxy: %s: retrying the worker for (%s)",
e8f95a682820a599fe41b22977010636be5c2717jim proxy_function, worker->hostname);
e8f95a682820a599fe41b22977010636be5c2717jim if (apr_time_now() > worker->s->error_time + worker->retry) {
e8f95a682820a599fe41b22977010636be5c2717jim ++worker->s->retries;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe worker->s->status &= ~PROXY_WORKER_IN_ERROR;
e8f95a682820a599fe41b22977010636be5c2717jim ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe "proxy: %s: worker for (%s) has been marked for retry",
e8f95a682820a599fe41b22977010636be5c2717jim proxy_function, worker->hostname);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes return OK;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes }
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes else
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes return DECLINED;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes }
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes else
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes return OK;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholesPROXY_DECLARE(int) ap_proxy_acquire_connection(const char *proxy_function,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes proxy_conn_rec **conn,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes proxy_worker *worker,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes server_rec *s)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_status_t rv;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (!PROXY_WORKER_IS_USABLE(worker)) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* Retry the worker */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_proxy_retry_worker(proxy_function, worker, s);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (!PROXY_WORKER_IS_USABLE(worker)) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "proxy: %s: disabled connection for (%s)",
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes proxy_function, worker->hostname);
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe return HTTP_SERVICE_UNAVAILABLE;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes }
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes }
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes#if APR_HAS_THREADS
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes if (worker->hmax && worker->cp->res) {
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes rv = apr_reslist_acquire(worker->cp->res, (void **)conn);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes }
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe else
e8f95a682820a599fe41b22977010636be5c2717jim#endif
e8f95a682820a599fe41b22977010636be5c2717jim {
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe /* create the new connection if the previous was destroyed */
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes if (!worker->cp->conn)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes connection_constructor((void **)conn, worker, worker->cp->pool);
8bdea88407c848c1c2693655e2f8b23abde12307bnicholes else {
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes *conn = worker->cp->conn;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes worker->cp->conn = NULL;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes }
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes rv = APR_SUCCESS;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes }
f05787953018140838ad51456c86c965d6a86267jim
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes if (rv != APR_SUCCESS) {
f05787953018140838ad51456c86c965d6a86267jim ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes "proxy: %s: failed to acquire connection for (%s)",
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes proxy_function, worker->hostname);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes return HTTP_SERVICE_UNAVAILABLE;
e8f95a682820a599fe41b22977010636be5c2717jim }
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes "proxy: %s: has acquired connection for (%s)",
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes proxy_function, worker->hostname);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes (*conn)->worker = worker;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes (*conn)->close = 0;
e8f95a682820a599fe41b22977010636be5c2717jim (*conn)->close_on_recycle = 0;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe#if APR_HAS_THREADS
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes (*conn)->inreslist = 0;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes#endif
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes return OK;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes}
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholesPROXY_DECLARE(int) ap_proxy_release_connection(const char *proxy_function,
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes proxy_conn_rec *conn,
e8f95a682820a599fe41b22977010636be5c2717jim server_rec *s)
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe{
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
e8f95a682820a599fe41b22977010636be5c2717jim "proxy: %s: has released connection for (%s)",
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes proxy_function, conn->worker->hostname);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes /* If there is a connection kill it's cleanup */
e8f95a682820a599fe41b22977010636be5c2717jim if (conn->connection) {
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes apr_pool_cleanup_kill(conn->connection->pool, conn, connection_cleanup);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes conn->connection = NULL;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes }
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes connection_cleanup(conn);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes return OK;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes}
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholesPROXY_DECLARE(int)
c4f16f709c79bb7e2ddffb532bc7708eab9a9691covenerap_proxy_determine_connection(apr_pool_t *p, request_rec *r,
c4f16f709c79bb7e2ddffb532bc7708eab9a9691covener proxy_server_conf *conf,
d64dd2fd4516c2b1b664c5e59c0628d9aff26984covener proxy_worker *worker,
d64dd2fd4516c2b1b664c5e59c0628d9aff26984covener proxy_conn_rec *conn,
d64dd2fd4516c2b1b664c5e59c0628d9aff26984covener apr_uri_t *uri,
d64dd2fd4516c2b1b664c5e59c0628d9aff26984covener char **url,
c4f16f709c79bb7e2ddffb532bc7708eab9a9691covener const char *proxyname,
c4f16f709c79bb7e2ddffb532bc7708eab9a9691covener apr_port_t proxyport,
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes char *server_portstr,
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes int server_portstr_size)
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes{
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes int server_port;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes apr_status_t err = APR_SUCCESS;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes /*
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes * Break up the URL to determine the host to connect to
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes */
e8f95a682820a599fe41b22977010636be5c2717jim
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes /* we break the URL into host, port, uri */
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes if (APR_SUCCESS != apr_uri_parse(p, *url, uri)) {
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes return ap_proxyerror(r, HTTP_BAD_REQUEST,
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes apr_pstrcat(p,"URI cannot be parsed: ", *url,
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe NULL));
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes }
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes if (!uri->port) {
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes uri->port = apr_uri_port_of_scheme(uri->scheme);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes }
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes "proxy: connecting %s to %s:%d", *url, uri->hostname,
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes uri->port);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes /*
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener * allocate these out of the specified connection pool
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener * The scheme handler decides if this is permanent or
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener * short living pool.
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf */
37af4b0cf648275b68ff41c866c665b4ccf4667dcovener /* are we connecting directly, or via a proxy? */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe if (!proxyname) {
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes *url = apr_pstrcat(p, uri->path, uri->query ? "?" : "",
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholes uri->query ? uri->query : "",
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes uri->fragment ? "#" : "",
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes uri->fragment ? uri->fragment : "", NULL);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes }
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes /*
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes * Make sure that we pick the the correct and valid worker.
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes * If a single keepalive connection triggers different workers,
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes * then we have a problem (we don't select the correct one).
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes * Do an expensive check in this case, where we compare the
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes * the hostnames associated between the two.
e8f95a682820a599fe41b22977010636be5c2717jim *
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes * TODO: Handle this much better...
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes */
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes if (!conn->hostname || !worker->is_address_reusable ||
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes (r->connection->keepalives &&
e8f95a682820a599fe41b22977010636be5c2717jim (r->proxyreq == PROXYREQ_PROXY || r->proxyreq == PROXYREQ_REVERSE) &&
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes (strcasecmp(conn->hostname, uri->hostname) != 0) ) ) {
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes if (proxyname) {
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes conn->hostname = apr_pstrdup(conn->pool, proxyname);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes conn->port = proxyport;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe } else {
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe conn->hostname = apr_pstrdup(conn->pool, uri->hostname);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes conn->port = uri->port;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes }
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes if (conn->sock) {
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes apr_socket_close(conn->sock);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes conn->sock = NULL;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes }
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes if (conn->connection) {
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes apr_pool_cleanup_kill(conn->connection->pool, conn, connection_cleanup);
490046d2a164ad86cc63bd789f32eaed663d239abnicholes conn->connection = NULL;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes }
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes err = apr_sockaddr_info_get(&(conn->addr),
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes conn->hostname, APR_UNSPEC,
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes conn->port, 0,
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes conn->pool);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes }
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes else if (!worker->cp->addr) {
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes if ((err = PROXY_THREAD_LOCK(worker)) != APR_SUCCESS) {
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes ap_log_error(APLOG_MARK, APLOG_ERR, err, r->server,
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes "proxy: lock");
8bdea88407c848c1c2693655e2f8b23abde12307bnicholes return HTTP_INTERNAL_SERVER_ERROR;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes }
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes /*
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes * Worker can have the single constant backend adress.
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes * The single DNS lookup is used once per worker.
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes * If dynamic change is needed then set the addr to NULL
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes * inside dynamic config to force the lookup.
e8f95a682820a599fe41b22977010636be5c2717jim */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe err = apr_sockaddr_info_get(&(worker->cp->addr),
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes conn->hostname, APR_UNSPEC,
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes conn->port, 0,
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes worker->cp->pool);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes conn->addr = worker->cp->addr;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes PROXY_THREAD_UNLOCK(worker);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes }
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes else
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes conn->addr = worker->cp->addr;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
e8f95a682820a599fe41b22977010636be5c2717jim if (err != APR_SUCCESS) {
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes return ap_proxyerror(r, HTTP_BAD_GATEWAY,
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes apr_pstrcat(p, "DNS lookup failure for: ",
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes conn->hostname, NULL));
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes }
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes /* Get the server port for the Via headers */
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes {
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes server_port = ap_get_server_port(r);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes if (ap_is_default_port(server_port, r)) {
8bdea88407c848c1c2693655e2f8b23abde12307bnicholes strcpy(server_portstr,"");
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes } else {
e8f95a682820a599fe41b22977010636be5c2717jim apr_snprintf(server_portstr, server_portstr_size, ":%d",
e8f95a682820a599fe41b22977010636be5c2717jim server_port);
e8f95a682820a599fe41b22977010636be5c2717jim }
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe }
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe /* check if ProxyBlock directive on this host */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe if (OK != ap_proxy_checkproxyblock(r, conf, conn->addr)) {
e8f95a682820a599fe41b22977010636be5c2717jim return ap_proxyerror(r, HTTP_FORBIDDEN,
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes "Connect to remote machine blocked");
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes }
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes "proxy: connected %s to %s:%d", *url, conn->hostname,
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes conn->port);
0c8aa496e9d7676ff8101783398f17c0da1900f7bnicholes return OK;
0c8aa496e9d7676ff8101783398f17c0da1900f7bnicholes}
0c8aa496e9d7676ff8101783398f17c0da1900f7bnicholes
0c8aa496e9d7676ff8101783398f17c0da1900f7bnicholesstatic int is_socket_connected(apr_socket_t *sock)
0c8aa496e9d7676ff8101783398f17c0da1900f7bnicholes
0c8aa496e9d7676ff8101783398f17c0da1900f7bnicholes{
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes apr_size_t buffer_len = 1;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes char test_buffer[1];
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes apr_status_t socket_status;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes apr_interval_time_t current_timeout;
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes /* save timeout */
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes apr_socket_timeout_get(sock, &current_timeout);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes /* set no timeout */
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes apr_socket_timeout_set(sock, 0);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes socket_status = apr_socket_recv(sock, test_buffer, &buffer_len);
03f4448f864e31ade79856ac8c264a5e6dce3b10bnicholes /* put back old timeout */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_socket_timeout_set(sock, current_timeout);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (APR_STATUS_IS_EOF(socket_status))
e8f95a682820a599fe41b22977010636be5c2717jim return 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return 1;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes}
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesPROXY_DECLARE(int) ap_proxy_connect_backend(const char *proxy_function,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes proxy_conn_rec *conn,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes proxy_worker *worker,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes server_rec *s)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_status_t rv;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes int connected = 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes int loglevel;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_sockaddr_t *backend_addr = conn->addr;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_socket_t *newsock;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (conn->sock) {
e8f95a682820a599fe41b22977010636be5c2717jim /*
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe * This increases the connection pool size
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * but the number of dropped connections is
e8f95a682820a599fe41b22977010636be5c2717jim * relatively small compared to connection lifetime
e8f95a682820a599fe41b22977010636be5c2717jim */
43c3e6a4b559b76b750c245ee95e2782c15b4296jim if (!(connected = is_socket_connected(conn->sock))) {
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes apr_socket_close(conn->sock);
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes conn->sock = NULL;
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes }
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes }
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes while (backend_addr && !connected) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if ((rv = apr_socket_create(&newsock, backend_addr->family,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes SOCK_STREAM, APR_PROTO_TCP,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes conn->pool)) != APR_SUCCESS) {
e8f95a682820a599fe41b22977010636be5c2717jim loglevel = backend_addr->next ? APLOG_DEBUG : APLOG_ERR;
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf ap_log_error(APLOG_MARK, loglevel, rv, s,
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf "proxy: %s: error creating fam %d socket for target %s",
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf proxy_function,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes backend_addr->family,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes worker->hostname);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * this could be an IPv6 address from the DNS but the
e8f95a682820a599fe41b22977010636be5c2717jim * local machine won't give us an IPv6 socket; hopefully the
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe * DNS returned an additional address to try
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
e8f95a682820a599fe41b22977010636be5c2717jim backend_addr = backend_addr->next;
e8f95a682820a599fe41b22977010636be5c2717jim continue;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes#if !defined(TPF) && !defined(BEOS)
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes if (worker->recv_buffer_size > 0 &&
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes (rv = apr_socket_opt_set(newsock, APR_SO_RCVBUF,
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes worker->recv_buffer_size))) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "apr_socket_opt_set(SO_RCVBUF): Failed to set "
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "ProxyReceiveBufferSize, using default");
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes#endif
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* Set a timeout on the socket */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (worker->timeout_set == 1) {
e8f95a682820a599fe41b22977010636be5c2717jim apr_socket_timeout_set(newsock, worker->timeout);
e8f95a682820a599fe41b22977010636be5c2717jim }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_socket_timeout_set(newsock, s->timeout);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* Set a keepalive option */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (worker->keepalive) {
e8f95a682820a599fe41b22977010636be5c2717jim if ((rv = apr_socket_opt_set(newsock,
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe APR_SO_KEEPALIVE, 1)) != APR_SUCCESS) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
e8f95a682820a599fe41b22977010636be5c2717jim "apr_socket_opt_set(SO_KEEPALIVE): Failed to set"
e8f95a682820a599fe41b22977010636be5c2717jim " Keepalive");
43c3e6a4b559b76b750c245ee95e2782c15b4296jim }
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes }
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes "proxy: %s: fam %d socket created to connect to %s",
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes proxy_function, backend_addr->family, worker->hostname);
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* make the connection out of the socket */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes rv = apr_socket_connect(newsock, backend_addr);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
e8f95a682820a599fe41b22977010636be5c2717jim /* if an error occurred, loop round and try again */
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf if (rv != APR_SUCCESS) {
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf apr_socket_close(newsock);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes loglevel = backend_addr->next ? APLOG_DEBUG : APLOG_ERR;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, loglevel, rv, s,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "proxy: %s: attempt to connect to %pI (%s) failed",
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes proxy_function,
e8f95a682820a599fe41b22977010636be5c2717jim backend_addr,
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe worker->hostname);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes backend_addr = backend_addr->next;
e8f95a682820a599fe41b22977010636be5c2717jim continue;
e8f95a682820a599fe41b22977010636be5c2717jim }
43c3e6a4b559b76b750c245ee95e2782c15b4296jim
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes conn->sock = newsock;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes connected = 1;
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes }
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes /*
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes * Put the entire worker to error state if
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * the PROXY_WORKER_IGNORE_ERRORS flag is not set.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Altrough some connections may be alive
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * no further connections to the worker could be made
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (!connected && PROXY_WORKER_IS_USABLE(worker) &&
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes !(worker->s->status & PROXY_WORKER_IGNORE_ERRORS)) {
e8f95a682820a599fe41b22977010636be5c2717jim worker->s->status |= PROXY_WORKER_IN_ERROR;
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf worker->s->error_time = apr_time_now();
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "ap_proxy_connect_backend disabling worker for (%s)",
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes worker->hostname);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes else {
e8f95a682820a599fe41b22977010636be5c2717jim worker->s->error_time = 0;
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe worker->s->retries = 0;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
e8f95a682820a599fe41b22977010636be5c2717jim return connected ? OK : DECLINED;
e8f95a682820a599fe41b22977010636be5c2717jim}
43c3e6a4b559b76b750c245ee95e2782c15b4296jim
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholesPROXY_DECLARE(int) ap_proxy_connection_create(const char *proxy_function,
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes proxy_conn_rec *conn,
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes conn_rec *c,
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes server_rec *s)
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes{
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_sockaddr_t *backend_addr = conn->addr;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes int rc;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
e8f95a682820a599fe41b22977010636be5c2717jim /*
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf * The socket is now open, create a new backend server connection
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes conn->connection = ap_run_create_connection(c->pool, s, conn->sock,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes c->id, c->sbh,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes c->bucket_alloc);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
e8f95a682820a599fe41b22977010636be5c2717jim if (!conn->connection) {
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowe /*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * the peer reset the connection already; ap_run_create_connection()
e8f95a682820a599fe41b22977010636be5c2717jim * closed the socket
e8f95a682820a599fe41b22977010636be5c2717jim */
43c3e6a4b559b76b750c245ee95e2782c15b4296jim ap_log_error(APLOG_MARK, APLOG_DEBUG, 0,
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes s, "proxy: %s: an error occurred creating a "
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes "new connection to %pI (%s)", proxy_function,
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes backend_addr, conn->hostname);
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes /* XXX: Will be closed when proxy_conn is closed */
3effb85eb3124c6f02cd89e22ffed0fc9afaddb9bnicholes apr_socket_close(conn->sock);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes conn->sock = NULL;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return HTTP_INTERNAL_SERVER_ERROR;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes }
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /*
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * register the connection cleanup to client connection
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * so that the connection can be closed or reused
e8f95a682820a599fe41b22977010636be5c2717jim */
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf apr_pool_cleanup_register(c->pool, (void *)conn,
9efd5708f6f0ab1992f6a5233eb622ad5e4eae18sf connection_cleanup,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes apr_pool_cleanup_null);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes /* For ssl connection to backend */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (conn->is_ssl) {
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if (!ap_proxy_ssl_enable(conn->connection)) {
54d22ed1c429b903b029bbd62621f11a9e286137minfrin ap_log_error(APLOG_MARK, APLOG_ERR, 0,
54d22ed1c429b903b029bbd62621f11a9e286137minfrin s, "proxy: %s: failed to enable ssl support "
54d22ed1c429b903b029bbd62621f11a9e286137minfrin "for %pI (%s)", proxy_function,
54d22ed1c429b903b029bbd62621f11a9e286137minfrin backend_addr, conn->hostname);
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return HTTP_INTERNAL_SERVER_ERROR;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin }
54d22ed1c429b903b029bbd62621f11a9e286137minfrin }
54d22ed1c429b903b029bbd62621f11a9e286137minfrin else {
54d22ed1c429b903b029bbd62621f11a9e286137minfrin /* TODO: See if this will break FTP */
88adce5ec0da39b41450ce1d5a77659168097e0cjorton ap_proxy_ssl_disable(conn->connection);
88adce5ec0da39b41450ce1d5a77659168097e0cjorton }
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
54d22ed1c429b903b029bbd62621f11a9e286137minfrin "proxy: %s: connection complete to %pI (%s)",
54d22ed1c429b903b029bbd62621f11a9e286137minfrin proxy_function, backend_addr, conn->hostname);
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin /* set up the connection filters */
54d22ed1c429b903b029bbd62621f11a9e286137minfrin rc = ap_run_pre_connection(conn->connection, conn->sock);
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if (rc != OK && rc != DONE) {
54d22ed1c429b903b029bbd62621f11a9e286137minfrin conn->connection->aborted = 1;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
54d22ed1c429b903b029bbd62621f11a9e286137minfrin "proxy: %s: pre_connection setup failed (%d)",
54d22ed1c429b903b029bbd62621f11a9e286137minfrin proxy_function, rc);
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return rc;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin }
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return OK;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin}
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrinint ap_proxy_lb_workers(void)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin{
54d22ed1c429b903b029bbd62621f11a9e286137minfrin /*
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * Since we can't resize the scoreboard when reconfiguring, we
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * have to impose a limit on the number of workers, we are
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * able to reconfigure to.
54d22ed1c429b903b029bbd62621f11a9e286137minfrin */
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if (!lb_workers_limit)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin lb_workers_limit = proxy_lb_workers + PROXY_DYNAMIC_BALANCER_LIMIT;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin return lb_workers_limit;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin}
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
09338db7fdcf82ecc189195347da3a3ed5d0287abnicholesPROXY_DECLARE(void) ap_proxy_backend_broke(request_rec *r,
09338db7fdcf82ecc189195347da3a3ed5d0287abnicholes apr_bucket_brigade *brigade)
09338db7fdcf82ecc189195347da3a3ed5d0287abnicholes{
09338db7fdcf82ecc189195347da3a3ed5d0287abnicholes apr_bucket *e;
09338db7fdcf82ecc189195347da3a3ed5d0287abnicholes conn_rec *c = r->connection;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin
54d22ed1c429b903b029bbd62621f11a9e286137minfrin r->no_cache = 1;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin /*
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * If this is a subrequest, then prevent also caching of the main
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * request.
54d22ed1c429b903b029bbd62621f11a9e286137minfrin */
54d22ed1c429b903b029bbd62621f11a9e286137minfrin if (r->main)
54d22ed1c429b903b029bbd62621f11a9e286137minfrin r->main->no_cache = 1;
54d22ed1c429b903b029bbd62621f11a9e286137minfrin e = ap_bucket_error_create(HTTP_BAD_GATEWAY, NULL, c->pool,
54d22ed1c429b903b029bbd62621f11a9e286137minfrin c->bucket_alloc);
54d22ed1c429b903b029bbd62621f11a9e286137minfrin APR_BRIGADE_INSERT_TAIL(brigade, e);
54d22ed1c429b903b029bbd62621f11a9e286137minfrin e = apr_bucket_eos_create(c->bucket_alloc);
54d22ed1c429b903b029bbd62621f11a9e286137minfrin APR_BRIGADE_INSERT_TAIL(brigade, e);
54d22ed1c429b903b029bbd62621f11a9e286137minfrin}
54d22ed1c429b903b029bbd62621f11a9e286137minfrin