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