Line data Source code
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 { namespace net {
16 :
17 : class nsHttpConnectionInfo;
18 : class nsHttpConnection;
19 :
20 : //-----------------------------------------------------------------------------
21 : // Abstract base class for a HTTP connection
22 : //-----------------------------------------------------------------------------
23 :
24 : // 5a66aed7-eede-468b-ac2b-e5fb431fcc5c
25 : #define NS_AHTTPCONNECTION_IID \
26 : { 0x5a66aed7, 0xeede, 0x468b, {0xac, 0x2b, 0xe5, 0xfb, 0x43, 0x1f, 0xcc, 0x5c }}
27 :
28 3 : class nsAHttpConnection : public nsISupports
29 : {
30 : public:
31 : NS_DECLARE_STATIC_IID_ACCESSOR(NS_AHTTPCONNECTION_IID)
32 :
33 : //-------------------------------------------------------------------------
34 : // NOTE: these methods may only be called on the socket thread.
35 : //-------------------------------------------------------------------------
36 :
37 : //
38 : // called by a transaction when the response headers have all been read.
39 : // the connection can force the transaction to reset it's response headers,
40 : // and prepare for a new set of response headers, by setting |*reset=TRUE|.
41 : //
42 : // @return failure code to close the transaction.
43 : //
44 : virtual MOZ_MUST_USE nsresult OnHeadersAvailable(nsAHttpTransaction *,
45 : nsHttpRequestHead *,
46 : nsHttpResponseHead *,
47 : bool *reset) = 0;
48 :
49 : //
50 : // called by a transaction to resume either sending or receiving data
51 : // after a transaction returned NS_BASE_STREAM_WOULD_BLOCK from its
52 : // ReadSegments/WriteSegments methods.
53 : //
54 : virtual MOZ_MUST_USE nsresult ResumeSend() = 0;
55 : virtual MOZ_MUST_USE nsresult ResumeRecv() = 0;
56 :
57 : // called by a transaction to force a "send/recv from network" iteration
58 : // even if not scheduled by socket associated with connection
59 : virtual MOZ_MUST_USE nsresult ForceSend() = 0;
60 : virtual MOZ_MUST_USE nsresult ForceRecv() = 0;
61 :
62 : // After a connection has had ResumeSend() called by a transaction,
63 : // and it is ready to write to the network it may need to know the
64 : // transaction that has data to write. This is only an issue for
65 : // multiplexed protocols like SPDY - h1
66 : // implicitly has this information in a 1:1 relationship with the
67 : // transaction(s) they manage.
68 0 : virtual void TransactionHasDataToWrite(nsAHttpTransaction *)
69 : {
70 : // by default do nothing - only multiplexed protocols need to overload
71 0 : return;
72 : }
73 :
74 : // This is the companion to *HasDataToWrite() for the case
75 : // when a gecko caller has called ResumeRecv() after being paused
76 0 : virtual void TransactionHasDataToRecv(nsAHttpTransaction *)
77 : {
78 : // by default do nothing - only multiplexed protocols need to overload
79 0 : return;
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 :
151 : NS_DEFINE_STATIC_IID_ACCESSOR(nsAHttpConnection, NS_AHTTPCONNECTION_IID)
152 :
153 : #define NS_DECL_NSAHTTPCONNECTION(fwdObject) \
154 : MOZ_MUST_USE nsresult OnHeadersAvailable(nsAHttpTransaction *, \
155 : nsHttpRequestHead *, \
156 : nsHttpResponseHead *, \
157 : bool *reset) override; \
158 : void CloseTransaction(nsAHttpTransaction *, nsresult) override; \
159 : MOZ_MUST_USE nsresult TakeTransport(nsISocketTransport **, \
160 : nsIAsyncInputStream **, \
161 : nsIAsyncOutputStream **) override; \
162 : bool IsPersistent() override; \
163 : bool IsReused() override; \
164 : void DontReuse() override; \
165 : MOZ_MUST_USE nsresult PushBack(const char *, uint32_t) override; \
166 : already_AddRefed<nsHttpConnection> TakeHttpConnection() override; \
167 : already_AddRefed<nsHttpConnection> HttpConnection() override; \
168 : /* \
169 : Thes methods below have automatic definitions that just forward the \
170 : function to a lower level connection object \
171 : */ \
172 : void GetConnectionInfo(nsHttpConnectionInfo **result) \
173 : override \
174 : { \
175 : if (!(fwdObject)) { \
176 : *result = nullptr; \
177 : return; \
178 : } \
179 : return (fwdObject)->GetConnectionInfo(result); \
180 : } \
181 : void GetSecurityInfo(nsISupports **result) override \
182 : { \
183 : if (!(fwdObject)) { \
184 : *result = nullptr; \
185 : return; \
186 : } \
187 : return (fwdObject)->GetSecurityInfo(result); \
188 : } \
189 : MOZ_MUST_USE nsresult ResumeSend() override \
190 : { \
191 : if (!(fwdObject)) \
192 : return NS_ERROR_FAILURE; \
193 : return (fwdObject)->ResumeSend(); \
194 : } \
195 : MOZ_MUST_USE nsresult ResumeRecv() override \
196 : { \
197 : if (!(fwdObject)) \
198 : return NS_ERROR_FAILURE; \
199 : return (fwdObject)->ResumeRecv(); \
200 : } \
201 : MOZ_MUST_USE nsresult ForceSend() override \
202 : { \
203 : if (!(fwdObject)) \
204 : return NS_ERROR_FAILURE; \
205 : return (fwdObject)->ForceSend(); \
206 : } \
207 : MOZ_MUST_USE nsresult ForceRecv() override \
208 : { \
209 : if (!(fwdObject)) \
210 : return NS_ERROR_FAILURE; \
211 : return (fwdObject)->ForceRecv(); \
212 : } \
213 : nsISocketTransport *Transport() \
214 : override \
215 : { \
216 : if (!(fwdObject)) \
217 : return nullptr; \
218 : return (fwdObject)->Transport(); \
219 : } \
220 : uint32_t Version() override \
221 : { \
222 : return (fwdObject) ? \
223 : (fwdObject)->Version() : \
224 : NS_HTTP_VERSION_UNKNOWN; \
225 : } \
226 : bool IsProxyConnectInProgress() override \
227 : { \
228 : return (!fwdObject) ? false : \
229 : (fwdObject)->IsProxyConnectInProgress(); \
230 : } \
231 : bool LastTransactionExpectedNoContent() override \
232 : { \
233 : return (!fwdObject) ? false : \
234 : (fwdObject)->LastTransactionExpectedNoContent(); \
235 : } \
236 : void SetLastTransactionExpectedNoContent(bool val) \
237 : override \
238 : { \
239 : if (fwdObject) \
240 : (fwdObject)->SetLastTransactionExpectedNoContent(val); \
241 : } \
242 : int64_t BytesWritten() override \
243 : { return fwdObject ? (fwdObject)->BytesWritten() : 0; } \
244 : void SetSecurityCallbacks(nsIInterfaceRequestor* aCallbacks) \
245 : override \
246 : { \
247 : if (fwdObject) \
248 : (fwdObject)->SetSecurityCallbacks(aCallbacks); \
249 : }
250 :
251 : // ThrottleResponse deliberately ommited since we want different implementation
252 : // for h1 and h2 connections.
253 :
254 : } // namespace net
255 : } // namespace mozilla
256 :
257 : #endif // nsAHttpConnection_h__
|