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