netwerk/protocol/http/nsHttpAuthCache.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/protocol/http/nsHttpAuthCache.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,260 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef nsHttpAuthCache_h__
    1.10 +#define nsHttpAuthCache_h__
    1.11 +
    1.12 +#include "nsError.h"
    1.13 +#include "nsTArray.h"
    1.14 +#include "nsAutoPtr.h"
    1.15 +#include "nsCOMPtr.h"
    1.16 +#include "plhash.h"
    1.17 +#include "nsIObserver.h"
    1.18 +
    1.19 +class nsCString;
    1.20 +
    1.21 +namespace mozilla {
    1.22 +namespace net {
    1.23 +
    1.24 +struct nsHttpAuthPath {
    1.25 +    struct nsHttpAuthPath *mNext;
    1.26 +    char                   mPath[1];
    1.27 +};
    1.28 +
    1.29 +//-----------------------------------------------------------------------------
    1.30 +// nsHttpAuthIdentity
    1.31 +//-----------------------------------------------------------------------------
    1.32 +
    1.33 +class nsHttpAuthIdentity
    1.34 +{
    1.35 +public:
    1.36 +    nsHttpAuthIdentity()
    1.37 +        : mUser(nullptr)
    1.38 +        , mPass(nullptr)
    1.39 +        , mDomain(nullptr)
    1.40 +    {
    1.41 +    }
    1.42 +    nsHttpAuthIdentity(const char16_t *domain,
    1.43 +                       const char16_t *user,
    1.44 +                       const char16_t *password)
    1.45 +        : mUser(nullptr)
    1.46 +    {
    1.47 +        Set(domain, user, password);
    1.48 +    }
    1.49 +   ~nsHttpAuthIdentity()
    1.50 +    {
    1.51 +        Clear();
    1.52 +    }
    1.53 +
    1.54 +    const char16_t *Domain()   const { return mDomain; }
    1.55 +    const char16_t *User()     const { return mUser; }
    1.56 +    const char16_t *Password() const { return mPass; }
    1.57 +
    1.58 +    nsresult Set(const char16_t *domain,
    1.59 +                 const char16_t *user,
    1.60 +                 const char16_t *password);
    1.61 +    nsresult Set(const nsHttpAuthIdentity &other) { return Set(other.mDomain, other.mUser, other.mPass); }
    1.62 +    void Clear();
    1.63 +
    1.64 +    bool Equals(const nsHttpAuthIdentity &other) const;
    1.65 +    bool IsEmpty() const { return !mUser; }
    1.66 +
    1.67 +private:
    1.68 +    // allocated as one contiguous blob, starting at mUser.
    1.69 +    char16_t *mUser;
    1.70 +    char16_t *mPass;
    1.71 +    char16_t *mDomain;
    1.72 +};
    1.73 +
    1.74 +//-----------------------------------------------------------------------------
    1.75 +// nsHttpAuthEntry
    1.76 +//-----------------------------------------------------------------------------
    1.77 +
    1.78 +class nsHttpAuthEntry
    1.79 +{
    1.80 +public:
    1.81 +    const char *Realm()       const { return mRealm; }
    1.82 +    const char *Creds()       const { return mCreds; }
    1.83 +    const char *Challenge()   const { return mChallenge; }
    1.84 +    const char16_t *Domain() const { return mIdent.Domain(); }
    1.85 +    const char16_t *User()   const { return mIdent.User(); }
    1.86 +    const char16_t *Pass()   const { return mIdent.Password(); }
    1.87 +    nsHttpAuthPath *RootPath()      { return mRoot; }
    1.88 +
    1.89 +    const nsHttpAuthIdentity &Identity() const { return mIdent; }
    1.90 +
    1.91 +    nsresult AddPath(const char *aPath);
    1.92 +
    1.93 +    nsCOMPtr<nsISupports> mMetaData;
    1.94 +
    1.95 +private:
    1.96 +    nsHttpAuthEntry(const char *path,
    1.97 +                    const char *realm,
    1.98 +                    const char *creds,
    1.99 +                    const char *challenge,
   1.100 +                    const nsHttpAuthIdentity *ident,
   1.101 +                    nsISupports *metadata)
   1.102 +        : mRoot(nullptr)
   1.103 +        , mTail(nullptr)
   1.104 +        , mRealm(nullptr)
   1.105 +    {
   1.106 +        Set(path, realm, creds, challenge, ident, metadata);
   1.107 +    }
   1.108 +   ~nsHttpAuthEntry();
   1.109 +
   1.110 +    nsresult Set(const char *path,
   1.111 +                 const char *realm,
   1.112 +                 const char *creds,
   1.113 +                 const char *challenge,
   1.114 +                 const nsHttpAuthIdentity *ident,
   1.115 +                 nsISupports *metadata);
   1.116 +
   1.117 +    nsHttpAuthIdentity mIdent;
   1.118 +
   1.119 +    nsHttpAuthPath *mRoot; //root pointer
   1.120 +    nsHttpAuthPath *mTail; //tail pointer
   1.121 +
   1.122 +    // allocated together in one blob, starting with mRealm.
   1.123 +    char *mRealm;
   1.124 +    char *mCreds;
   1.125 +    char *mChallenge;
   1.126 +
   1.127 +    friend class nsHttpAuthNode;
   1.128 +    friend class nsHttpAuthCache;
   1.129 +    friend class nsAutoPtr<nsHttpAuthEntry>; // needs to call the destructor
   1.130 +};
   1.131 +
   1.132 +//-----------------------------------------------------------------------------
   1.133 +// nsHttpAuthNode
   1.134 +//-----------------------------------------------------------------------------
   1.135 +
   1.136 +class nsHttpAuthNode
   1.137 +{
   1.138 +private:
   1.139 +    nsHttpAuthNode();
   1.140 +   ~nsHttpAuthNode();
   1.141 +
   1.142 +    // path can be null, in which case we'll search for an entry
   1.143 +    // with a null path.
   1.144 +    nsHttpAuthEntry *LookupEntryByPath(const char *path);
   1.145 +
   1.146 +    // realm must not be null
   1.147 +    nsHttpAuthEntry *LookupEntryByRealm(const char *realm);
   1.148 +
   1.149 +    // if a matching entry is found, then credentials will be changed.
   1.150 +    nsresult SetAuthEntry(const char *path,
   1.151 +                          const char *realm,
   1.152 +                          const char *credentials,
   1.153 +                          const char *challenge,
   1.154 +                          const nsHttpAuthIdentity *ident,
   1.155 +                          nsISupports *metadata);
   1.156 +
   1.157 +    void ClearAuthEntry(const char *realm);
   1.158 +
   1.159 +    uint32_t EntryCount() { return mList.Length(); }
   1.160 +
   1.161 +private:
   1.162 +    nsTArray<nsAutoPtr<nsHttpAuthEntry> > mList;
   1.163 +
   1.164 +    friend class nsHttpAuthCache;
   1.165 +};
   1.166 +
   1.167 +//-----------------------------------------------------------------------------
   1.168 +// nsHttpAuthCache
   1.169 +//  (holds a hash table from host:port to nsHttpAuthNode)
   1.170 +//-----------------------------------------------------------------------------
   1.171 +
   1.172 +class nsHttpAuthCache
   1.173 +{
   1.174 +public:
   1.175 +    nsHttpAuthCache();
   1.176 +   ~nsHttpAuthCache();
   1.177 +
   1.178 +    nsresult Init();
   1.179 +
   1.180 +    // |scheme|, |host|, and |port| are required
   1.181 +    // |path| can be null
   1.182 +    // |entry| is either null or a weak reference
   1.183 +    nsresult GetAuthEntryForPath(const char *scheme,
   1.184 +                                 const char *host,
   1.185 +                                 int32_t     port,
   1.186 +                                 const char *path,
   1.187 +                                 uint32_t    appId,
   1.188 +                                 bool        inBrowserElement,
   1.189 +                                 nsHttpAuthEntry **entry);
   1.190 +
   1.191 +    // |scheme|, |host|, and |port| are required
   1.192 +    // |realm| must not be null
   1.193 +    // |entry| is either null or a weak reference
   1.194 +    nsresult GetAuthEntryForDomain(const char *scheme,
   1.195 +                                   const char *host,
   1.196 +                                   int32_t     port,
   1.197 +                                   const char *realm,
   1.198 +                                   uint32_t    appId,
   1.199 +                                   bool        inBrowserElement,
   1.200 +                                   nsHttpAuthEntry **entry);
   1.201 +
   1.202 +    // |scheme|, |host|, and |port| are required
   1.203 +    // |path| can be null
   1.204 +    // |realm| must not be null
   1.205 +    // if |credentials|, |user|, |pass|, and |challenge| are each
   1.206 +    // null, then the entry is deleted.
   1.207 +    nsresult SetAuthEntry(const char *scheme,
   1.208 +                          const char *host,
   1.209 +                          int32_t     port,
   1.210 +                          const char *directory,
   1.211 +                          const char *realm,
   1.212 +                          const char *credentials,
   1.213 +                          const char *challenge,
   1.214 +                          uint32_t    appId,
   1.215 +                          bool        inBrowserElement,
   1.216 +                          const nsHttpAuthIdentity *ident,
   1.217 +                          nsISupports *metadata);
   1.218 +
   1.219 +    void ClearAuthEntry(const char *scheme,
   1.220 +                        const char *host,
   1.221 +                        int32_t     port,
   1.222 +                        const char *realm,
   1.223 +                        uint32_t    appId,
   1.224 +                        bool        inBrowserElement);
   1.225 +
   1.226 +    // expire all existing auth list entries including proxy auths.
   1.227 +    nsresult ClearAll();
   1.228 +
   1.229 +private:
   1.230 +    nsHttpAuthNode *LookupAuthNode(const char *scheme,
   1.231 +                                   const char *host,
   1.232 +                                   int32_t     port,
   1.233 +                                   uint32_t    appId,
   1.234 +                                   bool        inBrowserElement,
   1.235 +                                   nsCString  &key);
   1.236 +
   1.237 +    // hash table allocation functions
   1.238 +    static void*        AllocTable(void *, size_t size);
   1.239 +    static void         FreeTable(void *, void *item);
   1.240 +    static PLHashEntry* AllocEntry(void *, const void *key);
   1.241 +    static void         FreeEntry(void *, PLHashEntry *he, unsigned flag);
   1.242 +
   1.243 +    static PLHashAllocOps gHashAllocOps;
   1.244 +
   1.245 +    class AppDataClearObserver : public nsIObserver {
   1.246 +    public:
   1.247 +      NS_DECL_ISUPPORTS
   1.248 +      NS_DECL_NSIOBSERVER
   1.249 +      AppDataClearObserver(nsHttpAuthCache* aOwner) : mOwner(aOwner) {}
   1.250 +      virtual ~AppDataClearObserver() {}
   1.251 +      nsHttpAuthCache* mOwner;
   1.252 +    };
   1.253 +
   1.254 +    void ClearAppData(uint32_t appId, bool browserOnly);
   1.255 +
   1.256 +private:
   1.257 +    PLHashTable *mDB; // "host:port" --> nsHttpAuthNode
   1.258 +    nsRefPtr<AppDataClearObserver> mObserver;
   1.259 +};
   1.260 +
   1.261 +}} // namespace mozilla::net
   1.262 +
   1.263 +#endif // nsHttpAuthCache_h__

mercurial