embedding/components/windowwatcher/public/nsPromptUtils.h

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

mercurial