📄 proxy_util.c
字号:
* Is that okay, or should they be collapsed where possible? */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 = proxy_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 header " "returned by %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'; 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 = proxy_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;}long int ap_proxy_send_fb(BUFF *f, request_rec *r, cache_req *c){ int ok = 1; char buf[IOBUFSIZE]; long total_bytes_rcv; register int n, o, w; conn_rec *con = r->connection; int alt_to = 1; total_bytes_rcv = 0; if (c) c->written = 0;#ifdef CHARSET_EBCDIC /* The cache copy is ASCII, not EBCDIC, even for text/html) */ ap_bsetflag(f, B_ASCII2EBCDIC|B_EBCDIC2ASCII, 0); if (c != NULL && c->fp != NULL) ap_bsetflag(c->fp, B_ASCII2EBCDIC|B_EBCDIC2ASCII, 0); ap_bsetflag(con->client, B_ASCII2EBCDIC|B_EBCDIC2ASCII, 0);#endif /* Since we are reading from one buffer and writing to another, * it is unsafe to do a soft_timeout here, at least until the proxy * has its own timeout handler which can set both buffers to EOUT. */ ap_kill_timeout(r);#ifdef WIN32 /* works fine under win32, so leave it */ ap_hard_timeout("proxy send body", r); alt_to = 0;#else /* CHECKME! Since hard_timeout won't work in unix on sends with partial * cache completion, we have to alternate between hard_timeout * for reads, and soft_timeout for send. This is because we need * to get a return from ap_bwrite to be able to continue caching. * BUT, if we *can't* continue anyway, just use hard_timeout. */ if (c) { if (c->len <= 0 || c->cache_completion == 1) { ap_hard_timeout("proxy send body", r); alt_to = 0; } } else { ap_hard_timeout("proxy send body", r); alt_to = 0; }#endif while (ok) { if (alt_to) ap_hard_timeout("proxy send body", r); /* Read block from server */ n = ap_bread(f, buf, IOBUFSIZE); if (alt_to) ap_kill_timeout(r); else ap_reset_timeout(r); if (n == -1) { /* input error */ if (c != NULL) c = ap_proxy_cache_error(c); break; } if (n == 0) break; /* EOF */ o = 0; total_bytes_rcv += n; /* Write to cache first. */ if (c != NULL && c->fp != NULL) { if (ap_bwrite(c->fp, &buf[0], n) != n) { c = ap_proxy_cache_error(c); } else { c->written += n; } } /* Write the block to the client, detect aborted transfers */ while (n && !con->aborted) { if (alt_to) ap_soft_timeout("proxy send body", r); w = ap_bwrite(con->client, &buf[o], n); if (alt_to) ap_kill_timeout(r); else ap_reset_timeout(r); if (w <= 0) { if (c != NULL) { /* when a send failure occurs, we need to decide * whether to continue loading and caching the * document, or to abort the whole thing */ ok = (c->len > 0) && (c->cache_completion > 0) && (c->len * c->cache_completion < total_bytes_rcv); if (! ok) { ap_pclosef(c->req->pool, c->fp->fd); c->fp = NULL; unlink(c->tempfile); c = NULL; } } con->aborted = 1; break; } n -= w; o += w; } } if (!con->aborted) ap_bflush(con->client); ap_kill_timeout(r); return total_bytes_rcv;}/* * Sends response line and headers. Uses the client fd and the * headers_out array from the passed request_rec to talk to the client * and to properly set the headers it sends for things such as logging. * * A timeout should be set before calling this routine. */void ap_proxy_send_headers(request_rec *r, const char *respline, table *t){ int i; BUFF *fp = r->connection->client; table_entry *elts = (table_entry *) ap_table_elts(t)->elts; ap_bputs(respline, fp); ap_bputs(CRLF, fp); for (i = 0; i < ap_table_elts(t)->nelts; ++i) { if (elts[i].key != NULL) { ap_bvputs(fp, elts[i].key, ": ", elts[i].val, CRLF, NULL); /* FIXME: @@@ This used to be ap_table_set(), but I think * ap_table_addn() is correct. MnKr */ ap_table_addn(r->headers_out, elts[i].key, elts[i].val); } } ap_bputs(CRLF, fp);}/* * list is a comma-separated list of case-insensitive tokens, with * optional whitespace around the tokens. * The return returns 1 if the token val is found in the list, or 0 * otherwise. */int ap_proxy_liststr(const char *list, const char *val){ int len, i; const char *p; len = strlen(val); while (list != NULL) { p = strchr(list, ','); if (p != NULL) { i = p - list; do p++; while (ap_isspace(*p)); } else i = strlen(list); while (i > 0 && ap_isspace(list[i - 1])) i--; if (i == len && strncasecmp(list, val, len) == 0) return 1; list = p; } return 0;}#ifdef WIN32/* * On NT, the file system is NOT case sensitive. So, a == A * need to map to smaller set of characters */void ap_proxy_hash(const char *it, char *val, int ndepth, int nlength){ AP_MD5_CTX context; unsigned char digest[16]; char tmp[26]; int i, k, d; unsigned int x; static const char enc_table[32] = "abcdefghijklmnopqrstuvwxyz012345"; ap_MD5Init(&context); ap_MD5Update(&context, (const unsigned char *) it, strlen(it)); ap_MD5Final(digest, &context);/* encode 128 bits as 26 characters, using a modified uuencoding *//* the encoding is 5 bytes -> 8 characters * i.e. 128 bits is 3 x 5 bytes + 1 byte -> 3 * 8 characters + 2 characters */ for (i = 0, k = 0; i < 15; i += 5) { x = (digest[i] << 24) | (digest[i + 1] << 16) | (digest[i + 2] << 8) | digest[i + 3]; tmp[k++] = enc_table[x >> 27]; tmp[k++] = enc_table[(x >> 22) & 0x1f]; tmp[k++] = enc_table[(x >> 17) & 0x1f]; tmp[k++] = enc_table[(x >> 12) & 0x1f]; tmp[k++] = enc_table[(x >> 7) & 0x1f]; tmp[k++] = enc_table[(x >> 2) & 0x1f]; x = ((x & 0x3) << 8) | digest[i + 4]; tmp[k++] = enc_table[x >> 5]; tmp[k++] = enc_table[x & 0x1f]; }/* one byte left */ x = digest[15]; tmp[k++] = enc_table[x >> 3]; /* use up 5 bits */ tmp[k++] = enc_table[x & 0x7]; /* now split into directory levels */ for (i = k = d = 0; d < ndepth; ++d) { memcpy(&val[i], &tmp[k], nlength); k += nlength; val[i + nlength] = '/'; i += nlength + 1; } memcpy(&val[i], &tmp[k], 26 - k); val[i + 26 - k] = '\0';}#elsevoid ap_proxy_hash(const char *it, char *val, int ndepth, int nlength){ AP_MD5_CTX context; unsigned char digest[16]; char tmp[22]; int i, k, d; unsigned int x;#if defined(AIX) && defined(__ps2__) /* Believe it or not, AIX 1.x does not allow you to name a file '@', * so hack around it in the encoding. */ static const char enc_table[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_%";#else static const char enc_table[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_@";#endif ap_MD5Init(&context); ap_MD5Update(&context, (const unsigned char *) it, strlen(it)); ap_MD5Final(digest, &context);/* encode 128 bits as 22 characters, using a modified uuencoding *//* the encoding is 3 bytes -> 4 characters * i.e. 128 bits is 5 x 3 bytes + 1 byte -> 5 * 4 characters + 2 characters */ for (i = 0, k = 0; i < 15; i += 3) { x = (digest[i] << 16) | (digest[i + 1] << 8) | digest[i + 2]; tmp[k++] = enc_table[x >> 18]; tmp[k++] = enc_table[(x >> 12) & 0x3f]; tmp[k++] = enc_table[(x >> 6) & 0x3f]; tmp[k++] = enc_table[x & 0x3f]; }/* one byte left */ x = digest[15]; tmp[k++] = enc_table[x >> 2]; /* use up 6 bits */ tmp[k++] = enc_table[(x << 4) & 0x3f]; /* now split into directory levels */ for (i = k = d = 0; d < ndepth; ++d) { memcpy(&val[i], &tmp[k], nlength); k += nlength; val[i + nlength] = '/'; i += nlength + 1; } memcpy(&val[i], &tmp[k], 22 - k); val[i + 22 - k] = '\0';}#endif /* WIN32 *//* * Converts 8 hex digits to a time integer */int ap_proxy_hex2sec(const char *x){ int i, ch; unsigned int j; for (i = 0, j = 0; i < 8; i++) { ch = x[i]; j <<= 4; if (ap_isdigit(ch)) j |= ch - '0'; else if (ap_isupper(ch)) j |= ch - ('A' - 10); else j |= ch - ('a' - 10); } if (j == 0xffffffff) return -1; /* so that it works with 8-byte ints */ else return j;}/* * Converts a time integer to 8 hex digits */void ap_proxy_sec2hex(int t, char *y){ int i, ch; unsigned int j = t; for (i = 7; i >= 0; i--) { ch = j & 0xF; j >>= 4; if (ch >= 10) y[i] = ch + ('A' - 10); else y[i] = ch + '0'; } y[8] = '\0';}cache_req *ap_proxy_cache_error(cache_req *c){ ap_log_rerror(APLOG_MARK, APLOG_ERR, c->req, "proxy: error writing to cache file %s", c->tempfile); ap_pclosef(c->req->pool, c->fp->fd); c->fp = NULL; unlink(c->tempfile); return NULL;}int ap_proxyerror(request_rec *r, const char *message){ ap_table_setn(r->notes, "error-notes", ap_pstrcat(r->pool, "The proxy server could not handle the request " "<EM><A HREF=\"", r->uri, "\">", r->method, " ", r->uri, "</A></EM>.<P>\n" "Reason: <STRONG>", message, "</STRONG>", NULL)); r->status_line = "500 Proxy Error"; return HTTP_INTERNAL_SERVER_ERROR;}/* * This routine returns its own error message */const char * ap_proxy_host2addr(const char *host, struct hostent *reqhp)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -