/* ** Retry through Proxy AFTER Filter ** -------------------------------- ** This filter handles a 305 Use Proxy response and retries the request ** through the proxy */ PUBLIC int HTUseProxyFilter (HTRequest * request, HTResponse * response, void * param, int status) { HTAlertCallback * cbf = HTAlert_find(HT_A_CONFIRM); HTAnchor * proxy_anchor = HTResponse_redirection(response); if (!proxy_anchor) { HTTRACE(PROT_TRACE, "Use Proxy... No proxy location\n"); return HT_OK; } /* ** Add the proxy to the list. Assume HTTP access method only! ** Because evil servers may rediret the client to an untrusted ** proxy, we can only accept redirects for this particular ** server. Also, we do not know whether this is for HTTP or all ** other requests as well */ if ((cbf && (*cbf)(request, HT_A_CONFIRM, HT_MSG_PROXY, NULL,NULL,NULL))) { char * addr = HTAnchor_address(proxy_anchor); HTProxy_add("http", addr); HT_FREE(addr); /* ** Start new request through the proxy if we haven't reached the max ** number of redirections for this request */ if (HTRequest_doRetry(request)) { HTLoadAnchor(proxy_anchor, request); } else { HTRequest_addError(request, ERR_FATAL, NO, HTERR_MAX_REDIRECT, NULL, 0, "HTRedirectFilter"); } /* ** By returning HT_ERROR we make sure that this is the last handler to be ** called. We do this as we don't want any other filter to delete the ** request object now when we have just started a new one ourselves */ return HT_ERROR; } else { HTRequest_addError(request, ERR_FATAL, NO, HTERR_NO_AUTO_PROXY, NULL, 0, "HTUseProxyFilter"); return HT_OK; } }
/* ** 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); } } }
/* Load one line of configuration ** ------------------------------ ** Call this, for example, to load a X resource with config info. ** Returns YES if line OK, else NO */ PUBLIC BOOL HTRule_parseLine (HTList * list, const char * config) { HTRuleOp op; char * line = NULL; char * ptr; char * word1, * word2, * word3; int status; if (!config) return NO; if ((ptr = strchr(config, '#'))) *ptr = '\0'; StrAllocCopy(line, config); /* Get our own copy */ ptr = line; HTTRACE(APP_TRACE, "Rule Parse.. `%s\'\n" _ config ? config : "<null>"); if ((word1 = HTNextField(&ptr)) == NULL) { /* Empty line */ HT_FREE(line); return YES; } if ((word2 = HTNextField(&ptr)) == NULL) { HTTRACE(APP_TRACE, "Rule Parse.. Insufficient operands: `%s\'\n" _ line); HT_FREE(line); return NO; } word3 = HTNextField(&ptr); /* Look for things we recognize */ if (!strcasecomp(word1, "addtype")) { double quality; char * encoding = HTNextField(&ptr); status = ptr ? sscanf(ptr, "%lf", &quality) : 0; HTBind_add(word2, /* suffix */ word3, /* type */ encoding ? encoding : "binary", /* encoding */ NULL, /* cte */ NULL, /* language */ status >= 1? quality : 1.0); /* quality */ } else if (!strcasecomp(word1, "addencoding")) { double quality; status = ptr ? sscanf(ptr, "%lf", &quality) : 0; HTBind_addEncoding(word2, word3, status >= 1 ? quality : 1.0); } else if (!strcasecomp(word1, "addlanguage")) { double quality; status = ptr ? sscanf(ptr, "%lf", &quality) : 0; HTBind_addLanguage(word2, word3, status >= 1 ? quality : 1.0); } else if (!strcasecomp(word1, "presentation")) { HTList * converters = HTFormat_conversion(); double quality, secs, secs_per_byte; status = ptr ? sscanf(ptr,"%lf%lf%lf",&quality,&secs,&secs_per_byte):0; HTPresentation_add(converters, word2, word3, NULL, status >= 1 ? quality : 1.0, status >= 2 ? secs : 0.0, status >= 3 ? secs_per_byte : 0.0); } else if (!strcasecomp(word1, "proxy")) { HTProxy_add(word2, word3); } else if (!strcasecomp(word1, "noproxy")) { int port = 0; status = ptr ? sscanf(ptr, "%d", &port) : 0; HTNoProxy_add(word2, word3, port); } else if (!strcasecomp(word1, "gateway")) { HTGateway_add(word2, word3); } else { op = 0==strcasecomp(word1, "map") ? HT_Map : 0==strcasecomp(word1, "pass") ? HT_Pass : 0==strcasecomp(word1, "fail") ? HT_Fail : HT_Invalid; if (op == HT_Invalid) { HTTRACE(APP_TRACE, "Rule Parse.. Bad or unknown: `%s'\n" _ config); } else HTRule_add(list, op, word2, word3); } HT_FREE(line); return YES; }