michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set sw=2 ts=8 et tw=80 : */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: // HttpLog.h should generally be included first michael@0: #include "HttpLog.h" michael@0: michael@0: /* michael@0: Currently supported are HTTP-draft-[see nshttp.h]/2.0 spdy/3.1 and spdy/3 michael@0: */ michael@0: michael@0: #include "nsHttp.h" michael@0: #include "nsHttpHandler.h" michael@0: michael@0: #include "ASpdySession.h" michael@0: #include "PSpdyPush.h" michael@0: #include "SpdyPush3.h" michael@0: #include "SpdyPush31.h" michael@0: #include "Http2Push.h" michael@0: #include "SpdySession3.h" michael@0: #include "SpdySession31.h" michael@0: #include "Http2Session.h" michael@0: michael@0: #include "mozilla/Telemetry.h" michael@0: michael@0: namespace mozilla { michael@0: namespace net { michael@0: michael@0: ASpdySession * michael@0: ASpdySession::NewSpdySession(uint32_t version, michael@0: nsAHttpTransaction *aTransaction, michael@0: nsISocketTransport *aTransport, michael@0: int32_t aPriority) michael@0: { michael@0: // This is a necko only interface, so we can enforce version michael@0: // requests as a precondition michael@0: MOZ_ASSERT(version == SPDY_VERSION_3 || michael@0: version == SPDY_VERSION_31 || michael@0: version == NS_HTTP2_DRAFT_VERSION, michael@0: "Unsupported spdy version"); michael@0: michael@0: // Don't do a runtime check of IsSpdyV?Enabled() here because pref value michael@0: // may have changed since starting negotiation. The selected protocol comes michael@0: // from a list provided in the SERVER HELLO filtered by our acceptable michael@0: // versions, so there is no risk of the server ignoring our prefs. michael@0: michael@0: Telemetry::Accumulate(Telemetry::SPDY_VERSION2, version); michael@0: michael@0: if (version == SPDY_VERSION_3) michael@0: return new SpdySession3(aTransaction, aTransport, aPriority); michael@0: michael@0: if (version == SPDY_VERSION_31) michael@0: return new SpdySession31(aTransaction, aTransport, aPriority); michael@0: michael@0: if (version == NS_HTTP2_DRAFT_VERSION) michael@0: return new Http2Session(aTransaction, aTransport, aPriority); michael@0: michael@0: return nullptr; michael@0: } michael@0: michael@0: SpdyInformation::SpdyInformation() michael@0: { michael@0: Version[0] = SPDY_VERSION_3; michael@0: VersionString[0] = NS_LITERAL_CSTRING("spdy/3"); michael@0: michael@0: Version[1] = SPDY_VERSION_31; michael@0: VersionString[1] = NS_LITERAL_CSTRING("spdy/3.1"); michael@0: michael@0: Version[2] = NS_HTTP2_DRAFT_VERSION; michael@0: VersionString[2] = NS_LITERAL_CSTRING(NS_HTTP2_DRAFT_TOKEN); michael@0: } michael@0: michael@0: bool michael@0: SpdyInformation::ProtocolEnabled(uint32_t index) michael@0: { michael@0: MOZ_ASSERT(index < kCount, "index out of range"); michael@0: michael@0: switch (index) { michael@0: case 0: michael@0: return gHttpHandler->IsSpdyV3Enabled(); michael@0: case 1: michael@0: return gHttpHandler->IsSpdyV31Enabled(); michael@0: case 2: michael@0: return gHttpHandler->IsHttp2DraftEnabled(); michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: nsresult michael@0: SpdyInformation::GetNPNVersionIndex(const nsACString &npnString, michael@0: uint8_t *result) michael@0: { michael@0: if (npnString.IsEmpty()) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: for (uint32_t index = 0; index < kCount; ++index) { michael@0: if (npnString.Equals(VersionString[index])) { michael@0: *result = Version[index]; michael@0: return NS_OK; michael@0: } michael@0: } michael@0: michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: ////////////////////////////////////////// michael@0: // SpdyPushCache michael@0: ////////////////////////////////////////// michael@0: michael@0: SpdyPushCache::SpdyPushCache() michael@0: { michael@0: } michael@0: michael@0: SpdyPushCache::~SpdyPushCache() michael@0: { michael@0: mHashSpdy3.Clear(); michael@0: mHashSpdy31.Clear(); michael@0: mHashHttp2.Clear(); michael@0: } michael@0: michael@0: bool michael@0: SpdyPushCache::RegisterPushedStreamSpdy3(nsCString key, michael@0: SpdyPushedStream3 *stream) michael@0: { michael@0: LOG3(("SpdyPushCache::RegisterPushedStreamSpdy3 %s 0x%X\n", michael@0: key.get(), stream->StreamID())); michael@0: if(mHashSpdy3.Get(key)) michael@0: return false; michael@0: mHashSpdy3.Put(key, stream); michael@0: return true; michael@0: } michael@0: michael@0: SpdyPushedStream3 * michael@0: SpdyPushCache::RemovePushedStreamSpdy3(nsCString key) michael@0: { michael@0: SpdyPushedStream3 *rv = mHashSpdy3.Get(key); michael@0: LOG3(("SpdyPushCache::RemovePushedStream %s 0x%X\n", michael@0: key.get(), rv ? rv->StreamID() : 0)); michael@0: if (rv) michael@0: mHashSpdy3.Remove(key); michael@0: return rv; michael@0: } michael@0: michael@0: bool michael@0: SpdyPushCache::RegisterPushedStreamSpdy31(nsCString key, michael@0: SpdyPushedStream31 *stream) michael@0: { michael@0: LOG3(("SpdyPushCache::RegisterPushedStreamSpdy31 %s 0x%X\n", michael@0: key.get(), stream->StreamID())); michael@0: if(mHashSpdy31.Get(key)) michael@0: return false; michael@0: mHashSpdy31.Put(key, stream); michael@0: return true; michael@0: } michael@0: michael@0: SpdyPushedStream31 * michael@0: SpdyPushCache::RemovePushedStreamSpdy31(nsCString key) michael@0: { michael@0: SpdyPushedStream31 *rv = mHashSpdy31.Get(key); michael@0: LOG3(("SpdyPushCache::RemovePushedStream %s 0x%X\n", michael@0: key.get(), rv ? rv->StreamID() : 0)); michael@0: if (rv) michael@0: mHashSpdy31.Remove(key); michael@0: return rv; michael@0: } michael@0: michael@0: bool michael@0: SpdyPushCache::RegisterPushedStreamHttp2(nsCString key, michael@0: Http2PushedStream *stream) michael@0: { michael@0: LOG3(("SpdyPushCache::RegisterPushedStreamHttp2 %s 0x%X\n", michael@0: key.get(), stream->StreamID())); michael@0: if(mHashHttp2.Get(key)) michael@0: return false; michael@0: mHashHttp2.Put(key, stream); michael@0: return true; michael@0: } michael@0: michael@0: Http2PushedStream * michael@0: SpdyPushCache::RemovePushedStreamHttp2(nsCString key) michael@0: { michael@0: Http2PushedStream *rv = mHashHttp2.Get(key); michael@0: LOG3(("SpdyPushCache::RemovePushedStreamHttp2 %s 0x%X\n", michael@0: key.get(), rv ? rv->StreamID() : 0)); michael@0: if (rv) michael@0: mHashHttp2.Remove(key); michael@0: return rv; michael@0: } michael@0: michael@0: } // namespace mozilla::net michael@0: } // namespace mozilla michael@0: