Coverage Report

Created: 2018-09-25 14:53

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