|
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 "nsISupports.h" |
|
9 #include "nsTArray.h" |
|
10 |
|
11 class nsIInterfaceRequestor; |
|
12 class nsIEventTarget; |
|
13 class nsITransport; |
|
14 class nsILoadGroupConnectionInfo; |
|
15 |
|
16 namespace mozilla { namespace net { |
|
17 |
|
18 class nsAHttpConnection; |
|
19 class nsAHttpSegmentReader; |
|
20 class nsAHttpSegmentWriter; |
|
21 class nsHttpTransaction; |
|
22 class nsHttpPipeline; |
|
23 class nsHttpRequestHead; |
|
24 |
|
25 //---------------------------------------------------------------------------- |
|
26 // Abstract base class for a HTTP transaction: |
|
27 // |
|
28 // A transaction is a "sink" for the response data. The connection pushes |
|
29 // data to the transaction by writing to it. The transaction supports |
|
30 // WriteSegments and may refuse to accept data if its buffers are full (its |
|
31 // write function returns NS_BASE_STREAM_WOULD_BLOCK in this case). |
|
32 //---------------------------------------------------------------------------- |
|
33 |
|
34 class nsAHttpTransaction : public nsISupports |
|
35 { |
|
36 public: |
|
37 // called by the connection when it takes ownership of the transaction. |
|
38 virtual void SetConnection(nsAHttpConnection *) = 0; |
|
39 |
|
40 // used to obtain the connection associated with this transaction |
|
41 virtual nsAHttpConnection *Connection() = 0; |
|
42 |
|
43 // called by the connection to get security callbacks to set on the |
|
44 // socket transport. |
|
45 virtual void GetSecurityCallbacks(nsIInterfaceRequestor **) = 0; |
|
46 |
|
47 // called to report socket status (see nsITransportEventSink) |
|
48 virtual void OnTransportStatus(nsITransport* transport, |
|
49 nsresult status, uint64_t progress) = 0; |
|
50 |
|
51 // called to check the transaction status. |
|
52 virtual bool IsDone() = 0; |
|
53 virtual nsresult Status() = 0; |
|
54 virtual uint32_t Caps() = 0; |
|
55 |
|
56 // called to notify that a requested DNS cache entry was refreshed. |
|
57 virtual void SetDNSWasRefreshed() = 0; |
|
58 |
|
59 // called to find out how much request data is available for writing. |
|
60 virtual uint64_t Available() = 0; |
|
61 |
|
62 // called to read request data from the transaction. |
|
63 virtual nsresult ReadSegments(nsAHttpSegmentReader *reader, |
|
64 uint32_t count, uint32_t *countRead) = 0; |
|
65 |
|
66 // called to write response data to the transaction. |
|
67 virtual nsresult WriteSegments(nsAHttpSegmentWriter *writer, |
|
68 uint32_t count, uint32_t *countWritten) = 0; |
|
69 |
|
70 // called to close the transaction |
|
71 virtual void Close(nsresult reason) = 0; |
|
72 |
|
73 // called to indicate a failure with proxy CONNECT |
|
74 virtual void SetProxyConnectFailed() = 0; |
|
75 |
|
76 // called to retrieve the request headers of the transaction |
|
77 virtual nsHttpRequestHead *RequestHead() = 0; |
|
78 |
|
79 // determine the number of real http/1.x transactions on this |
|
80 // abstract object. Pipelines may have multiple, SPDY has 0, |
|
81 // normal http transactions have 1. |
|
82 virtual uint32_t Http1xTransactionCount() = 0; |
|
83 |
|
84 // called to remove the unused sub transactions from an object that can |
|
85 // handle multiple transactions simultaneously (i.e. pipelines or spdy). |
|
86 // |
|
87 // Returns NS_ERROR_NOT_IMPLEMENTED if the object does not implement |
|
88 // sub-transactions. |
|
89 // |
|
90 // Returns NS_ERROR_ALREADY_OPENED if the subtransactions have been |
|
91 // at least partially written and cannot be moved. |
|
92 // |
|
93 virtual nsresult TakeSubTransactions( |
|
94 nsTArray<nsRefPtr<nsAHttpTransaction> > &outTransactions) = 0; |
|
95 |
|
96 // called to add a sub-transaction in the case of pipelined transactions |
|
97 // classes that do not implement sub transactions |
|
98 // return NS_ERROR_NOT_IMPLEMENTED |
|
99 virtual nsresult AddTransaction(nsAHttpTransaction *transaction) = 0; |
|
100 |
|
101 // The total length of the outstanding pipeline comprised of transacations |
|
102 // and sub-transactions. |
|
103 virtual uint32_t PipelineDepth() = 0; |
|
104 |
|
105 // Used to inform the connection that it is being used in a pipelined |
|
106 // context. That may influence the handling of some errors. |
|
107 // The value is the pipeline position (> 1). |
|
108 virtual nsresult SetPipelinePosition(int32_t) = 0; |
|
109 virtual int32_t PipelinePosition() = 0; |
|
110 |
|
111 // Occasionally the abstract interface has to give way to base implementations |
|
112 // to respect differences between spdy, pipelines, etc.. |
|
113 // These Query* (and IsNUllTransaction()) functions provide a way to do |
|
114 // that without using xpcom or rtti. Any calling code that can't deal with |
|
115 // a null response from one of them probably shouldn't be using nsAHttpTransaction |
|
116 |
|
117 // If we used rtti this would be the result of doing |
|
118 // dynamic_cast<nsHttpPipeline *>(this).. i.e. it can be nullptr for |
|
119 // non pipeline implementations of nsAHttpTransaction |
|
120 virtual nsHttpPipeline *QueryPipeline() { return nullptr; } |
|
121 |
|
122 // equivalent to !!dynamic_cast<NullHttpTransaction *>(this) |
|
123 // A null transaction is expected to return BASE_STREAM_CLOSED on all of |
|
124 // its IO functions all the time. |
|
125 virtual bool IsNullTransaction() { return false; } |
|
126 |
|
127 // return the load group connection information associated with the transaction |
|
128 virtual nsILoadGroupConnectionInfo *LoadGroupConnectionInfo() { return nullptr; } |
|
129 |
|
130 // The base definition of these is done in nsHttpTransaction.cpp |
|
131 virtual bool ResponseTimeoutEnabled() const; |
|
132 virtual PRIntervalTime ResponseTimeout(); |
|
133 |
|
134 // Every transaction is classified into one of the types below. When using |
|
135 // HTTP pipelines, only transactions with the same type appear on the same |
|
136 // pipeline. |
|
137 enum Classifier { |
|
138 // Transactions that expect a short 304 (no-content) response |
|
139 CLASS_REVALIDATION, |
|
140 |
|
141 // Transactions for content expected to be CSS or JS |
|
142 CLASS_SCRIPT, |
|
143 |
|
144 // Transactions for content expected to be an image |
|
145 CLASS_IMAGE, |
|
146 |
|
147 // Transactions that cannot involve a pipeline |
|
148 CLASS_SOLO, |
|
149 |
|
150 // Transactions that do not fit any of the other categories. HTML |
|
151 // is normally GENERAL. |
|
152 CLASS_GENERAL, |
|
153 |
|
154 CLASS_MAX |
|
155 }; |
|
156 }; |
|
157 |
|
158 #define NS_DECL_NSAHTTPTRANSACTION \ |
|
159 void SetConnection(nsAHttpConnection *); \ |
|
160 nsAHttpConnection *Connection(); \ |
|
161 void GetSecurityCallbacks(nsIInterfaceRequestor **); \ |
|
162 void OnTransportStatus(nsITransport* transport, \ |
|
163 nsresult status, uint64_t progress); \ |
|
164 bool IsDone(); \ |
|
165 nsresult Status(); \ |
|
166 uint32_t Caps(); \ |
|
167 void SetDNSWasRefreshed(); \ |
|
168 uint64_t Available(); \ |
|
169 nsresult ReadSegments(nsAHttpSegmentReader *, uint32_t, uint32_t *); \ |
|
170 nsresult WriteSegments(nsAHttpSegmentWriter *, uint32_t, uint32_t *); \ |
|
171 void Close(nsresult reason); \ |
|
172 void SetProxyConnectFailed(); \ |
|
173 nsHttpRequestHead *RequestHead(); \ |
|
174 uint32_t Http1xTransactionCount(); \ |
|
175 nsresult TakeSubTransactions(nsTArray<nsRefPtr<nsAHttpTransaction> > &outTransactions); \ |
|
176 nsresult AddTransaction(nsAHttpTransaction *); \ |
|
177 uint32_t PipelineDepth(); \ |
|
178 nsresult SetPipelinePosition(int32_t); \ |
|
179 int32_t PipelinePosition(); |
|
180 |
|
181 //----------------------------------------------------------------------------- |
|
182 // nsAHttpSegmentReader |
|
183 //----------------------------------------------------------------------------- |
|
184 |
|
185 class nsAHttpSegmentReader |
|
186 { |
|
187 public: |
|
188 // any returned failure code stops segment iteration |
|
189 virtual nsresult OnReadSegment(const char *segment, |
|
190 uint32_t count, |
|
191 uint32_t *countRead) = 0; |
|
192 |
|
193 // Ask the segment reader to commit to accepting size bytes of |
|
194 // data from subsequent OnReadSegment() calls or throw hard |
|
195 // (i.e. not wouldblock) exceptions. Implementations |
|
196 // can return NS_ERROR_FAILURE if they never make commitments of that size |
|
197 // (the default), NS_OK if they make the commitment, or |
|
198 // NS_BASE_STREAM_WOULD_BLOCK if they cannot make the |
|
199 // commitment now but might in the future and forceCommitment is not true . |
|
200 // (forceCommitment requires a hard failure or OK at this moment.) |
|
201 // |
|
202 // SpdySession uses this to make sure frames are atomic. |
|
203 virtual nsresult CommitToSegmentSize(uint32_t size, bool forceCommitment) |
|
204 { |
|
205 return NS_ERROR_FAILURE; |
|
206 } |
|
207 }; |
|
208 |
|
209 #define NS_DECL_NSAHTTPSEGMENTREADER \ |
|
210 nsresult OnReadSegment(const char *, uint32_t, uint32_t *); |
|
211 |
|
212 //----------------------------------------------------------------------------- |
|
213 // nsAHttpSegmentWriter |
|
214 //----------------------------------------------------------------------------- |
|
215 |
|
216 class nsAHttpSegmentWriter |
|
217 { |
|
218 public: |
|
219 // any returned failure code stops segment iteration |
|
220 virtual nsresult OnWriteSegment(char *segment, |
|
221 uint32_t count, |
|
222 uint32_t *countWritten) = 0; |
|
223 }; |
|
224 |
|
225 #define NS_DECL_NSAHTTPSEGMENTWRITER \ |
|
226 nsresult OnWriteSegment(char *, uint32_t, uint32_t *); |
|
227 |
|
228 }} // namespace mozilla::net |
|
229 |
|
230 #endif // nsAHttpTransaction_h__ |