netwerk/protocol/http/nsHttpAuthManager.cpp

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

     1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 // HttpLog.h should generally be included first
     7 #include "HttpLog.h"
     9 #include "nsHttpHandler.h"
    10 #include "nsHttpAuthManager.h"
    11 #include "nsNetUtil.h"
    12 #include "nsIPrincipal.h"
    14 namespace mozilla {
    15 namespace net {
    17 NS_IMPL_ISUPPORTS(nsHttpAuthManager, nsIHttpAuthManager)
    19 nsHttpAuthManager::nsHttpAuthManager()
    20 {
    21 }
    23 nsresult nsHttpAuthManager::Init()
    24 {
    25   // get reference to the auth cache.  we assume that we will live
    26   // as long as gHttpHandler.  instantiate it if necessary.
    28   if (!gHttpHandler) {
    29     nsresult rv;
    30     nsCOMPtr<nsIIOService> ios = do_GetIOService(&rv);
    31     if (NS_FAILED(rv))
    32       return rv;
    34     nsCOMPtr<nsIProtocolHandler> handler;
    35     rv = ios->GetProtocolHandler("http", getter_AddRefs(handler));
    36     if (NS_FAILED(rv))
    37       return rv;
    39     // maybe someone is overriding our HTTP handler implementation?
    40     NS_ENSURE_TRUE(gHttpHandler, NS_ERROR_UNEXPECTED);
    41   }
    43   mAuthCache = gHttpHandler->AuthCache(false);
    44   mPrivateAuthCache = gHttpHandler->AuthCache(true);
    45   NS_ENSURE_TRUE(mAuthCache, NS_ERROR_FAILURE);
    46   NS_ENSURE_TRUE(mPrivateAuthCache, NS_ERROR_FAILURE);
    47   return NS_OK;
    48 }
    50 nsHttpAuthManager::~nsHttpAuthManager()
    51 {
    52 }
    54 NS_IMETHODIMP
    55 nsHttpAuthManager::GetAuthIdentity(const nsACString & aScheme,
    56                                    const nsACString & aHost,
    57                                    int32_t aPort,
    58                                    const nsACString & aAuthType,
    59                                    const nsACString & aRealm,
    60                                    const nsACString & aPath,
    61                                    nsAString & aUserDomain,
    62                                    nsAString & aUserName,
    63                                    nsAString & aUserPassword,
    64                                    bool aIsPrivate,
    65                                    nsIPrincipal* aPrincipal)
    66 {
    67   nsHttpAuthCache* auth_cache = aIsPrivate ? mPrivateAuthCache : mAuthCache;
    68   nsHttpAuthEntry * entry = nullptr;
    69   nsresult rv;
    70   uint32_t appId = NECKO_NO_APP_ID;
    71   bool inBrowserElement = false;
    72   if (aPrincipal) {
    73     appId = aPrincipal->GetAppId();
    74     inBrowserElement = aPrincipal->GetIsInBrowserElement();
    75   }
    77   if (!aPath.IsEmpty())
    78     rv = auth_cache->GetAuthEntryForPath(PromiseFlatCString(aScheme).get(),
    79                                          PromiseFlatCString(aHost).get(),
    80                                          aPort,
    81                                          PromiseFlatCString(aPath).get(),
    82                                          appId, inBrowserElement,
    83                                          &entry);
    84   else
    85     rv = auth_cache->GetAuthEntryForDomain(PromiseFlatCString(aScheme).get(),
    86                                            PromiseFlatCString(aHost).get(),
    87                                            aPort,
    88                                            PromiseFlatCString(aRealm).get(),
    89                                            appId, inBrowserElement,
    90                                            &entry);
    92   if (NS_FAILED(rv))
    93     return rv;
    94   if (!entry)
    95     return NS_ERROR_UNEXPECTED;
    97   aUserDomain.Assign(entry->Domain());
    98   aUserName.Assign(entry->User());
    99   aUserPassword.Assign(entry->Pass());
   100   return NS_OK;
   101 }
   103 NS_IMETHODIMP
   104 nsHttpAuthManager::SetAuthIdentity(const nsACString & aScheme,
   105                                    const nsACString & aHost,
   106                                    int32_t aPort,
   107                                    const nsACString & aAuthType,
   108                                    const nsACString & aRealm,
   109                                    const nsACString & aPath,
   110                                    const nsAString & aUserDomain,
   111                                    const nsAString & aUserName,
   112                                    const nsAString & aUserPassword,
   113                                    bool aIsPrivate,
   114                                    nsIPrincipal* aPrincipal)
   115 {
   116   nsHttpAuthIdentity ident(PromiseFlatString(aUserDomain).get(),
   117                            PromiseFlatString(aUserName).get(),
   118                            PromiseFlatString(aUserPassword).get());
   120   uint32_t appId = NECKO_NO_APP_ID;
   121   bool inBrowserElement = false;
   122   if (aPrincipal) {
   123     appId = aPrincipal->GetAppId();
   124     inBrowserElement = aPrincipal->GetIsInBrowserElement();
   125   }
   127   nsHttpAuthCache* auth_cache = aIsPrivate ? mPrivateAuthCache : mAuthCache;
   128   return auth_cache->SetAuthEntry(PromiseFlatCString(aScheme).get(),
   129                                   PromiseFlatCString(aHost).get(),
   130                                   aPort,
   131                                   PromiseFlatCString(aPath).get(),
   132                                   PromiseFlatCString(aRealm).get(),
   133                                   nullptr,  // credentials
   134                                   nullptr,  // challenge
   135                                   appId, inBrowserElement,
   136                                   &ident,
   137                                   nullptr); // metadata
   138 }
   140 NS_IMETHODIMP
   141 nsHttpAuthManager::ClearAll()
   142 {
   143   nsresult rv = mAuthCache->ClearAll();
   144   nsresult rv2 = mPrivateAuthCache->ClearAll();
   145   if (NS_FAILED(rv))
   146     return rv;
   147   if (NS_FAILED(rv2))
   148     return rv2;
   149   return NS_OK;
   150 }
   152 } // namespace mozilla::net
   153 } // namespace mozilla

mercurial