📄 htproxy.c
字号:
/* HTProxy.c** GATEWAY AND PROXY MANAGER**** (c) COPYRIGHT MIT 1995.** Please first read the full copyright statement in the file COPYRIGH.** @(#) $Id: HTProxy.c,v 2.21 2000/02/29 14:25:59 kahan Exp $**** Replaces the old env variables for gateways and proxies. However for** backward compatibility there is a function that reads the env variables** at start up. Note that there is a difference between a proxy and a** gateway!**** Authors** HF Henrik Frystyk, frystyk@w3.org** History** 4 Jun 95 Written on a rainy day*/#if !defined(HT_DIRECT_WAIS) && !defined(HT_DEFAULT_WAIS_GATEWAY)#define HT_DEFAULT_WAIS_GATEWAY "http://www.w3.org:8001/"#endif/* Library include files */#include "wwwsys.h"#include "WWWUtil.h"#include "WWWCore.h"#include "WWWHTTP.h"#include "WWWApp.h"#include "HTProxy.h" /* Implemented here *//* Variables and typedefs local to this module */typedef struct _HTProxy { char * access; char * url; /* URL of Gateway or Proxy */#ifdef HT_POSIX_REGEX regex_t * regex; /* Compiled regex */#endif} HTProxy;typedef struct _HTHostlist { char * access; char * host; /* Host or domain name */ unsigned port;#ifdef HT_POSIX_REGEX regex_t * regex; /* Compiled regex */#endif} HTHostList;PRIVATE HTList * proxies = NULL; /* List of proxy servers */PRIVATE HTList * gateways = NULL; /* List of gateways */PRIVATE HTList * noproxy = NULL; /* Don't proxy on these hosts and domains */PRIVATE int noproxy_is_onlyproxy = 0; /* Interpret the noproxy list as an onlyproxy one */#if 0PRIVATE HTList * onlyproxy = NULL; /* Proxy only on these hosts and domains */#endif/* ------------------------------------------------------------------------- */#ifdef HT_POSIX_REGEXPRIVATE char * get_regex_error (int errcode, regex_t * compiled){ size_t length = regerror (errcode, compiled, NULL, 0); char * str = NULL; if ((str = (char *) HT_MALLOC(length+1)) == NULL) HT_OUTOFMEM("get_regex_error"); (void) regerror (errcode, compiled, str, length); return str;}PRIVATE regex_t * get_regex_t (const char * regex_str, int cflags){ regex_t * regex = NULL; if (regex_str && *regex_str) { int status; if ((regex = (regex_t *) HT_CALLOC(1, sizeof(regex_t))) == NULL) HT_OUTOFMEM("get_regex_t"); if ((status = regcomp(regex, regex_str, cflags))) { char * err_msg = get_regex_error(status, regex); HTTRACE(PROT_TRACE, "HTProxy..... Regular expression error: %s\n" _ err_msg); HT_FREE(err_msg); HT_FREE(regex); } } return regex;}#endif/*** Existing entries are replaced with new ones*/PRIVATE BOOL add_object (HTList * list, const char * access, const char * url, BOOL regex, int regex_flags){ HTProxy *me; if (!list || !access || !url || !*url) return NO; if ((me = (HTProxy *) HT_CALLOC(1, sizeof(HTProxy))) == NULL) HT_OUTOFMEM("add_object"); StrAllocCopy(me->access, access); /* Access method */#ifdef HT_POSIX_REGEX /* ** If we support regular expressions then compile one up for ** this regular expression. Otherwise use is as a normal ** access scheme. */ if (regex) { me->regex = get_regex_t(access, regex_flags < 0 ? W3C_DEFAULT_REGEX_FLAGS : regex_flags); } else#endif { char *ptr = me->access; while ((*ptr = TOLOWER(*ptr))) ptr++; } me->url = HTParse(url, "", PARSE_ACCESS+PARSE_HOST+PARSE_PUNCTUATION); if (*(me->url+strlen(me->url)-1) != '/') StrAllocCat(me->url, "/"); me->url = HTSimplify(&me->url); /* See if we already have this one */ { HTList *cur = list; HTProxy *pres; while ((pres = (HTProxy *) HTList_nextObject(cur)) != NULL) { if (!strcmp(pres->access, me->access)) break; /* We already have it */ } if (pres) { HTTRACE(PROT_TRACE, "HTProxy..... replacing for `%s\' access %s\n" _ me->url _ me->access); HT_FREE(pres->access); HT_FREE(pres->url);#ifdef HT_POSIX_REGEX if (pres->regex) regfree(pres->regex);#endif HTList_removeObject(list, (void *) pres); HT_FREE(pres); } HTTRACE(PROT_TRACE, "HTProxy..... adding for `%s\' access %s\n" _ me->url _ me->access); HTList_addObject(list, (void *) me); } return YES;}PRIVATE BOOL remove_allObjects (HTList * list){ if (list) { HTList *cur = list; HTProxy *pres; while ((pres = (HTProxy *) HTList_nextObject(cur)) != NULL) { HT_FREE(pres->access); HT_FREE(pres->url);#ifdef HT_POSIX_REGEX if (pres->regex) regfree(pres->regex);#endif HT_FREE(pres); } return YES; } return NO;}/* Add an entry to a list of host names** ------------------------------------** Existing entries are replaced with new ones*/PRIVATE BOOL add_hostname (HTList * list, const char * host, const char * access, unsigned port, BOOL regex, int regex_flags){ HTHostList *me; if (!list || !host || !*host) return NO; if ((me = (HTHostList *) HT_CALLOC(1, sizeof(HTHostList))) == NULL) HT_OUTOFMEM("add_hostname");#ifdef HT_POSIX_REGEX if (regex) me->regex = get_regex_t(host, regex_flags < 0 ? W3C_DEFAULT_REGEX_FLAGS : regex_flags);#endif if (access) { char *ptr; StrAllocCopy(me->access, access); /* Access method */ ptr = me->access; while ((*ptr = TOLOWER(*ptr))) ptr++; } StrAllocCopy(me->host, host); /* Host name */ { char *ptr = me->host; while ((*ptr = TOLOWER(*ptr))) ptr++; } me->port = port; /* Port number */ HTTRACE(PROT_TRACE, "HTHostList.. adding `%s\' to list\n" _ me->host); HTList_addObject(list, (void *) me); return YES;}PRIVATE BOOL remove_AllHostnames (HTList * list){ if (list) { HTList *cur = list; HTHostList *pres; while ((pres = (HTHostList *) HTList_nextObject(cur)) != NULL) { HT_FREE(pres->access); HT_FREE(pres->host);#ifdef HT_POSIX_REGEX if (pres->regex) regfree(pres->regex);#endif HT_FREE(pres); } return YES; } return NO;}/* HTProxy_add** -----------** Registers a proxy as the server to contact for a specific** access method. `proxy' should be a fully valid name, like** "http://proxy.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 HTProxy_add (const char * access, const char * proxy){ /* ** If this is the first time here then also add a before filter to handle ** proxy authentication and the normal AA after filter as well. ** These filters will be removed if we remove all proxies again. */ if (!proxies) { proxies = HTList_new(); HTNet_addBefore(HTAA_proxyBeforeFilter, NULL, NULL, HT_FILTER_MIDDLE); HTNet_addAfter(HTAuthFilter, NULL, NULL, HT_NO_PROXY_ACCESS, HT_FILTER_MIDDLE); HTNet_addAfter(HTAuthFilter, NULL, NULL, HT_PROXY_REAUTH, HT_FILTER_MIDDLE); } return add_object(proxies, access, proxy, NO, -1);}/* HTProxy_addRegex** ----------------** Registers a proxy as the server to contact for any URL matching the** regular expression. `proxy' should be a fully valid name, like** "http://proxy.w3.org:8001".** If an entry exists for this access then delete it and use the ** new one. Returns YES if OK, else NO*/PUBLIC BOOL HTProxy_addRegex (const char * regex, const char * proxy, int regex_flags){ /* ** If this is the first time here then also add a before filter to handle ** proxy authentication and the normal AA after filter as well. ** These filters will be removed if we remove all proxies again. */ if (!proxies) { proxies = HTList_new(); HTNet_addBefore(HTAA_proxyBeforeFilter, NULL, NULL, HT_FILTER_MIDDLE); HTNet_addAfter(HTAuthFilter, NULL, NULL, HT_NO_PROXY_ACCESS, HT_FILTER_MIDDLE); HTNet_addAfter(HTAuthFilter, NULL, NULL, HT_PROXY_REAUTH, HT_FILTER_MIDDLE); }#ifdef HT_POSIX_REGEX return add_object(proxies, regex, proxy, YES, regex_flags);#else return add_object(proxies, regex, proxy, NO, -1);#endif}/*** Removes all registered proxies*/PUBLIC BOOL HTProxy_deleteAll (void){ if (remove_allObjects(proxies)) { HTList_delete(proxies); /* ** If we have no more proxies then there is no reason for checking ** proxy authentication. We therefore unregister the filters for ** handling proxy authentication */ HTNet_deleteBefore(HTAA_proxyBeforeFilter); HTNet_deleteAfterStatus(HT_NO_PROXY_ACCESS); HTNet_deleteAfterStatus(HT_PROXY_REAUTH); proxies = NULL; return YES; } return NO;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -