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