toolkit/system/unixproxy/nsUnixSystemProxySettings.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/system/unixproxy/nsUnixSystemProxySettings.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,542 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "nsISystemProxySettings.h"
    1.10 +#include "mozilla/ModuleUtils.h"
    1.11 +#include "nsIServiceManager.h"
    1.12 +#include "nsIGConfService.h"
    1.13 +#include "nsIURI.h"
    1.14 +#include "nsReadableUtils.h"
    1.15 +#include "nsArrayUtils.h"
    1.16 +#include "prnetdb.h"
    1.17 +#include "prenv.h"
    1.18 +#include "nsPrintfCString.h"
    1.19 +#include "nsNetUtil.h"
    1.20 +#include "nsISupportsPrimitives.h"
    1.21 +#include "nsIGSettingsService.h"
    1.22 +#include "nsInterfaceHashtable.h"
    1.23 +#include "mozilla/Attributes.h"
    1.24 +#include "nsIURI.h"
    1.25 +
    1.26 +class nsUnixSystemProxySettings MOZ_FINAL : public nsISystemProxySettings {
    1.27 +public:
    1.28 +  NS_DECL_ISUPPORTS
    1.29 +  NS_DECL_NSISYSTEMPROXYSETTINGS
    1.30 +
    1.31 +  nsUnixSystemProxySettings()
    1.32 +    : mSchemeProxySettings(5)
    1.33 +  {
    1.34 +  }
    1.35 +  nsresult Init();
    1.36 +
    1.37 +private:
    1.38 +  ~nsUnixSystemProxySettings() {}
    1.39 +  
    1.40 +  nsCOMPtr<nsIGConfService> mGConf;
    1.41 +  nsCOMPtr<nsIGSettingsService> mGSettings;
    1.42 +  nsCOMPtr<nsIGSettingsCollection> mProxySettings;
    1.43 +  nsInterfaceHashtable<nsCStringHashKey, nsIGSettingsCollection> mSchemeProxySettings;
    1.44 +  bool IsProxyMode(const char* aMode);
    1.45 +  nsresult SetProxyResultFromGConf(const char* aKeyBase, const char* aType, nsACString& aResult);
    1.46 +  nsresult GetProxyFromGConf(const nsACString& aScheme, const nsACString& aHost, int32_t aPort, nsACString& aResult);
    1.47 +  nsresult GetProxyFromGSettings(const nsACString& aScheme, const nsACString& aHost, int32_t aPort, nsACString& aResult);
    1.48 +  nsresult SetProxyResultFromGSettings(const char* aKeyBase, const char* aType, nsACString& aResult);
    1.49 +};
    1.50 +
    1.51 +NS_IMPL_ISUPPORTS(nsUnixSystemProxySettings, nsISystemProxySettings)
    1.52 +
    1.53 +NS_IMETHODIMP
    1.54 +nsUnixSystemProxySettings::GetMainThreadOnly(bool *aMainThreadOnly)
    1.55 +{
    1.56 +  // dbus prevents us from being threadsafe, but this routine should not block anyhow
    1.57 +  *aMainThreadOnly = true;
    1.58 +  return NS_OK;
    1.59 +}
    1.60 +
    1.61 +nsresult
    1.62 +nsUnixSystemProxySettings::Init()
    1.63 +{
    1.64 +  mGSettings = do_GetService(NS_GSETTINGSSERVICE_CONTRACTID);
    1.65 +  if (mGSettings) {
    1.66 +    mGSettings->GetCollectionForSchema(NS_LITERAL_CSTRING("org.gnome.system.proxy"),
    1.67 +                                       getter_AddRefs(mProxySettings));
    1.68 +  }
    1.69 +  if (!mProxySettings) {
    1.70 +    mGConf = do_GetService(NS_GCONFSERVICE_CONTRACTID);
    1.71 +  }
    1.72 +  
    1.73 +  return NS_OK;
    1.74 +}
    1.75 +
    1.76 +bool
    1.77 +nsUnixSystemProxySettings::IsProxyMode(const char* aMode)
    1.78 +{
    1.79 +  nsAutoCString mode;
    1.80 +  return NS_SUCCEEDED(mGConf->GetString(NS_LITERAL_CSTRING("/system/proxy/mode"), mode)) &&
    1.81 +         mode.EqualsASCII(aMode);
    1.82 +}
    1.83 +
    1.84 +nsresult
    1.85 +nsUnixSystemProxySettings::GetPACURI(nsACString& aResult)
    1.86 +{
    1.87 +  if (mProxySettings) {
    1.88 +    nsCString proxyMode;
    1.89 +    // Check if mode is auto
    1.90 +    nsresult rv = mProxySettings->GetString(NS_LITERAL_CSTRING("mode"), proxyMode);
    1.91 +    if (rv == NS_OK && proxyMode.Equals("auto")) {
    1.92 +      return mProxySettings->GetString(NS_LITERAL_CSTRING("autoconfig-url"), aResult);
    1.93 +    }
    1.94 +    /* The org.gnome.system.proxy schema has been found, but auto mode is not set.
    1.95 +     * Don't try the GConf and return empty string. */
    1.96 +    aResult.Truncate();
    1.97 +    return NS_OK;
    1.98 +  }
    1.99 +
   1.100 +  if (mGConf && IsProxyMode("auto")) {
   1.101 +    return mGConf->GetString(NS_LITERAL_CSTRING("/system/proxy/autoconfig_url"),
   1.102 +                             aResult);
   1.103 +  }
   1.104 +  // Return an empty string when auto mode is not set.
   1.105 +  aResult.Truncate();
   1.106 +  return NS_OK;
   1.107 +}
   1.108 +
   1.109 +static bool
   1.110 +IsInNoProxyList(const nsACString& aHost, int32_t aPort, const char* noProxyVal)
   1.111 +{
   1.112 +  NS_ASSERTION(aPort >= 0, "Negative port?");
   1.113 +  
   1.114 +  nsAutoCString noProxy(noProxyVal);
   1.115 +  if (noProxy.EqualsLiteral("*"))
   1.116 +    return true;
   1.117 +    
   1.118 +  noProxy.StripWhitespace();
   1.119 +  
   1.120 +  nsReadingIterator<char> pos;
   1.121 +  nsReadingIterator<char> end;
   1.122 +  noProxy.BeginReading(pos);
   1.123 +  noProxy.EndReading(end);
   1.124 +  while (pos != end) {
   1.125 +    nsReadingIterator<char> last = pos;
   1.126 +    nsReadingIterator<char> nextPos;
   1.127 +    if (FindCharInReadable(',', last, end)) {
   1.128 +      nextPos = last;
   1.129 +      ++nextPos;
   1.130 +    } else {
   1.131 +      last = end;
   1.132 +      nextPos = end;
   1.133 +    }
   1.134 +    
   1.135 +    nsReadingIterator<char> colon = pos;
   1.136 +    int32_t port = -1;
   1.137 +    if (FindCharInReadable(':', colon, last)) {
   1.138 +      ++colon;
   1.139 +      nsDependentCSubstring portStr(colon, last);
   1.140 +      nsAutoCString portStr2(portStr); // We need this for ToInteger. String API's suck.
   1.141 +      nsresult err;
   1.142 +      port = portStr2.ToInteger(&err);
   1.143 +      if (NS_FAILED(err)) {
   1.144 +        port = -2; // don't match any port, so we ignore this pattern
   1.145 +      }
   1.146 +      --colon;
   1.147 +    } else {
   1.148 +      colon = last;
   1.149 +    }
   1.150 +    
   1.151 +    if (port == -1 || port == aPort) {
   1.152 +      nsDependentCSubstring hostStr(pos, colon);
   1.153 +      // By using StringEndsWith instead of an equality comparator, we can include sub-domains
   1.154 +      if (StringEndsWith(aHost, hostStr, nsCaseInsensitiveCStringComparator()))
   1.155 +        return true;
   1.156 +    }
   1.157 +    
   1.158 +    pos = nextPos;
   1.159 +  }
   1.160 +  
   1.161 +  return false;
   1.162 +}
   1.163 +
   1.164 +static void SetProxyResult(const char* aType, const nsACString& aHost,
   1.165 +                           int32_t aPort, nsACString& aResult)
   1.166 +{
   1.167 +  aResult.AppendASCII(aType);
   1.168 +  aResult.Append(' ');
   1.169 +  aResult.Append(aHost);
   1.170 +  if (aPort > 0) {
   1.171 +    aResult.Append(':');
   1.172 +    aResult.Append(nsPrintfCString("%d", aPort));
   1.173 +  }
   1.174 +}
   1.175 +
   1.176 +static nsresult
   1.177 +GetProxyFromEnvironment(const nsACString& aScheme,
   1.178 +                        const nsACString& aHost,
   1.179 +                        int32_t aPort,
   1.180 +                        nsACString& aResult)
   1.181 +{
   1.182 +  nsAutoCString envVar;
   1.183 +  envVar.Append(aScheme);
   1.184 +  envVar.AppendLiteral("_proxy");
   1.185 +  const char* proxyVal = PR_GetEnv(envVar.get());
   1.186 +  if (!proxyVal) {
   1.187 +    proxyVal = PR_GetEnv("all_proxy");
   1.188 +    if (!proxyVal) {
   1.189 +      // Return failure so that the caller can detect the failure and
   1.190 +      // fall back to other proxy detection (e.g., WPAD)
   1.191 +      return NS_ERROR_FAILURE;
   1.192 +    }
   1.193 +  }
   1.194 +  
   1.195 +  const char* noProxyVal = PR_GetEnv("no_proxy");
   1.196 +  if (noProxyVal && IsInNoProxyList(aHost, aPort, noProxyVal)) {
   1.197 +    aResult.AppendLiteral("DIRECT");
   1.198 +    return NS_OK;
   1.199 +  }
   1.200 +  
   1.201 +  // Use our URI parser to crack the proxy URI
   1.202 +  nsCOMPtr<nsIURI> proxyURI;
   1.203 +  nsresult rv = NS_NewURI(getter_AddRefs(proxyURI), proxyVal);
   1.204 +  NS_ENSURE_SUCCESS(rv, rv);
   1.205 +
   1.206 +  // Is there a way to specify "socks://" or something in these environment
   1.207 +  // variables? I can't find any documentation.
   1.208 +  bool isHTTP;
   1.209 +  rv = proxyURI->SchemeIs("http", &isHTTP);
   1.210 +  NS_ENSURE_SUCCESS(rv, rv);
   1.211 +  if (!isHTTP)
   1.212 +    return NS_ERROR_UNKNOWN_PROTOCOL;
   1.213 +
   1.214 +  nsAutoCString proxyHost;
   1.215 +  rv = proxyURI->GetHost(proxyHost);
   1.216 +  NS_ENSURE_SUCCESS(rv, rv);
   1.217 +
   1.218 +  int32_t proxyPort;
   1.219 +  rv = proxyURI->GetPort(&proxyPort);
   1.220 +  NS_ENSURE_SUCCESS(rv, rv);
   1.221 +
   1.222 +  SetProxyResult("PROXY", proxyHost, proxyPort, aResult);
   1.223 +  return NS_OK;
   1.224 +}
   1.225 +
   1.226 +nsresult
   1.227 +nsUnixSystemProxySettings::SetProxyResultFromGConf(const char* aKeyBase, const char* aType,
   1.228 +                                                   nsACString& aResult)
   1.229 +{
   1.230 +  nsAutoCString hostKey;
   1.231 +  hostKey.AppendASCII(aKeyBase);
   1.232 +  hostKey.AppendLiteral("host");
   1.233 +  nsAutoCString host;
   1.234 +  nsresult rv = mGConf->GetString(hostKey, host);
   1.235 +  NS_ENSURE_SUCCESS(rv, rv);
   1.236 +  if (host.IsEmpty())
   1.237 +    return NS_ERROR_FAILURE;
   1.238 +  
   1.239 +  nsAutoCString portKey;
   1.240 +  portKey.AppendASCII(aKeyBase);
   1.241 +  portKey.AppendLiteral("port");
   1.242 +  int32_t port;
   1.243 +  rv = mGConf->GetInt(portKey, &port);
   1.244 +  NS_ENSURE_SUCCESS(rv, rv);
   1.245 +
   1.246 +  /* When port is 0, proxy is not considered as enabled even if host is set. */
   1.247 +  if (port == 0)
   1.248 +    return NS_ERROR_FAILURE;
   1.249 +
   1.250 +  SetProxyResult(aType, host, port, aResult);
   1.251 +  return NS_OK;
   1.252 +}
   1.253 +
   1.254 +nsresult
   1.255 +nsUnixSystemProxySettings::SetProxyResultFromGSettings(const char* aKeyBase, const char* aType,
   1.256 +                                                       nsACString& aResult)
   1.257 +{
   1.258 +  nsDependentCString key(aKeyBase);
   1.259 +
   1.260 +  nsCOMPtr<nsIGSettingsCollection> proxy_settings = mSchemeProxySettings.Get(key);
   1.261 +  nsresult rv;
   1.262 +  if (!proxy_settings) {
   1.263 +    rv = mGSettings->GetCollectionForSchema(key, getter_AddRefs(proxy_settings));
   1.264 +    NS_ENSURE_SUCCESS(rv, rv);
   1.265 +
   1.266 +    mSchemeProxySettings.Put(key, proxy_settings);
   1.267 +  }
   1.268 +
   1.269 +  nsAutoCString host;
   1.270 +  rv = proxy_settings->GetString(NS_LITERAL_CSTRING("host"), host);
   1.271 +  NS_ENSURE_SUCCESS(rv, rv);
   1.272 +  if (host.IsEmpty())
   1.273 +    return NS_ERROR_FAILURE;
   1.274 +  
   1.275 +  int32_t port;
   1.276 +  rv = proxy_settings->GetInt(NS_LITERAL_CSTRING("port"), &port);
   1.277 +  NS_ENSURE_SUCCESS(rv, rv);
   1.278 +    
   1.279 +  /* When port is 0, proxy is not considered as enabled even if host is set. */
   1.280 +  if (port == 0)
   1.281 +    return NS_ERROR_FAILURE;
   1.282 +
   1.283 +  SetProxyResult(aType, host, port, aResult);
   1.284 +  return NS_OK;
   1.285 +}
   1.286 +
   1.287 +/* copied from nsProtocolProxyService.cpp --- we should share this! */
   1.288 +static void
   1.289 +proxy_MaskIPv6Addr(PRIPv6Addr &addr, uint16_t mask_len)
   1.290 +{
   1.291 +  if (mask_len == 128)
   1.292 +    return;
   1.293 +
   1.294 +  if (mask_len > 96) {
   1.295 +    addr.pr_s6_addr32[3] = PR_htonl(
   1.296 +            PR_ntohl(addr.pr_s6_addr32[3]) & (~0L << (128 - mask_len)));
   1.297 +  }
   1.298 +  else if (mask_len > 64) {
   1.299 +    addr.pr_s6_addr32[3] = 0;
   1.300 +    addr.pr_s6_addr32[2] = PR_htonl(
   1.301 +            PR_ntohl(addr.pr_s6_addr32[2]) & (~0L << (96 - mask_len)));
   1.302 +  }
   1.303 +  else if (mask_len > 32) {
   1.304 +    addr.pr_s6_addr32[3] = 0;
   1.305 +    addr.pr_s6_addr32[2] = 0;
   1.306 +    addr.pr_s6_addr32[1] = PR_htonl(
   1.307 +            PR_ntohl(addr.pr_s6_addr32[1]) & (~0L << (64 - mask_len)));
   1.308 +  }
   1.309 +  else {
   1.310 +    addr.pr_s6_addr32[3] = 0;
   1.311 +    addr.pr_s6_addr32[2] = 0;
   1.312 +    addr.pr_s6_addr32[1] = 0;
   1.313 +    addr.pr_s6_addr32[0] = PR_htonl(
   1.314 +            PR_ntohl(addr.pr_s6_addr32[0]) & (~0L << (32 - mask_len)));
   1.315 +  }
   1.316 +}
   1.317 +
   1.318 +static bool ConvertToIPV6Addr(const nsACString& aName,
   1.319 +                                PRIPv6Addr* aAddr, int32_t* aMask)
   1.320 +{
   1.321 +  PRNetAddr addr;
   1.322 +  // try to convert hostname to IP
   1.323 +  if (PR_StringToNetAddr(PromiseFlatCString(aName).get(), &addr) != PR_SUCCESS)
   1.324 +    return false;
   1.325 +
   1.326 +  // convert parsed address to IPv6
   1.327 +  if (addr.raw.family == PR_AF_INET) {
   1.328 +    // convert to IPv4-mapped address
   1.329 +    PR_ConvertIPv4AddrToIPv6(addr.inet.ip, aAddr);
   1.330 +    if (aMask) {
   1.331 +      if (*aMask <= 32)
   1.332 +        *aMask += 96;
   1.333 +      else
   1.334 +        return false;
   1.335 +    }
   1.336 +  } else if (addr.raw.family == PR_AF_INET6) {
   1.337 +    // copy the address
   1.338 +    memcpy(aAddr, &addr.ipv6.ip, sizeof(PRIPv6Addr));
   1.339 +  } else {
   1.340 +    return false;
   1.341 +  }
   1.342 +  
   1.343 +  return true;
   1.344 +}
   1.345 +
   1.346 +static bool HostIgnoredByProxy(const nsACString& aIgnore,
   1.347 +                               const nsACString& aHost)
   1.348 +{
   1.349 +  if (aIgnore.Equals(aHost, nsCaseInsensitiveCStringComparator()))
   1.350 +    return true;
   1.351 +
   1.352 +  if (aIgnore.First() == '*' &&
   1.353 +      StringEndsWith(aHost, nsDependentCSubstring(aIgnore, 1),
   1.354 +                     nsCaseInsensitiveCStringComparator()))
   1.355 +    return true;
   1.356 +
   1.357 +  int32_t mask = 128;
   1.358 +  nsReadingIterator<char> start;
   1.359 +  nsReadingIterator<char> slash;
   1.360 +  nsReadingIterator<char> end;
   1.361 +  aIgnore.BeginReading(start);
   1.362 +  aIgnore.BeginReading(slash);
   1.363 +  aIgnore.EndReading(end);
   1.364 +  if (FindCharInReadable('/', slash, end)) {
   1.365 +    ++slash;
   1.366 +    nsDependentCSubstring maskStr(slash, end);
   1.367 +    nsAutoCString maskStr2(maskStr);
   1.368 +    nsresult err;
   1.369 +    mask = maskStr2.ToInteger(&err);
   1.370 +    if (NS_FAILED(err)) {
   1.371 +      mask = 128;
   1.372 +    }
   1.373 +    --slash;
   1.374 +  } else {
   1.375 +    slash = end;
   1.376 +  }
   1.377 +
   1.378 +  nsDependentCSubstring ignoreStripped(start, slash);
   1.379 +  PRIPv6Addr ignoreAddr, hostAddr;
   1.380 +  if (!ConvertToIPV6Addr(ignoreStripped, &ignoreAddr, &mask) ||
   1.381 +      !ConvertToIPV6Addr(aHost, &hostAddr, nullptr))
   1.382 +    return false;
   1.383 +
   1.384 +  proxy_MaskIPv6Addr(ignoreAddr, mask);
   1.385 +  proxy_MaskIPv6Addr(hostAddr, mask);
   1.386 +  
   1.387 +  return memcmp(&ignoreAddr, &hostAddr, sizeof(PRIPv6Addr)) == 0;
   1.388 +}
   1.389 +
   1.390 +nsresult
   1.391 +nsUnixSystemProxySettings::GetProxyFromGConf(const nsACString& aScheme,
   1.392 +                                             const nsACString& aHost,
   1.393 +                                             int32_t aPort,
   1.394 +                                             nsACString& aResult)
   1.395 +{
   1.396 +  bool masterProxySwitch = false;
   1.397 +  mGConf->GetBool(NS_LITERAL_CSTRING("/system/http_proxy/use_http_proxy"), &masterProxySwitch);
   1.398 +  // if no proxy is set in GConf return NS_ERROR_FAILURE
   1.399 +  if (!(IsProxyMode("manual") || masterProxySwitch)) {
   1.400 +    return NS_ERROR_FAILURE;
   1.401 +  }
   1.402 +  
   1.403 +  nsCOMPtr<nsIArray> ignoreList;
   1.404 +  if (NS_SUCCEEDED(mGConf->GetStringList(NS_LITERAL_CSTRING("/system/http_proxy/ignore_hosts"),
   1.405 +                                         getter_AddRefs(ignoreList))) && ignoreList) {
   1.406 +    uint32_t len = 0;
   1.407 +    ignoreList->GetLength(&len);
   1.408 +    for (uint32_t i = 0; i < len; ++i) {
   1.409 +      nsCOMPtr<nsISupportsString> str = do_QueryElementAt(ignoreList, i);
   1.410 +      if (str) {
   1.411 +        nsAutoString s;
   1.412 +        if (NS_SUCCEEDED(str->GetData(s)) && !s.IsEmpty()) {
   1.413 +          if (HostIgnoredByProxy(NS_ConvertUTF16toUTF8(s), aHost)) {
   1.414 +            aResult.AppendLiteral("DIRECT");
   1.415 +            return NS_OK;
   1.416 +          }
   1.417 +        }
   1.418 +      }
   1.419 +    }
   1.420 +  }
   1.421 +
   1.422 +  bool useHttpProxyForAll = false;
   1.423 +  // This setting sometimes doesn't exist, don't bail on failure
   1.424 +  mGConf->GetBool(NS_LITERAL_CSTRING("/system/http_proxy/use_same_proxy"), &useHttpProxyForAll);
   1.425 +
   1.426 +  nsresult rv;
   1.427 +  if (!useHttpProxyForAll) {
   1.428 +    rv = SetProxyResultFromGConf("/system/proxy/socks_", "SOCKS", aResult);
   1.429 +    if (NS_SUCCEEDED(rv))
   1.430 +      return rv;
   1.431 +  }
   1.432 +  
   1.433 +  if (aScheme.LowerCaseEqualsLiteral("http") || useHttpProxyForAll) {
   1.434 +    rv = SetProxyResultFromGConf("/system/http_proxy/", "PROXY", aResult);
   1.435 +  } else if (aScheme.LowerCaseEqualsLiteral("https")) {
   1.436 +    rv = SetProxyResultFromGConf("/system/proxy/secure_", "PROXY", aResult);
   1.437 +  } else if (aScheme.LowerCaseEqualsLiteral("ftp")) {
   1.438 +    rv = SetProxyResultFromGConf("/system/proxy/ftp_", "PROXY", aResult);
   1.439 +  } else {
   1.440 +    rv = NS_ERROR_FAILURE;
   1.441 +  }
   1.442 +  
   1.443 +  return rv;
   1.444 +}
   1.445 +
   1.446 +nsresult
   1.447 +nsUnixSystemProxySettings::GetProxyFromGSettings(const nsACString& aScheme,
   1.448 +                                                 const nsACString& aHost,
   1.449 +                                                 int32_t aPort,
   1.450 +                                                 nsACString& aResult)
   1.451 +{
   1.452 +  nsCString proxyMode; 
   1.453 +  nsresult rv = mProxySettings->GetString(NS_LITERAL_CSTRING("mode"), proxyMode);
   1.454 +  NS_ENSURE_SUCCESS(rv, rv);
   1.455 +  
   1.456 +  // return NS_ERROR_FAILURE when no proxy is set
   1.457 +  if (!proxyMode.Equals("manual")) {
   1.458 +    return NS_ERROR_FAILURE;
   1.459 +  }
   1.460 +
   1.461 +  nsCOMPtr<nsIArray> ignoreList;
   1.462 +  if (NS_SUCCEEDED(mProxySettings->GetStringList(NS_LITERAL_CSTRING("ignore-hosts"),
   1.463 +                                                 getter_AddRefs(ignoreList))) && ignoreList) {
   1.464 +    uint32_t len = 0;
   1.465 +    ignoreList->GetLength(&len);
   1.466 +    for (uint32_t i = 0; i < len; ++i) {
   1.467 +      nsCOMPtr<nsISupportsCString> str = do_QueryElementAt(ignoreList, i);
   1.468 +      if (str) {
   1.469 +        nsCString s;
   1.470 +        if (NS_SUCCEEDED(str->GetData(s)) && !s.IsEmpty()) {
   1.471 +          if (HostIgnoredByProxy(s, aHost)) {
   1.472 +            aResult.AppendLiteral("DIRECT");
   1.473 +            return NS_OK;
   1.474 +          }
   1.475 +        }
   1.476 +      }
   1.477 +    }
   1.478 +  }
   1.479 +
   1.480 +  if (aScheme.LowerCaseEqualsLiteral("http")) {
   1.481 +    rv = SetProxyResultFromGSettings("org.gnome.system.proxy.http", "PROXY", aResult);
   1.482 +  } else if (aScheme.LowerCaseEqualsLiteral("https")) {
   1.483 +    rv = SetProxyResultFromGSettings("org.gnome.system.proxy.https", "PROXY", aResult);
   1.484 +    /* Try to use HTTP proxy when HTTPS proxy is not explicitly defined */
   1.485 +    if (rv != NS_OK) 
   1.486 +      rv = SetProxyResultFromGSettings("org.gnome.system.proxy.http", "PROXY", aResult);
   1.487 +  } else if (aScheme.LowerCaseEqualsLiteral("ftp")) {
   1.488 +    rv = SetProxyResultFromGSettings("org.gnome.system.proxy.ftp", "PROXY", aResult);
   1.489 +  } else {
   1.490 +    rv = NS_ERROR_FAILURE;
   1.491 +  }
   1.492 +  if (rv != NS_OK) {
   1.493 +     /* If proxy for scheme is not specified, use SOCKS proxy for all schemes */
   1.494 +     rv = SetProxyResultFromGSettings("org.gnome.system.proxy.socks", "SOCKS", aResult);
   1.495 +  }
   1.496 +  
   1.497 +  if (NS_FAILED(rv)) {
   1.498 +    aResult.AppendLiteral("DIRECT");
   1.499 +  }
   1.500 +  
   1.501 +  return NS_OK;
   1.502 +}
   1.503 +
   1.504 +nsresult
   1.505 +nsUnixSystemProxySettings::GetProxyForURI(const nsACString & aSpec,
   1.506 +                                          const nsACString & aScheme,
   1.507 +                                          const nsACString & aHost,
   1.508 +                                          const int32_t      aPort,
   1.509 +                                          nsACString & aResult)
   1.510 +{
   1.511 +  if (mProxySettings) {
   1.512 +    nsresult rv = GetProxyFromGSettings(aScheme, aHost, aPort, aResult);
   1.513 +    if (NS_SUCCEEDED(rv))
   1.514 +      return rv;
   1.515 +  }
   1.516 +  if (mGConf)
   1.517 +    return GetProxyFromGConf(aScheme, aHost, aPort, aResult);
   1.518 +
   1.519 +  return GetProxyFromEnvironment(aScheme, aHost, aPort, aResult);
   1.520 +}
   1.521 +
   1.522 +#define NS_UNIXSYSTEMPROXYSERVICE_CID  /* 0fa3158c-d5a7-43de-9181-a285e74cf1d4 */\
   1.523 +     { 0x0fa3158c, 0xd5a7, 0x43de, \
   1.524 +       {0x91, 0x81, 0xa2, 0x85, 0xe7, 0x4c, 0xf1, 0xd4 } }
   1.525 +
   1.526 +NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsUnixSystemProxySettings, Init)
   1.527 +NS_DEFINE_NAMED_CID(NS_UNIXSYSTEMPROXYSERVICE_CID);
   1.528 +
   1.529 +static const mozilla::Module::CIDEntry kUnixProxyCIDs[] = {
   1.530 +  { &kNS_UNIXSYSTEMPROXYSERVICE_CID, false, nullptr, nsUnixSystemProxySettingsConstructor },
   1.531 +  { nullptr }
   1.532 +};
   1.533 +
   1.534 +static const mozilla::Module::ContractIDEntry kUnixProxyContracts[] = {
   1.535 +  { NS_SYSTEMPROXYSETTINGS_CONTRACTID, &kNS_UNIXSYSTEMPROXYSERVICE_CID },
   1.536 +  { nullptr }
   1.537 +};
   1.538 +
   1.539 +static const mozilla::Module kUnixProxyModule = {
   1.540 +  mozilla::Module::kVersion,
   1.541 +  kUnixProxyCIDs,
   1.542 +  kUnixProxyContracts
   1.543 +};
   1.544 +
   1.545 +NSMODULE_DEFN(nsUnixProxyModule) = &kUnixProxyModule;

mercurial