More proxy auto config work.

This commit is contained in:
gagan%netscape.com 2000-05-05 09:49:42 +00:00
parent 12143c560d
commit 3f3e541a66
6 changed files with 322 additions and 74 deletions

View File

@ -52,6 +52,7 @@ CPPSRCS = \
nsDirectoryIndexStream.cpp \
nsStreamLoader.cpp \
nsProtocolProxyService.cpp \
nsProxyAutoConfigUtils.cpp \
$(NULL)
# we don't want the shared lib, but we want to force the creation of a

View File

@ -49,6 +49,7 @@ CPP_OBJS = \
.\$(OBJDIR)\nsDirectoryIndexStream.obj \
.\$(OBJDIR)\nsStreamLoader.obj \
.\$(OBJDIR)\nsProtocolProxyService.obj \
.\$(OBJDIR)\nsProxyAutoConfigUtils.obj \
$(NULL)
INCS = $(INCS) \

View File

@ -0,0 +1,134 @@
/* -*- Mode: Java; tab-width: 4; c-basic-offset: 4; -*-
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
*/
/*
Script for simple URI based filtering.
To add a new filter scroll down to the
last function (FilterMain) in this file.
- Gagan Saksena 05/04/00
*/
const kFILTERS_PROGID = "component://mozilla/network/filters";
const kFILTERS_CID = Components.ID("{4677ea1e-1dd2-11b2-8654-e836efb6995c}");
const nsIWebFilters = Components.interfaces.nsIWebFilters;
const nsIURI = Components.interfaces.nsIURI;
function debug(msg)
{
dump(msg + "\n");
}
function nsFilterManager() {};
nsFilterManager.prototype = {
allowLoading: function(uri) {
return FilterMain(uri);
}
}
var filterModule = new Object();
filterModule.registerSelf =
function (compMgr, fileSpec, location, type) {
dump("*** Registering Web Filters (a Javascript module!)\n");
compMgr.registerComponentWithType(kFILTERS_CID,
"Javascript Web Filters",
kFILTERS_PROGID,
fileSpec, location,
true, true, type);
}
filterModule.getClassObject =
function (compMgr, cid, iid) {
if (!cid.equals(kFILTERS_CID))
throw Components.results.NS_ERROR_NO_INTERFACE;
if (!iid.equals(Components.interfaces.nsIFactory))
throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
return filtersFactory;
}
filterModule.canUnload =
function(compMgr) {
dump("*** Unloading Web Filters...\n");
return true;
}
var filtersFactory = new Object();
filtersFactory.CreateInstance =
function (outer, iid) {
if (outer != null)
throw Components.results.NS_ERROR_NO_AGGREGATION;
if (!iid.equals(nsIWebFilters) &&
!iid.equals(Components.interfaces.nsISupports)) {
// shouldn't this be NO_INTERFACE?
throw Components.results.NS_ERROR_INVALID_ARG;
}
return FilterMan;
}
function NSGetModule(compMgr, fileSpec) {
return filterModule;
}
var FilterMan= new nsFilterManager();
// -----------------------------------------------------------------
// Sample block hosts list...
var blockHosts = new Array(
"ad.linkexchange.com",
".tv.com",
"doubleclick.net"
);
// The main filter function! Use the arrays above to build more...
// @return true if you want it to load.
// @return false if you don't like that uri.
function FilterMain(url) {
uri = url.QueryInterface(nsIURI);
try {
/* Commented since currently only HTTP asks for filters...
// Always let the non-relevant ones go...!
// otherwise it would mess up your skin (chrome/resource and such...)
if ((uri.scheme).search("http") == -1)
return true;
*/
// Sample host matches- uri.host
for (var i=0; i < blockHosts.length; ++i) {
if (String(uri.host).search(blockHosts[i]) != -1)
return false;
}
}
catch (ex) { // any problems just continue
return true;
}
return true;
}

View File

