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
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
23 * Gagan Saksena <gagan@netscape.com> (original author)
24 * Darin Fisher <darin@netscape.com>
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 #ifndef nsHttpAuthCache_h__
41 #define nsHttpAuthCache_h__
45 #include "nsVoidArray.h"
46 #include "nsAString.h"
53 struct nsHttpAuthPath
{
54 struct nsHttpAuthPath
*mNext
;
58 //-----------------------------------------------------------------------------
60 //-----------------------------------------------------------------------------
62 class nsHttpAuthIdentity
71 nsHttpAuthIdentity(const PRUnichar
*domain
,
72 const PRUnichar
*user
,
73 const PRUnichar
*password
)
76 Set(domain
, user
, password
);
83 const PRUnichar
*Domain() const { return mDomain
; }
84 const PRUnichar
*User() const { return mUser
; }
85 const PRUnichar
*Password() const { return mPass
; }
87 nsresult
Set(const PRUnichar
*domain
,
88 const PRUnichar
*user
,
89 const PRUnichar
*password
);
90 nsresult
Set(const nsHttpAuthIdentity
&other
) { return Set(other
.mDomain
, other
.mUser
, other
.mPass
); }
93 PRBool
Equals(const nsHttpAuthIdentity
&other
) const;
94 PRBool
IsEmpty() const { return !mUser
; }
97 // allocated as one contiguous blob, starting at mUser.
103 //-----------------------------------------------------------------------------
105 //-----------------------------------------------------------------------------
107 class nsHttpAuthEntry
110 const char *Realm() const { return mRealm
; }
111 const char *Creds() const { return mCreds
; }
112 const char *Challenge() const { return mChallenge
; }
113 const PRUnichar
*Domain() const { return mIdent
.Domain(); }
114 const PRUnichar
*User() const { return mIdent
.User(); }
115 const PRUnichar
*Pass() const { return mIdent
.Password(); }
116 nsHttpAuthPath
*RootPath() { return mRoot
; }
118 const nsHttpAuthIdentity
&Identity() const { return mIdent
; }
120 nsresult
AddPath(const char *aPath
);
122 nsCOMPtr
<nsISupports
> mMetaData
;
125 nsHttpAuthEntry(const char *path
,
128 const char *challenge
,
129 const nsHttpAuthIdentity
&ident
,
130 nsISupports
*metadata
)
135 Set(path
, realm
, creds
, challenge
, ident
, metadata
);
139 nsresult
Set(const char *path
,
142 const char *challenge
,
143 const nsHttpAuthIdentity
&ident
,
144 nsISupports
*metadata
);
146 nsHttpAuthIdentity mIdent
;
148 nsHttpAuthPath
*mRoot
; //root pointer
149 nsHttpAuthPath
*mTail
; //tail pointer
151 // allocated together in one blob, starting with mRealm.
156 friend class nsHttpAuthNode
;
157 friend class nsHttpAuthCache
;
160 //-----------------------------------------------------------------------------
162 //-----------------------------------------------------------------------------
170 // path can be null, in which case we'll search for an entry
172 nsHttpAuthEntry
*LookupEntryByPath(const char *path
);
174 // realm must not be null
175 nsHttpAuthEntry
*LookupEntryByRealm(const char *realm
);
177 // if a matching entry is found, then credentials will be changed.
178 nsresult
SetAuthEntry(const char *path
,
180 const char *credentials
,
181 const char *challenge
,
182 const nsHttpAuthIdentity
&ident
,
183 nsISupports
*metadata
);
185 void ClearAuthEntry(const char *realm
);
187 PRUint32
EntryCount() { return (PRUint32
) mList
.Count(); }
190 nsVoidArray mList
; // list of nsHttpAuthEntry objects
192 friend class nsHttpAuthCache
;
195 //-----------------------------------------------------------------------------
197 // (holds a hash table from host:port to nsHttpAuthNode)
198 //-----------------------------------------------------------------------------
200 class nsHttpAuthCache
208 // |scheme|, |host|, and |port| are required
209 // |path| can be null
210 // |entry| is either null or a weak reference
211 nsresult
GetAuthEntryForPath(const char *scheme
,
215 nsHttpAuthEntry
**entry
);
217 // |scheme|, |host|, and |port| are required
218 // |realm| must not be null
219 // |entry| is either null or a weak reference
220 nsresult
GetAuthEntryForDomain(const char *scheme
,
224 nsHttpAuthEntry
**entry
);
226 // |scheme|, |host|, and |port| are required
227 // |path| can be null
228 // |realm| must not be null
229 // if |credentials|, |user|, |pass|, and |challenge| are each
230 // null, then the entry is deleted.
231 nsresult
SetAuthEntry(const char *scheme
,
234 const char *directory
,
236 const char *credentials
,
237 const char *challenge
,
238 const nsHttpAuthIdentity
&ident
,
239 nsISupports
*metadata
);
241 void ClearAuthEntry(const char *scheme
,
246 // expire all existing auth list entries including proxy auths.
250 nsHttpAuthNode
*LookupAuthNode(const char *scheme
,
255 // hash table allocation functions
256 static void* AllocTable(void *, PRSize size
);
257 static void FreeTable(void *, void *item
);
258 static PLHashEntry
* AllocEntry(void *, const void *key
);
259 static void FreeEntry(void *, PLHashEntry
*he
, PRUintn flag
);
261 static PLHashAllocOps gHashAllocOps
;
264 PLHashTable
*mDB
; // "host:port" --> nsHttpAuthNode
267 #endif // nsHttpAuthCache_h__