From 7cba0da805ba63a9f0745cefdf2a79b2efdb243e Mon Sep 17 00:00:00 2001 From: Josh Aas Date: Wed, 6 May 2009 16:44:11 -0400 Subject: [PATCH] Improve proxy exception parsing on Mac OS X and Windows. b=470207 sr=roc --- .../osxproxy/nsOSXSystemProxySettings.mm | 86 +++++++++---------- .../nsWindowsSystemProxySettings.cpp | 10 +-- 2 files changed, 47 insertions(+), 49 deletions(-) diff --git a/toolkit/system/osxproxy/nsOSXSystemProxySettings.mm b/toolkit/system/osxproxy/nsOSXSystemProxySettings.mm index 888008cfc336..9a8abefa0bf8 100644 --- a/toolkit/system/osxproxy/nsOSXSystemProxySettings.mm +++ b/toolkit/system/osxproxy/nsOSXSystemProxySettings.mm @@ -22,10 +22,11 @@ * the Initial Developer. All Rights Reserved. * * Contributor(s): - * James Bunton (jamesbunton@fastmail.fm) - * Diane Trout (diane@ghic.org) - * Robert O'Callahan (rocallahan@novell.com) - * Håkan Waara (hwaara@gmail.com) + * James Bunton + * Diane Trout + * Robert O'Callahan + * Håkan Waara + * Josh Aas * * Alternatively, the contents of this file may be used under the terms of * either the GNU General Public License Version 2 or later (the "GPL"), or @@ -74,7 +75,7 @@ public: nsresult FindSCProxyPort(nsIURI* aURI, nsACString& aResultHost, PRInt32& aResultPort); // is host:port on the proxy exception list? - PRBool IsInExceptionList(const nsACString& aHost, PRInt32 aPort) const; + PRBool IsInExceptionList(const nsACString& aHost) const; private: ~nsOSXSystemProxySettings(); @@ -261,48 +262,49 @@ nsOSXSystemProxySettings::GetAutoconfigURL(nsCAutoString& aResult) const } static PRBool -IsHostProxyEntry(const nsACString& aHost, PRInt32 aPort, NSString* aStr) +IsHostProxyEntry(const nsACString& aHost, const nsACString& aOverride) { - NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN; + nsCAutoString host(aHost); + nsCAutoString override(aOverride); - nsCAutoString proxyEntry([aStr UTF8String]); + /* + printf("IsHostProxyEntry\nRequest: %s\nOverride: %s\n", + nsPromiseFlatCString(host).get(), nsPromiseFlatCString(override).get()); + */ - nsReadingIterator start; - nsReadingIterator colon; - nsReadingIterator end; + PRInt32 overrideLength = override.Length(); + PRInt32 tokenStart = 0; + PRInt32 offset = 0; + PRBool star = PR_FALSE; - proxyEntry.BeginReading(start); - proxyEntry.EndReading(end); - colon = start; - PRInt32 port = -1; - - if (FindCharInReadable(':', colon, end)) { - ++colon; - nsDependentCSubstring portStr(colon, end); - nsCAutoString portStr2(portStr); - PRInt32 err; - port = portStr2.ToInteger(&err); - if (err != 0) { - port = -2; // don't match any port, so we ignore this pattern - } - --colon; - } else { - colon = end; - } - - if (port == -1 || port == aPort) { - nsDependentCSubstring hostStr(start, colon); - if (StringEndsWith(aHost, hostStr, nsCaseInsensitiveCStringComparator())) { - return PR_TRUE; + while (tokenStart < overrideLength) { + PRInt32 tokenEnd = override.FindChar('*', tokenStart); + if (tokenEnd == tokenStart) { + // Star is the first character in the token. + star = PR_TRUE; + tokenStart++; + // If the character following the '*' is a '.' character then skip + // it so that "*.foo.com" allows "foo.com". + if (override.FindChar('.', tokenStart) == tokenStart) + tokenStart++; + } else { + if (tokenEnd == -1) + tokenEnd = overrideLength; // no '*' char, match rest of string + nsCAutoString token(Substring(override, tokenStart, tokenEnd - tokenStart)); + offset = host.Find(token, offset); + if (offset == -1 || (!star && offset)) + return PR_FALSE; + star = PR_FALSE; + tokenStart = tokenEnd; + offset += token.Length(); } } - NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(PR_FALSE); + return (star || (offset == static_cast(host.Length()))); } PRBool -nsOSXSystemProxySettings::IsInExceptionList(const nsACString& aHost, - PRInt32 aPort) const +nsOSXSystemProxySettings::IsInExceptionList(const nsACString& aHost) const { NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN; @@ -315,14 +317,14 @@ nsOSXSystemProxySettings::IsInExceptionList(const nsACString& aHost, NSString* currentValue = NULL; while ((currentValue = [exceptionEnumerator nextObject])) { NS_ENSURE_TRUE([currentValue isKindOfClass:[NSString class]], PR_FALSE); - if (IsHostProxyEntry(aHost, aPort, currentValue)) + nsCAutoString overrideStr([currentValue UTF8String]); + if (IsHostProxyEntry(aHost, overrideStr)) return PR_TRUE; } NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(PR_FALSE); } - nsresult nsOSXSystemProxySettings::GetPACURI(nsACString& aResult) { @@ -350,15 +352,11 @@ nsOSXSystemProxySettings::GetProxyForURI(nsIURI* aURI, nsACString& aResult) nsresult rv = aURI->GetHost(host); NS_ENSURE_SUCCESS(rv, rv); - PRInt32 port; - rv = aURI->GetPort(&port); - NS_ENSURE_SUCCESS(rv, rv); - PRInt32 proxyPort; nsCAutoString proxyHost; rv = FindSCProxyPort(aURI, proxyHost, proxyPort); - if (NS_FAILED(rv) || IsInExceptionList(host, port)) { + if (NS_FAILED(rv) || IsInExceptionList(host)) { aResult.AssignLiteral("DIRECT"); } else { aResult.Assign(NS_LITERAL_CSTRING("PROXY ") + proxyHost + nsPrintfCString(":%d", proxyPort)); diff --git a/toolkit/system/windowsproxy/nsWindowsSystemProxySettings.cpp b/toolkit/system/windowsproxy/nsWindowsSystemProxySettings.cpp index 4158a5b33b4f..ef53eb76e7ee 100644 --- a/toolkit/system/windowsproxy/nsWindowsSystemProxySettings.cpp +++ b/toolkit/system/windowsproxy/nsWindowsSystemProxySettings.cpp @@ -21,7 +21,7 @@ * the Initial Developer. All Rights Reserved. * * Contributor(s): - * Mitchell Field (mitch_1_2@live.com.au) + * Mitchell Field * * Alternatively, the contents of this file may be used under the terms of * either the GNU General Public License Version 2 or later (the "GPL"), or @@ -181,6 +181,10 @@ nsWindowsSystemProxySettings::PatternMatch(const nsACString& aHost, if (tokenEnd == tokenStart) { star = PR_TRUE; tokenStart++; + // If the character following the '*' is a '.' character then skip + // it so that "*.foo.com" allows "foo.com". + if (override.FindChar('.', tokenStart) == tokenStart) + tokenStart++; } else { if (tokenEnd == -1) tokenEnd = overrideLength; @@ -231,10 +235,6 @@ nsWindowsSystemProxySettings::GetProxyForURI(nsIURI* aURI, nsACString& aResult) rv = aURI->GetHost(host); NS_ENSURE_SUCCESS(rv, rv); - PRInt32 port; - rv = aURI->GetPort(&port); - NS_ENSURE_SUCCESS(rv, rv); - if (MatchOverride(host)) { SetProxyResultDirect(aResult); return NS_OK;