Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/netwerk/protocol/http/AlternateServices.h
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set sw=2 ts=8 et tw=80 : */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
/*
8
Alt-Svc allows separation of transport routing from the origin host without
9
using a proxy. See https://httpwg.github.io/http-extensions/alt-svc.html and
10
https://tools.ietf.org/html/draft-ietf-httpbis-alt-svc-06
11
12
 Nice To Have Future Enhancements::
13
 * flush on network change event when we have an indicator
14
 * use established https channel for http instead separate of conninfo hash
15
 * pin via http-tls header
16
 * clear based on origin when a random fail happens not just 421
17
 * upon establishment of channel, cancel and retry trans that have not yet written anything
18
 * persistent storage (including private browsing filter)
19
 * memory reporter for cache, but this is rather tiny
20
*/
21
22
#ifndef mozilla_net_AlternateServices_h
23
#define mozilla_net_AlternateServices_h
24
25
#include "mozilla/DataStorage.h"
26
#include "nsRefPtrHashtable.h"
27
#include "nsString.h"
28
#include "nsIInterfaceRequestor.h"
29
#include "nsIStreamListener.h"
30
#include "nsISpeculativeConnect.h"
31
#include "mozilla/BasePrincipal.h"
32
33
class nsILoadInfo;
34
35
namespace mozilla { namespace net {
36
37
class nsProxyInfo;
38
class nsHttpConnectionInfo;
39
class nsHttpTransaction;
40
class nsHttpChannel;
41
class WellKnownChecker;
42
43
class AltSvcMapping
44
{
45
  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(AltSvcMapping)
46
47
private: // ctor from ProcessHeader
48
  AltSvcMapping(DataStorage *storage,
49
                int32_t storageEpoch,
50
                const nsACString &originScheme,
51
                const nsACString &originHost,
52
                int32_t originPort,
53
                const nsACString &username,
54
                bool privateBrowsing,
55
                uint32_t expiresAt,
56
                const nsACString &alternateHost,
57
                int32_t alternatePort,
58
                const nsACString &npnToken,
59
                const OriginAttributes &originAttributes);
60
public:
61
  AltSvcMapping(DataStorage *storage, int32_t storageEpoch, const nsCString &serialized);
62
63
  static void ProcessHeader(const nsCString &buf, const nsCString &originScheme,
64
                            const nsCString &originHost, int32_t originPort,
65
                            const nsACString &username, bool privateBrowsing,
66
                            nsIInterfaceRequestor *callbacks, nsProxyInfo *proxyInfo,
67
                            uint32_t caps, const OriginAttributes &originAttributes);
68
69
  // AcceptableProxy() decides whether a particular proxy configuration (pi) is suitable
70
  // for use with Alt-Svc. No proxy (including a null pi) is suitable.
71
  static bool AcceptableProxy(nsProxyInfo *pi);
72
73
0
  const nsCString &AlternateHost() const { return mAlternateHost; }
74
0
  const nsCString &OriginHost() const { return mOriginHost; }
75
0
  uint32_t OriginPort() const { return mOriginPort; }
76
0
  const nsCString &HashKey() const { return mHashKey; }
77
0
  uint32_t AlternatePort() const { return mAlternatePort; }
78
0
  bool Validated() { return mValidated; }
79
0
  int32_t GetExpiresAt() { return mExpiresAt; }
80
  bool RouteEquals(AltSvcMapping *map);
81
0
  bool HTTPS() { return mHttps; }
82
83
  void GetConnectionInfo(nsHttpConnectionInfo **outCI, nsProxyInfo *pi,
84
                         const OriginAttributes &originAttributes);
85
86
  int32_t TTL();
87
0
  int32_t StorageEpoch() { return mStorageEpoch; }
88
0
  bool    Private() { return mPrivate; }
89
90
  void SetValidated(bool val);
91
  void SetMixedScheme(bool val);
92
  void SetExpiresAt(int32_t val);
93
  void SetExpired();
94
  void Sync();
95
96
  static void MakeHashKey(nsCString &outKey,
97
                          const nsACString &originScheme,
98
                          const nsACString &originHost,
99
                          int32_t originPort,
100
                          bool privateBrowsing,
101
                          const OriginAttributes &originAttributes);
102
103
private:
104
0
  virtual ~AltSvcMapping() = default;
105
  void     SyncString(const nsCString& val);
106
  RefPtr<DataStorage> mStorage;
107
  int32_t             mStorageEpoch;
108
  void Serialize (nsCString &out);
109
110
  nsCString mHashKey;
111
112
  // If you change any of these members, update Serialize()
113
  nsCString mAlternateHost;
114
  MOZ_INIT_OUTSIDE_CTOR int32_t mAlternatePort;
115
116
  nsCString mOriginHost;
117
  MOZ_INIT_OUTSIDE_CTOR int32_t mOriginPort;
118
119
  nsCString mUsername;
120
  MOZ_INIT_OUTSIDE_CTOR bool mPrivate;
121
122
  MOZ_INIT_OUTSIDE_CTOR uint32_t mExpiresAt; // alt-svc mappping
123
124
  MOZ_INIT_OUTSIDE_CTOR bool mValidated;
125
  MOZ_INIT_OUTSIDE_CTOR bool mHttps; // origin is https://
126
  MOZ_INIT_OUTSIDE_CTOR bool mMixedScheme; // .wk allows http and https on same con
127
128
  nsCString mNPNToken;
129
130
  OriginAttributes mOriginAttributes;
131
};
132
133
class AltSvcOverride : public nsIInterfaceRequestor
134
                     , public nsISpeculativeConnectionOverrider
135
{
136
public:
137
  NS_DECL_THREADSAFE_ISUPPORTS
138
  NS_DECL_NSISPECULATIVECONNECTIONOVERRIDER
139
  NS_DECL_NSIINTERFACEREQUESTOR
140
141
  explicit AltSvcOverride(nsIInterfaceRequestor *aRequestor)
142
0
    : mCallbacks(aRequestor) {}
143
144
private:
145
0
  virtual ~AltSvcOverride() = default;
146
  nsCOMPtr<nsIInterfaceRequestor> mCallbacks;
147
};
148
149
class TransactionObserver final : public nsIStreamListener
150
{
151
public:
152
  NS_DECL_THREADSAFE_ISUPPORTS
153
  NS_DECL_NSISTREAMLISTENER
154
  NS_DECL_NSIREQUESTOBSERVER
155
156
  TransactionObserver(nsHttpChannel *channel, WellKnownChecker *checker);
157
  void Complete(nsHttpTransaction *, nsresult);
158
private:
159
  friend class WellKnownChecker;
160
0
  virtual ~TransactionObserver() = default;
161
162
  nsCOMPtr<nsISupports> mChannelRef;
163
  nsHttpChannel        *mChannel;
164
  WellKnownChecker     *mChecker;
165
  nsCString             mWKResponse;
166
167
  bool mRanOnce;
168
  bool mAuthOK; // confirmed no TLS failure
169
  bool mVersionOK; // connection h2
170
  bool mStatusOK; // HTTP Status 200
171
};
172
173
class AltSvcCache
174
{
175
public:
176
1
  AltSvcCache() : mStorageEpoch(0) {}
177
0
  virtual ~AltSvcCache () = default;
178
  void UpdateAltServiceMapping(AltSvcMapping *map, nsProxyInfo *pi,
179
                               nsIInterfaceRequestor *, uint32_t caps,
180
                               const OriginAttributes &originAttributes); // main thread
181
  already_AddRefed<AltSvcMapping> GetAltServiceMapping(const nsACString &scheme,
182
                                                       const nsACString &host,
183
                                                       int32_t port, bool pb,
184
                                                       const OriginAttributes &originAttributes);
185
  void ClearAltServiceMappings();
186
  void ClearHostMapping(const nsACString &host, int32_t port, const OriginAttributes &originAttributes);
187
  void ClearHostMapping(nsHttpConnectionInfo *ci);
188
0
  DataStorage *GetStoragePtr() { return mStorage.get(); }
189
0
  int32_t      StorageEpoch()  { return mStorageEpoch; }
190
191
private:
192
  already_AddRefed<AltSvcMapping> LookupMapping(const nsCString &key, bool privateBrowsing);
193
  RefPtr<DataStorage>             mStorage;
194
  int32_t                         mStorageEpoch;
195
};
196
197
} // namespace net
198
} // namespace mozilla
199
200
#endif // include guard