⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 proxy_util.c

📁 apache 安装教程 apache 安装教程
💻 C
📖 第 1 页 / 共 4 页
字号:
/* Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements.  See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License.  You may obtain a copy of the License at * *     http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *//* Utility routines for Apache proxy */#include "mod_proxy.h"#include "http_main.h"#include "ap_md5.h"#include "multithread.h"#include "http_log.h"#include "util_uri.h"#include "util_date.h"          /* get ap_checkmask() decl. */static int proxy_match_ipaddr(struct dirconn_entry *This, request_rec *r);static int proxy_match_domainname(struct dirconn_entry *This, request_rec *r);static int proxy_match_hostname(struct dirconn_entry *This, request_rec *r);static int proxy_match_word(struct dirconn_entry *This, request_rec *r);static struct per_thread_data *get_per_thread_data(void);/* already called in the knowledge that the characters are hex digits */int ap_proxy_hex2c(const char *x){    int i;#ifndef CHARSET_EBCDIC    int ch;    ch = x[0];    if (ap_isdigit(ch))        i = ch - '0';    else if (ap_isupper(ch))        i = ch - ('A' - 10);    else        i = ch - ('a' - 10);    i <<= 4;    ch = x[1];    if (ap_isdigit(ch))        i += ch - '0';    else if (ap_isupper(ch))        i += ch - ('A' - 10);    else        i += ch - ('a' - 10);    return i;#else                           /* CHARSET_EBCDIC */    return (1 == sscanf(x, "%2x", &i)) ? os_toebcdic[i & 0xFF] : 0;#endif                          /* CHARSET_EBCDIC */}void ap_proxy_c2hex(int ch, char *x){#ifndef CHARSET_EBCDIC    int i;    x[0] = '%';    i = (ch & 0xF0) >> 4;    if (i >= 10)        x[1] = ('A' - 10) + i;    else        x[1] = '0' + i;    i = ch & 0x0F;    if (i >= 10)        x[2] = ('A' - 10) + i;    else        x[2] = '0' + i;#else                           /* CHARSET_EBCDIC */    static const char ntoa[] = {"0123456789ABCDEF"};    ch = os_toascii[ch & 0xFF];    x[0] = '%';    x[1] = ntoa[(ch >> 4) & 0x0F];    x[2] = ntoa[ch & 0x0F];    x[3] = '\0';#endif                          /* CHARSET_EBCDIC */}/* * canonicalise a URL-encoded string *//* * Convert a URL-encoded string to canonical form. * It decodes characters which need not be encoded, * and encodes those which must be encoded, and does not touch * those which must not be touched. */char *ap_proxy_canonenc(pool *p, const char *x, int len, enum enctype t,                             enum proxyreqtype isenc){    int i, j, ch;    char *y;    const char *allowed;        /* characters which should not be encoded */    const char *reserved;       /* characters which much not be en/de-coded *//* N.B. in addition to :@&=, this allows ';' in an http path * and '?' in an ftp path -- this may be revised * * Also, it makes a '+' character in a search string reserved, as * it may be form-encoded. (Although RFC 1738 doesn't allow this - * it only permits ; / ? : @ = & as reserved chars.) */    if (t == enc_path)        allowed = "$-_.+!*'(),;:@&=";    else if (t == enc_search)        allowed = "$-_.!*'(),;:@&=";    else if (t == enc_user)        allowed = "$-_.+!*'(),;@&=";    else if (t == enc_fpath)        allowed = "$-_.+!*'(),?:@&=";    else                        /* if (t == enc_parm) */        allowed = "$-_.+!*'(),?/:@&=";    if (t == enc_path)        reserved = "/";    else if (t == enc_search)        reserved = "+";    else        reserved = "";    y = ap_palloc(p, 3 * len + 1);    for (i = 0, j = 0; i < len; i++, j++) {/* always handle '/' first */        ch = x[i];        if (strchr(reserved, ch)) {            y[j] = ch;            continue;        }/* decode it if not already done */        if (isenc != NOT_PROXY && ch == '%') {            if (!ap_isxdigit(x[i + 1]) || !ap_isxdigit(x[i + 2]))                return NULL;            ch = ap_proxy_hex2c(&x[i + 1]);            i += 2;            if (ch != 0 && strchr(reserved, ch)) {      /* keep it encoded */                ap_proxy_c2hex(ch, &y[j]);                j += 2;                continue;            }        }/* recode it, if necessary */        if (!ap_isalnum(ch) && !strchr(allowed, ch)) {            ap_proxy_c2hex(ch, &y[j]);            j += 2;        }        else            y[j] = ch;    }    y[j] = '\0';    return y;}/* * Parses network-location. *    urlp           on input the URL; on output the path, after the leading / *    user           NULL if no user/password permitted *    password       holder for password *    host           holder for host *    port           port number; only set if one is supplied. * * Returns an error string. */char *     ap_proxy_canon_netloc(pool *p, char **const urlp, char **userp,                                char **passwordp, char **hostp, int *port){    int i;    char *strp, *host, *url = *urlp;    char *user = NULL, *password = NULL;    if (url[0] != '/' || url[1] != '/')        return "Malformed URL";    host = url + 2;    url = strchr(host, '/');    if (url == NULL)        url = "";    else        *(url++) = '\0';        /* skip seperating '/' */    /* find _last_ '@' since it might occur in user/password part */    strp = strrchr(host, '@');    if (strp != NULL) {        *strp = '\0';        user = host;        host = strp + 1;/* find password */        strp = strchr(user, ':');        if (strp != NULL) {            *strp = '\0';            password = ap_proxy_canonenc(p, strp + 1, strlen(strp + 1), enc_user, STD_PROXY);            if (password == NULL)                return "Bad %-escape in URL (password)";        }        user = ap_proxy_canonenc(p, user, strlen(user), enc_user, STD_PROXY);        if (user == NULL)            return "Bad %-escape in URL (username)";    }    if (userp != NULL) {        *userp = user;    }    if (passwordp != NULL) {        *passwordp = password;    }    strp = strrchr(host, ':');    if (strp != NULL) {        *(strp++) = '\0';        for (i = 0; strp[i] != '\0'; i++)            if (!ap_isdigit(strp[i]))                break;        /* if (i == 0) the no port was given; keep default */        if (strp[i] != '\0') {            return "Bad port number in URL";        }        else if (i > 0) {            *port = atoi(strp);            if (*port > 65535)                return "Port number in URL > 65535";        }    }    ap_str_tolower(host);       /* DNS names are case-insensitive */    if (*host == '\0')        return "Missing host in URL";/* check hostname syntax */    for (i = 0; host[i] != '\0'; i++)        if (!ap_isdigit(host[i]) && host[i] != '.')            break;    /* must be an IP address */#if defined(WIN32) || defined(NETWARE) || defined(TPF) || defined(BEOS)    if (host[i] == '\0' && (inet_addr(host) == -1))#else    if (host[i] == '\0' && (ap_inet_addr(host) == -1 || inet_network(host) == -1))#endif    {        return "Bad IP address in URL";    }    *urlp = url;    *hostp = host;    return NULL;}static const char *const lwday[7] ={"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};/* * If the date is a valid RFC 850 date or asctime() date, then it * is converted to the RFC 1123 format, otherwise it is not modified. * This routine is not very fast at doing conversions, as it uses * sscanf and sprintf. However, if the date is already correctly * formatted, then it exits very quickly. */const char *     ap_proxy_date_canon(pool *p, const char *x){    int wk, mday, year, hour, min, sec, mon;    char *q, month[4], zone[4], week[4];    q = strchr(x, ',');    /* check for RFC 850 date */    if (q != NULL && q - x > 3 && q[1] == ' ') {        *q = '\0';        for (wk = 0; wk < 7; wk++)            if (strcmp(x, lwday[wk]) == 0)                break;        *q = ',';        if (wk == 7)            return x;           /* not a valid date */        if (q[4] != '-' || q[8] != '-' || q[11] != ' ' || q[14] != ':' ||            q[17] != ':' || strcmp(&q[20], " GMT") != 0)            return x;        if (sscanf(q + 2, "%u-%3s-%u %u:%u:%u %3s", &mday, month, &year,                   &hour, &min, &sec, zone) != 7)            return x;        if (year < 70)            year += 2000;        else            year += 1900;    }    else {/* check for acstime() date */        if (x[3] != ' ' || x[7] != ' ' || x[10] != ' ' || x[13] != ':' ||            x[16] != ':' || x[19] != ' ' || x[24] != '\0')            return x;        if (sscanf(x, "%3s %3s %u %u:%u:%u %u", week, month, &mday, &hour,                   &min, &sec, &year) != 7)            return x;        for (wk = 0; wk < 7; wk++)            if (strcmp(week, ap_day_snames[wk]) == 0)                break;        if (wk == 7)            return x;    }/* check date */    for (mon = 0; mon < 12; mon++)        if (strcmp(month, ap_month_snames[mon]) == 0)            break;    if (mon == 12)        return x;    q = ap_palloc(p, 30);    ap_snprintf(q, 30, "%s, %.2d %s %d %.2d:%.2d:%.2d GMT", ap_day_snames[wk], mday,                ap_month_snames[mon], year, hour, min, sec);    return q;}/* * Reads headers from a buffer and returns an array of headers. * Returns NULL on file error * This routine tries to deal with too long lines and continuation lines. * * Note: Currently the headers are passed through unmerged. This has to be * done so that headers which react badly to merging (such as Set-Cookie * headers, which contain commas within the date field) do not get stuffed * up. */table *ap_proxy_read_headers(request_rec *r, char *buffer, int size, BUFF *f){    table *resp_hdrs;    int len;    char *value, *end;    char field[MAX_STRING_LEN];    resp_hdrs = ap_make_table(r->pool, 20);    /*     * Read header lines until we get the empty separator line, a read error,     * the connection closes (EOF), or we timeout.     */    while ((len = ap_getline(buffer, size, f, 1)) > 0) {        if (!(value = strchr(buffer, ':'))) {   /* Find the colon separator */            /*             * Buggy MS IIS servers sometimes return invalid headers (an             * extra "HTTP/1.0 200, OK" line sprinkled in between the usual             * MIME headers). Try to deal with it in a sensible way, but log             * the fact. XXX: The mask check is buggy if we ever see an             * HTTP/1.10             */            if (!ap_checkmask(buffer, "HTTP/#.# ###*")) {                /* Nope, it wasn't even an extra HTTP header. Give up. */                return NULL;            }            ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_NOERRNO, r->server,                         "proxy: Ignoring duplicate HTTP status line "                         "returned by buggy server %s (%s)", r->uri, r->method);            continue;        }        *value = '\0';        ++value;        /*         * XXX: RFC2068 defines only SP and HT as whitespace, this test is         * wrong... and so are many others probably.         */        while (ap_isspace(*value))            ++value;            /* Skip to start of value   */        /* should strip trailing whitespace as well */        for (end = &value[strlen(value) - 1]; end > value && ap_isspace(*end); --end)            *end = '\0';        /* make sure we add so as not to destroy duplicated headers */        ap_table_add(resp_hdrs, buffer, value);        /* the header was too long; at the least we should skip extra data */        if (len >= size - 1) {            while ((len = ap_getline(field, MAX_STRING_LEN, f, 1))                   >= MAX_STRING_LEN - 1) {                /* soak up the extra data */            }            if (len == 0)       /* time to exit the larger loop as well */                break;        }    }    return resp_hdrs;}/* read data from (socket BUFF*) f, write it to: * - c->fp, if it is open * - r->connection->client, if nowrite == 0 */long int ap_proxy_send_fb(BUFF *f, request_rec *r, cache_req *c, off_t len, int nowrite, int chunked, size_t recv_buffer_size){    int ok, end_of_chunk;    char *buf;    size_t buf_size;    long remaining = 0;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -