Bug 1233360 - Part 1: Move out OSX bypass rules checker; r?bagder draft
authorLiang-Heng Chen <xeonchen@mozilla.com>
Wed, 04 May 2016 11:30:46 +0800
changeset 365715 ba84927988aceea90c0add842230bb5ba95d5b36
parent 365494 674a552743785c28c75866969aad513bd8eaf6ae
child 365716 0df1736b6da3fea3e0d65b06b06aaf9ed5e882d8
child 365932 5411036976021a0dbc2e23cadb268b4ac56868a0
child 366230 32bdc9df77512f8d5607777142495098fe65787e
push id17835
push userbmo:xeonchen@mozilla.com
push dateWed, 11 May 2016 11:09:33 +0000
reviewersbagder
bugs1233360
milestone49.0a1
Bug 1233360 - Part 1: Move out OSX bypass rules checker; r?bagder MozReview-Commit-ID: KSykcVkfUmW
toolkit/system/osxproxy/ProxyUtils.h
toolkit/system/osxproxy/ProxyUtils.mm
toolkit/system/osxproxy/moz.build
toolkit/system/osxproxy/nsOSXSystemProxySettings.mm
new file mode 100644
--- /dev/null
+++ b/toolkit/system/osxproxy/ProxyUtils.h
@@ -0,0 +1,21 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef mozilla_toolkit_system_osxproxy_ProxyUtils_h
+#define mozilla_toolkit_system_osxproxy_ProxyUtils_h
+
+#include "nsStringGlue.h"
+
+namespace mozilla {
+namespace toolkit {
+namespace system {
+
+bool IsHostProxyEntry(const nsACString& aHost, const nsACString& aOverride);
+
+} // namespace system
+} // namespace toolkit
+} // namespace mozilla
+
+#endif // mozilla_toolkit_system_osxproxy_ProxyUtils_h
new file mode 100644
--- /dev/null
+++ b/toolkit/system/osxproxy/ProxyUtils.mm
@@ -0,0 +1,50 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "ProxyUtils.h"
+
+namespace mozilla {
+namespace toolkit {
+namespace system {
+
+bool IsHostProxyEntry(const nsACString& aHost, const nsACString& aOverride)
+{
+  nsAutoCString host(aHost);
+  nsAutoCString override(aOverride);
+
+  int32_t overrideLength = override.Length();
+  int32_t tokenStart = 0;
+  int32_t offset = 0;
+  bool star = false;
+
+  while (tokenStart < overrideLength) {
+    int32_t tokenEnd = override.FindChar('*', tokenStart);
+    if (tokenEnd == tokenStart) {
+      // Star is the first character in the token.
+      star = 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
+      nsAutoCString token(Substring(override, tokenStart, tokenEnd - tokenStart));
+      offset = host.Find(token, offset);
+      if (offset == -1 || (!star && offset))
+        return false;
+      star = false;
+      tokenStart = tokenEnd;
+      offset += token.Length();
+    }
+  }
+
+  return (star || (offset == static_cast<int32_t>(host.Length())));
+}
+
+} // namespace system
+} // namespace toolkit
+} // namespace mozilla
--- a/toolkit/system/osxproxy/moz.build
+++ b/toolkit/system/osxproxy/moz.build
@@ -1,11 +1,12 @@
 # -*- Mode: python; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*-
 # vim: set filetype=python:
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 SOURCES += [
     'nsOSXSystemProxySettings.mm',
+    'ProxyUtils.mm',
 ]
 
 FINAL_LIBRARY = 'xul'
--- a/toolkit/system/osxproxy/nsOSXSystemProxySettings.mm
+++ b/toolkit/system/osxproxy/nsOSXSystemProxySettings.mm
@@ -11,16 +11,17 @@
 #include "mozilla/ModuleUtils.h"
 #include "nsIServiceManager.h"
 #include "nsPrintfCString.h"
 #include "nsNetCID.h"
 #include "nsISupportsPrimitives.h"
 #include "nsIURI.h"
 #include "nsObjCExceptions.h"
 #include "mozilla/Attributes.h"
+#include "ProxyUtils.h"
 
 class nsOSXSystemProxySettings final : public nsISystemProxySettings {
 public:
   NS_DECL_THREADSAFE_ISUPPORTS
   NS_DECL_NSISYSTEMPROXYSETTINGS
 
   nsOSXSystemProxySettings();
   nsresult Init();
@@ -227,69 +228,32 @@ nsOSXSystemProxySettings::GetAutoconfigU
     return NS_OK;
   }
 
   return NS_ERROR_FAILURE;
 
   NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
 }
 
-static bool
-IsHostProxyEntry(const nsACString& aHost, const nsACString& aOverride)
-{
-  nsAutoCString host(aHost);
-  nsAutoCString override(aOverride);
-
-  int32_t overrideLength = override.Length();
-  int32_t tokenStart = 0;
-  int32_t offset = 0;
-  bool star = false;
-
-  while (tokenStart < overrideLength) {
-    int32_t tokenEnd = override.FindChar('*', tokenStart);
-    if (tokenEnd == tokenStart) {
-      // Star is the first character in the token.
-      star = 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
-      nsAutoCString token(Substring(override, tokenStart, tokenEnd - tokenStart));
-      offset = host.Find(token, offset);
-      if (offset == -1 || (!star && offset))
-        return false;
-      star = false;
-      tokenStart = tokenEnd;
-      offset += token.Length();
-    }
-  }
-
-  return (star || (offset == static_cast<int32_t>(host.Length())));
-}
-
 bool
 nsOSXSystemProxySettings::IsInExceptionList(const nsACString& aHost) const
 {
   NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
 
   NS_ENSURE_TRUE(mProxyDict != NULL, false);
 
   NSArray* exceptionList = [mProxyDict objectForKey:(NSString*)kSCPropNetProxiesExceptionsList];
   NS_ENSURE_TRUE(exceptionList == NULL || [exceptionList isKindOfClass:[NSArray class]], false);
 
   NSEnumerator* exceptionEnumerator = [exceptionList objectEnumerator];
   NSString* currentValue = NULL;
   while ((currentValue = [exceptionEnumerator nextObject])) {
     NS_ENSURE_TRUE([currentValue isKindOfClass:[NSString class]], false);
     nsAutoCString overrideStr([currentValue UTF8String]);
-    if (IsHostProxyEntry(aHost, overrideStr))
+    if (mozilla::toolkit::system::IsHostProxyEntry(aHost, overrideStr))
       return true;
   }
 
   NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(false);
 }
 
 nsresult
 nsOSXSystemProxySettings::GetPACURI(nsACString& aResult)