ASpdySession.cpp 3.1 KB

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