netwerk/protocol/http/nsHttpAuthCache.h

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

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.)

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

mercurial