Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1205)

Side by Side Diff: net/http/http_pipelined_connection_impl.h

Issue 7289006: Basic HTTP pipelining support (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Added unit tests Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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_log.h"
17 #include "net/base/ssl_config_service.h"
18 #include "net/base/upload_data_stream.h"
19 #include "net/http/http_pipelined_connection.h"
20 #include "net/http/http_request_info.h"
21 #include "net/proxy/proxy_info.h"
22
23 namespace net {
24
25 class ClientSocketHandle;
26 class GrowableIOBuffer;
27 class HttpPipelinedHost;
mmenke 2011/08/23 19:05:25 nit: No longer needed.
James Simonsen 2011/08/26 22:19:07 Done.
28 class HttpPipelinedStream;
29 class HttpRequestHeaders;
30 class HttpResponseInfo;
31 class HttpStreamParser;
32 class IOBuffer;
33 class SSLCertRequestInfo;
34 class SSLInfo;
35
36 // This class manages all of the state for a single pipelined connection. It
37 // tracks the order that HTTP requests are sent and enforces that the
38 // subsequent reads occur in the appropriate order.
39 //
40 // If an error occurs related to pipelining, ERR_PIPELINE_EVICTION will be
41 // returned to the client. This indicates the client should retry the request
42 // without pipelining.
43 class HttpPipelinedConnectionImpl : public HttpPipelinedConnection {
44 public:
45 HttpPipelinedConnectionImpl(ClientSocketHandle* connection,
46 Owner* owner,
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 HttpStream* CreateNewStream() OVERRIDE;
57
58 // Used by HttpPipelinedHost.
59 int depth() const OVERRIDE { return stream_state_map_.size(); }
mmenke 2011/08/23 19:05:25 virtual should always be used with OVERRIDE.
James Simonsen 2011/08/26 22:19:07 Done.
60 bool usable() const OVERRIDE { return usable_; }
61 bool active() const OVERRIDE { return active_; }
62
63 // Used by HttpStreamFactoryImpl.
64 const SSLConfig& used_ssl_config() const OVERRIDE { return used_ssl_config_; }
65 const ProxyInfo& used_proxy_info() const OVERRIDE { return used_proxy_info_; }
66 const NetLog::Source& source() const OVERRIDE { return net_log_.source(); }
67 bool was_npn_negotiated() const OVERRIDE { return was_npn_negotiated_; }
68
69 // Used by HttpPipelinedStream.
70 void OnStreamDeleted(int pipeline_id) OVERRIDE;
71
72 // Effective implementation of HttpStream. Note that we don't directly
73 // implement that interface. Instead, these functions will be called by the
74 // pass-through methods in HttpPipelinedStream.
75 void InitializeParser(int pipeline_id,
76 const HttpRequestInfo* request,
77 const BoundNetLog& net_log);
78
79 int SendRequest(int pipeline_id,
80 const std::string& request_line,
81 const HttpRequestHeaders& headers,
82 UploadDataStream* request_body,
83 HttpResponseInfo* response,
84 CompletionCallback* callback);
85
86 int ReadResponseHeaders(int pipeline_id,
87 CompletionCallback* callback);
88
89 int ReadResponseBody(int pipeline_id,
90 IOBuffer* buf, int buf_len,
91 CompletionCallback* callback);
92
93 void Close(int pipeline_id,
94 bool not_reusable);
95
96 uint64 GetUploadProgress(int pipeline_id) const;
97
98 HttpResponseInfo* GetResponseInfo(int pipeline_id);
99
100 bool IsResponseBodyComplete(int pipeline_id) const;
101
102 bool CanFindEndOfResponse(int pipeline_id) const;
103
104 bool IsMoreDataBuffered(int pipeline_id) const;
105
106 bool IsConnectionReused(int pipeline_id) const;
107
108 void SetConnectionReused(int pipeline_id);
109
110 void GetSSLInfo(int pipeline_id,
111 SSLInfo* ssl_info);
112
113 void GetSSLCertRequestInfo(int pipeline_id,
114 SSLCertRequestInfo* cert_request_info);
115
116 private:
117 enum StreamState {
118 STREAM_CREATED,
119 STREAM_BOUND,
120 STREAM_SENT,
121 STREAM_CLOSED,
122 STREAM_UNUSED,
123 };
124 enum SendRequestState {
125 SEND_STATE_NEXT_REQUEST,
126 SEND_STATE_COMPLETE,
127 SEND_STATE_NONE,
128 SEND_STATE_UNUSABLE,
129 };
130 enum ReadHeadersState {
131 READ_STATE_NEXT_HEADERS,
132 READ_STATE_COMPLETE,
133 READ_STATE_WAITING_FOR_CLOSE,
134 READ_STATE_STREAM_CLOSED,
135 READ_STATE_NONE,
136 READ_STATE_UNUSABLE,
137 };
138
139 struct DeferredSendRequest {
140 int pipeline_id;
141 std::string request_line;
142 HttpRequestHeaders headers;
143 UploadDataStream* request_body;
144 HttpResponseInfo* response;
145 CompletionCallback* callback;
146 };
147
148 // Called after the first request is processed, allowing the rest of this
149 // pipeline to fill up with any pending requests.
150 void FillPipeline();
151
152 // Responsible for sending one request at a time and waiting until each
153 // comepletes.
154 int DoSendRequestLoop(int result);
155
156 // Called when an asynchronous Send() completes.
157 void OnSendIOCallback(int result);
158
159 // Sends the next deferred request. This may be called immediately after
160 // SendRequest(), or it may be in a new task after a prior send completes in
161 // DoSendComplete().
mmenke 2011/08/23 19:05:25 Thanks for adding all these comments. Makes it mu
162 int DoSendNextRequest(int result);
163
164 // Notifies the user that the send has completed. This may be called directly
165 // after SendRequest() for a synchronous request, or it may be called in
166 // response to OnSendIOCallback for an asynchronous request.
167 int DoSendComplete(int result);
168
169 // Evicts all unsent deferred requests. This is called if there is a Send()
170 // error or one of our sockets informs us the connection is no longer
mmenke 2011/08/23 19:05:25 nit: "One of our streams"
James Simonsen 2011/08/26 22:19:07 Done.
171 // reusable.
172 int DoEvictPendingSendRequests(int result);
173
174 // Ensures that only the active request's HttpPipelinedSocket can read from
175 // the underlying socket until it completes. A HttpPipelinedSocket informs us
176 // that it's done by calling Close().
177 int DoReadHeadersLoop(int result);
178
179 // Called when the pending asynchronous ReadResponseHeaders() completes.
180 void OnReadIOCallback(int result);
181
182 // Determines if the next response in the pipeline is ready to be read. A
mmenke 2011/08/23 19:05:25 nit: Remove "A" at end of line.
James Simonsen 2011/08/26 22:19:07 Done.
183 // If it's ready, then we call ReadResponseHeaders() on the underlying parser.
184 // HttpPipelinedSocket indicates its readiness by calling
185 // ReadResponseHeaders(). This function may be called immediately after
186 // ReadResponseHeaders(), or it may be called in a new task after a previous
187 // HttpPipelinedSocket finishes its work.
188 int DoReadNextHeaders(int result);
189
190 // Notifies the user that reading the headers has completed. This may happen
191 // directly after DoReadNextHeaders() if the response is already available.
192 // Otherwise, it is called in response to OnReadIOCallback().
193 int DoReadHeadersComplete(int result);
194
195 // This is a holding state. It does not do anything, except exit the
196 // DoReadHeadersLoop(). It is called after DoReadHeadersComplete().
197 int DoReadWaitingForClose(int result);
198
199 // Cleans up the state associated with the active request. Invokes
200 // DoReadNextHeaders() in a new task to start the next response. This is
201 // called after the active request's HttpPipelinedSocket calls Close().
202 int DoReadStreamClosed();
203
204 // Removes all pending ReadResponseHeaders() requests from the queue. This may
205 // happen if there is an error with the pipeline or one of our
206 // HttpPipelinedSockets indicates the connection was suddenly closed.
207 int DoEvictPendingReadHeaders(int result);
208
209 // Invokes the user's callback in response to one of our own IO callbacks
210 // being invoked. This may be used for either Send or Read callbacks.
211 void FireUserCallback(CompletionCallback* callback, int result);
212
213 Owner* owner_;
214 scoped_ptr<ClientSocketHandle> connection_;
215 SSLConfig used_ssl_config_;
216 ProxyInfo used_proxy_info_;
217 BoundNetLog net_log_;
218 bool was_npn_negotiated_;
219 scoped_refptr<GrowableIOBuffer> read_buf_;
220 int next_pipeline_id_;
221 bool active_;
222 bool usable_;
223 ScopedRunnableMethodFactory<HttpPipelinedConnectionImpl> method_factory_;
224
225 typedef std::map<int, StreamState> StreamStateMap;
226 StreamStateMap stream_state_map_;
227 typedef std::map<int, HttpStreamParser*> ParserMap;
228 ParserMap parser_map_;
229
230 std::queue<int> request_order_;
231
232 std::queue<DeferredSendRequest> deferred_request_queue_;
233 SendRequestState send_next_state_;
234 CompletionCallbackImpl<HttpPipelinedConnectionImpl> send_io_callback_;
235 CompletionCallback* send_user_callback_;
236
237 ReadHeadersState read_next_state_;
238 CompletionCallbackImpl<HttpPipelinedConnectionImpl> read_io_callback_;
239 CompletionCallback* read_user_callback_;
240 typedef std::map<int, CompletionCallback*> CallbackMap;
241 CallbackMap callback_map_;
242
243 DISALLOW_COPY_AND_ASSIGN(HttpPipelinedConnectionImpl);
244 };
245
246 } // namespace net
247
248 #endif // NET_HTTP_HTTP_PIPELINED_CONNECTION_IMPL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698