nsAHttpTransaction.h (13680B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 #ifndef nsAHttpTransaction_h__ 6 #define nsAHttpTransaction_h__ 7 8 #include "nsTArray.h" 9 #include "nsWeakReference.h" 10 #include "nsIRequest.h" 11 #include "nsITRRSkipReason.h" 12 #include "nsILoadInfo.h" 13 14 #ifdef Status 15 /* Xlib headers insist on this for some reason... Nuke it because 16 it'll override our member name */ 17 typedef Status __StatusTmp; 18 # undef Status 19 typedef __StatusTmp Status; 20 #endif 21 22 class nsIDNSHTTPSSVCRecord; 23 class nsIInterfaceRequestor; 24 class nsIRequestContext; 25 class nsISVCBRecord; 26 class nsITLSSocketControl; 27 class nsITransport; 28 29 namespace mozilla { 30 namespace net { 31 32 class nsAHttpConnection; 33 class nsAHttpSegmentReader; 34 class nsAHttpSegmentWriter; 35 class nsHttpTransaction; 36 class nsHttpRequestHead; 37 class nsHttpConnectionInfo; 38 class NullHttpTransaction; 39 40 enum class ExtendedCONNECTSupport { UNSURE, NO_SUPPORT, SUPPORTED }; 41 42 //---------------------------------------------------------------------------- 43 // Abstract base class for a HTTP transaction: 44 // 45 // A transaction is a "sink" for the response data. The connection pushes 46 // data to the transaction by writing to it. The transaction supports 47 // WriteSegments and may refuse to accept data if its buffers are full (its 48 // write function returns NS_BASE_STREAM_WOULD_BLOCK in this case). 49 //---------------------------------------------------------------------------- 50 51 // 2af6d634-13e3-494c-8903-c9dce5c22fc0 52 #define NS_AHTTPTRANSACTION_IID \ 53 {0x2af6d634, 0x13e3, 0x494c, {0x89, 0x03, 0xc9, 0xdc, 0xe5, 0xc2, 0x2f, 0xc0}} 54 55 class nsAHttpTransaction : public nsSupportsWeakReference { 56 public: 57 NS_INLINE_DECL_STATIC_IID(NS_AHTTPTRANSACTION_IID) 58 59 // called by the connection when it takes ownership of the transaction. 60 virtual void SetConnection(nsAHttpConnection*) = 0; 61 62 // called by the connection after a successfull activation of this transaction 63 // in other words, tells the transaction it transitioned to the "active" 64 // state. 65 virtual void OnActivated() {} 66 67 // used to obtain the connection associated with this transaction 68 virtual nsAHttpConnection* Connection() = 0; 69 70 // called by the connection to get security callbacks to set on the 71 // socket transport. 72 virtual void GetSecurityCallbacks(nsIInterfaceRequestor**) = 0; 73 74 // called to report socket status (see nsITransportEventSink) 75 virtual void OnTransportStatus(nsITransport* transport, nsresult status, 76 int64_t progress) = 0; 77 78 // called to check the transaction status. 79 virtual bool IsDone() = 0; 80 virtual nsresult Status() = 0; 81 virtual uint32_t Caps() = 0; 82 83 // called to read request data from the transaction. 84 [[nodiscard]] virtual nsresult ReadSegments(nsAHttpSegmentReader* reader, 85 uint32_t count, 86 uint32_t* countRead) = 0; 87 88 // called to write response data to the transaction. 89 [[nodiscard]] virtual nsresult WriteSegments(nsAHttpSegmentWriter* writer, 90 uint32_t count, 91 uint32_t* countWritten) = 0; 92 93 // These versions of the functions allow the overloader to specify whether or 94 // not it is safe to call *Segments() in a loop while they return OK. 95 // The callee should turn again to false if it is not, otherwise leave 96 // untouched 97 [[nodiscard]] virtual nsresult ReadSegmentsAgain(nsAHttpSegmentReader* reader, 98 uint32_t count, 99 uint32_t* countRead, 100 bool* again) { 101 return ReadSegments(reader, count, countRead); 102 } 103 [[nodiscard]] virtual nsresult WriteSegmentsAgain( 104 nsAHttpSegmentWriter* writer, uint32_t count, uint32_t* countWritten, 105 bool* again) { 106 return WriteSegments(writer, count, countWritten); 107 } 108 109 // called to close the transaction 110 virtual void Close(nsresult reason) = 0; 111 112 // called to indicate a failure with proxy CONNECT 113 virtual void SetProxyConnectFailed() = 0; 114 115 // called to retrieve the request headers of the transaction 116 virtual nsHttpRequestHead* RequestHead() = 0; 117 118 // determine the number of real http/1.x transactions on this 119 // abstract object. Pipelines had multiple, SPDY has 0, 120 // normal http transactions have 1. 121 virtual uint32_t Http1xTransactionCount() = 0; 122 123 // called to remove the unused sub transactions from an object that can 124 // handle multiple transactions simultaneously (i.e. h2). 125 // 126 // Returns NS_ERROR_NOT_IMPLEMENTED if the object does not implement 127 // sub-transactions. 128 // 129 // Returns NS_ERROR_ALREADY_OPENED if the subtransactions have been 130 // at least partially written and cannot be moved. 131 // 132 [[nodiscard]] virtual nsresult TakeSubTransactions( 133 nsTArray<RefPtr<nsAHttpTransaction> >& outTransactions) = 0; 134 135 // Occasionally the abstract interface has to give way to base implementations 136 // to respect differences between spdy, h2, etc.. 137 // These Query* (and IsNullTransaction()) functions provide a way to do 138 // that without using xpcom or rtti. Any calling code that can't deal with 139 // a null response from one of them probably shouldn't be using 140 // nsAHttpTransaction 141 142 // equivalent to !!dynamic_cast<NullHttpTransaction *>(this) 143 // A null transaction is expected to return BASE_STREAM_CLOSED on all of 144 // its IO functions all the time. 145 virtual bool IsNullTransaction() { return false; } 146 virtual NullHttpTransaction* QueryNullTransaction() { return nullptr; } 147 148 // If we used rtti this would be the result of doing 149 // dynamic_cast<nsHttpTransaction *>(this).. i.e. it can be nullptr for 150 // non nsHttpTransaction implementations of nsAHttpTransaction 151 virtual nsHttpTransaction* QueryHttpTransaction() { return nullptr; } 152 153 // return the request context associated with the transaction 154 virtual nsIRequestContext* RequestContext() { return nullptr; } 155 156 // return the connection information associated with the transaction 157 virtual nsHttpConnectionInfo* ConnectionInfo() = 0; 158 159 // The base definition of these is done in nsHttpTransaction.cpp 160 virtual bool ResponseTimeoutEnabled() const; 161 virtual PRIntervalTime ResponseTimeout(); 162 163 // conceptually the socket control is part of the connection, but sometimes 164 // in the case of TLS tunneled within TLS the transaction might present 165 // a more specific socket control that cannot be represented as a layer in 166 // the connection due to multiplexing. This interface represents such an 167 // overload. If it returns NS_FAILURE the connection should be considered 168 // authoritative. 169 [[nodiscard]] virtual nsresult GetTransactionTLSSocketControl( 170 nsITLSSocketControl**) { 171 return NS_ERROR_NOT_IMPLEMENTED; 172 } 173 174 virtual void DisableSpdy() {} 175 // When called, we disallow to connect through a Http/2 proxy. 176 virtual void DisableHttp2ForProxy() {} 177 virtual void DisableHttp3(bool aAllowRetryHTTPSRR) {} 178 virtual void MakeNonSticky() {} 179 virtual void MakeRestartable() {} 180 virtual void ReuseConnectionOnRestartOK(bool) {} 181 virtual void SetIsHttp2Websocket(bool) {} 182 virtual bool IsHttp2Websocket() { return false; } 183 virtual void SetTRRInfo(nsIRequest::TRRMode aMode, 184 TRRSkippedReason aSkipReason) {}; 185 virtual bool AllowedToConnectToIpAddressSpace( 186 nsILoadInfo::IPAddressSpace aTargetIpAddressSpace) { 187 return true; 188 }; 189 190 // We call this function if we want to use alt-svc host again on the next 191 // restart. If this function is not called on the next restart the 192 // transaction will use the original route. 193 // For example in case we receive a GOAWAY frame from a server, we can 194 // restart and use the same alt-svc. If we get VersionFallback we do not 195 // want to use the alt-svc on the restart. 196 virtual void DoNotRemoveAltSvc() {} 197 198 // We call this function if we do want to reset IP family preference again on 199 // the next restart. 200 virtual void DoNotResetIPFamilyPreference() {} 201 202 // Returns true if early-data is possible and transaction will remember 203 // that it is in 0RTT mode (to know should it rewide transaction or not 204 // in the case of an error). 205 [[nodiscard]] virtual bool Do0RTT() { return false; } 206 // This function will be called when a tls handshake has been finished and 207 // we know whether early-data that was sent has been accepted or not, e.g. 208 // do we need to restart a transaction. This will be called only if Do0RTT 209 // returns true. 210 // If aRestart parameter is true we need to restart the transaction, 211 // otherwise the erly-data has been accepted and we can continue the 212 // transaction. 213 // If aAlpnChanged is true (and we were assuming http/2), we'll need to take 214 // the transactions out of the session, rewind them all, and start them back 215 // over as http/1 transactions 216 // The function will return success or failure of the transaction restart. 217 [[nodiscard]] virtual nsresult Finish0RTT(bool aRestart, bool aAlpnChanged) { 218 return NS_ERROR_NOT_IMPLEMENTED; 219 } 220 221 virtual uint64_t BrowserId() { 222 MOZ_ASSERT(false); 223 return 0; 224 } 225 226 virtual void OnProxyConnectComplete(int32_t aResponseCode) {} 227 228 virtual nsresult FetchHTTPSRR() { return NS_ERROR_NOT_IMPLEMENTED; } 229 virtual nsresult OnHTTPSRRAvailable(nsIDNSHTTPSSVCRecord* aHTTPSSVCRecord, 230 nsISVCBRecord* aHighestPriorityRecord, 231 const nsACString& aCname) { 232 return NS_ERROR_NOT_IMPLEMENTED; 233 } 234 virtual bool IsForWebTransport() { return false; } 235 virtual bool IsResettingForTunnelConn() { return false; } 236 virtual void SetResettingForTunnelConn(bool aValue) {} 237 238 virtual void InvokeCallback() {} 239 virtual bool IsForFallback() { return false; } 240 }; 241 242 #define NS_DECL_NSAHTTPTRANSACTION \ 243 void SetConnection(nsAHttpConnection*) override; \ 244 nsAHttpConnection* Connection() override; \ 245 void GetSecurityCallbacks(nsIInterfaceRequestor**) override; \ 246 void OnTransportStatus(nsITransport* transport, nsresult status, \ 247 int64_t progress) override; \ 248 bool IsDone() override; \ 249 nsresult Status() override; \ 250 uint32_t Caps() override; \ 251 [[nodiscard]] virtual nsresult ReadSegments(nsAHttpSegmentReader*, uint32_t, \ 252 uint32_t*) override; \ 253 [[nodiscard]] virtual nsresult WriteSegments(nsAHttpSegmentWriter*, \ 254 uint32_t, uint32_t*) override; \ 255 virtual void Close(nsresult reason) override; \ 256 nsHttpConnectionInfo* ConnectionInfo() override; \ 257 void SetProxyConnectFailed() override; \ 258 virtual nsHttpRequestHead* RequestHead() override; \ 259 uint32_t Http1xTransactionCount() override; \ 260 [[nodiscard]] nsresult TakeSubTransactions( \ 261 nsTArray<RefPtr<nsAHttpTransaction> >& outTransactions) override; 262 263 //----------------------------------------------------------------------------- 264 // nsAHttpSegmentReader 265 //----------------------------------------------------------------------------- 266 267 class nsAHttpSegmentReader { 268 NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING 269 270 public: 271 // any returned failure code stops segment iteration 272 [[nodiscard]] virtual nsresult OnReadSegment(const char* segment, 273 uint32_t count, 274 uint32_t* countRead) = 0; 275 276 // Ask the segment reader to commit to accepting size bytes of 277 // data from subsequent OnReadSegment() calls or throw hard 278 // (i.e. not wouldblock) exceptions. Implementations 279 // can return NS_ERROR_FAILURE if they never make commitments of that size 280 // (the default), NS_OK if they make the commitment, or 281 // NS_BASE_STREAM_WOULD_BLOCK if they cannot make the 282 // commitment now but might in the future and forceCommitment is not true . 283 // (forceCommitment requires a hard failure or OK at this moment.) 284 // 285 // SpdySession uses this to make sure frames are atomic. 286 [[nodiscard]] virtual nsresult CommitToSegmentSize(uint32_t size, 287 bool forceCommitment) { 288 return NS_ERROR_FAILURE; 289 } 290 }; 291 292 #define NS_DECL_NSAHTTPSEGMENTREADER \ 293 [[nodiscard]] nsresult OnReadSegment(const char*, uint32_t, uint32_t*) \ 294 override; 295 296 //----------------------------------------------------------------------------- 297 // nsAHttpSegmentWriter 298 //----------------------------------------------------------------------------- 299 300 class nsAHttpSegmentWriter { 301 public: 302 // any returned failure code stops segment iteration 303 [[nodiscard]] virtual nsresult OnWriteSegment(char* segment, uint32_t count, 304 uint32_t* countWritten) = 0; 305 }; 306 307 #define NS_DECL_NSAHTTPSEGMENTWRITER \ 308 [[nodiscard]] nsresult OnWriteSegment(char*, uint32_t, uint32_t*) override; 309 310 } // namespace net 311 } // namespace mozilla 312 313 #endif // nsAHttpTransaction_h__