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 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #ifndef NSPROMPTUTILS_H_ |
michael@0 | 6 | #define NSPROMPTUTILS_H_ |
michael@0 | 7 | |
michael@0 | 8 | #include "nsIHttpChannel.h" |
michael@0 | 9 | |
michael@0 | 10 | /** |
michael@0 | 11 | * @file |
michael@0 | 12 | * This file defines some helper functions that simplify interaction |
michael@0 | 13 | * with authentication prompts. |
michael@0 | 14 | */ |
michael@0 | 15 | |
michael@0 | 16 | /** |
michael@0 | 17 | * Given a username (possibly in DOMAIN\user form) and password, parses the |
michael@0 | 18 | * domain out of the username if necessary and sets domain, username and |
michael@0 | 19 | * password on the auth information object. |
michael@0 | 20 | */ |
michael@0 | 21 | inline void |
michael@0 | 22 | NS_SetAuthInfo(nsIAuthInformation* aAuthInfo, const nsString& user, |
michael@0 | 23 | const nsString& password) |
michael@0 | 24 | { |
michael@0 | 25 | uint32_t flags; |
michael@0 | 26 | aAuthInfo->GetFlags(&flags); |
michael@0 | 27 | if (flags & nsIAuthInformation::NEED_DOMAIN) { |
michael@0 | 28 | // Domain is separated from username by a backslash |
michael@0 | 29 | int32_t idx = user.FindChar(char16_t('\\')); |
michael@0 | 30 | if (idx == kNotFound) { |
michael@0 | 31 | aAuthInfo->SetUsername(user); |
michael@0 | 32 | } else { |
michael@0 | 33 | aAuthInfo->SetDomain(Substring(user, 0, idx)); |
michael@0 | 34 | aAuthInfo->SetUsername(Substring(user, idx + 1)); |
michael@0 | 35 | } |
michael@0 | 36 | } else { |
michael@0 | 37 | aAuthInfo->SetUsername(user); |
michael@0 | 38 | } |
michael@0 | 39 | aAuthInfo->SetPassword(password); |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | /** |
michael@0 | 43 | * Gets the host and port from a channel and authentication info. This is the |
michael@0 | 44 | * "logical" host and port for this authentication, i.e. for a proxy |
michael@0 | 45 | * authentication it refers to the proxy, while for a host authentication it |
michael@0 | 46 | * is the actual host. |
michael@0 | 47 | * |
michael@0 | 48 | * @param machineProcessing |
michael@0 | 49 | * When this parameter is true, the host will be returned in ASCII |
michael@0 | 50 | * (instead of UTF-8; this is relevant when IDN is used). In addition, |
michael@0 | 51 | * the port will be returned as the real port even when it was not |
michael@0 | 52 | * explicitly specified (when false, the port will be returned as -1 in |
michael@0 | 53 | * this case) |
michael@0 | 54 | */ |
michael@0 | 55 | inline void |
michael@0 | 56 | NS_GetAuthHostPort(nsIChannel* aChannel, nsIAuthInformation* aAuthInfo, |
michael@0 | 57 | bool machineProcessing, nsCString& host, int32_t* port) |
michael@0 | 58 | { |
michael@0 | 59 | nsCOMPtr<nsIURI> uri; |
michael@0 | 60 | nsresult rv = aChannel->GetURI(getter_AddRefs(uri)); |
michael@0 | 61 | if (NS_FAILED(rv)) |
michael@0 | 62 | return; |
michael@0 | 63 | |
michael@0 | 64 | // Have to distinguish proxy auth and host auth here... |
michael@0 | 65 | uint32_t flags; |
michael@0 | 66 | aAuthInfo->GetFlags(&flags); |
michael@0 | 67 | if (flags & nsIAuthInformation::AUTH_PROXY) { |
michael@0 | 68 | nsCOMPtr<nsIProxiedChannel> proxied(do_QueryInterface(aChannel)); |
michael@0 | 69 | NS_ASSERTION(proxied, "proxy auth needs nsIProxiedChannel"); |
michael@0 | 70 | |
michael@0 | 71 | nsCOMPtr<nsIProxyInfo> info; |
michael@0 | 72 | proxied->GetProxyInfo(getter_AddRefs(info)); |
michael@0 | 73 | NS_ASSERTION(info, "proxy auth needs nsIProxyInfo"); |
michael@0 | 74 | |
michael@0 | 75 | nsAutoCString idnhost; |
michael@0 | 76 | info->GetHost(idnhost); |
michael@0 | 77 | info->GetPort(port); |
michael@0 | 78 | |
michael@0 | 79 | if (machineProcessing) { |
michael@0 | 80 | nsCOMPtr<nsIIDNService> idnService = |
michael@0 | 81 | do_GetService(NS_IDNSERVICE_CONTRACTID); |
michael@0 | 82 | if (idnService) { |
michael@0 | 83 | idnService->ConvertUTF8toACE(idnhost, host); |
michael@0 | 84 | } else { |
michael@0 | 85 | // Not much we can do here... |
michael@0 | 86 | host = idnhost; |
michael@0 | 87 | } |
michael@0 | 88 | } else { |
michael@0 | 89 | host = idnhost; |
michael@0 | 90 | } |
michael@0 | 91 | } else { |
michael@0 | 92 | if (machineProcessing) { |
michael@0 | 93 | uri->GetAsciiHost(host); |
michael@0 | 94 | *port = NS_GetRealPort(uri); |
michael@0 | 95 | } else { |
michael@0 | 96 | uri->GetHost(host); |
michael@0 | 97 | uri->GetPort(port); |
michael@0 | 98 | } |
michael@0 | 99 | } |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | /** |
michael@0 | 103 | * Creates the key for looking up passwords in the password manager. This |
michael@0 | 104 | * function uses the same format that Gecko functions have always used, thus |
michael@0 | 105 | * ensuring backwards compatibility. |
michael@0 | 106 | */ |
michael@0 | 107 | inline void |
michael@0 | 108 | NS_GetAuthKey(nsIChannel* aChannel, nsIAuthInformation* aAuthInfo, |
michael@0 | 109 | nsCString& key) |
michael@0 | 110 | { |
michael@0 | 111 | // HTTP does this differently from other protocols |
michael@0 | 112 | nsCOMPtr<nsIHttpChannel> http(do_QueryInterface(aChannel)); |
michael@0 | 113 | if (!http) { |
michael@0 | 114 | nsCOMPtr<nsIURI> uri; |
michael@0 | 115 | aChannel->GetURI(getter_AddRefs(uri)); |
michael@0 | 116 | uri->GetPrePath(key); |
michael@0 | 117 | return; |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | // NOTE: For backwards-compatibility reasons, this must be the ASCII host. |
michael@0 | 121 | nsCString host; |
michael@0 | 122 | int32_t port = -1; |
michael@0 | 123 | |
michael@0 | 124 | NS_GetAuthHostPort(aChannel, aAuthInfo, true, host, &port); |
michael@0 | 125 | |
michael@0 | 126 | nsAutoString realm; |
michael@0 | 127 | aAuthInfo->GetRealm(realm); |
michael@0 | 128 | |
michael@0 | 129 | // Now assemble the key: host:port (realm) |
michael@0 | 130 | key.Append(host); |
michael@0 | 131 | key.Append(':'); |
michael@0 | 132 | key.AppendInt(port); |
michael@0 | 133 | key.AppendLiteral(" ("); |
michael@0 | 134 | AppendUTF16toUTF8(realm, key); |
michael@0 | 135 | key.Append(')'); |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | #endif |
michael@0 | 139 |