📄 proxy_util.c
字号:
while (h_len > 0 && host[h_len - 1] == '.') --h_len; return h_len > d_len && strncasecmp(&host[h_len - d_len], This->name, d_len) == 0;}/* Return TRUE if addr represents a host name */int ap_proxy_is_hostname(struct dirconn_entry *This, pool *p){ struct hostent host; char *addr = This->name; int i; /* Host names must not start with a '.' */ if (addr[0] == '.') return 0; /* rfc1035 says DNS names must consist of "[-a-zA-Z0-9]" and '.' */ for (i = 0; ap_isalnum(addr[i]) || addr[i] == '-' || addr[i] == '.'; ++i);#if 0 if (addr[i] == ':') { fprintf(stderr, "@@@@ handle optional port in proxy_is_hostname()\n"); /* @@@@ handle optional port */ }#endif if (addr[i] != '\0' || ap_proxy_host2addr(addr, &host) != NULL) return 0; This->hostentry = ap_pduphostent(p, &host); /* Strip trailing dots */ for (i = strlen(addr) - 1; i > 0 && addr[i] == '.'; --i) addr[i] = '\0'; This->matcher = proxy_match_hostname; return 1;}/* Return TRUE if host "host" is equal to host2 "host2" */static int proxy_match_hostname(struct dirconn_entry *This, request_rec *r){ char *host = This->name; const char *host2 = proxy_get_host_of_request(r); int h2_len; int h1_len; if (host == NULL || host2 == NULL) return 0; /* oops! */ h2_len = strlen(host2); h1_len = strlen(host);#if 0 unsigned long *ip_list; /* Try to deal with multiple IP addr's for a host */ for (ip_list = *This->hostentry->h_addr_list; *ip_list != 0UL; ++ip_list) if (*ip_list == ? ? ? ? ? ? ? ? ? ? ? ? ?) return 1;#endif /* Ignore trailing dots in host2 comparison: */ while (h2_len > 0 && host2[h2_len - 1] == '.') --h2_len; while (h1_len > 0 && host[h1_len - 1] == '.') --h1_len; return h1_len == h2_len && strncasecmp(host, host2, h1_len) == 0;}/* Return TRUE if addr is to be matched as a word */int ap_proxy_is_word(struct dirconn_entry *This, pool *p){ This->matcher = proxy_match_word; return 1;}/* Return TRUE if string "str2" occurs literally in "str1" */static int proxy_match_word(struct dirconn_entry *This, request_rec *r){ const char *host = proxy_get_host_of_request(r); return host != NULL && strstr(host, This->name) != NULL;}int ap_proxy_doconnect(int sock, struct sockaddr_in *addr, request_rec *r){ int i; ap_hard_timeout("proxy connect", r); do { i = connect(sock, (struct sockaddr *)addr, sizeof(struct sockaddr_in));#if defined(WIN32) || defined(NETWARE) if (i == SOCKET_ERROR) errno = WSAGetLastError();#endif /* WIN32 */ } while (i == -1 && errno == EINTR); if (i == -1) { ap_log_rerror(APLOG_MARK, APLOG_ERR, r, "proxy connect to %s port %d failed", inet_ntoa(addr->sin_addr), ntohs(addr->sin_port)); } ap_kill_timeout(r); return i;}/* This function is called by ap_table_do() for all header lines * (from proxy_http.c and proxy_ftp.c) * It is passed a cache_req struct pointer and a MIME field and value pair */int ap_proxy_send_hdr_line(void *p, const char *key, const char *value){ cache_req *c = (cache_req *)p; if (key == NULL || value == NULL || value[0] == '\0') return 1; if (c->fp != NULL && ap_bvputs(c->fp, key, ": ", value, CRLF, NULL) == -1) { ap_log_rerror(APLOG_MARK, APLOG_ERR, c->req, "proxy: error writing header to %s", c->tempfile); c = ap_proxy_cache_error(c); return 0; /* no need to continue, it failed already */ } return 1; /* tell ap_table_do() to continue calling us * for more headers */}/* send a text line to one or two BUFF's; return line length */unsigned ap_proxy_bputs2(const char *data, BUFF *client, cache_req *cache){ unsigned len = ap_bputs(data, client); if (cache != NULL && cache->fp != NULL) ap_bputs(data, cache->fp); return len;}/* do a HTTP/1.1 age calculation */time_t ap_proxy_current_age(cache_req *c, const time_t age_value){ time_t apparent_age, corrected_received_age, response_delay, corrected_initial_age, resident_time, current_age; /* Perform an HTTP/1.1 age calculation. (RFC2616 13.2.3) */ apparent_age = MAX(0, c->resp_time - c->date); corrected_received_age = MAX(apparent_age, age_value); response_delay = c->resp_time - c->req_time; corrected_initial_age = corrected_received_age + response_delay; resident_time = time(NULL) - c->resp_time; current_age = corrected_initial_age + resident_time; return (current_age);}/* open a cache file and return a pointer to a BUFF */BUFF *ap_proxy_open_cachefile(request_rec *r, char *filename){ BUFF *cachefp = NULL; int cfd; if (filename != NULL) { cfd = open(filename, O_RDWR | O_BINARY); if (cfd != -1) { ap_note_cleanups_for_fd(r->pool, cfd); cachefp = ap_bcreate(r->pool, B_RD | B_WR); ap_bpushfd(cachefp, cfd, cfd); } else if (errno != ENOENT) ap_log_rerror(APLOG_MARK, APLOG_ERR, r, "proxy: error opening cache file %s", filename); else ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, r->server, "File %s not found", filename); } return cachefp;}/* create a cache file and return a pointer to a BUFF */BUFF *ap_proxy_create_cachefile(request_rec *r, char *filename){ BUFF *cachefp = NULL; int cfd; if (filename != NULL) { cfd = open(filename, O_WRONLY | O_CREAT | O_EXCL | O_BINARY, 0622); if (cfd != -1) { ap_note_cleanups_for_fd(r->pool, cfd); cachefp = ap_bcreate(r->pool, B_WR); ap_bpushfd(cachefp, -1, cfd); } else if (errno != ENOENT) ap_log_rerror(APLOG_MARK, APLOG_ERR, r, "proxy: error creating cache file %s", filename); } return cachefp;}/* Clear all connection-based headers from headers table */void ap_proxy_clear_connection(pool *p, table *headers){ const char *name; char *next = ap_pstrdup(p, ap_table_get(headers, "Connection")); /* Some proxies (Squid, ICS) use the non-standard "Proxy-Connection" header. */ ap_table_unset(headers, "Proxy-Connection"); if (next != NULL) { while (*next) { name = next; while (*next && !ap_isspace(*next) && (*next != ',')) ++next; while (*next && (ap_isspace(*next) || (*next == ','))) { *next = '\0'; ++next; } ap_table_unset(headers, name); } ap_table_unset(headers, "Connection"); } /* unset hop-by-hop headers defined in RFC2616 13.5.1 */ ap_table_unset(headers,"Keep-Alive"); /* * XXX: @@@ FIXME: "Proxy-Authenticate" should IMO *not* be stripped * because in a chain of proxies some "front" proxy might need * proxy authentication, while a "back-end" proxy which needs none can * simply pass the "Proxy-Authenticate" back to the client, and pass * the client's "Proxy-Authorization" to the front-end proxy. * (See the note in proxy_http.c for the "Proxy-Authorization" case.) * * MnKr 04/2002 */ ap_table_unset(headers,"Proxy-Authenticate"); ap_table_unset(headers,"TE"); ap_table_unset(headers,"Trailer"); /* it is safe to just chop the transfer-encoding header * here, because proxy doesn't support any other encodings * to the backend other than chunked. */ ap_table_unset(headers,"Transfer-Encoding"); ap_table_unset(headers,"Upgrade");}/* overlay one table on another * keys in base will be replaced by keys in overlay * * Note: this has to be done in a special way, due * to some nastiness when it comes to having multiple * headers in the overlay table. First, we remove all * the headers in the base table that are found in the * overlay table, then we simply concatenate the * tables together. * * The base and overlay tables need not be in the same * pool (and probably won't be). * * If the base table is changed in any way through * being overlayed with the overlay table, this * function returns a 1. */int ap_proxy_table_replace(table *base, table *overlay){ table_entry *elts = (table_entry *)overlay->a.elts; int i, q = 0; const char *val; /* remove overlay's keys from base */ for (i = 0; i < overlay->a.nelts; ++i) { val = ap_table_get(base, elts[i].key); if (!val || strcmp(val, elts[i].val)) { q = 1; } if (val) { ap_table_unset(base, elts[i].key); } } /* add overlay to base */ for (i = 0; i < overlay->a.nelts; ++i) { ap_table_add(base, elts[i].key, elts[i].val); } return q;}/* read the response line * This function reads a single line of response from the server, * and returns a status code. * It also populates the request_rec with the resultant status, and * returns backasswards status (HTTP/0.9). */int ap_proxy_read_response_line(BUFF *f, request_rec *r, char *buffer, int size, int *backasswards, int *major, int *minor) { long len; len = ap_getline(buffer, size-1, f, 0); if (len == -1) { ap_bclose(f); ap_kill_timeout(r); return ap_proxyerror(r, HTTP_BAD_GATEWAY, "Error reading from remote server"); } else if (len == 0) { ap_bclose(f); ap_kill_timeout(r); return ap_proxyerror(r, HTTP_BAD_GATEWAY, "Document contains no data"); } /* * Is it an HTTP/1 response? Do some sanity checks on the response. (This * is buggy if we ever see an HTTP/1.10) */ if (ap_checkmask(buffer, "HTTP/#.# ###*")) { if (2 != sscanf(buffer, "HTTP/%u.%u", major, minor)) { /* if no response, default to HTTP/1.1 - is this correct? */ *major = 1; *minor = 1; } /* If not an HTTP/1 message */ if (*major < 1) { ap_bclose(f); ap_kill_timeout(r); return HTTP_BAD_GATEWAY; } *backasswards = 0; /* there need not be a reason phrase in the response, * and ap_getline() already deleted trailing whitespace. * But RFC2616 requires a SP after the Status-Code. Add one: */ if (strlen(buffer) < sizeof("HTTP/1.x 200 ")-1) buffer = ap_pstrcat(r->pool, buffer, " ", NULL); buffer[12] = '\0'; r->status = atoi(&buffer[9]); buffer[12] = ' '; r->status_line = ap_pstrdup(r->pool, &buffer[9]); /* if the response was 100 continue, soak up any headers */ if (r->status == 100) { ap_proxy_read_headers(r, buffer, size, f); } } else { /* an http/0.9 response */ *backasswards = 1; r->status = 200; r->status_line = "200 OK"; *major = 0; *minor = 9; } return OK;}#if defined WIN32static DWORD tls_index;BOOL WINAPI DllMain(HINSTANCE dllhandle, DWORD reason, LPVOID reserved){ LPVOID memptr; switch (reason) { case DLL_PROCESS_ATTACH: tls_index = TlsAlloc(); case DLL_THREAD_ATTACH: /* intentional no break */ TlsSetValue(tls_index, malloc(sizeof(struct per_thread_data))); break; case DLL_THREAD_DETACH: memptr = TlsGetValue(tls_index); if (memptr) { free(memptr); TlsSetValue(tls_index, 0); } break; } return TRUE;}#endifstatic struct per_thread_data *get_per_thread_data(void){#if defined(WIN32) return (struct per_thread_data *)TlsGetValue(tls_index);#else static APACHE_TLS struct per_thread_data sptd; return &sptd;#endif}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -