home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1999 April / PCO0499.ISO / filesbbs / os2 / apach134.arj / APACH134.ZIP / src / modules / proxy / proxy_util.c < prev   
Encoding:
C/C++ Source or Header  |  1999-01-01  |  35.7 KB  |  1,277 lines

  1. /* ====================================================================
  2.  * Copyright (c) 1996-1999 The Apache Group.  All rights reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  *
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer. 
  10.  *
  11.  * 2. Redistributions in binary form must reproduce the above copyright
  12.  *    notice, this list of conditions and the following disclaimer in
  13.  *    the documentation and/or other materials provided with the
  14.  *    distribution.
  15.  *
  16.  * 3. All advertising materials mentioning features or use of this
  17.  *    software must display the following acknowledgment:
  18.  *    "This product includes software developed by the Apache Group
  19.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  20.  *
  21.  * 4. The names "Apache Server" and "Apache Group" must not be used to
  22.  *    endorse or promote products derived from this software without
  23.  *    prior written permission. For written permission, please contact
  24.  *    apache@apache.org.
  25.  *
  26.  * 5. Products derived from this software may not be called "Apache"
  27.  *    nor may "Apache" appear in their names without prior written
  28.  *    permission of the Apache Group.
  29.  *
  30.  * 6. Redistributions of any form whatsoever must retain the following
  31.  *    acknowledgment:
  32.  *    "This product includes software developed by the Apache Group
  33.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  34.  *
  35.  * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
  36.  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
  39.  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  46.  * OF THE POSSIBILITY OF SUCH DAMAGE.
  47.  * ====================================================================
  48.  *
  49.  * This software consists of voluntary contributions made by many
  50.  * individuals on behalf of the Apache Group and was originally based
  51.  * on public domain software written at the National Center for
  52.  * Supercomputing Applications, University of Illinois, Urbana-Champaign.
  53.  * For more information on the Apache Group and the Apache HTTP server
  54.  * project, please see <http://www.apache.org/>.
  55.  *
  56.  */
  57.  
  58. /* Utility routines for Apache proxy */
  59. #include "mod_proxy.h"
  60. #include "http_main.h"
  61. #include "ap_md5.h"
  62. #include "multithread.h"
  63. #include "http_log.h"
  64. #include "util_uri.h"
  65. #include "util_date.h"    /* get ap_checkmask() decl. */
  66.  
  67. static int proxy_match_ipaddr(struct dirconn_entry *This, request_rec *r);
  68. static int proxy_match_domainname(struct dirconn_entry *This, request_rec *r);
  69. static int proxy_match_hostname(struct dirconn_entry *This, request_rec *r);
  70. static int proxy_match_word(struct dirconn_entry *This, request_rec *r);
  71.  
  72. /* already called in the knowledge that the characters are hex digits */
  73. int ap_proxy_hex2c(const char *x)
  74. {
  75.     int i, ch;
  76.  
  77. #ifndef CHARSET_EBCDIC
  78.     ch = x[0];
  79.     if (ap_isdigit(ch))
  80.     i = ch - '0';
  81.     else if (ap_isupper(ch))
  82.     i = ch - ('A' - 10);
  83.     else
  84.     i = ch - ('a' - 10);
  85.     i <<= 4;
  86.  
  87.     ch = x[1];
  88.     if (ap_isdigit(ch))
  89.     i += ch - '0';
  90.     else if (ap_isupper(ch))
  91.     i += ch - ('A' - 10);
  92.     else
  93.     i += ch - ('a' - 10);
  94.     return i;
  95. #else /*CHARSET_EBCDIC*/
  96.     return (1 == sscanf(x, "%2x", &i)) ? os_toebcdic[i&0xFF] : 0;
  97. #endif /*CHARSET_EBCDIC*/
  98. }
  99.  
  100. void ap_proxy_c2hex(int ch, char *x)
  101. {
  102. #ifndef CHARSET_EBCDIC
  103.     int i;
  104.  
  105.     x[0] = '%';
  106.     i = (ch & 0xF0) >> 4;
  107.     if (i >= 10)
  108.     x[1] = ('A' - 10) + i;
  109.     else
  110.     x[1] = '0' + i;
  111.  
  112.     i = ch & 0x0F;
  113.     if (i >= 10)
  114.     x[2] = ('A' - 10) + i;
  115.     else
  116.     x[2] = '0' + i;
  117. #else /*CHARSET_EBCDIC*/
  118.     static const char ntoa[] = { "0123456789ABCDEF" };
  119.     ch &= 0xFF;
  120.     x[0] = '%';
  121.     x[1] = ntoa[(os_toascii[ch]>>4)&0x0F];
  122.     x[2] = ntoa[os_toascii[ch]&0x0F];
  123.     x[3] = '\0';
  124. #endif /*CHARSET_EBCDIC*/
  125. }
  126.  
  127. /*
  128.  * canonicalise a URL-encoded string
  129.  */
  130.  
  131. /*
  132.  * Convert a URL-encoded string to canonical form.
  133.  * It decodes characters which need not be encoded,
  134.  * and encodes those which must be encoded, and does not touch
  135.  * those which must not be touched.
  136.  */
  137. char *
  138.      ap_proxy_canonenc(pool *p, const char *x, int len, enum enctype t, int isenc)
  139. {
  140.     int i, j, ch;
  141.     char *y;
  142.     const char *allowed;    /* characters which should not be encoded */
  143.     const char *reserved;    /* characters which much not be en/de-coded */
  144.  
  145. /* N.B. in addition to :@&=, this allows ';' in an http path
  146.  * and '?' in an ftp path -- this may be revised
  147.  * 
  148.  * Also, it makes a '+' character in a search string reserved, as
  149.  * it may be form-encoded. (Although RFC 1738 doesn't allow this -
  150.  * it only permits ; / ? : @ = & as reserved chars.)
  151.  */
  152.     if (t == enc_path)
  153.     allowed = "$-_.+!*'(),;:@&=";
  154.     else if (t == enc_search)
  155.     allowed = "$-_.!*'(),;:@&=";
  156.     else if (t == enc_user)
  157.     allowed = "$-_.+!*'(),;@&=";
  158.     else if (t == enc_fpath)
  159.     allowed = "$-_.+!*'(),?:@&=";
  160.     else            /* if (t == enc_parm) */
  161.     allowed = "$-_.+!*'(),?/:@&=";
  162.  
  163.     if (t == enc_path)
  164.     reserved = "/";
  165.     else if (t == enc_search)
  166.     reserved = "+";
  167.     else
  168.     reserved = "";
  169.  
  170.     y = ap_palloc(p, 3 * len + 1);
  171.  
  172.     for (i = 0, j = 0; i < len; i++, j++) {
  173. /* always handle '/' first */
  174.     ch = x[i];
  175.     if (strchr(reserved, ch)) {
  176.         y[j] = ch;
  177.         continue;
  178.     }
  179. /* decode it if not already done */
  180.     if (isenc && ch == '%') {
  181.         if (!isxdigit(x[i + 1]) || !isxdigit(x[i + 2]))
  182.         return NULL;
  183.         ch = ap_proxy_hex2c(&x[i + 1]);
  184.         i += 2;
  185.         if (ch != 0 && strchr(reserved, ch)) {    /* keep it encoded */
  186.         ap_proxy_c2hex(ch, &y[j]);
  187.         j += 2;
  188.         continue;
  189.         }
  190.     }
  191. /* recode it, if necessary */
  192.     if (!ap_isalnum(ch) && !strchr(allowed, ch)) {
  193.         ap_proxy_c2hex(ch, &y[j]);
  194.         j += 2;
  195.     }
  196.     else
  197.         y[j] = ch;
  198.     }
  199.     y[j] = '\0';
  200.     return y;
  201. }
  202.  
  203. /*
  204.  * Parses network-location.
  205.  *    urlp           on input the URL; on output the path, after the leading /
  206.  *    user           NULL if no user/password permitted
  207.  *    password       holder for password
  208.  *    host           holder for host
  209.  *    port           port number; only set if one is supplied.
  210.  *
  211.  * Returns an error string.
  212.  */
  213. char *
  214.      ap_proxy_canon_netloc(pool *p, char **const urlp, char **userp,
  215.             char **passwordp, char **hostp, int *port)
  216. {
  217.     int i;
  218.     char *strp, *host, *url = *urlp;
  219.     char *user = NULL, *password = NULL;
  220.  
  221.     if (url[0] != '/' || url[1] != '/')
  222.     return "Malformed URL";
  223.     host = url + 2;
  224.     url = strchr(host, '/');
  225.     if (url == NULL)
  226.     url = "";
  227.     else
  228.     *(url++) = '\0';    /* skip seperating '/' */
  229.  
  230.     /* find _last_ '@' since it might occur in user/password part */
  231.     strp = strrchr(host, '@');
  232.  
  233.     if (strp != NULL) {
  234.     *strp = '\0';
  235.     user = host;
  236.     host = strp + 1;
  237.  
  238. /* find password */
  239.     strp = strchr(user, ':');
  240.     if (strp != NULL) {
  241.         *strp = '\0';
  242.         password = ap_proxy_canonenc(p, strp + 1, strlen(strp + 1), enc_user, 1);
  243.         if (password == NULL)
  244.         return "Bad %-escape in URL (password)";
  245.     }
  246.  
  247.     user = ap_proxy_canonenc(p, user, strlen(user), enc_user, 1);
  248.     if (user == NULL)
  249.         return "Bad %-escape in URL (username)";
  250.     }
  251.     if (userp != NULL) {
  252.     *userp = user;
  253.     }
  254.     if (passwordp != NULL) {
  255.     *passwordp = password;
  256.     }
  257.  
  258.     strp = strrchr(host, ':');
  259.     if (strp != NULL) {
  260.     *(strp++) = '\0';
  261.  
  262.     for (i = 0; strp[i] != '\0'; i++)
  263.         if (!ap_isdigit(strp[i]))
  264.         break;
  265.  
  266.     /* if (i == 0) the no port was given; keep default */
  267.     if (strp[i] != '\0') {
  268.         return "Bad port number in URL";
  269.     } else if (i > 0) {
  270.         *port = atoi(strp);
  271.         if (*port > 65535)
  272.         return "Port number in URL > 65535";
  273.     }
  274.     }
  275.     ap_str_tolower(host);        /* DNS names are case-insensitive */
  276.     if (*host == '\0')
  277.     return "Missing host in URL";
  278. /* check hostname syntax */
  279.     for (i = 0; host[i] != '\0'; i++)
  280.     if (!ap_isdigit(host[i]) && host[i] != '.')
  281.         break;
  282.     /* must be an IP address */
  283. #ifdef WIN32
  284.     if (host[i] == '\0' && (inet_addr(host) == -1))
  285. #else
  286.     if (host[i] == '\0' && (ap_inet_addr(host) == -1 || inet_network(host) == -1))
  287. #endif
  288.     {
  289.     return "Bad IP address in URL";
  290.     }
  291.  
  292. /*    if (strchr(host,'.') == NULL && domain != NULL)
  293.    host = pstrcat(p, host, domain, NULL);
  294.  */
  295.     *urlp = url;
  296.     *hostp = host;
  297.  
  298.     return NULL;
  299. }
  300.  
  301. static const char * const lwday[7] =
  302. {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
  303.  
  304. /*
  305.  * If the date is a valid RFC 850 date or asctime() date, then it
  306.  * is converted to the RFC 1123 format, otherwise it is not modified.
  307.  * This routine is not very fast at doing conversions, as it uses
  308.  * sscanf and sprintf. However, if the date is already correctly
  309.  * formatted, then it exits very quickly.
  310.  */
  311. const char *
  312.      ap_proxy_date_canon(pool *p, const char *x)
  313. {
  314.     int wk, mday, year, hour, min, sec, mon;
  315.     char *q, month[4], zone[4], week[4];
  316.  
  317.     q = strchr(x, ',');
  318.     /* check for RFC 850 date */
  319.     if (q != NULL && q - x > 3 && q[1] == ' ') {
  320.     *q = '\0';
  321.     for (wk = 0; wk < 7; wk++)
  322.         if (strcmp(x, lwday[wk]) == 0)
  323.         break;
  324.     *q = ',';
  325.     if (wk == 7)
  326.         return x;        /* not a valid date */
  327.     if (q[4] != '-' || q[8] != '-' || q[11] != ' ' || q[14] != ':' ||
  328.         q[17] != ':' || strcmp(&q[20], " GMT") != 0)
  329.         return x;
  330.     if (sscanf(q + 2, "%u-%3s-%u %u:%u:%u %3s", &mday, month, &year,
  331.            &hour, &min, &sec, zone) != 7)
  332.         return x;
  333.     if (year < 70)
  334.         year += 2000;
  335.     else
  336.         year += 1900;
  337.     }
  338.     else {
  339. /* check for acstime() date */
  340.     if (x[3] != ' ' || x[7] != ' ' || x[10] != ' ' || x[13] != ':' ||
  341.         x[16] != ':' || x[19] != ' ' || x[24] != '\0')
  342.         return x;
  343.     if (sscanf(x, "%3s %3s %u %u:%u:%u %u", week, month, &mday, &hour,
  344.            &min, &sec, &year) != 7)
  345.         return x;
  346.     for (wk = 0; wk < 7; wk++)
  347.         if (strcmp(week, ap_day_snames[wk]) == 0)
  348.         break;
  349.     if (wk == 7)
  350.         return x;
  351.     }
  352.  
  353. /* check date */
  354.     for (mon = 0; mon < 12; mon++)
  355.     if (strcmp(month, ap_month_snames[mon]) == 0)
  356.         break;
  357.     if (mon == 12)
  358.     return x;
  359.  
  360.     q = ap_palloc(p, 30);
  361.     ap_snprintf(q, 30, "%s, %.2d %s %d %.2d:%.2d:%.2d GMT", ap_day_snames[wk], mday,
  362.         ap_month_snames[mon], year, hour, min, sec);
  363.     return q;
  364. }
  365.  
  366.  
  367. /* NOTE: This routine is taken from http_protocol::getline()
  368.  * because the old code found in the proxy module was too
  369.  * difficult to understand and maintain.
  370.  */
  371. /* Get a line of protocol input, including any continuation lines
  372.  * caused by MIME folding (or broken clients) if fold != 0, and place it
  373.  * in the buffer s, of size n bytes, without the ending newline.
  374.  *
  375.  * Returns -1 on error, or the length of s.
  376.  *
  377.  * Note: Because bgets uses 1 char for newline and 1 char for NUL,
  378.  *       the most we can get is (n - 2) actual characters if it
  379.  *       was ended by a newline, or (n - 1) characters if the line
  380.  *       length exceeded (n - 1).  So, if the result == (n - 1),
  381.  *       then the actual input line exceeded the buffer length,
  382.  *       and it would be a good idea for the caller to puke 400 or 414.
  383.  */
  384. static int proxy_getline(char *s, int n, BUFF *in, int fold)
  385. {
  386.     char *pos, next;
  387.     int retval;
  388.     int total = 0;
  389.  
  390.     pos = s;
  391.  
  392.     do {
  393.         retval = ap_bgets(pos, n, in);     /* retval == -1 if error, 0 if EOF */
  394.  
  395.         if (retval <= 0)
  396.             return ((retval < 0) && (total == 0)) ? -1 : total;
  397.  
  398.         /* retval is the number of characters read, not including NUL      */
  399.  
  400.         n -= retval;            /* Keep track of how much of s is full     */
  401.         pos += (retval - 1);    /* and where s ends                        */
  402.         total += retval;        /* and how long s has become               */
  403.  
  404.         if (*pos == '\n') {     /* Did we get a full line of input?        */
  405.             *pos = '\0';
  406.             --total;
  407.             ++n;
  408.         }
  409.         else
  410.             return total;       /* if not, input line exceeded buffer size */
  411.  
  412.         /* Continue appending if line folding is desired and
  413.          * the last line was not empty and we have room in the buffer and
  414.          * the next line begins with a continuation character.
  415.          */
  416.     } while (fold && (retval != 1) && (n > 1)
  417.                   && (ap_blookc(&next, in) == 1)
  418.                   && ((next == ' ') || (next == '\t')));
  419.  
  420.     return total;
  421. }
  422.  
  423.  
  424. /*
  425.  * Reads headers from a buffer and returns an array of headers.
  426.  * Returns NULL on file error
  427.  * This routine tries to deal with too long lines and continuation lines.
  428.  * @@@: XXX: FIXME: currently the headers are passed thru un-merged. 
  429.  * Is that okay, or should they be collapsed where possible?
  430.  */
  431. table *ap_proxy_read_headers(request_rec *r, char *buffer, int size, BUFF *f)
  432. {
  433.     table *resp_hdrs;
  434.     int len;
  435.     char *value, *end;
  436.     char field[MAX_STRING_LEN];
  437.  
  438.     resp_hdrs = ap_make_table(r->pool, 20);
  439.  
  440.     /*
  441.      * Read header lines until we get the empty separator line, a read error,
  442.      * the connection closes (EOF), or we timeout.
  443.      */
  444.     while ((len = proxy_getline(buffer, size, f, 1)) > 0) {
  445.     
  446.     if (!(value = strchr(buffer, ':'))) {     /* Find the colon separator */
  447.  
  448.         /* Buggy MS IIS servers sometimes return invalid headers
  449.          * (an extra "HTTP/1.0 200, OK" line sprinkled in between
  450.          * the usual MIME headers). Try to deal with it in a sensible
  451.          * way, but log the fact.
  452.          * XXX: The mask check is buggy if we ever see an HTTP/1.10 */
  453.  
  454.         if (!ap_checkmask(buffer, "HTTP/#.# ###*")) {
  455.         /* Nope, it wasn't even an extra HTTP header. Give up. */
  456.         return NULL;
  457.         }
  458.  
  459.         ap_log_error(APLOG_MARK, APLOG_WARNING|APLOG_NOERRNO, r->server,
  460.              "proxy: Ignoring duplicate HTTP header "
  461.              "returned by %s (%s)", r->uri, r->method);
  462.         continue;
  463.     }
  464.  
  465.         *value = '\0';
  466.         ++value;
  467.     /* XXX: RFC2068 defines only SP and HT as whitespace, this test is
  468.      * wrong... and so are many others probably.
  469.      */
  470.         while (ap_isspace(*value))
  471.             ++value;            /* Skip to start of value   */
  472.  
  473.     /* should strip trailing whitespace as well */
  474.     for (end = &value[strlen(value)-1]; end > value && ap_isspace(*end); --end)
  475.         *end = '\0';
  476.  
  477.         ap_table_add(resp_hdrs, buffer, value);
  478.  
  479.     /* the header was too long; at the least we should skip extra data */
  480.     if (len >= size - 1) { 
  481.         while ((len = proxy_getline(field, MAX_STRING_LEN, f, 1))
  482.             >= MAX_STRING_LEN - 1) {
  483.         /* soak up the extra data */
  484.         }
  485.         if (len == 0) /* time to exit the larger loop as well */
  486.         break;
  487.     }
  488.     }
  489.     return resp_hdrs;
  490. }
  491.  
  492. long int ap_proxy_send_fb(BUFF *f, request_rec *r, cache_req *c)
  493. {
  494.     int  ok = 1;
  495.     char buf[IOBUFSIZE];
  496.     long total_bytes_rcv;
  497.     register int n, o, w;
  498.     conn_rec *con = r->connection;
  499.     int alt_to = 1;
  500.  
  501.     total_bytes_rcv = 0;
  502.     if (c)
  503.         c->written = 0;
  504.  
  505. #ifdef CHARSET_EBCDIC
  506.     /* The cache copy is ASCII, not EBCDIC, even for text/html) */
  507.     ap_bsetflag(f, B_ASCII2EBCDIC|B_EBCDIC2ASCII, 0);
  508.     if (c != NULL && c->fp != NULL)
  509.     ap_bsetflag(c->fp, B_ASCII2EBCDIC|B_EBCDIC2ASCII, 0);
  510.     ap_bsetflag(con->client, B_ASCII2EBCDIC|B_EBCDIC2ASCII, 0);
  511. #endif
  512.  
  513.     /* Since we are reading from one buffer and writing to another,
  514.      * it is unsafe to do a soft_timeout here, at least until the proxy
  515.      * has its own timeout handler which can set both buffers to EOUT.
  516.      */
  517.  
  518.     ap_kill_timeout(r);
  519.  
  520. #ifdef WIN32
  521.     /* works fine under win32, so leave it */
  522.     ap_hard_timeout("proxy send body", r);
  523.     alt_to = 0;
  524. #else
  525.     /* CHECKME! Since hard_timeout won't work in unix on sends with partial
  526.      * cache completion, we have to alternate between hard_timeout
  527.      * for reads, and soft_timeout for send.  This is because we need
  528.      * to get a return from ap_bwrite to be able to continue caching.
  529.      * BUT, if we *can't* continue anyway, just use hard_timeout.
  530.      */
  531.  
  532.     if (c) {
  533.         if (c->len <= 0 || c->cache_completion == 1) {
  534.             ap_hard_timeout("proxy send body", r);
  535.             alt_to = 0;
  536.         }
  537.     } else {
  538.         ap_hard_timeout("proxy send body", r);
  539.         alt_to = 0;
  540.     }
  541. #endif
  542.  
  543.     while (ok) {
  544.         if (alt_to)
  545.             ap_hard_timeout("proxy send body", r);
  546.  
  547.     /* Read block from server */
  548.     n = ap_bread(f, buf, IOBUFSIZE);
  549.  
  550.         if (alt_to)
  551.             ap_kill_timeout(r);
  552.         else
  553.             ap_reset_timeout(r);
  554.  
  555.     if (n == -1) {        /* input error */
  556.         if (c != NULL)
  557.         c = ap_proxy_cache_error(c);
  558.         break;
  559.     }
  560.     if (n == 0)
  561.         break;        /* EOF */
  562.     o = 0;
  563.     total_bytes_rcv += n;
  564.  
  565.     /* Write to cache first. */
  566.         if (c != NULL && c->fp != NULL) {
  567.             if (ap_bwrite(c->fp, &buf[0], n) != n) {
  568.                 c = ap_proxy_cache_error(c);
  569.             } else {
  570.                 c->written += n;
  571.             }
  572.         }
  573.  
  574.     /* Write the block to the client, detect aborted transfers */
  575.         while (n && !con->aborted) {
  576.             if (alt_to)
  577.                 ap_soft_timeout("proxy send body", r);
  578.  
  579.             w = ap_bwrite(con->client, &buf[o], n);
  580.  
  581.             if (alt_to)
  582.                 ap_kill_timeout(r);
  583.             else
  584.                 ap_reset_timeout(r);
  585.  
  586.             if (w <= 0) {
  587.                 if (c != NULL && c->fp != NULL) {
  588.                     /* when a send failure occurs, we need to decide
  589.                      * whether to continue loading and caching the
  590.                      * document, or to abort the whole thing
  591.                      */
  592.                     ok = (c->len > 0) &&
  593.                          (c->cache_completion > 0) &&
  594.                          (c->len * c->cache_completion < total_bytes_rcv);
  595.  
  596.                     if (! ok) {
  597.                         ap_pclosef(c->req->pool, c->fp->fd);
  598.                         c->fp = NULL;
  599.                         unlink(c->tempfile);
  600.             c = NULL;
  601.                     }
  602.                 }
  603.                 con->aborted = 1;
  604.                 break;
  605.             }
  606.             n -= w;
  607.             o += w;
  608.         }
  609.     }
  610.  
  611.     if (!con->aborted)
  612.     ap_bflush(con->client);
  613.  
  614.     ap_kill_timeout(r);
  615.     return total_bytes_rcv;
  616. }
  617.  
  618. /*
  619.  * Sends response line and headers.  Uses the client fd and the 
  620.  * headers_out array from the passed request_rec to talk to the client
  621.  * and to properly set the headers it sends for things such as logging.
  622.  * 
  623.  * A timeout should be set before calling this routine.
  624.  */
  625. void ap_proxy_send_headers(request_rec *r, const char *respline, table *t)
  626. {
  627.     int i;
  628.     BUFF *fp = r->connection->client;
  629.     table_entry *elts = (table_entry *) ap_table_elts(t)->elts;
  630.  
  631.     ap_bputs(respline, fp);
  632.     ap_bputs(CRLF, fp);
  633.  
  634.     for (i = 0; i < ap_table_elts(t)->nelts; ++i) {
  635.     if (elts[i].key != NULL) {
  636.         ap_bvputs(fp, elts[i].key, ": ", elts[i].val, CRLF, NULL);
  637.         /* FIXME: @@@ This used to be ap_table_set(), but I think
  638.          * ap_table_addn() is correct. MnKr */
  639.         ap_table_addn(r->headers_out, elts[i].key, elts[i].val);
  640.     }
  641.     }
  642.  
  643.     ap_bputs(CRLF, fp);
  644. }
  645.  
  646.  
  647. /*
  648.  * list is a comma-separated list of case-insensitive tokens, with
  649.  * optional whitespace around the tokens.
  650.  * The return returns 1 if the token val is found in the list, or 0
  651.  * otherwise.
  652.  */
  653. int ap_proxy_liststr(const char *list, const char *val)
  654. {
  655.     int len, i;
  656.     const char *p;
  657.  
  658.     len = strlen(val);
  659.  
  660.     while (list != NULL) {
  661.     p = strchr(list, ',');
  662.     if (p != NULL) {
  663.         i = p - list;
  664.         do
  665.         p++;
  666.         while (ap_isspace(*p));
  667.     }
  668.     else
  669.         i = strlen(list);
  670.  
  671.     while (i > 0 && ap_isspace(list[i - 1]))
  672.         i--;
  673.     if (i == len && strncasecmp(list, val, len) == 0)
  674.         return 1;
  675.     list = p;
  676.     }
  677.     return 0;
  678. }
  679.  
  680. #ifdef CASE_BLIND_FILESYSTEM
  681.  
  682. /*
  683.  * On some platforms, the file system is NOT case sensitive. So, a == A
  684.  * need to map to smaller set of characters
  685.  */
  686. void ap_proxy_hash(const char *it, char *val, int ndepth, int nlength)
  687. {
  688.     AP_MD5_CTX context;
  689.     unsigned char digest[16];
  690.     char tmp[26];
  691.     int i, k, d;
  692.     unsigned int x;
  693.     static const char enc_table[32] = "abcdefghijklmnopqrstuvwxyz012345";
  694.  
  695.     ap_MD5Init(&context);
  696.     ap_MD5Update(&context, (const unsigned char *) it, strlen(it));
  697.     ap_MD5Final(digest, &context);
  698.  
  699. /* encode 128 bits as 26 characters, using a modified uuencoding */
  700. /* the encoding is 5 bytes -> 8 characters
  701.  * i.e. 128 bits is 3 x 5 bytes + 1 byte -> 3 * 8 characters + 2 characters
  702.  */
  703.     for (i = 0, k = 0; i < 15; i += 5) {
  704.     x = (digest[i] << 24) | (digest[i + 1] << 16) | (digest[i + 2] << 8) | digest[i + 3];
  705.     tmp[k++] = enc_table[x >> 27];
  706.     tmp[k++] = enc_table[(x >> 22) & 0x1f];
  707.     tmp[k++] = enc_table[(x >> 17) & 0x1f];
  708.     tmp[k++] = enc_table[(x >> 12) & 0x1f];
  709.     tmp[k++] = enc_table[(x >> 7) & 0x1f];
  710.     tmp[k++] = enc_table[(x >> 2) & 0x1f];
  711.     x = ((x & 0x3) << 8) | digest[i + 4];
  712.     tmp[k++] = enc_table[x >> 5];
  713.     tmp[k++] = enc_table[x & 0x1f];
  714.     }
  715. /* one byte left */
  716.     x = digest[15];
  717.     tmp[k++] = enc_table[x >> 3];    /* use up 5 bits */
  718.     tmp[k++] = enc_table[x & 0x7];
  719.     /* now split into directory levels */
  720.  
  721.     for (i = k = d = 0; d < ndepth; ++d) {
  722.     memcpy(&val[i], &tmp[k], nlength);
  723.     k += nlength;
  724.     val[i + nlength] = '/';
  725.     i += nlength + 1;
  726.     }
  727.     memcpy(&val[i], &tmp[k], 26 - k);
  728.     val[i + 26 - k] = '\0';
  729. }
  730.  
  731. #else
  732.  
  733. void ap_proxy_hash(const char *it, char *val, int ndepth, int nlength)
  734. {
  735.     AP_MD5_CTX context;
  736.     unsigned char digest[16];
  737.     char tmp[22];
  738.     int i, k, d;
  739.     unsigned int x;
  740. #if defined(AIX) && defined(__ps2__)
  741.     /* Believe it or not, AIX 1.x does not allow you to name a file '@',
  742.      * so hack around it in the encoding. */
  743.     static const char enc_table[64] =
  744.     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_%";
  745. #else
  746.     static const char enc_table[64] =
  747.     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_@";
  748. #endif
  749.  
  750.     ap_MD5Init(&context);
  751.     ap_MD5Update(&context, (const unsigned char *) it, strlen(it));
  752.     ap_MD5Final(digest, &context);
  753.  
  754. /* encode 128 bits as 22 characters, using a modified uuencoding */
  755. /* the encoding is 3 bytes -> 4 characters
  756.  * i.e. 128 bits is 5 x 3 bytes + 1 byte -> 5 * 4 characters + 2 characters
  757.  */
  758.     for (i = 0, k = 0; i < 15; i += 3) {
  759.     x = (digest[i] << 16) | (digest[i + 1] << 8) | digest[i + 2];
  760.     tmp[k++] = enc_table[x >> 18];
  761.     tmp[k++] = enc_table[(x >> 12) & 0x3f];
  762.     tmp[k++] = enc_table[(x >> 6) & 0x3f];
  763.     tmp[k++] = enc_table[x & 0x3f];
  764.     }
  765. /* one byte left */
  766.     x = digest[15];
  767.     tmp[k++] = enc_table[x >> 2];    /* use up 6 bits */
  768.     tmp[k++] = enc_table[(x << 4) & 0x3f];
  769.     /* now split into directory levels */
  770.  
  771.     for (i = k = d = 0; d < ndepth; ++d) {
  772.     memcpy(&val[i], &tmp[k], nlength);
  773.     k += nlength;
  774.     val[i + nlength] = '/';
  775.     i += nlength + 1;
  776.     }
  777.     memcpy(&val[i], &tmp[k], 22 - k);
  778.     val[i + 22 - k] = '\0';
  779. }
  780.  
  781. #endif /* CASE_BLIND_FILESYSTEM */
  782.  
  783. /*
  784.  * Converts 8 hex digits to a time integer
  785.  */
  786. int ap_proxy_hex2sec(const char *x)
  787. {
  788.     int i, ch;
  789.     unsigned int j;
  790.  
  791.     for (i = 0, j = 0; i < 8; i++) {
  792.     ch = x[i];
  793.     j <<= 4;
  794.     if (ap_isdigit(ch))
  795.         j |= ch - '0';
  796.     else if (ap_isupper(ch))
  797.         j |= ch - ('A' - 10);
  798.     else
  799.         j |= ch - ('a' - 10);
  800.     }
  801.     if (j == 0xffffffff)
  802.     return -1;        /* so that it works with 8-byte ints */
  803.     else
  804.     return j;
  805. }
  806.  
  807. /*
  808.  * Converts a time integer to 8 hex digits
  809.  */
  810. void ap_proxy_sec2hex(int t, char *y)
  811. {
  812.     int i, ch;
  813.     unsigned int j = t;
  814.  
  815.     for (i = 7; i >= 0; i--) {
  816.     ch = j & 0xF;
  817.     j >>= 4;
  818.     if (ch >= 10)
  819.         y[i] = ch + ('A' - 10);
  820.     else
  821.         y[i] = ch + '0';
  822.     }
  823.     y[8] = '\0';
  824. }
  825.  
  826.  
  827. cache_req *ap_proxy_cache_error(cache_req *c)
  828. {
  829.     ap_log_rerror(APLOG_MARK, APLOG_ERR, c->req,
  830.          "proxy: error writing to cache file %s", c->tempfile);
  831.     ap_pclosef(c->req->pool, c->fp->fd);
  832.     c->fp = NULL;
  833.     unlink(c->tempfile);
  834.     return NULL;
  835. }
  836.  
  837. int ap_proxyerror(request_rec *r, const char *message)
  838. {
  839.     ap_table_setn(r->notes, "error-notes",
  840.           ap_pstrcat(r->pool, 
  841.                  "The proxy server could not handle the request "
  842.                  "<EM><A HREF=\"", r->uri, "\">",
  843.                  r->method, " ", r->uri, "</A></EM>.<P>\n"
  844.                  "Reason: <STRONG>", message, "</STRONG>", NULL));
  845.     r->status_line = "500 Proxy Error";
  846.     return HTTP_INTERNAL_SERVER_ERROR;
  847. }
  848.  
  849. /*
  850.  * This routine returns its own error message
  851.  */
  852. const char *
  853.      ap_proxy_host2addr(const char *host, struct hostent *reqhp)
  854. {
  855.     int i;
  856.     struct hostent *hp;
  857.     static APACHE_TLS struct hostent hpbuf;
  858.     static APACHE_TLS u_long ipaddr;
  859.     static APACHE_TLS char *charpbuf[2];
  860.  
  861.     for (i = 0; host[i] != '\0'; i++)
  862.     if (!ap_isdigit(host[i]) && host[i] != '.')
  863.         break;
  864.  
  865.     if (host[i] != '\0') {
  866.     hp = gethostbyname(host);
  867.     if (hp == NULL)
  868.         return "Host not found";
  869.     }
  870.     else {
  871.     ipaddr = ap_inet_addr(host);
  872.     hp = gethostbyaddr((char *) &ipaddr, sizeof(u_long), AF_INET);
  873.     if (hp == NULL) {
  874.         memset(&hpbuf, 0, sizeof(hpbuf));
  875.         hpbuf.h_name = 0;
  876.         hpbuf.h_addrtype = AF_INET;
  877.         hpbuf.h_length = sizeof(u_long);
  878.         hpbuf.h_addr_list = charpbuf;
  879.         hpbuf.h_addr_list[0] = (char *) &ipaddr;
  880.         hpbuf.h_addr_list[1] = 0;
  881.         hp = &hpbuf;
  882.     }
  883.     }
  884.     *reqhp = *hp;
  885.     return NULL;
  886. }
  887.  
  888. static const char *
  889.      proxy_get_host_of_request(request_rec *r)
  890. {
  891.     char *url, *user = NULL, *password = NULL, *err, *host;
  892.     int port = -1;
  893.  
  894.     if (r->hostname != NULL)
  895.     return r->hostname;
  896.  
  897.     /* Set url to the first char after "scheme://" */
  898.     if ((url = strchr(r->uri, ':')) == NULL
  899.     || url[1] != '/' || url[2] != '/')
  900.     return NULL;
  901.  
  902.     url = ap_pstrdup(r->pool, &url[1]);    /* make it point to "//", which is what proxy_canon_netloc expects */
  903.  
  904.     err = ap_proxy_canon_netloc(r->pool, &url, &user, &password, &host, &port);
  905.  
  906.     if (err != NULL)
  907.     ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, r,
  908.              "%s", err);
  909.  
  910.     r->hostname = host;
  911.  
  912.     return host;        /* ought to return the port, too */
  913. }
  914.  
  915. /* Return TRUE if addr represents an IP address (or an IP network address) */
  916. int ap_proxy_is_ipaddr(struct dirconn_entry *This, pool *p)
  917. {
  918.     const char *addr = This->name;
  919.     long ip_addr[4];
  920.     int i, quads;
  921.     long bits;
  922.  
  923.     /* if the address is given with an explicit netmask, use that */
  924.     /* Due to a deficiency in ap_inet_addr(), it is impossible to parse */
  925.     /* "partial" addresses (with less than 4 quads) correctly, i.e.  */
  926.     /* 192.168.123 is parsed as 192.168.0.123, which is not what I want. */
  927.     /* I therefore have to parse the IP address manually: */
  928.     /*if (proxy_readmask(This->name, &This->addr.s_addr, &This->mask.s_addr) == 0) */
  929.     /* addr and mask were set by proxy_readmask() */
  930.     /*return 1; */
  931.  
  932.     /* Parse IP addr manually, optionally allowing */
  933.     /* abbreviated net addresses like 192.168. */
  934.  
  935.     /* Iterate over up to 4 (dotted) quads. */
  936.     for (quads = 0; quads < 4 && *addr != '\0'; ++quads) {
  937.     char *tmp;
  938.  
  939.     if (*addr == '/' && quads > 0)    /* netmask starts here. */
  940.         break;
  941.  
  942.     if (!ap_isdigit(*addr))
  943.         return 0;        /* no digit at start of quad */
  944.  
  945.     ip_addr[quads] = strtol(addr, &tmp, 0);
  946.  
  947.     if (tmp == addr)    /* expected a digit, found something else */
  948.         return 0;
  949.  
  950.     if (ip_addr[quads] < 0 || ip_addr[quads] > 255) {
  951.         /* invalid octet */
  952.         return 0;
  953.     }
  954.  
  955.     addr = tmp;
  956.  
  957.     if (*addr == '.' && quads != 3)
  958.         ++addr;        /* after the 4th quad, a dot would be illegal */
  959.     }
  960.  
  961.     for (This->addr.s_addr = 0, i = 0; i < quads; ++i)
  962.     This->addr.s_addr |= htonl(ip_addr[i] << (24 - 8 * i));
  963.  
  964.     if (addr[0] == '/' && ap_isdigit(addr[1])) {    /* net mask follows: */
  965.     char *tmp;
  966.  
  967.     ++addr;
  968.  
  969.     bits = strtol(addr, &tmp, 0);
  970.  
  971.     if (tmp == addr)    /* expected a digit, found something else */
  972.         return 0;
  973.  
  974.     addr = tmp;
  975.  
  976.     if (bits < 0 || bits > 32)    /* netmask must be between 0 and 32 */
  977.         return 0;
  978.  
  979.     }
  980.     else {
  981.     /* Determine (i.e., "guess") netmask by counting the */
  982.     /* number of trailing .0's; reduce #quads appropriately */
  983.     /* (so that 192.168.0.0 is equivalent to 192.168.)        */
  984.     while (quads > 0 && ip_addr[quads - 1] == 0)
  985.         --quads;
  986.  
  987.     /* "IP Address should be given in dotted-quad form, optionally followed by a netmask (e.g., 192.168.111.0/24)"; */
  988.     if (quads < 1)
  989.         return 0;
  990.  
  991.     /* every zero-byte counts as 8 zero-bits */
  992.     bits = 8 * quads;
  993.  
  994.     if (bits != 32)        /* no warning for fully qualified IP address */
  995.         fprintf(stderr, "Warning: NetMask not supplied with IP-Addr; guessing: %s/%ld\n",
  996.             inet_ntoa(This->addr), bits);
  997.     }
  998.  
  999.     This->mask.s_addr = htonl(INADDR_NONE << (32 - bits));
  1000.  
  1001.     if (*addr == '\0' && (This->addr.s_addr & ~This->mask.s_addr) != 0) {
  1002.     fprintf(stderr, "Warning: NetMask and IP-Addr disagree in %s/%ld\n",
  1003.         inet_ntoa(This->addr), bits);
  1004.     This->addr.s_addr &= This->mask.s_addr;
  1005.     fprintf(stderr, "         Set to %s/%ld\n",
  1006.         inet_ntoa(This->addr), bits);
  1007.     }
  1008.  
  1009.     if (*addr == '\0') {
  1010.     This->matcher = proxy_match_ipaddr;
  1011.     return 1;
  1012.     }
  1013.     else
  1014.     return (*addr == '\0');    /* okay iff we've parsed the whole string */
  1015. }
  1016.  
  1017. /* Return TRUE if addr represents an IP address (or an IP network address) */
  1018. static int proxy_match_ipaddr(struct dirconn_entry *This, request_rec *r)
  1019. {
  1020.     int i;
  1021.     int ip_addr[4];
  1022.     struct in_addr addr;
  1023.     struct in_addr *ip_list;
  1024.     char **ip_listptr;
  1025.     const char *found;
  1026.     const char *host = proxy_get_host_of_request(r);
  1027.  
  1028.     if (host == NULL)   /* oops! */
  1029.        return 0;
  1030.  
  1031.     memset(&addr, '\0', sizeof addr);
  1032.     memset(ip_addr, '\0', sizeof ip_addr);
  1033.  
  1034.     if (4 == sscanf(host, "%d.%d.%d.%d", &ip_addr[0], &ip_addr[1], &ip_addr[2], &ip_addr[3])) {
  1035.     for (addr.s_addr = 0, i = 0; i < 4; ++i)
  1036.         addr.s_addr |= htonl(ip_addr[i] << (24 - 8 * i));
  1037.  
  1038.     if (This->addr.s_addr == (addr.s_addr & This->mask.s_addr)) {
  1039. #if DEBUGGING
  1040.         fprintf(stderr, "1)IP-Match: %s[%s] <-> ", host, inet_ntoa(addr));
  1041.         fprintf(stderr, "%s/", inet_ntoa(This->addr));
  1042.         fprintf(stderr, "%s\n", inet_ntoa(This->mask));
  1043. #endif
  1044.         return 1;
  1045.     }
  1046. #if DEBUGGING
  1047.     else {
  1048.         fprintf(stderr, "1)IP-NoMatch: %s[%s] <-> ", host, inet_ntoa(addr));
  1049.         fprintf(stderr, "%s/", inet_ntoa(This->addr));
  1050.         fprintf(stderr, "%s\n", inet_ntoa(This->mask));
  1051.     }
  1052. #endif
  1053.     }
  1054.     else {
  1055.     struct hostent the_host;
  1056.  
  1057.     memset(&the_host, '\0', sizeof the_host);
  1058.     found = ap_proxy_host2addr(host, &the_host);
  1059.  
  1060.     if (found != NULL) {
  1061. #if DEBUGGING
  1062.         fprintf(stderr, "2)IP-NoMatch: hostname=%s msg=%s\n", host, found);
  1063. #endif
  1064.         return 0;
  1065.     }
  1066.  
  1067.     if (the_host.h_name != NULL)
  1068.         found = the_host.h_name;
  1069.     else
  1070.         found = host;
  1071.  
  1072.     /* Try to deal with multiple IP addr's for a host */
  1073.     for (ip_listptr = the_host.h_addr_list; *ip_listptr; ++ip_listptr) {
  1074.         ip_list = (struct in_addr *) *ip_listptr;
  1075.         if (This->addr.s_addr == (ip_list->s_addr & This->mask.s_addr)) {
  1076. #if DEBUGGING
  1077.         fprintf(stderr, "3)IP-Match: %s[%s] <-> ", found, inet_ntoa(*ip_list));
  1078.         fprintf(stderr, "%s/", inet_ntoa(This->addr));
  1079.         fprintf(stderr, "%s\n", inet_ntoa(This->mask));
  1080. #endif
  1081.         return 1;
  1082.         }
  1083. #if DEBUGGING
  1084.         else {
  1085.         fprintf(stderr, "3)IP-NoMatch: %s[%s] <-> ", found, inet_ntoa(*ip_list));
  1086.         fprintf(stderr, "%s/", inet_ntoa(This->addr));
  1087.         fprintf(stderr, "%s\n", inet_ntoa(This->mask));
  1088.         }
  1089. #endif
  1090.     }
  1091.     }
  1092.  
  1093.     return 0;
  1094. }
  1095.  
  1096. /* Return TRUE if addr represents a domain name */
  1097. int ap_proxy_is_domainname(struct dirconn_entry *This, pool *p)
  1098. {
  1099.     char *addr = This->name;
  1100.     int i;
  1101.  
  1102.     /* Domain name must start with a '.' */
  1103.     if (addr[0] != '.')
  1104.     return 0;
  1105.  
  1106.     /* rfc1035 says DNS names must consist of "[-a-zA-Z0-9]" and '.' */
  1107.     for (i = 0; ap_isalnum(addr[i]) || addr[i] == '-' || addr[i] == '.'; ++i)
  1108.     continue;
  1109.  
  1110. #if 0
  1111.     if (addr[i] == ':') {
  1112.     fprintf(stderr, "@@@@ handle optional port in proxy_is_domainname()\n");
  1113.     /* @@@@ handle optional port */
  1114.     }
  1115. #endif
  1116.  
  1117.     if (addr[i] != '\0')
  1118.     return 0;
  1119.  
  1120.     /* Strip trailing dots */
  1121.     for (i = strlen(addr) - 1; i > 0 && addr[i] == '.'; --i)
  1122.     addr[i] = '\0';
  1123.  
  1124.     This->matcher = proxy_match_domainname;
  1125.     return 1;
  1126. }
  1127.  
  1128. /* Return TRUE if host "host" is in domain "domain" */
  1129. static int proxy_match_domainname(struct dirconn_entry *This, request_rec *r)
  1130. {
  1131.     const char *host = proxy_get_host_of_request(r);
  1132.     int d_len = strlen(This->name), h_len;
  1133.  
  1134.     if (host == NULL)        /* some error was logged already */
  1135.     return 0;
  1136.  
  1137.     h_len = strlen(host);
  1138.  
  1139.     /* @@@ do this within the setup? */
  1140.     /* Ignore trailing dots in domain comparison: */
  1141.     while (d_len > 0 && This->name[d_len - 1] == '.')
  1142.     --d_len;
  1143.     while (h_len > 0 && host[h_len - 1] == '.')
  1144.     --h_len;
  1145.     return h_len > d_len
  1146.     && strncasecmp(&host[h_len - d_len], This->name, d_len) == 0;
  1147. }
  1148.  
  1149. /* Return TRUE if addr represents a host name */
  1150. int ap_proxy_is_hostname(struct dirconn_entry *This, pool *p)
  1151. {
  1152.     struct hostent host;
  1153.     char *addr = This->name;
  1154.     int i;
  1155.  
  1156.     /* Host names must not start with a '.' */
  1157.     if (addr[0] == '.')
  1158.     return 0;
  1159.  
  1160.     /* rfc1035 says DNS names must consist of "[-a-zA-Z0-9]" and '.' */
  1161.     for (i = 0; ap_isalnum(addr[i]) || addr[i] == '-' || addr[i] == '.'; ++i);
  1162.  
  1163. #if 0
  1164.     if (addr[i] == ':') {
  1165.     fprintf(stderr, "@@@@ handle optional port in proxy_is_hostname()\n");
  1166.     /* @@@@ handle optional port */
  1167.     }
  1168. #endif
  1169.  
  1170.     if (addr[i] != '\0' || ap_proxy_host2addr(addr, &host) != NULL)
  1171.     return 0;
  1172.  
  1173.     This->hostentry = ap_pduphostent (p, &host);
  1174.  
  1175.     /* Strip trailing dots */
  1176.     for (i = strlen(addr) - 1; i > 0 && addr[i] == '.'; --i)
  1177.     addr[i] = '\0';
  1178.  
  1179.     This->matcher = proxy_match_hostname;
  1180.     return 1;
  1181. }
  1182.  
  1183. /* Return TRUE if host "host" is equal to host2 "host2" */
  1184. static int proxy_match_hostname(struct dirconn_entry *This, request_rec *r)
  1185. {
  1186.     char *host = This->name;
  1187.     const char *host2 = proxy_get_host_of_request(r);
  1188.     int h2_len;
  1189.     int h1_len;
  1190.  
  1191.     if (host == NULL || host2 == NULL)
  1192.        return 0; /* oops! */
  1193.  
  1194.     h2_len = strlen(host2);
  1195.     h1_len = strlen(host);
  1196.  
  1197. #if 0
  1198.     unsigned long *ip_list;
  1199.  
  1200.     /* Try to deal with multiple IP addr's for a host */
  1201.     for (ip_list = *This->hostentry->h_addr_list; *ip_list != 0UL; ++ip_list)
  1202.     if (*ip_list == ? ? ? ? ? ? ? ? ? ? ? ? ?)
  1203.         return 1;
  1204. #endif
  1205.  
  1206.     /* Ignore trailing dots in host2 comparison: */
  1207.     while (h2_len > 0 && host2[h2_len - 1] == '.')
  1208.     --h2_len;
  1209.     while (h1_len > 0 && host[h1_len - 1] == '.')
  1210.     --h1_len;
  1211.     return h1_len == h2_len
  1212.     && strncasecmp(host, host2, h1_len) == 0;
  1213. }
  1214.  
  1215. /* Return TRUE if addr is to be matched as a word */
  1216. int ap_proxy_is_word(struct dirconn_entry *This, pool *p)
  1217. {
  1218.     This->matcher = proxy_match_word;
  1219.     return 1;
  1220. }
  1221.  
  1222. /* Return TRUE if string "str2" occurs literally in "str1" */
  1223. static int proxy_match_word(struct dirconn_entry *This, request_rec *r)
  1224. {
  1225.     const char *host = proxy_get_host_of_request(r);
  1226.     return host != NULL && strstr(host, This->name) != NULL;
  1227. }
  1228.  
  1229. int ap_proxy_doconnect(int sock, struct sockaddr_in *addr, request_rec *r)
  1230. {
  1231.     int i;
  1232.  
  1233.     ap_hard_timeout("proxy connect", r);
  1234.     do {
  1235.     i = connect(sock, (struct sockaddr *) addr, sizeof(struct sockaddr_in));
  1236. #ifdef WIN32
  1237.     if (i == SOCKET_ERROR)
  1238.         errno = WSAGetLastError();
  1239. #endif /* WIN32 */
  1240.     } while (i == -1 && errno == EINTR);
  1241.     if (i == -1) {
  1242.     ap_log_rerror(APLOG_MARK, APLOG_ERR, r,
  1243.              "proxy connect to %s port %d failed",
  1244.              inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
  1245.     }
  1246.     ap_kill_timeout(r);
  1247.  
  1248.     return i;
  1249. }
  1250.  
  1251. /* This function is called by ap_table_do() for all header lines */
  1252. /* (from proxy_http.c and proxy_ftp.c) */
  1253. /* It is passed a table_do_args struct pointer and a MIME field and value pair */
  1254. int ap_proxy_send_hdr_line(void *p, const char *key, const char *value)
  1255. {
  1256.     struct tbl_do_args *parm = (struct tbl_do_args *)p;
  1257.  
  1258.     if (key == NULL || value == NULL || value[0] == '\0')
  1259.     return 1;
  1260.     if (!parm->req->assbackwards)
  1261.     ap_rvputs(parm->req, key, ": ", value, CRLF, NULL);
  1262.     if (parm->cache != NULL && parm->cache->fp != NULL &&
  1263.     ap_bvputs(parm->cache->fp, key, ": ", value, CRLF, NULL) == -1)
  1264.         parm->cache = ap_proxy_cache_error(parm->cache);
  1265.     return 1; /* tell ap_table_do() to continue calling us for more headers */
  1266. }
  1267.  
  1268. /* send a text line to one or two BUFF's; return line length */
  1269. unsigned ap_proxy_bputs2(const char *data, BUFF *client, cache_req *cache)
  1270. {
  1271.     unsigned len = ap_bputs(data, client);
  1272.     if (cache != NULL && cache->fp != NULL)
  1273.     ap_bputs(data, cache->fp);
  1274.     return len;
  1275. }
  1276.  
  1277.