HttpConnectionUDP.h (5258B)
1 /* -*- Mode: C++; tab-width: 4; 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 6 #ifndef HttpConnectionUDP_h__ 7 #define HttpConnectionUDP_h__ 8 9 #include "HttpConnectionBase.h" 10 #include "nsHttpConnectionInfo.h" 11 #include "nsHttpResponseHead.h" 12 #include "nsAHttpTransaction.h" 13 #include "nsCOMPtr.h" 14 #include "nsProxyRelease.h" 15 #include "prinrval.h" 16 #include "mozilla/Mutex.h" 17 #include "ARefBase.h" 18 #include "TimingStruct.h" 19 #include "HttpTrafficAnalyzer.h" 20 21 #include "nsIAsyncInputStream.h" 22 #include "nsIAsyncOutputStream.h" 23 #include "nsISupportsPriority.h" 24 #include "nsIInterfaceRequestor.h" 25 #include "nsITimer.h" 26 #include "Http3Session.h" 27 28 class nsIDNSRecord; 29 class nsISocketTransport; 30 class nsITLSSocketControl; 31 32 namespace mozilla { 33 namespace net { 34 35 class nsHttpHandler; 36 class ASpdySession; 37 38 // 1dcc863e-db90-4652-a1fe-13fea0b54e46 39 #define HTTPCONNECTIONUDP_IID \ 40 {0xb97d2036, 0xb441, 0x48be, {0xb3, 0x1e, 0x25, 0x3e, 0xe8, 0x32, 0xdd, 0x67}} 41 42 //----------------------------------------------------------------------------- 43 // HttpConnectionUDP - represents a connection to a HTTP3 server 44 // 45 // NOTE: this objects lives on the socket thread only. it should not be 46 // accessed from any other thread. 47 //----------------------------------------------------------------------------- 48 49 class HttpConnectionUDP final : public HttpConnectionBase, 50 public nsIUDPSocketSyncListener, 51 public nsIInterfaceRequestor { 52 private: 53 virtual ~HttpConnectionUDP(); 54 55 public: 56 NS_INLINE_DECL_STATIC_IID(HTTPCONNECTIONUDP_IID) 57 NS_DECL_HTTPCONNECTIONBASE 58 NS_DECL_THREADSAFE_ISUPPORTS 59 NS_DECL_NSIUDPSOCKETSYNCLISTENER 60 NS_DECL_NSIINTERFACEREQUESTOR 61 62 HttpConnectionUDP(); 63 64 [[nodiscard]] nsresult Init(nsHttpConnectionInfo* info, 65 nsIDNSRecord* dnsRecord, nsresult status, 66 nsIInterfaceRequestor* callbacks, uint32_t caps); 67 [[nodiscard]] nsresult InitWithSocket(nsHttpConnectionInfo* info, 68 nsIUDPSocket* aSocket, 69 NetAddr aPeerAddr, 70 nsIInterfaceRequestor* callbacks, 71 uint32_t caps); 72 73 friend class HttpConnectionUDPForceIO; 74 75 [[nodiscard]] static nsresult ReadFromStream(nsIInputStream*, void*, 76 const char*, uint32_t, uint32_t, 77 uint32_t*); 78 79 bool UsingHttp3() override { return true; } 80 81 void OnQuicTimeoutExpired(); 82 83 int64_t BytesWritten() override; 84 85 nsresult GetSelfAddr(NetAddr* addr) override; 86 nsresult GetPeerAddr(NetAddr* addr) override; 87 bool ResolvedByTRR() override; 88 nsIRequest::TRRMode EffectiveTRRMode() override; 89 TRRSkippedReason TRRSkipReason() override; 90 bool GetEchConfigUsed() override { return false; } 91 92 void NotifyDataRead(); 93 void NotifyDataWrite(); 94 95 Http3Stats GetStats(); 96 97 void ResetTransaction(nsHttpTransaction* aHttpTransaction); 98 99 void HandleTunnelResponse(nsHttpTransaction* aHttpTransaction, 100 uint16_t responseStatus, bool* reset); 101 102 nsresult CreateTunnelStream(nsAHttpTransaction* httpTransaction, 103 HttpConnectionBase** aHttpConnection, 104 bool aIsExtendedCONNECT = false) override; 105 106 void OnConnected(); 107 108 private: 109 nsresult InitCommon(nsIUDPSocket* aSocket, const NetAddr& aPeerAddr, 110 nsIInterfaceRequestor* callbacks, uint32_t caps, 111 bool isInTunnel); 112 [[nodiscard]] nsresult OnTransactionDone(nsresult reason); 113 nsresult RecvData(); 114 nsresult SendData(); 115 already_AddRefed<nsIInputStream> CreateProxyConnectStream( 116 nsAHttpTransaction* trans); 117 118 private: 119 RefPtr<nsHttpHandler> mHttpHandler; // keep gHttpHandler alive 120 121 RefPtr<nsIAsyncInputStream> mInputOverflow; 122 123 bool mConnectedTransport = false; 124 bool mDontReuse = false; 125 bool mIsReused = false; 126 bool mLastTransactionExpectedNoContent = false; 127 bool mConnected = false; 128 129 int32_t mPriority = nsISupportsPriority::PRIORITY_NORMAL; 130 131 private: 132 // For ForceSend() 133 static void ForceSendIO(nsITimer* aTimer, void* aClosure); 134 [[nodiscard]] nsresult MaybeForceSendIO(); 135 bool mForceSendPending = false; 136 nsCOMPtr<nsITimer> mForceSendTimer; 137 138 PRIntervalTime mLastRequestBytesSentTime = 0; 139 nsCOMPtr<nsIUDPSocket> mSocket; 140 141 nsCOMPtr<nsINetAddr> mSelfAddr; 142 nsCOMPtr<nsINetAddr> mPeerAddr; 143 bool mResolvedByTRR = false; 144 nsIRequest::TRRMode mEffectiveTRRMode = nsIRequest::TRR_DEFAULT_MODE; 145 TRRSkippedReason mTRRSkipReason = nsITRRSkipReason::TRR_UNSET; 146 147 private: 148 // Http3 149 RefPtr<Http3Session> mHttp3Session; 150 nsCString mAlpnToken; 151 bool mIsInTunnel = false; 152 bool mProxyConnectSucceeded = false; 153 nsTArray<RefPtr<nsHttpTransaction>> mQueuedHttpConnectTransaction; 154 nsTArray<RefPtr<nsHttpTransaction>> mQueuedConnectUdpTransaction; 155 }; 156 157 } // namespace net 158 } // namespace mozilla 159 160 #endif // HttpConnectionUDP_h__