Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : #ifndef nsHttpAuthCache_h__
7 : #define nsHttpAuthCache_h__
8 :
9 : #include "nsError.h"
10 : #include "nsTArray.h"
11 : #include "nsAutoPtr.h"
12 : #include "nsCOMPtr.h"
13 : #include "plhash.h"
14 : #include "nsIObserver.h"
15 :
16 : class nsCString;
17 :
18 : namespace mozilla {
19 :
20 : class OriginAttributesPattern;
21 :
22 : namespace net {
23 :
24 : struct nsHttpAuthPath {
25 : struct nsHttpAuthPath *mNext;
26 : char mPath[1];
27 : };
28 :
29 : //-----------------------------------------------------------------------------
30 : // nsHttpAuthIdentity
31 : //-----------------------------------------------------------------------------
32 :
33 : class nsHttpAuthIdentity
34 : {
35 : public:
36 12 : nsHttpAuthIdentity()
37 12 : : mUser(nullptr)
38 : , mPass(nullptr)
39 12 : , mDomain(nullptr)
40 : {
41 12 : }
42 0 : nsHttpAuthIdentity(const char16_t *domain,
43 : const char16_t *user,
44 : const char16_t *password)
45 0 : : mUser(nullptr)
46 : {
47 0 : DebugOnly<nsresult> rv = Set(domain, user, password);
48 0 : MOZ_ASSERT(NS_SUCCEEDED(rv));
49 0 : }
50 12 : ~nsHttpAuthIdentity()
51 12 : {
52 12 : Clear();
53 12 : }
54 :
55 0 : const char16_t *Domain() const { return mDomain; }
56 0 : const char16_t *User() const { return mUser; }
57 0 : const char16_t *Password() const { return mPass; }
58 :
59 : MOZ_MUST_USE nsresult Set(const char16_t *domain,
60 : const char16_t *user,
61 : const char16_t *password);
62 0 : MOZ_MUST_USE nsresult Set(const nsHttpAuthIdentity &other)
63 : {
64 0 : return Set(other.mDomain, other.mUser, other.mPass);
65 : }
66 : void Clear();
67 :
68 : bool Equals(const nsHttpAuthIdentity &other) const;
69 0 : bool IsEmpty() const { return !mUser; }
70 :
71 : private:
72 : // allocated as one contiguous blob, starting at mUser.
73 : char16_t *mUser;
74 : char16_t *mPass;
75 : char16_t *mDomain;
76 : };
77 :
78 : //-----------------------------------------------------------------------------
79 : // nsHttpAuthEntry
80 : //-----------------------------------------------------------------------------
81 :
82 : class nsHttpAuthEntry
83 : {
84 : public:
85 0 : const char *Realm() const { return mRealm; }
86 0 : const char *Creds() const { return mCreds; }
87 0 : const char *Challenge() const { return mChallenge; }
88 0 : const char16_t *Domain() const { return mIdent.Domain(); }
89 0 : const char16_t *User() const { return mIdent.User(); }
90 0 : const char16_t *Pass() const { return mIdent.Password(); }
91 0 : nsHttpAuthPath *RootPath() { return mRoot; }
92 :
93 0 : const nsHttpAuthIdentity &Identity() const { return mIdent; }
94 :
95 : MOZ_MUST_USE nsresult AddPath(const char *aPath);
96 :
97 : nsCOMPtr<nsISupports> mMetaData;
98 :
99 : private:
100 0 : nsHttpAuthEntry(const char *path,
101 : const char *realm,
102 : const char *creds,
103 : const char *challenge,
104 : const nsHttpAuthIdentity *ident,
105 : nsISupports *metadata)
106 0 : : mRoot(nullptr)
107 : , mTail(nullptr)
108 0 : , mRealm(nullptr)
109 : {
110 0 : DebugOnly<nsresult> rv = Set(path, realm, creds, challenge, ident, metadata);
111 0 : MOZ_ASSERT(NS_SUCCEEDED(rv));
112 0 : }
113 : ~nsHttpAuthEntry();
114 :
115 : MOZ_MUST_USE nsresult Set(const char *path,
116 : const char *realm,
117 : const char *creds,
118 : const char *challenge,
119 : const nsHttpAuthIdentity *ident,
120 : nsISupports *metadata);
121 :
122 : nsHttpAuthIdentity mIdent;
123 :
124 : nsHttpAuthPath *mRoot; //root pointer
125 : nsHttpAuthPath *mTail; //tail pointer
126 :
127 : // allocated together in one blob, starting with mRealm.
128 : char *mRealm;
129 : char *mCreds;
130 : char *mChallenge;
131 :
132 : friend class nsHttpAuthNode;
133 : friend class nsHttpAuthCache;
134 : friend class nsAutoPtr<nsHttpAuthEntry>; // needs to call the destructor
135 : };
136 :
137 : //-----------------------------------------------------------------------------
138 : // nsHttpAuthNode
139 : //-----------------------------------------------------------------------------
140 :
141 : class nsHttpAuthNode
142 : {
143 : private:
144 : nsHttpAuthNode();
145 : ~nsHttpAuthNode();
146 :
147 : // path can be null, in which case we'll search for an entry
148 : // with a null path.
149 : nsHttpAuthEntry *LookupEntryByPath(const char *path);
150 :
151 : // realm must not be null
152 : nsHttpAuthEntry *LookupEntryByRealm(const char *realm);
153 :
154 : // if a matching entry is found, then credentials will be changed.
155 : MOZ_MUST_USE nsresult SetAuthEntry(const char *path,
156 : const char *realm,
157 : const char *credentials,
158 : const char *challenge,
159 : const nsHttpAuthIdentity *ident,
160 : nsISupports *metadata);
161 :
162 : void ClearAuthEntry(const char *realm);
163 :
164 : uint32_t EntryCount() { return mList.Length(); }
165 :
166 : private:
167 : nsTArray<nsAutoPtr<nsHttpAuthEntry> > mList;
168 :
169 : friend class nsHttpAuthCache;
170 : };
171 :
172 : //-----------------------------------------------------------------------------
173 : // nsHttpAuthCache
174 : // (holds a hash table from host:port to nsHttpAuthNode)
175 : //-----------------------------------------------------------------------------
176 :
177 : class nsHttpAuthCache
178 : {
179 : public:
180 : nsHttpAuthCache();
181 : ~nsHttpAuthCache();
182 :
183 : MOZ_MUST_USE nsresult Init();
184 :
185 : // |scheme|, |host|, and |port| are required
186 : // |path| can be null
187 : // |entry| is either null or a weak reference
188 : MOZ_MUST_USE nsresult GetAuthEntryForPath(const char *scheme,
189 : const char *host,
190 : int32_t port,
191 : const char *path,
192 : nsACString const &originSuffix,
193 : nsHttpAuthEntry **entry);
194 :
195 : // |scheme|, |host|, and |port| are required
196 : // |realm| must not be null
197 : // |entry| is either null or a weak reference
198 : MOZ_MUST_USE nsresult GetAuthEntryForDomain(const char *scheme,
199 : const char *host,
200 : int32_t port,
201 : const char *realm,
202 : nsACString const &originSuffix,
203 : nsHttpAuthEntry **entry);
204 :
205 : // |scheme|, |host|, and |port| are required
206 : // |path| can be null
207 : // |realm| must not be null
208 : // if |credentials|, |user|, |pass|, and |challenge| are each
209 : // null, then the entry is deleted.
210 : MOZ_MUST_USE nsresult SetAuthEntry(const char *scheme,
211 : const char *host,
212 : int32_t port,
213 : const char *directory,
214 : const char *realm,
215 : const char *credentials,
216 : const char *challenge,
217 : nsACString const &originSuffix,
218 : const nsHttpAuthIdentity *ident,
219 : nsISupports *metadata);
220 :
221 : void ClearAuthEntry(const char *scheme,
222 : const char *host,
223 : int32_t port,
224 : const char *realm,
225 : nsACString const &originSuffix);
226 :
227 : // expire all existing auth list entries including proxy auths.
228 : MOZ_MUST_USE nsresult ClearAll();
229 :
230 : private:
231 : nsHttpAuthNode *LookupAuthNode(const char *scheme,
232 : const char *host,
233 : int32_t port,
234 : nsACString const &originSuffix,
235 : nsCString &key);
236 :
237 : // hash table allocation functions
238 : static void* AllocTable(void *, size_t size);
239 : static void FreeTable(void *, void *item);
240 : static PLHashEntry* AllocEntry(void *, const void *key);
241 : static void FreeEntry(void *, PLHashEntry *he, unsigned flag);
242 :
243 : static PLHashAllocOps gHashAllocOps;
244 :
245 : class OriginClearObserver : public nsIObserver {
246 0 : virtual ~OriginClearObserver() {}
247 : public:
248 : NS_DECL_ISUPPORTS
249 : NS_DECL_NSIOBSERVER
250 4 : explicit OriginClearObserver(nsHttpAuthCache* aOwner) : mOwner(aOwner) {}
251 : nsHttpAuthCache* mOwner;
252 : };
253 :
254 : void ClearOriginData(OriginAttributesPattern const &pattern);
255 :
256 : private:
257 : PLHashTable *mDB; // "host:port" --> nsHttpAuthNode
258 : RefPtr<OriginClearObserver> mObserver;
259 : };
260 :
261 : } // namespace net
262 : } // namespace mozilla
263 :
264 : #endif // nsHttpAuthCache_h__
|