Bug 458256. Use LoadLibraryW instead of LoadLibrary (patch by DougT). r+sr=vlad
[wine-gecko.git] / netwerk / protocol / http / src / nsHttpAuthManager.cpp
blob4238774fdb588b1ddcaeae9187e561932471ead1
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is Sun Microsystem.
18 * Portions created by Sun Microsystem are Copyright (C) 2003 by
19 * Sun Microsystem. All Rights Reserved.
21 * Contributor(s):
22 * Louie Zhao <louie.zhao@sun.com>
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "nsHttpHandler.h"
39 #include "nsHttpChannel.h"
40 #include "nsHttpAuthManager.h"
41 #include "nsReadableUtils.h"
42 #include "nsNetUtil.h"
44 NS_IMPL_ISUPPORTS1(nsHttpAuthManager, nsIHttpAuthManager)
46 nsHttpAuthManager::nsHttpAuthManager()
50 nsresult nsHttpAuthManager::Init()
52 // get reference to the auth cache. we assume that we will live
53 // as long as gHttpHandler. instantiate it if necessary.
55 if (!gHttpHandler) {
56 nsresult rv;
57 nsCOMPtr<nsIIOService> ios = do_GetIOService(&rv);
58 if (NS_FAILED(rv))
59 return rv;
61 nsCOMPtr<nsIProtocolHandler> handler;
62 rv = ios->GetProtocolHandler("http", getter_AddRefs(handler));
63 if (NS_FAILED(rv))
64 return rv;
66 // maybe someone is overriding our HTTP handler implementation?
67 NS_ENSURE_TRUE(gHttpHandler, NS_ERROR_UNEXPECTED);
70 mAuthCache = gHttpHandler->AuthCache();
71 NS_ENSURE_TRUE(mAuthCache, NS_ERROR_FAILURE);
72 return NS_OK;
75 nsHttpAuthManager::~nsHttpAuthManager()
79 NS_IMETHODIMP
80 nsHttpAuthManager::GetAuthIdentity(const nsACString & aScheme,
81 const nsACString & aHost,
82 PRInt32 aPort,
83 const nsACString & aAuthType,
84 const nsACString & aRealm,
85 const nsACString & aPath,
86 nsAString & aUserDomain,
87 nsAString & aUserName,
88 nsAString & aUserPassword)
90 nsHttpAuthEntry * entry = nsnull;
91 nsresult rv;
92 if (!aPath.IsEmpty())
93 rv = mAuthCache->GetAuthEntryForPath(PromiseFlatCString(aScheme).get(),
94 PromiseFlatCString(aHost).get(),
95 aPort,
96 PromiseFlatCString(aPath).get(),
97 &entry);
98 else
99 rv = mAuthCache->GetAuthEntryForDomain(PromiseFlatCString(aScheme).get(),
100 PromiseFlatCString(aHost).get(),
101 aPort,
102 PromiseFlatCString(aRealm).get(),
103 &entry);
105 if (NS_FAILED(rv))
106 return rv;
107 if (!entry)
108 return NS_ERROR_UNEXPECTED;
110 aUserDomain.Assign(entry->Domain());
111 aUserName.Assign(entry->User());
112 aUserPassword.Assign(entry->Pass());
113 return NS_OK;
116 NS_IMETHODIMP
117 nsHttpAuthManager::SetAuthIdentity(const nsACString & aScheme,
118 const nsACString & aHost,
119 PRInt32 aPort,
120 const nsACString & aAuthType,
121 const nsACString & aRealm,
122 const nsACString & aPath,
123 const nsAString & aUserDomain,
124 const nsAString & aUserName,
125 const nsAString & aUserPassword)
127 nsHttpAuthIdentity ident(PromiseFlatString(aUserDomain).get(),
128 PromiseFlatString(aUserName).get(),
129 PromiseFlatString(aUserPassword).get());
131 return mAuthCache->SetAuthEntry(PromiseFlatCString(aScheme).get(),
132 PromiseFlatCString(aHost).get(),
133 aPort,
134 PromiseFlatCString(aPath).get(),
135 PromiseFlatCString(aRealm).get(),
136 nsnull, // credentials
137 nsnull, // challenge
138 ident,
139 nsnull); // metadata
142 NS_IMETHODIMP
143 nsHttpAuthManager::ClearAll()
145 return mAuthCache->ClearAll();