1 /* -*- Mode: C++; tab-width: 8; 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 // HttpLog.h should generally be included first
8 #include "HttpLog.h"
9 
10 /*
11   Currently supported is h2
12 */
13 
14 #include "nsHttp.h"
15 #include "nsHttpHandler.h"
16 
17 #include "ASpdySession.h"
18 #include "PSpdyPush.h"
19 #include "Http2Push.h"
20 #include "Http2Session.h"
21 
22 #include "mozilla/Telemetry.h"
23 
24 namespace mozilla {
25 namespace net {
26 
NewSpdySession(net::SpdyVersion version,nsISocketTransport * aTransport,bool attemptingEarlyData)27 ASpdySession* ASpdySession::NewSpdySession(net::SpdyVersion version,
28                                            nsISocketTransport* aTransport,
29                                            bool attemptingEarlyData) {
30   // This is a necko only interface, so we can enforce version
31   // requests as a precondition
32   MOZ_ASSERT(version == SpdyVersion::HTTP_2, "Unsupported spdy version");
33 
34   // Don't do a runtime check of IsSpdyV?Enabled() here because pref value
35   // may have changed since starting negotiation. The selected protocol comes
36   // from a list provided in the SERVER HELLO filtered by our acceptable
37   // versions, so there is no risk of the server ignoring our prefs.
38 
39   return new Http2Session(aTransport, version, attemptingEarlyData);
40 }
41 
SpdyInformation()42 SpdyInformation::SpdyInformation() {
43   // highest index of enabled protocols is the
44   // most preferred for ALPN negotiaton
45   Version[0] = SpdyVersion::HTTP_2;
46   VersionString[0] = NS_LITERAL_CSTRING("h2");
47   ALPNCallbacks[0] = Http2Session::ALPNCallback;
48 }
49 
ProtocolEnabled(uint32_t index) const50 bool SpdyInformation::ProtocolEnabled(uint32_t index) const {
51   MOZ_ASSERT(index < kCount, "index out of range");
52 
53   return gHttpHandler->IsHttp2Enabled();
54 }
55 
GetNPNIndex(const nsACString & npnString,uint32_t * result) const56 nsresult SpdyInformation::GetNPNIndex(const nsACString& npnString,
57                                       uint32_t* result) const {
58   if (npnString.IsEmpty()) return NS_ERROR_FAILURE;
59 
60   for (uint32_t index = 0; index < kCount; ++index) {
61     if (npnString.Equals(VersionString[index])) {
62       *result = index;
63       return NS_OK;
64     }
65   }
66 
67   return NS_ERROR_FAILURE;
68 }
69 
70 //////////////////////////////////////////
71 // SpdyPushCache
72 //////////////////////////////////////////
73 
~SpdyPushCache()74 SpdyPushCache::~SpdyPushCache() { mHashHttp2.Clear(); }
75 
RegisterPushedStreamHttp2(const nsCString & key,Http2PushedStream * stream)76 bool SpdyPushCache::RegisterPushedStreamHttp2(const nsCString& key,
77                                               Http2PushedStream* stream) {
78   LOG3(("SpdyPushCache::RegisterPushedStreamHttp2 %s 0x%X\n", key.get(),
79         stream->StreamID()));
80   if (mHashHttp2.Get(key)) {
81     LOG3(("SpdyPushCache::RegisterPushedStreamHttp2 %s 0x%X duplicate key\n",
82           key.get(), stream->StreamID()));
83     return false;
84   }
85   mHashHttp2.Put(key, stream);
86   return true;
87 }
88 
RemovePushedStreamHttp2(const nsCString & key)89 Http2PushedStream* SpdyPushCache::RemovePushedStreamHttp2(
90     const nsCString& key) {
91   Http2PushedStream* rv = mHashHttp2.Get(key);
92   LOG3(("SpdyPushCache::RemovePushedStreamHttp2 %s 0x%X\n", key.get(),
93         rv ? rv->StreamID() : 0));
94   if (rv) mHashHttp2.Remove(key);
95   return rv;
96 }
97 
RemovePushedStreamHttp2ByID(const nsCString & key,const uint32_t & streamID)98 Http2PushedStream* SpdyPushCache::RemovePushedStreamHttp2ByID(
99     const nsCString& key, const uint32_t& streamID) {
100   Http2PushedStream* rv = mHashHttp2.Get(key);
101   LOG3(("SpdyPushCache::RemovePushedStreamHttp2ByID %s 0x%X 0x%X", key.get(),
102         rv ? rv->StreamID() : 0, streamID));
103   if (rv && streamID == rv->StreamID()) {
104     mHashHttp2.Remove(key);
105   } else {
106     // Ensure we overwrite our rv with null in case the stream IDs don't match
107     rv = nullptr;
108   }
109   return rv;
110 }
111 
112 }  // namespace net
113 }  // namespace mozilla
114