michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef nsHttpAuthCache_h__ michael@0: #define nsHttpAuthCache_h__ michael@0: michael@0: #include "nsError.h" michael@0: #include "nsTArray.h" michael@0: #include "nsAutoPtr.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "plhash.h" michael@0: #include "nsIObserver.h" michael@0: michael@0: class nsCString; michael@0: michael@0: namespace mozilla { michael@0: namespace net { michael@0: michael@0: struct nsHttpAuthPath { michael@0: struct nsHttpAuthPath *mNext; michael@0: char mPath[1]; michael@0: }; michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: // nsHttpAuthIdentity michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: class nsHttpAuthIdentity michael@0: { michael@0: public: michael@0: nsHttpAuthIdentity() michael@0: : mUser(nullptr) michael@0: , mPass(nullptr) michael@0: , mDomain(nullptr) michael@0: { michael@0: } michael@0: nsHttpAuthIdentity(const char16_t *domain, michael@0: const char16_t *user, michael@0: const char16_t *password) michael@0: : mUser(nullptr) michael@0: { michael@0: Set(domain, user, password); michael@0: } michael@0: ~nsHttpAuthIdentity() michael@0: { michael@0: Clear(); michael@0: } michael@0: michael@0: const char16_t *Domain() const { return mDomain; } michael@0: const char16_t *User() const { return mUser; } michael@0: const char16_t *Password() const { return mPass; } michael@0: michael@0: nsresult Set(const char16_t *domain, michael@0: const char16_t *user, michael@0: const char16_t *password); michael@0: nsresult Set(const nsHttpAuthIdentity &other) { return Set(other.mDomain, other.mUser, other.mPass); } michael@0: void Clear(); michael@0: michael@0: bool Equals(const nsHttpAuthIdentity &other) const; michael@0: bool IsEmpty() const { return !mUser; } michael@0: michael@0: private: michael@0: // allocated as one contiguous blob, starting at mUser. michael@0: char16_t *mUser; michael@0: char16_t *mPass; michael@0: char16_t *mDomain; michael@0: }; michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: // nsHttpAuthEntry michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: class nsHttpAuthEntry michael@0: { michael@0: public: michael@0: const char *Realm() const { return mRealm; } michael@0: const char *Creds() const { return mCreds; } michael@0: const char *Challenge() const { return mChallenge; } michael@0: const char16_t *Domain() const { return mIdent.Domain(); } michael@0: const char16_t *User() const { return mIdent.User(); } michael@0: const char16_t *Pass() const { return mIdent.Password(); } michael@0: nsHttpAuthPath *RootPath() { return mRoot; } michael@0: michael@0: const nsHttpAuthIdentity &Identity() const { return mIdent; } michael@0: michael@0: nsresult AddPath(const char *aPath); michael@0: michael@0: nsCOMPtr mMetaData; michael@0: michael@0: private: michael@0: nsHttpAuthEntry(const char *path, michael@0: const char *realm, michael@0: const char *creds, michael@0: const char *challenge, michael@0: const nsHttpAuthIdentity *ident, michael@0: nsISupports *metadata) michael@0: : mRoot(nullptr) michael@0: , mTail(nullptr) michael@0: , mRealm(nullptr) michael@0: { michael@0: Set(path, realm, creds, challenge, ident, metadata); michael@0: } michael@0: ~nsHttpAuthEntry(); michael@0: michael@0: nsresult Set(const char *path, michael@0: const char *realm, michael@0: const char *creds, michael@0: const char *challenge, michael@0: const nsHttpAuthIdentity *ident, michael@0: nsISupports *metadata); michael@0: michael@0: nsHttpAuthIdentity mIdent; michael@0: michael@0: nsHttpAuthPath *mRoot; //root pointer michael@0: nsHttpAuthPath *mTail; //tail pointer michael@0: michael@0: // allocated together in one blob, starting with mRealm. michael@0: char *mRealm; michael@0: char *mCreds; michael@0: char *mChallenge; michael@0: michael@0: friend class nsHttpAuthNode; michael@0: friend class nsHttpAuthCache; michael@0: friend class nsAutoPtr; // needs to call the destructor michael@0: }; michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: // nsHttpAuthNode michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: class nsHttpAuthNode michael@0: { michael@0: private: michael@0: nsHttpAuthNode(); michael@0: ~nsHttpAuthNode(); michael@0: michael@0: // path can be null, in which case we'll search for an entry michael@0: // with a null path. michael@0: nsHttpAuthEntry *LookupEntryByPath(const char *path); michael@0: michael@0: // realm must not be null michael@0: nsHttpAuthEntry *LookupEntryByRealm(const char *realm); michael@0: michael@0: // if a matching entry is found, then credentials will be changed. michael@0: nsresult SetAuthEntry(const char *path, michael@0: const char *realm, michael@0: const char *credentials, michael@0: const char *challenge, michael@0: const nsHttpAuthIdentity *ident, michael@0: nsISupports *metadata); michael@0: michael@0: void ClearAuthEntry(const char *realm); michael@0: michael@0: uint32_t EntryCount() { return mList.Length(); } michael@0: michael@0: private: michael@0: nsTArray > mList; michael@0: michael@0: friend class nsHttpAuthCache; michael@0: }; michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: // nsHttpAuthCache michael@0: // (holds a hash table from host:port to nsHttpAuthNode) michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: class nsHttpAuthCache michael@0: { michael@0: public: michael@0: nsHttpAuthCache(); michael@0: ~nsHttpAuthCache(); michael@0: michael@0: nsresult Init(); michael@0: michael@0: // |scheme|, |host|, and |port| are required michael@0: // |path| can be null michael@0: // |entry| is either null or a weak reference michael@0: nsresult GetAuthEntryForPath(const char *scheme, michael@0: const char *host, michael@0: int32_t port, michael@0: const char *path, michael@0: uint32_t appId, michael@0: bool inBrowserElement, michael@0: nsHttpAuthEntry **entry); michael@0: michael@0: // |scheme|, |host|, and |port| are required michael@0: // |realm| must not be null michael@0: // |entry| is either null or a weak reference michael@0: nsresult GetAuthEntryForDomain(const char *scheme, michael@0: const char *host, michael@0: int32_t port, michael@0: const char *realm, michael@0: uint32_t appId, michael@0: bool inBrowserElement, michael@0: nsHttpAuthEntry **entry); michael@0: michael@0: // |scheme|, |host|, and |port| are required michael@0: // |path| can be null michael@0: // |realm| must not be null michael@0: // if |credentials|, |user|, |pass|, and |challenge| are each michael@0: // null, then the entry is deleted. michael@0: nsresult SetAuthEntry(const char *scheme, michael@0: const char *host, michael@0: int32_t port, michael@0: const char *directory, michael@0: const char *realm, michael@0: const char *credentials, michael@0: const char *challenge, michael@0: uint32_t appId, michael@0: bool inBrowserElement, michael@0: const nsHttpAuthIdentity *ident, michael@0: nsISupports *metadata); michael@0: michael@0: void ClearAuthEntry(const char *scheme, michael@0: const char *host, michael@0: int32_t port, michael@0: const char *realm, michael@0: uint32_t appId, michael@0: bool inBrowserElement); michael@0: michael@0: // expire all existing auth list entries including proxy auths. michael@0: nsresult ClearAll(); michael@0: michael@0: private: michael@0: nsHttpAuthNode *LookupAuthNode(const char *scheme, michael@0: const char *host, michael@0: int32_t port, michael@0: uint32_t appId, michael@0: bool inBrowserElement, michael@0: nsCString &key); michael@0: michael@0: // hash table allocation functions michael@0: static void* AllocTable(void *, size_t size); michael@0: static void FreeTable(void *, void *item); michael@0: static PLHashEntry* AllocEntry(void *, const void *key); michael@0: static void FreeEntry(void *, PLHashEntry *he, unsigned flag); michael@0: michael@0: static PLHashAllocOps gHashAllocOps; michael@0: michael@0: class AppDataClearObserver : public nsIObserver { michael@0: public: michael@0: NS_DECL_ISUPPORTS michael@0: NS_DECL_NSIOBSERVER michael@0: AppDataClearObserver(nsHttpAuthCache* aOwner) : mOwner(aOwner) {} michael@0: virtual ~AppDataClearObserver() {} michael@0: nsHttpAuthCache* mOwner; michael@0: }; michael@0: michael@0: void ClearAppData(uint32_t appId, bool browserOnly); michael@0: michael@0: private: michael@0: PLHashTable *mDB; // "host:port" --> nsHttpAuthNode michael@0: nsRefPtr mObserver; michael@0: }; michael@0: michael@0: }} // namespace mozilla::net michael@0: michael@0: #endif // nsHttpAuthCache_h__