OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef NET_HTTP_HTTP_PIPELINED_CONNECTION_IMPL_H_ | |
6 #define NET_HTTP_HTTP_PIPELINED_CONNECTION_IMPL_H_ | |
7 #pragma once | |
8 | |
9 #include <map> | |
10 #include <queue> | |
11 #include <string> | |
12 | |
13 #include "base/basictypes.h" | |
14 #include "base/task.h" | |
15 #include "net/base/completion_callback.h" | |
16 #include "net/base/net_export.h" | |
17 #include "net/base/net_log.h" | |
18 #include "net/base/ssl_config_service.h" | |
19 #include "net/base/upload_data_stream.h" | |
20 #include "net/http/http_pipelined_connection.h" | |
21 #include "net/http/http_request_info.h" | |
22 #include "net/proxy/proxy_info.h" | |
23 | |
24 namespace net { | |
25 | |
26 class ClientSocketHandle; | |
27 class GrowableIOBuffer; | |
28 class HttpRequestHeaders; | |
29 class HttpResponseInfo; | |
30 class HttpStreamParser; | |
31 class IOBuffer; | |
32 class SSLCertRequestInfo; | |
33 class SSLInfo; | |
34 | |
35 // This class manages all of the state for a single pipelined connection. It | |
36 // tracks the order that HTTP requests are sent and enforces that the | |
37 // subsequent reads occur in the appropriate order. | |
38 // | |
39 // If an error occurs related to pipelining, ERR_PIPELINE_EVICTION will be | |
40 // returned to the client. This indicates the client should retry the request | |
41 // without pipelining. | |
42 class NET_EXPORT_PRIVATE HttpPipelinedConnectionImpl | |
43 : public HttpPipelinedConnection { | |
44 public: | |
45 HttpPipelinedConnectionImpl(ClientSocketHandle* connection, | |
46 Delegate* delegate, | |
47 const SSLConfig& used_ssl_config, | |
48 const ProxyInfo& used_proxy_info, | |
49 const BoundNetLog& net_log, | |
50 bool was_npn_negotiated); | |
51 ~HttpPipelinedConnectionImpl(); | |
52 | |
53 // HttpPipelinedConnection interface. | |
54 | |
55 // Used by HttpStreamFactoryImpl and friends. | |
56 virtual HttpPipelinedStream* CreateNewStream() OVERRIDE; | |
57 | |
58 // Used by HttpPipelinedHost. | |
59 virtual int depth() const OVERRIDE { return stream_state_map_.size(); } | |
60 virtual bool usable() const OVERRIDE { return usable_; } | |
61 virtual bool active() const OVERRIDE { return active_; } | |
62 | |
63 // Used by HttpStreamFactoryImpl. | |
64 virtual const SSLConfig& used_ssl_config() const OVERRIDE { | |
65 return used_ssl_config_; | |
66 } | |
67 virtual const ProxyInfo& used_proxy_info() const OVERRIDE { | |
68 return used_proxy_info_; | |
69 } | |
70 virtual const NetLog::Source& source() const OVERRIDE { | |
71 return net_log_.source(); | |
72 } | |
73 virtual bool was_npn_negotiated() const OVERRIDE { | |
74 return was_npn_negotiated_; | |
75 } | |
76 | |
77 // Used by HttpPipelinedStream. | |
78 virtual void OnStreamDeleted(int pipeline_id) OVERRIDE; | |
79 | |
80 // Effective implementation of HttpStream. Note that we don't directly | |
81 // implement that interface. Instead, these functions will be called by the | |
82 // pass-through methods in HttpPipelinedStream. | |
83 void InitializeParser(int pipeline_id, | |
84 const HttpRequestInfo* request, | |
85 const BoundNetLog& net_log); | |
86 | |
87 int SendRequest(int pipeline_id, | |
88 const std::string& request_line, | |
89 const HttpRequestHeaders& headers, | |
90 UploadDataStream* request_body, | |
91 HttpResponseInfo* response, | |
92 CompletionCallback* callback); | |
93 | |
94 int ReadResponseHeaders(int pipeline_id, | |
95 CompletionCallback* callback); | |
96 | |
97 int ReadResponseBody(int pipeline_id, | |
98 IOBuffer* buf, int buf_len, | |
99 CompletionCallback* callback); | |
100 | |
101 void Close(int pipeline_id, | |
102 bool not_reusable); | |
103 | |
104 uint64 GetUploadProgress(int pipeline_id) const; | |
105 | |
106 HttpResponseInfo* GetResponseInfo(int pipeline_id); | |
107 | |
108 bool IsResponseBodyComplete(int pipeline_id) const; | |
109 | |
110 bool CanFindEndOfResponse(int pipeline_id) const; | |
111 | |
112 bool IsMoreDataBuffered(int pipeline_id) const; | |
113 | |
114 bool IsConnectionReused(int pipeline_id) const; | |
115 | |
116 void SetConnectionReused(int pipeline_id); | |
117 | |
118 void GetSSLInfo(int pipeline_id, | |
119 SSLInfo* ssl_info); | |
120 | |
121 void GetSSLCertRequestInfo(int pipeline_id, | |
122 SSLCertRequestInfo* cert_request_info); | |
123 | |
124 private: | |
125 enum StreamState { | |
126 STREAM_CREATED, | |
127 STREAM_BOUND, | |
128 STREAM_SENDING, | |
129 STREAM_SENT, | |
130 STREAM_READ_PENDING, | |
131 STREAM_ACTIVE, | |
132 STREAM_CLOSED, | |
133 STREAM_UNUSED, | |
134 }; | |
135 enum SendRequestState { | |
136 SEND_STATE_NEXT_REQUEST, | |
137 SEND_STATE_COMPLETE, | |
138 SEND_STATE_NONE, | |
139 SEND_STATE_UNUSABLE, | |
140 }; | |
141 enum ReadHeadersState { | |
142 READ_STATE_NEXT_HEADERS, | |
143 READ_STATE_COMPLETE, | |
144 READ_STATE_WAITING_FOR_CLOSE, | |
145 READ_STATE_STREAM_CLOSED, | |
146 READ_STATE_NONE, | |
147 READ_STATE_UNUSABLE, | |
148 }; | |
149 | |
150 struct DeferredSendRequest { | |
151 int pipeline_id; | |
152 std::string request_line; | |
153 HttpRequestHeaders headers; | |
154 UploadDataStream* request_body; | |
155 HttpResponseInfo* response; | |
156 CompletionCallback* callback; | |
157 }; | |
158 | |
159 typedef std::map<int, CompletionCallback*> CallbackMap; | |
160 typedef std::map<int, HttpStreamParser*> ParserMap; | |
161 typedef std::map<int, StreamState> StreamStateMap; | |
162 | |
163 // Called after the first request is processed, allowing the rest of this | |
164 // pipeline to fill up with any pending requests. | |
165 void FillPipeline(); | |
166 | |
167 // Responsible for sending one request at a time and waiting until each | |
168 // comepletes. | |
169 int DoSendRequestLoop(int result); | |
170 | |
171 // Called when an asynchronous Send() completes. | |
172 void OnSendIOCallback(int result); | |
173 | |
174 // Sends the next deferred request. This may be called immediately after | |
175 // SendRequest(), or it may be in a new task after a prior send completes in | |
176 // DoSendComplete(). | |
177 int DoSendNextRequest(int result); | |
178 | |
179 // Notifies the user that the send has completed. This may be called directly | |
180 // after SendRequest() for a synchronous request, or it may be called in | |
181 // response to OnSendIOCallback for an asynchronous request. | |
182 int DoSendComplete(int result); | |
183 | |
184 // Evicts all unsent deferred requests. This is called if there is a Send() | |
185 // error or one of our streams informs us the connection is no longer | |
186 // reusable. | |
187 int DoEvictPendingSendRequests(int result); | |
188 | |
189 // Ensures that only the active request's HttpPipelinedSocket can read from | |
190 // the underlying socket until it completes. A HttpPipelinedSocket informs us | |
191 // that it's done by calling Close(). | |
192 int DoReadHeadersLoop(int result); | |
193 | |
194 // Called when the pending asynchronous ReadResponseHeaders() completes. | |
195 void OnReadIOCallback(int result); | |
196 | |
197 // Determines if the next response in the pipeline is ready to be read. | |
198 // If it's ready, then we call ReadResponseHeaders() on the underlying parser. | |
199 // HttpPipelinedSocket indicates its readiness by calling | |
200 // ReadResponseHeaders(). This function may be called immediately after | |
201 // ReadResponseHeaders(), or it may be called in a new task after a previous | |
202 // HttpPipelinedSocket finishes its work. | |
203 int DoReadNextHeaders(int result); | |
204 | |
205 // Notifies the user that reading the headers has completed. This may happen | |
206 // directly after DoReadNextHeaders() if the response is already available. | |
207 // Otherwise, it is called in response to OnReadIOCallback(). | |
208 int DoReadHeadersComplete(int result); | |
209 | |
210 // This is a holding state. It does not do anything, except exit the | |
211 // DoReadHeadersLoop(). It is called after DoReadHeadersComplete(). | |
212 int DoReadWaitingForClose(int result); | |
213 | |
214 // Cleans up the state associated with the active request. Invokes | |
215 // DoReadNextHeaders() in a new task to start the next response. This is | |
216 // called after the active request's HttpPipelinedSocket calls Close(). | |
217 int DoReadStreamClosed(); | |
218 | |
219 // Removes all pending ReadResponseHeaders() requests from the queue. This may | |
220 // happen if there is an error with the pipeline or one of our | |
221 // HttpPipelinedSockets indicates the connection was suddenly closed. | |
222 int DoEvictPendingReadHeaders(int result); | |
223 | |
224 // Invokes the user's callback in response to ReadResponseHeaders() completing | |
225 // on an underlying parser. This might be invoked in response to our own | |
226 // read_io_callback_ being invoked, or it may be due to ReadResponseHeaders() | |
227 // completing synchronously after we've already returned ERR_IO_PENDING to the | |
228 // user. | |
229 void FireReadUserCallback(int result); | |
230 | |
231 Delegate* delegate_; | |
232 scoped_ptr<ClientSocketHandle> connection_; | |
233 SSLConfig used_ssl_config_; | |
234 ProxyInfo used_proxy_info_; | |
235 BoundNetLog net_log_; | |
236 bool was_npn_negotiated_; | |
237 scoped_refptr<GrowableIOBuffer> read_buf_; | |
238 int next_pipeline_id_; | |
239 bool active_; | |
240 bool usable_; | |
241 bool completed_one_request_; | |
242 ScopedRunnableMethodFactory<HttpPipelinedConnectionImpl> method_factory_; | |
243 | |
244 StreamStateMap stream_state_map_; | |
willchan no longer on Chromium
2011/09/29 20:20:07
I don't see why there are so many maps. Aren't the
James Simonsen
2011/10/12 01:36:58
Done.
| |
245 ParserMap parser_map_; | |
246 | |
247 std::queue<int> request_order_; | |
248 | |
249 std::queue<DeferredSendRequest> deferred_request_queue_; | |
250 SendRequestState send_next_state_; | |
251 CompletionCallbackImpl<HttpPipelinedConnectionImpl> send_io_callback_; | |
252 CompletionCallback* send_user_callback_; | |
253 | |
254 ReadHeadersState read_next_state_; | |
255 CompletionCallbackImpl<HttpPipelinedConnectionImpl> read_io_callback_; | |
256 CompletionCallback* read_user_callback_; | |
257 CallbackMap callback_map_; | |
258 | |
259 DISALLOW_COPY_AND_ASSIGN(HttpPipelinedConnectionImpl); | |
260 }; | |
261 | |
262 } // namespace net | |
263 | |
264 #endif // NET_HTTP_HTTP_PIPELINED_CONNECTION_IMPL_H_ | |
OLD | NEW |