@ -23,8 +23,10 @@
#include "nsProtocolProxyService.h"
#include "nsIServiceManager.h"
#include "nsXPIDLString.h"
#include "nsIProxyAutoConfig.h"
static NS_DEFINE_CID(kPrefServiceCID, NS_PREF_CID);
static const char PROXY_PREFS[] = "network.proxy";
PRInt32 PR_CALLBACK ProxyPrefsCallback(const char* pref, void* instance)
{
@ -39,7 +41,7 @@ NS_IMPL_ISUPPORTS1(nsProtocolProxyService, nsIProtocolProxyService);
nsProtocolProxyService::nsProtocolProxyService():
mUseProxy(PR_FALSE)
mUseProxy(0)
{
NS_INIT_REFCNT();
}
@ -101,7 +103,7 @@ nsProtocolProxyService::PrefsChanged(const char* pref) {
PRInt32 type = -1;
rv = mPrefs->GetIntPref("network.proxy.type",&type);
if (NS_SUCCEEDED(rv))
mUseProxy = (type == 1); // type == 2 is autoconfig stuff
mUseProxy = type; // type == 2 is autoconfig stuff
}
if (!pref || !PL_strcmp(pref, "network.proxy.http"))
@ -216,7 +218,8 @@ nsProtocolProxyService::ExamineForProxy(nsIURI *aURI, nsIProxy *aProxy) {
// if proxies are enabled and this host:port combo is
// supposed to use a proxy, check for a proxy.
if (!mUseProxy || !CanUseProxy(aURI)) {
if ((0 == mUseProxy) ||
((1 == mUseProxy) && !CanUseProxy(aURI))) {
rv = aProxy->SetProxyHost(nsnull);
if (NS_FAILED(rv)) return rv;
rv = aProxy->SetProxyPort(-1);
@ -224,6 +227,29 @@ nsProtocolProxyService::ExamineForProxy(nsIURI *aURI, nsIProxy *aProxy) {
return NS_OK;
}
// Proxy auto config magic...
if (2 == mUseProxy)
{
// later on put this in a member variable TODO
nsCOMPtr<nsIProxyAutoConfig> pac =
do_CreateInstance(NS_PROXY_AUTO_CONFIG_PROGID, &rv);
if (NS_SUCCEEDED(rv))
{
nsXPIDLCString p_host;
PRInt32 p_port;
if (NS_SUCCEEDED(rv = pac->ProxyForURL(
aURI, getter_Copies(p_host), &p_port)))
{
if (NS_FAILED(rv = aProxy->SetProxyHost(p_host)))
return rv;
if (NS_FAILED(rv = aProxy->SetProxyPort(p_port)))
return rv;
return NS_OK;
}
}
return rv;
}
nsXPIDLCString scheme;
rv = aURI->GetScheme(getter_Copies(scheme));
if (NS_FAILED(rv)) return rv;
@ -337,7 +363,8 @@ nsProtocolProxyService::LoadFilters(const char* filters)
char* endproxy = np+1; // at least that...
char* portLocation = 0;
PRInt32 nport = 0; // no proxy port
while (*endproxy && (*endproxy != ',' && !nsCRT::IsAsciiSpace(*endproxy)))
while (*endproxy && (*endproxy != ',' &&
!nsCRT::IsAsciiSpace(*endproxy)))
{
if (*endproxy == ':')
portLocation = endproxy;
@ -359,4 +386,3 @@ nsProtocolProxyService::LoadFilters(const char* filters)
}
}

View File

@ -61,7 +61,7 @@ protected:
PRBool CanUseProxy(nsIURI* aURI);
nsCOMPtr<nsIPref> mPrefs;
PRBool mUseProxy;
PRUint16 mUseProxy;
nsXPIDLCString mHTTPProxyHost;
PRInt32 mHTTPProxyPort;

View File

@ -21,107 +21,193 @@
*/
/*
Script for the proxy auto config in the new world order.
Script for Proxy Auto Config in the new world order.
- Gagan Saksena 04/24/00
*/
var pac_progid= "component://mozilla/network/proxy_autoconfig";
const kPAC_PROGID = "component://mozilla/network/proxy_autoconfig";
const kPAC_CID = Components.ID("{63ac8c66-1dd2-11b2-b070-84d00d3eaece}");
const nsIProxyAutoConfig = Components.interfaces.nsIProxyAutoConfig;
function debug(msg)
{
dump(msg);
}
function testSelf() {
try {
var pacMan =
Components.classes['component://mozilla/network/proxy_autoconfig']
.createInstance();
//pacMan.ProxyForURL();
}
catch (e) {
debug("oh oh...");
debug(e);
}
}
function Init()
{
debug("nsProxyAutoConfig.js: Init()\n");
PacMan = new nsProxyAutoConfig();
//testSelf();
dump(msg + "\n");
}
// implementor of nsIProxyAutoConfig
function nsProxyAutoConfig() {};
nsProxyAutoConfig.prototype = {
ProxyForURL: function(url, host, port) {
uri = url.QueryInterface(Components.interfaces.nsIURI);
print("PAC.js uri= " + uri.spec);
// Call the original function-
var proxy = FindProxyForURL(uri.spec, uri.host);
debug("Proxy = " + proxy);
// TODO add code here that parses stupid PAC return strings
// to host and port pair...
// TODO warn about SOCKS
// test dummy for now...
host = "localhost";
port = 4321;
host.value = "localhost";
port.value = 4444;
}
}
var pacModule = {
firstTime: true,
var pacModule = new Object();
registerSelf: function (compMgr, fileSpec, location, type) {
if (this.firstTime) {
dump("*** Deferring registration of Proxy Auto Config\n");
this.firstTime = false;
throw Components.results.NS_ERROR_FACTORY_REGISTER_AGAIN;
}
dump("*** Registering Proxy Auto Config\n");
compMgr.registerComponentWithType(this.pacCID,
pacModule.registerSelf =
function (compMgr, fileSpec, location, type) {
dump("*** Registering Proxy Auto Config (a Javascript module!) \n");
compMgr.registerComponentWithType(kPAC_CID,
"Proxy Auto Config",
"component://mozilla/network/proxy_autoconfig",
fileSpec,
location,
true,
true,
type);
},
kPAC_PROGID,
fileSpec, location,
true, true, type);
}
getClassObject: function (compMgr, cid, iid) {
if (!cid.equals(this.pacCID))
pacModule.getClassObject =
function (compMgr, cid, iid) {
if (!cid.equals(kPAC_CID))
throw Components.results.NS_ERROR_NO_INTERFACE;
if (!iid.equals(Components.interfaces.nsIFactory))
throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
return this.pacFactory;
},
return pacFactory;
}
pacCID: Components.ID("{63ac8c66-1dd2-11b2-b070-84d00d3eaece}"),
pacFactory: {
CreateInstance: function (outer, iid) {
if (outer != null)
throw Components.results.NS_ERROR_NO_AGGREGATION;
if (!iid.equals(Components.interfaces.nsIProxyAutoConfig) &&
!iid.equals(Components.interfaces.nsISupports)) {
// shouldn't this be NO_INTERFACE?
throw Components.results.NS_ERROR_INVALID_ARG;
}
return PacMan;
}
},
canUnload: function (compMgr) {
pacModule.canUnload =
function (compMgr) {
dump("*** Unloading Proxy Auto Config...\n");
return true;
}
}
var pacFactory = new Object();
pacFactory.CreateInstance =
function (outer, iid) {
if (outer != null)
throw Components.results.NS_ERROR_NO_AGGREGATION;
if (!iid.equals(nsIProxyAutoConfig) &&
!iid.equals(Components.interfaces.nsISupports)) {
// shouldn't this be NO_INTERFACE?
throw Components.results.NS_ERROR_INVALID_ARG;
}
return PacMan;
}
function NSGetModule(compMgr, fileSpec) {
return pacModule;
}
var PacMan;
Init();
var PacMan = new nsProxyAutoConfig() ;
// dumb hacks for "special" functions that should have long been
// taken out and shot, and shot again... most of them.
var date = new Date();
function dateRange(dmy) {
var intval = parseInt(dmy, 10);
if (isNaN(intval)) { // it's a month
return (date.toString().toLowerCase().search(dmy.toLowerCase()) != -1);
}
if (intval <= 31) { // it's a day
return (date.getDate() == intval);
}
else { // it's an year
return (date.getFullYear() == intval);
}
}
function dateRange(dmy1, dmy2) {
return false;
}
function dateRange(d1, m1, y1, d2, m2, y2) {
var date1 = new Date(y1,m1,d1);
var date2 = new Date(y2,m2,d2);
return (date >= date1) && (date <= date2);
}
function dnsDomainIs(host, domain) {
return (host.search("/" + domain + "$/") != -1);
}
function dnsDomainLevels(host) {
return host.split('.').length-1;
}
function dnsResolve(host) {
// call back into IOService... TODO
return "127.0.0.1";
}
function isInNet(host, pattern, mask) {
return false;
}
function isPlainHostName(host) {
return (host.search("\.") == -1);
}
function isResolvable(host) {
// call back into IOService and get the result... TODO
return false;
}
function localHostOrDomainIs(host, hostdom) {
if (isPlainHostName(host)) {
return (hostdom.search("/^" + host + "/") != -1);
}
else {
return (host == hostdom); //TODO check
}
}
function myIpAddress() {
// call into IOService ... TODO
return "127.0.0.1";
}
function shExpMatch(str, shexp) {
return false;
}
function timeRange(hour) {
return false;
}
function timeRange(h1, h2) {
return false;
}
function timeRange(h1, m1, h2, m2) {
return false;
}
function timeRange(h1, m1, s1, h2, m2, s2) {
return false;
}
function timeRange(h1, m1, s1, h2, m2, s2, gmt) {
return false;
}
function weekdayRange(wd1, wd2, gmt) {
return false;
}
///--------- replace everything below with your existing pac file ...
// Sample implementation ...
function FindProxyForURL(url, host)
{
if (isPlainHostName(host) ||
dnsDomainIs(host, ".mozilla.org") ||
isResolvable(host))
return "DIRECT";
else
return "PROXY tegu.mozilla.org:3130; DIRECT";
}