📄 proxy_util.c
字号:
/* ==================================================================== * Copyright (c) 1996-1998 The Apache Group. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. All advertising materials mentioning features or use of this * software must display the following acknowledgment: * "This product includes software developed by the Apache Group * for use in the Apache HTTP server project (http://www.apache.org/)." * * 4. The names "Apache Server" and "Apache Group" must not be used to * endorse or promote products derived from this software without * prior written permission. For written permission, please contact * apache@apache.org. * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Group. * * 6. Redistributions of any form whatsoever must retain the following * acknowledgment: * "This product includes software developed by the Apache Group * for use in the Apache HTTP server project (http://www.apache.org/)." * * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Group and was originally based * on public domain software written at the National Center for * Supercomputing Applications, University of Illinois, Urbana-Champaign. * For more information on the Apache Group and the Apache HTTP server * project, please see <http://www.apache.org/>. * *//* 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);/* already called in the knowledge that the characters are hex digits */int ap_proxy_hex2c(const char *x){ int i, ch;#ifndef CHARSET_EBCDIC 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 &= 0xFF; x[0] = '%'; x[1] = ntoa[(os_toascii[ch]>>4)&0x0F]; x[2] = ntoa[os_toascii[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, int 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 && ch == '%') { if (!isxdigit(x[i + 1]) || !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, 1); if (password == NULL) return "Bad %-escape in URL (password)"; } user = ap_proxy_canonenc(p, user, strlen(user), enc_user, 1); 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 || strp[i] != '\0') return "Bad port number in URL"; *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 */#ifdef WIN32 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"; }/* if (strchr(host,'.') == NULL && domain != NULL) host = pstrcat(p, host, domain, NULL); */ *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;}/* NOTE: This routine is taken from http_protocol::getline() * because the old code found in the proxy module was too * difficult to understand and maintain. *//* Get a line of protocol input, including any continuation lines * caused by MIME folding (or broken clients) if fold != 0, and place it * in the buffer s, of size n bytes, without the ending newline. * * Returns -1 on error, or the length of s. * * Note: Because bgets uses 1 char for newline and 1 char for NUL, * the most we can get is (n - 2) actual characters if it * was ended by a newline, or (n - 1) characters if the line * length exceeded (n - 1). So, if the result == (n - 1), * then the actual input line exceeded the buffer length, * and it would be a good idea for the caller to puke 400 or 414. */static int proxy_getline(char *s, int n, BUFF *in, int fold){ char *pos, next; int retval; int total = 0; pos = s; do { retval = ap_bgets(pos, n, in); /* retval == -1 if error, 0 if EOF */ if (retval <= 0) return ((retval < 0) && (total == 0)) ? -1 : total; /* retval is the number of characters read, not including NUL */ n -= retval; /* Keep track of how much of s is full */ pos += (retval - 1); /* and where s ends */ total += retval; /* and how long s has become */ if (*pos == '\n') { /* Did we get a full line of input? */ *pos = '\0'; --total; ++n; } else return total; /* if not, input line exceeded buffer size */ /* Continue appending if line folding is desired and * the last line was not empty and we have room in the buffer and * the next line begins with a continuation character. */ } while (fold && (retval != 1) && (n > 1) && (ap_blookc(&next, in) == 1) && ((next == ' ') || (next == '\t'))); return total;}/* * 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. * @@@: XXX: FIXME: currently the headers are passed thru un-merged.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -