Http2StreamTunnel.h (4656B)
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 #ifndef mozilla_net_Http2StreamTunnel_h 8 #define mozilla_net_Http2StreamTunnel_h 9 10 #include "Http2StreamBase.h" 11 #include "nsHttpConnection.h" 12 #include "nsWeakReference.h" 13 14 namespace mozilla::net { 15 16 class OutputStreamTunnel; 17 class InputStreamTunnel; 18 19 // c881f764-a183-45cb-9dec-d9872b2f47b2 20 #define NS_HTTP2STREAMTUNNEL_IID \ 21 {0xc881f764, 0xa183, 0x45cb, {0x9d, 0xec, 0xd9, 0x87, 0x2b, 0x2f, 0x47, 0xb2}} 22 23 class Http2StreamTunnel : public Http2StreamBase, public nsISocketTransport { 24 public: 25 NS_INLINE_DECL_STATIC_IID(NS_HTTP2STREAMTUNNEL_IID) 26 NS_DECL_ISUPPORTS_INHERITED 27 NS_DECL_NSITRANSPORT 28 NS_DECL_NSISOCKETTRANSPORT 29 30 Http2StreamTunnel(Http2Session* session, int32_t priority, uint64_t bcId, 31 nsHttpConnectionInfo* aConnectionInfo); 32 bool IsTunnel() override { return true; } 33 34 already_AddRefed<nsHttpConnection> CreateHttpConnection( 35 nsAHttpTransaction* httpTransaction, nsIInterfaceRequestor* aCallbacks, 36 PRIntervalTime aRtt, bool aIsExtendedCONNECT); 37 38 nsHttpConnectionInfo* ConnectionInfo() override { return mConnectionInfo; } 39 40 void SetRequestDone() { mSendClosed = true; } 41 nsresult Condition() override { return mCondition; } 42 void CloseStream(nsresult reason) override; 43 void DisableSpdy() override { 44 if (mTransaction) { 45 mTransaction->DisableHttp2ForProxy(); 46 } 47 } 48 void ReuseConnectionOnRestartOK(bool aReuse) override { 49 // Do nothing here. We'll never reuse the connection. 50 } 51 void MakeNonSticky() override {} 52 53 void SetTransactionId(uintptr_t aId) { mId = aId; }; 54 uintptr_t GetTransactionId() const { return mId; } 55 56 protected: 57 ~Http2StreamTunnel(); 58 nsresult CallToReadData(uint32_t count, uint32_t* countRead) override; 59 nsresult CallToWriteData(uint32_t count, uint32_t* countWritten) override; 60 void HandleResponseHeaders(nsACString& aHeadersOut, 61 int32_t httpResponseCode) override; 62 nsresult GenerateHeaders(nsCString& aCompressedData, 63 uint8_t& firstFrameFlags) override; 64 bool CloseSendStreamWhenDone() override { return mSendClosed; } 65 66 RefPtr<nsAHttpTransaction> mTransaction; 67 68 void ClearTransactionsBlockedOnTunnel(); 69 70 RefPtr<OutputStreamTunnel> mOutput; 71 RefPtr<InputStreamTunnel> mInput; 72 RefPtr<nsHttpConnectionInfo> mConnectionInfo; 73 bool mSendClosed{false}; 74 nsresult mCondition{NS_OK}; 75 // Stores the address of the transaction that created this Http2StreamTunnel. 76 uintptr_t mId{0}; 77 }; 78 79 // f9d10060-f5d4-443e-ba59-f84ea975c5f0 80 #define NS_OUTPUTSTREAMTUNNEL_IID \ 81 {0xf9d10060, 0xf5d4, 0x443e, {0xba, 0x59, 0xf8, 0x4e, 0xa9, 0x75, 0xc5, 0xf0}} 82 83 class OutputStreamTunnel : public nsIAsyncOutputStream { 84 public: 85 NS_INLINE_DECL_STATIC_IID(NS_OUTPUTSTREAMTUNNEL_IID) 86 NS_DECL_THREADSAFE_ISUPPORTS 87 NS_DECL_NSIOUTPUTSTREAM 88 NS_DECL_NSIASYNCOUTPUTSTREAM 89 90 explicit OutputStreamTunnel(Http2StreamTunnel* aStream); 91 92 nsresult OnSocketReady(nsresult condition); 93 void MaybeSetRequestDone(nsIOutputStreamCallback* aCallback); 94 95 private: 96 virtual ~OutputStreamTunnel(); 97 98 nsresult GetStream(Http2StreamTunnel** aStream); 99 nsresult GetSession(Http2Session** aSession); 100 101 WeakPtr<Http2StreamTunnel> mWeakStream; 102 nsCOMPtr<nsIOutputStreamCallback> mCallback; 103 nsresult mCondition{NS_OK}; 104 }; 105 106 class InputStreamTunnel : public nsIAsyncInputStream { 107 public: 108 NS_DECL_THREADSAFE_ISUPPORTS 109 NS_DECL_NSIINPUTSTREAM 110 NS_DECL_NSIASYNCINPUTSTREAM 111 112 explicit InputStreamTunnel(Http2StreamTunnel* aStream); 113 114 nsresult OnSocketReady(nsresult condition); 115 bool HasCallback() { return !!mCallback; } 116 117 private: 118 virtual ~InputStreamTunnel(); 119 120 nsresult GetStream(Http2StreamTunnel** aStream); 121 nsresult GetSession(Http2Session** aSession); 122 123 WeakPtr<Http2StreamTunnel> mWeakStream; 124 nsCOMPtr<nsIInputStreamCallback> mCallback; 125 nsresult mCondition{NS_OK}; 126 }; 127 128 class Http2StreamWebSocket : public Http2StreamTunnel { 129 public: 130 Http2StreamWebSocket(Http2Session* session, int32_t priority, uint64_t bcId, 131 nsHttpConnectionInfo* aConnectionInfo); 132 void CloseStream(nsresult reason) override; 133 134 protected: 135 virtual ~Http2StreamWebSocket(); 136 nsresult GenerateHeaders(nsCString& aCompressedData, 137 uint8_t& firstFrameFlags) override; 138 }; 139 140 } // namespace mozilla::net 141 142 #endif // mozilla_net_Http2StreamTunnel_h