📄 htproxy.c
字号:
/* HTGateway_add** -------------** Registers a gateway as the server to contact for a specific** access method. `gateway' should be a fully valid name, like** "http://gateway.w3.org:8001" but domain name is not required.** If an entry exists for this access then delete it and use the ** ne one. Returns YES if OK, else NO*/PUBLIC BOOL HTGateway_add (const char * access, const char * gate){ if (!gateways) gateways = HTList_new(); return add_object(gateways, access, gate, NO, -1);}/*** Removes all registered gateways*/PUBLIC BOOL HTGateway_deleteAll (void){ if (remove_allObjects(gateways)) { HTList_delete(gateways); gateways = NULL; return YES; } return NO;}/* HTNoProxy_add** -------------** Registers a host name or a domain as a place where no proxy should** be contacted - for example a very fast link. If `port' is '0' then** it applies to all ports and if `access' is NULL then it applies to** to all access methods.**** Examples: w3.org** www.close.com*/PUBLIC BOOL HTNoProxy_add (const char * host, const char * access, unsigned port){ if (!noproxy) noproxy = HTList_new(); return add_hostname(noproxy, host, access, port, NO, -1);}/* HTNoProxy_addRegex** ------------------** Registers a regular expression where URIs matching this expression** should go directly and not via a proxy.***/PUBLIC BOOL HTNoProxy_addRegex (const char * regex, int regex_flags){ if (!noproxy) noproxy = HTList_new(); #ifdef HT_POSIX_REGEX return add_hostname(noproxy, regex, NULL, 0, YES, regex_flags);#else return add_hostname(noproxy, regex, NULL, 0, NO, -1);#endif}/* HTNoProxy_deleteAll** -------------------** Removes all registered no_proxy directives*/PUBLIC BOOL HTNoProxy_deleteAll (void){ if (remove_AllHostnames(noproxy)) { HTList_delete(noproxy); noproxy = NULL; return YES; } return NO;}/* HTNProxy_noProxyIsOnlyProxy** `----------------------------** Returns the state of the noproxy_is_onlyproxy flag*/PUBLIC int HTProxy_NoProxyIsOnlyProxy (void){ return noproxy_is_onlyproxy;}/* HTNProxy_setNoProxyisOnlyProxy** --------------------------** Sets the state of the noproxy_is_onlyproxy flag*/PUBLIC void HTProxy_setNoProxyIsOnlyProxy (int value){ noproxy_is_onlyproxy = value;}/* HTProxy_find** ------------** This function evaluates the lists of registered proxies and if** one is found for the actual access method and it is not registered** in the `noproxy' list, then a URL containing the host to be contacted** is returned to the caller. This string must be freed be the caller.**** Returns: proxy If OK (must be freed by caller)** NULL If no proxy is found or error*/PUBLIC char * HTProxy_find (const char * url){ char * access; char * proxy = NULL; int no_proxy_found = 0; if (!url || !proxies) return NULL; access = HTParse(url, "", PARSE_ACCESS); /* First check if the host (if any) is registered in the noproxy list */ if (noproxy) { char *host = HTParse(url, "", PARSE_HOST); char *ptr; unsigned port=0; if ((ptr = strchr(host, ':')) != NULL) { *ptr++ = '\0'; /* Chop off port */ if (*ptr) port = (unsigned) atoi(ptr); } if (*host) { /* If we have a host name */ HTList *cur = noproxy; HTHostList *pres; while ((pres = (HTHostList *) HTList_nextObject(cur)) != NULL) {#ifdef HT_POSIX_REGEX if (pres->regex) { BOOL match = regexec(pres->regex, url, 0, NULL, 0) ? NO : YES; if (match) { HTTRACE(PROT_TRACE, "GetProxy.... No proxy directive found: `%s\'\n" _ pres->host); no_proxy_found = 1; break; } } else#endif if (!pres->access || (pres->access && !strcmp(pres->access, access))) { if ((pres->port == 0) || (pres->port == port)) { char *np = pres->host+strlen(pres->host); char *hp = host+strlen(host); while (np>=pres->host && hp>=host && (*np--==*hp--)); if (np==pres->host-1 && (hp==host-1 || *hp=='.')) { HTTRACE(PROT_TRACE, "GetProxy.... No proxy directive found: `%s\'\n" _ pres->host); no_proxy_found = 1; break; } } } } } HT_FREE(host); } if ((no_proxy_found && !noproxy_is_onlyproxy) || (!no_proxy_found && noproxy_is_onlyproxy)) { HT_FREE(access); return NULL; } /* Now check if we have a proxy registered for this access method */ { HTList *cur = proxies; HTProxy *pres; while ((pres = (HTProxy *) HTList_nextObject(cur)) != NULL) {#ifdef HT_POSIX_REGEX if (pres->regex) { BOOL match = regexec(pres->regex, url, 0, NULL, 0) ? NO : YES; if (match) { StrAllocCopy(proxy, pres->url); HTTRACE(PROT_TRACE, "GetProxy.... Found: `%s\'\n" _ pres->url); break; } } else#endif if (!strcmp(pres->access, access)) { StrAllocCopy(proxy, pres->url); HTTRACE(PROT_TRACE, "GetProxy.... Found: `%s\'\n" _ pres->url); break; } } } HT_FREE(access); return proxy;}/* HTGateway_find** --------------** This function evaluates the lists of registered gateways and if** one is found for the actual access method then it is returned**** Returns: gateway If OK (must be freed by caller)** NULL If no gateway is found or error*/PUBLIC char * HTGateway_find (const char * url){ char * access; char * gateway = NULL; if (!url || !gateways) return NULL; access = HTParse(url, "", PARSE_ACCESS); /* Check if we have a gateway registered for this access method */ { HTList *cur = gateways; HTProxy *pres; while ((pres = (HTProxy *) HTList_nextObject(cur)) != NULL) { if (!strcmp(pres->access, access)) { StrAllocCopy(gateway, pres->url); HTTRACE(PROT_TRACE, "GetGateway.. Found: `%s\'\n" _ pres->url); break; } } } HT_FREE(access); return gateway;}/*** This function maintains backwards compatibility with the old ** environment variables and searches for the most common values:** http, ftp, news, wais, and gopher*/PUBLIC void HTProxy_getEnvVar (void){ char buf[80]; static const char *accesslist[] = { "http", "ftp", "news", "wais", "gopher", NULL }; const char **access = accesslist; HTTRACE(PROT_TRACE, "Proxy....... Looking for environment variables\n"); while (*access) { BOOL found = NO; char *gateway=NULL; char *proxy=NULL; /* Search for proxy gateways */ if (found == NO) { strcpy(buf, *access); strcat(buf, "_proxy"); if ((proxy = (char *) getenv(buf)) && *proxy) { HTProxy_add(*access, proxy); found = YES; } /* Try the same with upper case */ if (found == NO) { char * up = buf; while ((*up = TOUPPER(*up))) up++; if ((proxy = (char *) getenv(buf)) && *proxy) { HTProxy_add(*access, proxy); found = YES; } } } /* As a last resort, search for gateway servers */ if (found == NO) { strcpy(buf, "WWW_"); strcat(buf, *access); strcat(buf, "_GATEWAY"); if ((gateway = (char *) getenv(buf)) && *gateway) { HTGateway_add(*access, gateway); found = YES; } } ++access; } /* Search for `noproxy' directive */ { char *noproxy = getenv("no_proxy"); if (noproxy && *noproxy) { char *str = NULL; char *strptr; char *name; StrAllocCopy(str, noproxy); /* Get copy we can mutilate */ strptr = str; while ((name = HTNextField(&strptr)) != NULL) { char *portstr = strchr(name, ':'); unsigned port=0; if (portstr) { *portstr++ = '\0'; if (*portstr) port = (unsigned) atoi(portstr); } /* Register it for all access methods */ HTNoProxy_add(name, NULL, port); } HT_FREE(str); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -