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