2001-09-29 00:14:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2001-05-12 01:04:09 +04:00
|
|
|
|
|
|
|
#ifndef nsHttpAuthCache_h__
|
|
|
|
#define nsHttpAuthCache_h__
|
|
|
|
|
|
|
|
#include "nsError.h"
|
2009-02-23 04:05:28 +03:00
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsAutoPtr.h"
|
2018-03-03 02:04:25 +03:00
|
|
|
#include "nsClassHashtable.h"
|
2001-12-05 02:49:06 +03:00
|
|
|
#include "nsCOMPtr.h"
|
2018-03-03 02:04:25 +03:00
|
|
|
#include "nsHashKeys.h"
|
2017-08-17 02:48:52 +03:00
|
|
|
#include "nsStringFwd.h"
|
2012-12-05 11:33:20 +04:00
|
|
|
#include "nsIObserver.h"
|
|
|
|
|
2014-01-08 02:05:56 +04:00
|
|
|
namespace mozilla {
|
2015-10-29 12:43:00 +03:00
|
|
|
|
|
|
|
class OriginAttributesPattern;
|
|
|
|
|
2014-01-08 02:05:56 +04:00
|
|
|
namespace net {
|
|
|
|
|
2003-05-19 21:47:22 +04:00
|
|
|
struct nsHttpAuthPath {
|
|
|
|
struct nsHttpAuthPath *mNext;
|
|
|
|
char mPath[1];
|
|
|
|
};
|
|
|
|
|
2003-03-26 08:05:49 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsHttpAuthIdentity
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class nsHttpAuthIdentity
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsHttpAuthIdentity()
|
2012-07-30 18:20:58 +04:00
|
|
|
: mUser(nullptr)
|
|
|
|
, mPass(nullptr)
|
|
|
|
, mDomain(nullptr)
|
2003-03-26 08:05:49 +03:00
|
|
|
{
|
|
|
|
}
|
2018-04-10 22:11:02 +03:00
|
|
|
nsHttpAuthIdentity(const char16_t* domain,
|
|
|
|
const char16_t* user,
|
|
|
|
const char16_t* password)
|
|
|
|
: mUser(nullptr)
|
|
|
|
, mPass{ nullptr }
|
|
|
|
, mDomain{ nullptr }
|
2003-03-26 08:05:49 +03:00
|
|
|
{
|
2018-04-10 22:11:02 +03:00
|
|
|
DebugOnly<nsresult> rv = Set(domain, user, password);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
2003-03-26 08:05:49 +03:00
|
|
|
}
|
|
|
|
~nsHttpAuthIdentity()
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t *Domain() const { return mDomain; }
|
|
|
|
const char16_t *User() const { return mUser; }
|
|
|
|
const char16_t *Password() const { return mPass; }
|
2003-03-26 08:05:49 +03:00
|
|
|
|
2016-12-27 10:22:07 +03:00
|
|
|
MOZ_MUST_USE nsresult Set(const char16_t *domain,
|
|
|
|
const char16_t *user,
|
|
|
|
const char16_t *password);
|
|
|
|
MOZ_MUST_USE nsresult Set(const nsHttpAuthIdentity &other)
|
|
|
|
{
|
|
|
|
return Set(other.mDomain, other.mUser, other.mPass);
|
|
|
|
}
|
2003-03-26 08:05:49 +03:00
|
|
|
void Clear();
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool Equals(const nsHttpAuthIdentity &other) const;
|
|
|
|
bool IsEmpty() const { return !mUser; }
|
2003-03-26 08:05:49 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
// allocated as one contiguous blob, starting at mUser.
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t *mUser;
|
|
|
|
char16_t *mPass;
|
|
|
|
char16_t *mDomain;
|
2003-03-26 08:05:49 +03:00
|
|
|
};
|
|
|
|
|
2001-12-05 02:49:06 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsHttpAuthEntry
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class nsHttpAuthEntry
|
|
|
|
{
|
|
|
|
public:
|
2003-03-26 08:05:49 +03:00
|
|
|
const char *Realm() const { return mRealm; }
|
|
|
|
const char *Creds() const { return mCreds; }
|
|
|
|
const char *Challenge() const { return mChallenge; }
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t *Domain() const { return mIdent.Domain(); }
|
|
|
|
const char16_t *User() const { return mIdent.User(); }
|
|
|
|
const char16_t *Pass() const { return mIdent.Password(); }
|
2003-05-19 21:47:22 +04:00
|
|
|
nsHttpAuthPath *RootPath() { return mRoot; }
|
2003-03-26 08:05:49 +03:00
|
|
|
|
|
|
|
const nsHttpAuthIdentity &Identity() const { return mIdent; }
|
2013-05-16 17:30:42 +04:00
|
|
|
|
2016-12-27 10:22:07 +03:00
|
|
|
MOZ_MUST_USE nsresult AddPath(const char *aPath);
|
2013-05-16 17:30:42 +04:00
|
|
|
|
2003-03-26 08:05:49 +03:00
|
|
|
nsCOMPtr<nsISupports> mMetaData;
|
2001-12-05 02:49:06 +03:00
|
|
|
|
|
|
|
private:
|
2018-04-10 22:11:02 +03:00
|
|
|
nsHttpAuthEntry(const char* path,
|
|
|
|
const char* realm,
|
|
|
|
const char* creds,
|
|
|
|
const char* challenge,
|
|
|
|
const nsHttpAuthIdentity* ident,
|
|
|
|
nsISupports* metadata)
|
|
|
|
: mRoot(nullptr)
|
|
|
|
, mTail(nullptr)
|
|
|
|
, mRealm(nullptr)
|
|
|
|
, mCreds{ nullptr }
|
|
|
|
, mChallenge{ nullptr }
|
|
|
|
{
|
|
|
|
DebugOnly<nsresult> rv =
|
|
|
|
Set(path, realm, creds, challenge, ident, metadata);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
2003-03-26 08:05:49 +03:00
|
|
|
}
|
2001-12-05 02:49:06 +03:00
|
|
|
~nsHttpAuthEntry();
|
|
|
|
|
2016-12-27 10:22:07 +03:00
|
|
|
MOZ_MUST_USE nsresult Set(const char *path,
|
|
|
|
const char *realm,
|
|
|
|
const char *creds,
|
|
|
|
const char *challenge,
|
|
|
|
const nsHttpAuthIdentity *ident,
|
|
|
|
nsISupports *metadata);
|
2003-03-26 08:05:49 +03:00
|
|
|
|
|
|
|
nsHttpAuthIdentity mIdent;
|
|
|
|
|
2003-05-19 21:47:22 +04:00
|
|
|
nsHttpAuthPath *mRoot; //root pointer
|
|
|
|
nsHttpAuthPath *mTail; //tail pointer
|
|
|
|
|
|
|
|
// allocated together in one blob, starting with mRealm.
|
2003-03-26 08:05:49 +03:00
|
|
|
char *mRealm;
|
|
|
|
char *mCreds;
|
|
|
|
char *mChallenge;
|
2001-12-05 02:49:06 +03:00
|
|
|
|
|
|
|
friend class nsHttpAuthNode;
|
2003-03-26 08:05:49 +03:00
|
|
|
friend class nsHttpAuthCache;
|
2009-02-23 04:05:28 +03:00
|
|
|
friend class nsAutoPtr<nsHttpAuthEntry>; // needs to call the destructor
|
2001-12-05 02:49:06 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsHttpAuthNode
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class nsHttpAuthNode
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
nsHttpAuthNode();
|
|
|
|
~nsHttpAuthNode();
|
|
|
|
|
|
|
|
// path can be null, in which case we'll search for an entry
|
|
|
|
// with a null path.
|
2003-03-26 08:05:49 +03:00
|
|
|
nsHttpAuthEntry *LookupEntryByPath(const char *path);
|
2001-12-05 02:49:06 +03:00
|
|
|
|
|
|
|
// realm must not be null
|
2003-03-26 08:05:49 +03:00
|
|
|
nsHttpAuthEntry *LookupEntryByRealm(const char *realm);
|
2001-12-05 02:49:06 +03:00
|
|
|
|
|
|
|
// if a matching entry is found, then credentials will be changed.
|
2016-12-27 10:22:07 +03:00
|
|
|
MOZ_MUST_USE nsresult SetAuthEntry(const char *path,
|
|
|
|
const char *realm,
|
|
|
|
const char *credentials,
|
|
|
|
const char *challenge,
|
|
|
|
const nsHttpAuthIdentity *ident,
|
|
|
|
nsISupports *metadata);
|
2001-12-05 02:49:06 +03:00
|
|
|
|
2003-03-26 08:05:49 +03:00
|
|
|
void ClearAuthEntry(const char *realm);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t EntryCount() { return mList.Length(); }
|
2001-12-05 02:49:06 +03:00
|
|
|
|
|
|
|
private:
|
2009-02-23 04:05:28 +03:00
|
|
|
nsTArray<nsAutoPtr<nsHttpAuthEntry> > mList;
|
2001-12-05 02:49:06 +03:00
|
|
|
|
|
|
|
friend class nsHttpAuthCache;
|
2018-03-03 02:04:25 +03:00
|
|
|
friend class nsAutoPtr<nsHttpAuthNode>; // needs to call the destructor
|
2001-12-05 02:49:06 +03:00
|
|
|
};
|
|
|
|
|
2001-05-12 01:04:09 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsHttpAuthCache
|
2001-12-05 02:49:06 +03:00
|
|
|
// (holds a hash table from host:port to nsHttpAuthNode)
|
2001-05-12 01:04:09 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class nsHttpAuthCache
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsHttpAuthCache();
|
|
|
|
~nsHttpAuthCache();
|
|
|
|
|
2004-04-08 03:34:35 +04:00
|
|
|
// |scheme|, |host|, and |port| are required
|
2001-12-05 02:49:06 +03:00
|
|
|
// |path| can be null
|
|
|
|
// |entry| is either null or a weak reference
|
2016-12-27 10:22:07 +03:00
|
|
|
MOZ_MUST_USE nsresult GetAuthEntryForPath(const char *scheme,
|
|
|
|
const char *host,
|
|
|
|
int32_t port,
|
|
|
|
const char *path,
|
|
|
|
nsACString const &originSuffix,
|
|
|
|
nsHttpAuthEntry **entry);
|
2001-12-05 02:49:06 +03:00
|
|
|
|
2004-04-08 03:34:35 +04:00
|
|
|
// |scheme|, |host|, and |port| are required
|
2001-12-05 02:49:06 +03:00
|
|
|
// |realm| must not be null
|
|
|
|
// |entry| is either null or a weak reference
|
2016-12-27 10:22:07 +03:00
|
|
|
MOZ_MUST_USE nsresult GetAuthEntryForDomain(const char *scheme,
|
|
|
|
const char *host,
|
|
|
|
int32_t port,
|
|
|
|
const char *realm,
|
|
|
|
nsACString const &originSuffix,
|
|
|
|
nsHttpAuthEntry **entry);
|
2001-12-05 02:49:06 +03:00
|
|
|
|
2004-04-08 03:34:35 +04:00
|
|
|
// |scheme|, |host|, and |port| are required
|
2001-12-05 02:49:06 +03:00
|
|
|
// |path| can be null
|
|
|
|
// |realm| must not be null
|
|
|
|
// if |credentials|, |user|, |pass|, and |challenge| are each
|
|
|
|
// null, then the entry is deleted.
|
2016-12-27 10:22:07 +03:00
|
|
|
MOZ_MUST_USE nsresult SetAuthEntry(const char *scheme,
|
|
|
|
const char *host,
|
|
|
|
int32_t port,
|
|
|
|
const char *directory,
|
|
|
|
const char *realm,
|
|
|
|
const char *credentials,
|
|
|
|
const char *challenge,
|
|
|
|
nsACString const &originSuffix,
|
|
|
|
const nsHttpAuthIdentity *ident,
|
|
|
|
nsISupports *metadata);
|
2001-12-05 02:49:06 +03:00
|
|
|
|
2004-04-08 03:34:35 +04:00
|
|
|
void ClearAuthEntry(const char *scheme,
|
|
|
|
const char *host,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t port,
|
2012-12-05 11:33:20 +04:00
|
|
|
const char *realm,
|
2015-10-29 12:43:00 +03:00
|
|
|
nsACString const &originSuffix);
|
2003-03-26 08:05:49 +03:00
|
|
|
|
2013-05-16 17:30:42 +04:00
|
|
|
// expire all existing auth list entries including proxy auths.
|
2016-12-27 10:22:07 +03:00
|
|
|
MOZ_MUST_USE nsresult ClearAll();
|
2001-05-12 01:04:09 +04:00
|
|
|
|
|
|
|
private:
|
2004-04-08 03:34:35 +04:00
|
|
|
nsHttpAuthNode *LookupAuthNode(const char *scheme,
|
|
|
|
const char *host,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t port,
|
2015-10-29 12:43:00 +03:00
|
|
|
nsACString const &originSuffix,
|
2004-04-08 03:34:35 +04:00
|
|
|
nsCString &key);
|
2001-05-12 01:04:09 +04:00
|
|
|
|
2015-10-29 12:43:00 +03:00
|
|
|
class OriginClearObserver : public nsIObserver {
|
|
|
|
virtual ~OriginClearObserver() {}
|
2012-12-05 11:33:20 +04:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
2015-10-29 12:43:00 +03:00
|
|
|
explicit OriginClearObserver(nsHttpAuthCache* aOwner) : mOwner(aOwner) {}
|
2012-12-05 11:33:20 +04:00
|
|
|
nsHttpAuthCache* mOwner;
|
|
|
|
};
|
|
|
|
|
2015-10-29 12:43:00 +03:00
|
|
|
void ClearOriginData(OriginAttributesPattern const &pattern);
|
2013-05-16 17:30:42 +04:00
|
|
|
|
2001-05-12 01:04:09 +04:00
|
|
|
private:
|
2018-03-03 02:04:25 +03:00
|
|
|
using AuthNodeTable = nsClassHashtable<nsCStringHashKey, nsHttpAuthNode>;
|
|
|
|
AuthNodeTable mDB; // "host:port" --> nsHttpAuthNode
|
2015-10-29 12:43:00 +03:00
|
|
|
RefPtr<OriginClearObserver> mObserver;
|
2001-05-12 01:04:09 +04:00
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|
2014-01-08 02:05:56 +04:00
|
|
|
|
2001-05-12 01:04:09 +04:00
|
|
|
#endif // nsHttpAuthCache_h__
|