Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / netwerk / protocol / http / src / nsHttpAuthCache.h
blobb308d8def80265f6fd08a4c51a17e80f48eb4681
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
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
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__
43 #include "nsHttp.h"
44 #include "nsError.h"
45 #include "nsVoidArray.h"
46 #include "nsAString.h"
47 #include "nsString.h"
48 #include "nsCOMPtr.h"
49 #include "plhash.h"
50 #include "nsCRT.h"
53 struct nsHttpAuthPath {
54 struct nsHttpAuthPath *mNext;
55 char mPath[1];
58 //-----------------------------------------------------------------------------
59 // nsHttpAuthIdentity
60 //-----------------------------------------------------------------------------
62 class nsHttpAuthIdentity
64 public:
65 nsHttpAuthIdentity()
66 : mUser(nsnull)
67 , mPass(nsnull)
68 , mDomain(nsnull)
71 nsHttpAuthIdentity(const PRUnichar *domain,
72 const PRUnichar *user,
73 const PRUnichar *password)
74 : mUser(nsnull)
76 Set(domain, user, password);
78 ~nsHttpAuthIdentity()
80 Clear();
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); }
91 void Clear();
93 PRBool Equals(const nsHttpAuthIdentity &other) const;
94 PRBool IsEmpty() const { return !mUser; }
96 private:
97 // allocated as one contiguous blob, starting at mUser.
98 PRUnichar *mUser;
99 PRUnichar *mPass;
100 PRUnichar *mDomain;
103 //-----------------------------------------------------------------------------
104 // nsHttpAuthEntry
105 //-----------------------------------------------------------------------------
107 class nsHttpAuthEntry
109 public:
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;
124 private:
125 nsHttpAuthEntry(const char *path,
126 const char *realm,
127 const char *creds,
128 const char *challenge,
129 const nsHttpAuthIdentity &ident,
130 nsISupports *metadata)
131 : mRoot(nsnull)
132 , mTail(nsnull)
133 , mRealm(nsnull)
135 Set(path, realm, creds, challenge, ident, metadata);
137 ~nsHttpAuthEntry();
139 nsresult Set(const char *path,
140 const char *realm,
141 const char *creds,
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.
152 char *mRealm;
153 char *mCreds;
154 char *mChallenge;
156 friend class nsHttpAuthNode;
157 friend class nsHttpAuthCache;
160 //-----------------------------------------------------------------------------
161 // nsHttpAuthNode
162 //-----------------------------------------------------------------------------
164 class nsHttpAuthNode
166 private:
167 nsHttpAuthNode();
168 ~nsHttpAuthNode();
170 // path can be null, in which case we'll search for an entry
171 // with a null path.
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,
179 const char *realm,
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(); }
189 private:
190 nsVoidArray mList; // list of nsHttpAuthEntry objects
192 friend class nsHttpAuthCache;
195 //-----------------------------------------------------------------------------
196 // nsHttpAuthCache
197 // (holds a hash table from host:port to nsHttpAuthNode)
198 //-----------------------------------------------------------------------------
200 class nsHttpAuthCache
202 public:
203 nsHttpAuthCache();
204 ~nsHttpAuthCache();
206 nsresult Init();
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,
212 const char *host,
213 PRInt32 port,
214 const char *path,
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,
221 const char *host,
222 PRInt32 port,
223 const char *realm,
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,
232 const char *host,
233 PRInt32 port,
234 const char *directory,
235 const char *realm,
236 const char *credentials,
237 const char *challenge,
238 const nsHttpAuthIdentity &ident,
239 nsISupports *metadata);
241 void ClearAuthEntry(const char *scheme,
242 const char *host,
243 PRInt32 port,
244 const char *realm);
246 // expire all existing auth list entries including proxy auths.
247 nsresult ClearAll();
249 private:
250 nsHttpAuthNode *LookupAuthNode(const char *scheme,
251 const char *host,
252 PRInt32 port,
253 nsCString &key);
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;
263 private:
264 PLHashTable *mDB; // "host:port" --> nsHttpAuthNode
267 #endif // nsHttpAuthCache_h__