tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

nsAHttpConnection.h (15532B)


      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 nsAHttpConnection_h__
      6 #define nsAHttpConnection_h__
      7 
      8 #include "mozilla/net/DNS.h"
      9 #include "nsHttp.h"
     10 #include "nsISupports.h"
     11 #include "nsAHttpTransaction.h"
     12 #include "WebTransportSessionBase.h"
     13 #include "HttpTrafficAnalyzer.h"
     14 #include "nsIRequest.h"
     15 
     16 class nsIAsyncInputStream;
     17 class nsIAsyncOutputStream;
     18 class nsISocketTransport;
     19 class nsITLSSocketControl;
     20 
     21 namespace mozilla {
     22 namespace net {
     23 
     24 class nsHttpConnectionInfo;
     25 class HttpConnectionBase;
     26 class nsHttpRequestHead;
     27 class nsHttpResponseHead;
     28 
     29 //-----------------------------------------------------------------------------
     30 // Abstract base class for a HTTP connection
     31 //-----------------------------------------------------------------------------
     32 
     33 // 5a66aed7-eede-468b-ac2b-e5fb431fcc5c
     34 #define NS_AHTTPCONNECTION_IID \
     35  {0x5a66aed7, 0xeede, 0x468b, {0xac, 0x2b, 0xe5, 0xfb, 0x43, 0x1f, 0xcc, 0x5c}}
     36 
     37 class nsAHttpConnection : public nsISupports {
     38 public:
     39  NS_INLINE_DECL_STATIC_IID(NS_AHTTPCONNECTION_IID)
     40 
     41  NS_DECL_THREADSAFE_ISUPPORTS
     42 
     43  //-------------------------------------------------------------------------
     44  // NOTE: these methods may only be called on the socket thread.
     45  //-------------------------------------------------------------------------
     46 
     47  //
     48  // called by a transaction when the response headers have all been read.
     49  // the connection can force the transaction to reset it's response headers,
     50  // and prepare for a new set of response headers, by setting |*reset=TRUE|.
     51  //
     52  // @return failure code to close the transaction.
     53  //
     54  [[nodiscard]] virtual nsresult OnHeadersAvailable(nsAHttpTransaction*,
     55                                                    nsHttpRequestHead*,
     56                                                    nsHttpResponseHead*,
     57                                                    bool* reset) = 0;
     58 
     59  //
     60  // called by a transaction to resume either sending or receiving data
     61  // after a transaction returned NS_BASE_STREAM_WOULD_BLOCK from its
     62  // ReadSegments/WriteSegments methods.
     63  //
     64  [[nodiscard]] virtual nsresult ResumeSend() = 0;
     65  [[nodiscard]] virtual nsresult ResumeRecv() = 0;
     66 
     67  // called by a transaction to force a "send/recv from network" iteration
     68  // even if not scheduled by socket associated with connection
     69  [[nodiscard]] virtual nsresult ForceSend() = 0;
     70  [[nodiscard]] virtual nsresult ForceRecv() = 0;
     71 
     72  // After a connection has had ResumeSend() called by a transaction,
     73  // and it is ready to write to the network it may need to know the
     74  // transaction that has data to write. This is only an issue for
     75  // multiplexed protocols like SPDY - h1
     76  // implicitly has this information in a 1:1 relationship with the
     77  // transaction(s) they manage.
     78  virtual void TransactionHasDataToWrite(nsAHttpTransaction*) {
     79    // by default do nothing - only multiplexed protocols need to overload
     80  }
     81 
     82  // This is the companion to *HasDataToWrite() for the case
     83  // when a gecko caller has called ResumeRecv() after being paused
     84  virtual void TransactionHasDataToRecv(nsAHttpTransaction*) {
     85    // by default do nothing - only multiplexed protocols need to overload
     86  }
     87 
     88  // called by the connection manager to close a transaction being processed
     89  // by this connection.
     90  //
     91  // @param transaction
     92  //        the transaction being closed.
     93  // @param reason
     94  //        the reason for closing the transaction.  NS_BASE_STREAM_CLOSED
     95  //        is equivalent to NS_OK.
     96  //
     97  virtual void CloseTransaction(nsAHttpTransaction* transaction,
     98                                nsresult reason) = 0;
     99 
    100  // get a reference to the connection's connection info object.
    101  virtual void GetConnectionInfo(nsHttpConnectionInfo**) = 0;
    102 
    103  // get the transport level information for this connection. This may fail
    104  // if it is in use.
    105  [[nodiscard]] virtual nsresult TakeTransport(nsISocketTransport**,
    106                                               nsIAsyncInputStream**,
    107                                               nsIAsyncOutputStream**) = 0;
    108 
    109  [[nodiscard]] virtual WebTransportSessionBase* GetWebTransportSession(
    110      nsAHttpTransaction* aTransaction) = 0;
    111 
    112  // called by a transaction to get the TLS socket control from the socket.
    113  virtual void GetTLSSocketControl(nsITLSSocketControl**) = 0;
    114 
    115  // called by a transaction to determine whether or not the connection is
    116  // persistent... important in determining the end of a response.
    117  virtual bool IsPersistent() = 0;
    118 
    119  // called to determine or set if a connection has been reused.
    120  virtual bool IsReused() = 0;
    121  virtual void DontReuse() = 0;
    122 
    123  // called by a transaction when the transaction reads more from the socket
    124  // than it should have (eg. containing part of the next response).
    125  [[nodiscard]] virtual nsresult PushBack(const char* data,
    126                                          uint32_t length) = 0;
    127 
    128  // Used to determine if the connection wants read events even though
    129  // it has not written out a transaction. Used when a connection has issued
    130  // a preamble such as a proxy ssl CONNECT sequence.
    131  virtual bool IsProxyConnectInProgress() = 0;
    132 
    133  // Used by a transaction to manage the state of previous response bodies on
    134  // the same connection and work around buggy servers.
    135  virtual bool LastTransactionExpectedNoContent() = 0;
    136  virtual void SetLastTransactionExpectedNoContent(bool) = 0;
    137 
    138  // Transfer the base http connection object along with a
    139  // reference to it to the caller.
    140  virtual already_AddRefed<HttpConnectionBase> TakeHttpConnection() = 0;
    141 
    142  // Like TakeHttpConnection() but do not drop our own ref
    143  virtual already_AddRefed<HttpConnectionBase> HttpConnection() = 0;
    144 
    145  // Get the nsISocketTransport used by the connection without changing
    146  // references or ownership.
    147  virtual nsISocketTransport* Transport() = 0;
    148 
    149  // The number of transaction bytes written out on this HTTP Connection, does
    150  // not count CONNECT tunnel setup
    151  virtual int64_t BytesWritten() = 0;
    152 
    153  // Update the callbacks used to provide security info. May be called on
    154  // any thread.
    155  virtual void SetSecurityCallbacks(nsIInterfaceRequestor* aCallbacks) = 0;
    156 
    157  // nsHttp.h version
    158  virtual HttpVersion Version() = 0;
    159 
    160  // A notification of the current active tab id change.
    161  virtual void CurrentBrowserIdChanged(uint64_t id) = 0;
    162 
    163  // categories set by nsHttpTransaction to identify how this connection is
    164  // being used.
    165  virtual void SetTrafficCategory(HttpTrafficCategory) = 0;
    166 
    167  virtual nsresult GetSelfAddr(NetAddr* addr) = 0;
    168  virtual nsresult GetPeerAddr(NetAddr* addr) = 0;
    169  virtual bool ResolvedByTRR() = 0;
    170  virtual nsIRequest::TRRMode EffectiveTRRMode() = 0;
    171  virtual nsITRRSkipReason::value TRRSkipReason() = 0;
    172  virtual bool GetEchConfigUsed() = 0;
    173  virtual PRIntervalTime LastWriteTime() = 0;
    174  virtual void SetCloseReason(ConnectionCloseReason aReason) = 0;
    175 
    176  friend class DeleteAHttpConnection;
    177  void DeleteSelfOnSocketThread();
    178 
    179 protected:
    180  virtual ~nsAHttpConnection();
    181 };
    182 
    183 #define NS_DECL_NSAHTTPCONNECTION(fwdObject)                                 \
    184  [[nodiscard]] nsresult OnHeadersAvailable(                                 \
    185      nsAHttpTransaction*, nsHttpRequestHead*, nsHttpResponseHead*,          \
    186      bool* reset) override;                                                 \
    187  void CloseTransaction(nsAHttpTransaction*, nsresult) override;             \
    188  [[nodiscard]] nsresult TakeTransport(                                      \
    189      nsISocketTransport**, nsIAsyncInputStream**, nsIAsyncOutputStream**)   \
    190      override;                                                              \
    191  [[nodiscard]] WebTransportSessionBase* GetWebTransportSession(             \
    192      nsAHttpTransaction* aTransaction) override;                            \
    193  bool IsPersistent() override;                                              \
    194  bool IsReused() override;                                                  \
    195  void DontReuse() override;                                                 \
    196  [[nodiscard]] nsresult PushBack(const char*, uint32_t) override;           \
    197  already_AddRefed<HttpConnectionBase> TakeHttpConnection() override;        \
    198  already_AddRefed<HttpConnectionBase> HttpConnection() override;            \
    199  void CurrentBrowserIdChanged(uint64_t id) override;                        \
    200  /*                                                                         \
    201     Thes methods below have automatic definitions that just forward the     \
    202     function to a lower level connection object                             \
    203  */                                                                         \
    204  void GetConnectionInfo(nsHttpConnectionInfo** result) override {           \
    205    if (!(fwdObject)) {                                                      \
    206      *result = nullptr;                                                     \
    207      return;                                                                \
    208    }                                                                        \
    209    return (fwdObject)->GetConnectionInfo(result);                           \
    210  }                                                                          \
    211  void GetTLSSocketControl(nsITLSSocketControl** result) override {          \
    212    if (!(fwdObject)) {                                                      \
    213      *result = nullptr;                                                     \
    214      return;                                                                \
    215    }                                                                        \
    216    return (fwdObject)->GetTLSSocketControl(result);                         \
    217  }                                                                          \
    218  [[nodiscard]] nsresult ResumeSend() override {                             \
    219    if (!(fwdObject)) return NS_ERROR_FAILURE;                               \
    220    return (fwdObject)->ResumeSend();                                        \
    221  }                                                                          \
    222  [[nodiscard]] nsresult ResumeRecv() override {                             \
    223    if (!(fwdObject)) return NS_ERROR_FAILURE;                               \
    224    return (fwdObject)->ResumeRecv();                                        \
    225  }                                                                          \
    226  [[nodiscard]] nsresult ForceSend() override {                              \
    227    if (!(fwdObject)) return NS_ERROR_FAILURE;                               \
    228    return (fwdObject)->ForceSend();                                         \
    229  }                                                                          \
    230  [[nodiscard]] nsresult ForceRecv() override {                              \
    231    if (!(fwdObject)) return NS_ERROR_FAILURE;                               \
    232    return (fwdObject)->ForceRecv();                                         \
    233  }                                                                          \
    234  nsISocketTransport* Transport() override {                                 \
    235    if (!(fwdObject)) return nullptr;                                        \
    236    return (fwdObject)->Transport();                                         \
    237  }                                                                          \
    238  HttpVersion Version() override {                                           \
    239    return (fwdObject) ? (fwdObject)->Version()                              \
    240                       : mozilla::net::HttpVersion::UNKNOWN;                 \
    241  }                                                                          \
    242  bool IsProxyConnectInProgress() override {                                 \
    243    return (!(fwdObject)) ? false : (fwdObject)->IsProxyConnectInProgress(); \
    244  }                                                                          \
    245  bool LastTransactionExpectedNoContent() override {                         \
    246    return (!(fwdObject)) ? false                                            \
    247                          : (fwdObject)->LastTransactionExpectedNoContent(); \
    248  }                                                                          \
    249  void SetLastTransactionExpectedNoContent(bool val) override {              \
    250    if (fwdObject) (fwdObject)->SetLastTransactionExpectedNoContent(val);    \
    251  }                                                                          \
    252  int64_t BytesWritten() override {                                          \
    253    return (fwdObject) ? (fwdObject)->BytesWritten() : 0;                    \
    254  }                                                                          \
    255  void SetSecurityCallbacks(nsIInterfaceRequestor* aCallbacks) override {    \
    256    if (fwdObject) (fwdObject)->SetSecurityCallbacks(aCallbacks);            \
    257  }                                                                          \
    258  void SetTrafficCategory(HttpTrafficCategory aCategory) override {          \
    259    if (fwdObject) (fwdObject)->SetTrafficCategory(aCategory);               \
    260  }                                                                          \
    261  nsresult GetSelfAddr(NetAddr* addr) override {                             \
    262    if (!(fwdObject)) return NS_ERROR_FAILURE;                               \
    263    return (fwdObject)->GetSelfAddr(addr);                                   \
    264  }                                                                          \
    265  nsresult GetPeerAddr(NetAddr* addr) override {                             \
    266    if (!(fwdObject)) return NS_ERROR_FAILURE;                               \
    267    return (fwdObject)->GetPeerAddr(addr);                                   \
    268  }                                                                          \
    269  bool ResolvedByTRR() override {                                            \
    270    return (!(fwdObject)) ? false : (fwdObject)->ResolvedByTRR();            \
    271  }                                                                          \
    272  nsIRequest::TRRMode EffectiveTRRMode() override {                          \
    273    return (!(fwdObject)) ? nsIRequest::TRR_DEFAULT_MODE                     \
    274                          : (fwdObject)->EffectiveTRRMode();                 \
    275  }                                                                          \
    276  nsITRRSkipReason::value TRRSkipReason() override {                         \
    277    return (!(fwdObject)) ? nsITRRSkipReason::TRR_UNSET                      \
    278                          : (fwdObject)->TRRSkipReason();                    \
    279  }                                                                          \
    280  bool GetEchConfigUsed() override {                                         \
    281    return (!(fwdObject)) ? false : (fwdObject)->GetEchConfigUsed();         \
    282  }                                                                          \
    283  void SetCloseReason(ConnectionCloseReason aReason) override {              \
    284    if (fwdObject) (fwdObject)->SetCloseReason(aReason);                     \
    285  }                                                                          \
    286  PRIntervalTime LastWriteTime() override;
    287 
    288 // ThrottleResponse deliberately ommited since we want different implementation
    289 // for h1 and h2 connections.
    290 
    291 }  // namespace net
    292 }  // namespace mozilla
    293 
    294 #endif  // nsAHttpConnection_h__