| OLD | NEW |
| 1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef NET_HTTP_HTTP_NETWORK_TRANSACTION_H_ | 5 #ifndef NET_HTTP_HTTP_NETWORK_TRANSACTION_H_ |
| 6 #define NET_HTTP_HTTP_NETWORK_TRANSACTION_H_ | 6 #define NET_HTTP_HTTP_NETWORK_TRANSACTION_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/ref_counted.h" | 10 #include "base/ref_counted.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "net/http/http_response_info.h" | 21 #include "net/http/http_response_info.h" |
| 22 #include "net/http/http_transaction.h" | 22 #include "net/http/http_transaction.h" |
| 23 #include "net/proxy/proxy_service.h" | 23 #include "net/proxy/proxy_service.h" |
| 24 #include "net/socket/client_socket_handle.h" | 24 #include "net/socket/client_socket_handle.h" |
| 25 #include "net/socket/client_socket_pool.h" | 25 #include "net/socket/client_socket_pool.h" |
| 26 #include "testing/gtest/include/gtest/gtest_prod.h" | 26 #include "testing/gtest/include/gtest/gtest_prod.h" |
| 27 | 27 |
| 28 namespace net { | 28 namespace net { |
| 29 | 29 |
| 30 class ClientSocketFactory; | 30 class ClientSocketFactory; |
| 31 class HttpChunkedDecoder; | |
| 32 class HttpNetworkSession; | 31 class HttpNetworkSession; |
| 33 class HttpStream; | 32 class HttpStream; |
| 34 class UploadDataStream; | |
| 35 | 33 |
| 36 class HttpNetworkTransaction : public HttpTransaction { | 34 class HttpNetworkTransaction : public HttpTransaction { |
| 37 public: | 35 public: |
| 38 explicit HttpNetworkTransaction(HttpNetworkSession* session); | 36 explicit HttpNetworkTransaction(HttpNetworkSession* session); |
| 39 | 37 |
| 40 virtual ~HttpNetworkTransaction(); | 38 virtual ~HttpNetworkTransaction(); |
| 41 | 39 |
| 42 // HttpTransaction methods: | 40 // HttpTransaction methods: |
| 43 virtual int Start(const HttpRequestInfo* request_info, | 41 virtual int Start(const HttpRequestInfo* request_info, |
| 44 CompletionCallback* callback, | 42 CompletionCallback* callback, |
| (...skipping 10 matching lines...) Expand all Loading... |
| 55 } | 53 } |
| 56 | 54 |
| 57 virtual int Read(IOBuffer* buf, int buf_len, CompletionCallback* callback); | 55 virtual int Read(IOBuffer* buf, int buf_len, CompletionCallback* callback); |
| 58 virtual const HttpResponseInfo* GetResponseInfo() const; | 56 virtual const HttpResponseInfo* GetResponseInfo() const; |
| 59 virtual LoadState GetLoadState() const; | 57 virtual LoadState GetLoadState() const; |
| 60 virtual uint64 GetUploadProgress() const; | 58 virtual uint64 GetUploadProgress() const; |
| 61 | 59 |
| 62 private: | 60 private: |
| 63 FRIEND_TEST(HttpNetworkTransactionTest, ResetStateForRestart); | 61 FRIEND_TEST(HttpNetworkTransactionTest, ResetStateForRestart); |
| 64 | 62 |
| 65 // This version of IOBuffer lets us use a string as the real storage and | |
| 66 // "move" the data pointer inside the string before using it to do actual IO. | |
| 67 class RequestHeaders : public net::IOBuffer { | |
| 68 public: | |
| 69 RequestHeaders() : net::IOBuffer() {} | |
| 70 ~RequestHeaders() { data_ = NULL; } | |
| 71 | |
| 72 void SetDataOffset(size_t offset) { | |
| 73 data_ = const_cast<char*>(headers_.data()) + offset; | |
| 74 } | |
| 75 | |
| 76 // This is intentionally a public member. | |
| 77 std::string headers_; | |
| 78 }; | |
| 79 | |
| 80 // This version of IOBuffer lets us use a malloc'ed buffer as the real storage | |
| 81 // and "move" the data pointer inside the buffer before using it to do actual | |
| 82 // IO. | |
| 83 class ResponseHeaders : public net::IOBuffer { | |
| 84 public: | |
| 85 ResponseHeaders() : net::IOBuffer() {} | |
| 86 ~ResponseHeaders() { data_ = NULL; } | |
| 87 | |
| 88 void set_data(size_t offset) { data_ = headers_.get() + offset; } | |
| 89 char* headers() { return headers_.get(); } | |
| 90 void Reset() { headers_.reset(); } | |
| 91 void Realloc(size_t new_size); | |
| 92 | |
| 93 private: | |
| 94 scoped_ptr_malloc<char> headers_; | |
| 95 }; | |
| 96 | |
| 97 enum State { | 63 enum State { |
| 98 STATE_RESOLVE_PROXY, | 64 STATE_RESOLVE_PROXY, |
| 99 STATE_RESOLVE_PROXY_COMPLETE, | 65 STATE_RESOLVE_PROXY_COMPLETE, |
| 100 STATE_INIT_CONNECTION, | 66 STATE_INIT_CONNECTION, |
| 101 STATE_INIT_CONNECTION_COMPLETE, | 67 STATE_INIT_CONNECTION_COMPLETE, |
| 102 STATE_SOCKS_CONNECT, | 68 STATE_SOCKS_CONNECT, |
| 103 STATE_SOCKS_CONNECT_COMPLETE, | 69 STATE_SOCKS_CONNECT_COMPLETE, |
| 104 STATE_SSL_CONNECT, | 70 STATE_SSL_CONNECT, |
| 105 STATE_SSL_CONNECT_COMPLETE, | 71 STATE_SSL_CONNECT_COMPLETE, |
| 106 STATE_WRITE_HEADERS, | 72 STATE_SEND_REQUEST, |
| 107 STATE_WRITE_HEADERS_COMPLETE, | 73 STATE_SEND_REQUEST_COMPLETE, |
| 108 STATE_WRITE_BODY, | |
| 109 STATE_WRITE_BODY_COMPLETE, | |
| 110 STATE_READ_HEADERS, | 74 STATE_READ_HEADERS, |
| 111 STATE_READ_HEADERS_COMPLETE, | 75 STATE_READ_HEADERS_COMPLETE, |
| 112 STATE_READ_BODY, | 76 STATE_READ_BODY, |
| 113 STATE_READ_BODY_COMPLETE, | 77 STATE_READ_BODY_COMPLETE, |
| 114 STATE_DRAIN_BODY_FOR_AUTH_RESTART, | 78 STATE_DRAIN_BODY_FOR_AUTH_RESTART, |
| 115 STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE, | 79 STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE, |
| 116 STATE_NONE | 80 STATE_NONE |
| 117 }; | 81 }; |
| 118 | 82 |
| 119 enum ProxyMode { | 83 enum ProxyMode { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 134 // ERR_IO_PENDING, then the result from OnIOComplete will be passed to the | 98 // ERR_IO_PENDING, then the result from OnIOComplete will be passed to the |
| 135 // next state method as the result arg. | 99 // next state method as the result arg. |
| 136 int DoResolveProxy(); | 100 int DoResolveProxy(); |
| 137 int DoResolveProxyComplete(int result); | 101 int DoResolveProxyComplete(int result); |
| 138 int DoInitConnection(); | 102 int DoInitConnection(); |
| 139 int DoInitConnectionComplete(int result); | 103 int DoInitConnectionComplete(int result); |
| 140 int DoSOCKSConnect(); | 104 int DoSOCKSConnect(); |
| 141 int DoSOCKSConnectComplete(int result); | 105 int DoSOCKSConnectComplete(int result); |
| 142 int DoSSLConnect(); | 106 int DoSSLConnect(); |
| 143 int DoSSLConnectComplete(int result); | 107 int DoSSLConnectComplete(int result); |
| 144 int DoWriteHeaders(); | 108 int DoSendRequest(); |
| 145 int DoWriteHeadersComplete(int result); | 109 int DoSendRequestComplete(int result); |
| 146 int DoWriteBody(); | |
| 147 int DoWriteBodyComplete(int result); | |
| 148 int DoReadHeaders(); | 110 int DoReadHeaders(); |
| 149 int DoReadHeadersComplete(int result); | 111 int DoReadHeadersComplete(int result); |
| 150 int DoReadBody(); | 112 int DoReadBody(); |
| 151 int DoReadBodyComplete(int result); | 113 int DoReadBodyComplete(int result); |
| 152 int DoDrainBodyForAuthRestart(); | 114 int DoDrainBodyForAuthRestart(); |
| 153 int DoDrainBodyForAuthRestartComplete(int result); | 115 int DoDrainBodyForAuthRestartComplete(int result); |
| 154 | 116 |
| 155 // Record histograms of latency until Connect() completes. | 117 // Record histograms of latency until Connect() completes. |
| 156 static void LogTCPConnectedMetrics(const ClientSocketHandle& handle); | 118 static void LogTCPConnectedMetrics(const ClientSocketHandle& handle); |
| 157 | 119 |
| 158 // Record histogram of time until first byte of header is received. | 120 // Record histogram of time until first byte of header is received. |
| 159 void LogTransactionConnectedMetrics() const; | 121 void LogTransactionConnectedMetrics() const; |
| 160 | 122 |
| 161 // Record histogram of latency (durations until last byte received). | 123 // Record histogram of latency (durations until last byte received). |
| 162 void LogTransactionMetrics() const; | 124 void LogTransactionMetrics() const; |
| 163 | 125 |
| 164 // Writes a log message to help debugging in the field when we block a proxy | 126 // Writes a log message to help debugging in the field when we block a proxy |
| 165 // response to a CONNECT request. | 127 // response to a CONNECT request. |
| 166 void LogBlockedTunnelResponse(int response_code) const; | 128 void LogBlockedTunnelResponse(int response_code) const; |
| 167 | 129 |
| 168 static void LogIOErrorMetrics(const ClientSocketHandle& handle); | 130 static void LogIOErrorMetrics(const ClientSocketHandle& handle); |
| 169 | 131 |
| 170 // Called when header_buf_ contains the complete response headers. | |
| 171 int DidReadResponseHeaders(); | |
| 172 | |
| 173 // Called to handle a certificate error. Returns OK if the error should be | 132 // Called to handle a certificate error. Returns OK if the error should be |
| 174 // ignored. Otherwise, stores the certificate in response_.ssl_info and | 133 // ignored. Otherwise, stores the certificate in response_.ssl_info and |
| 175 // returns the same error code. | 134 // returns the same error code. |
| 176 int HandleCertificateError(int error); | 135 int HandleCertificateError(int error); |
| 177 | 136 |
| 178 // Called to handle a client certificate request. | 137 // Called to handle a client certificate request. |
| 179 int HandleCertificateRequest(int error); | 138 int HandleCertificateRequest(int error); |
| 180 | 139 |
| 181 // Called to possibly recover from an SSL handshake error. Sets next_state_ | 140 // Called to possibly recover from an SSL handshake error. Sets next_state_ |
| 182 // and returns OK if recovering from the error. Otherwise, the same error | 141 // and returns OK if recovering from the error. Otherwise, the same error |
| 183 // code is returned. | 142 // code is returned. |
| 184 int HandleSSLHandshakeError(int error); | 143 int HandleSSLHandshakeError(int error); |
| 185 | 144 |
| 186 // Called to possibly recover from the given error. Sets next_state_ and | 145 // Called to possibly recover from the given error. Sets next_state_ and |
| 187 // returns OK if recovering from the error. Otherwise, the same error code | 146 // returns OK if recovering from the error. Otherwise, the same error code |
| 188 // is returned. | 147 // is returned. |
| 189 int HandleIOError(int error); | 148 int HandleIOError(int error); |
| 190 | 149 |
| 150 // Gets the response headers from the HttpStream. |
| 151 HttpResponseHeaders* GetResponseHeaders() const; |
| 152 |
| 191 // Called when we reached EOF or got an error. Returns true if we should | 153 // Called when we reached EOF or got an error. Returns true if we should |
| 192 // resend the request. |error| is OK when we reached EOF. | 154 // resend the request. |error| is OK when we reached EOF. |
| 193 bool ShouldResendRequest(int error) const; | 155 bool ShouldResendRequest(int error) const; |
| 194 | 156 |
| 195 // Resets the connection and the request headers for resend. Called when | 157 // Resets the connection and the request headers for resend. Called when |
| 196 // ShouldResendRequest() is true. | 158 // ShouldResendRequest() is true. |
| 197 void ResetConnectionAndRequestForResend(); | 159 void ResetConnectionAndRequestForResend(); |
| 198 | 160 |
| 199 // Called when we encounter a network error that could be resolved by trying | 161 // Called when we encounter a network error that could be resolved by trying |
| 200 // a new proxy configuration. If there is another proxy configuration to try | 162 // a new proxy configuration. If there is another proxy configuration to try |
| 201 // then this method sets next_state_ appropriately and returns either OK or | 163 // then this method sets next_state_ appropriately and returns either OK or |
| 202 // ERR_IO_PENDING depending on whether or not the new proxy configuration is | 164 // ERR_IO_PENDING depending on whether or not the new proxy configuration is |
| 203 // available synchronously or asynchronously. Otherwise, the given error | 165 // available synchronously or asynchronously. Otherwise, the given error |
| 204 // code is simply returned. | 166 // code is simply returned. |
| 205 int ReconsiderProxyAfterError(int error); | 167 int ReconsiderProxyAfterError(int error); |
| 206 | 168 |
| 207 // Decides the policy when the connection is closed before the end of headers | 169 // Decides the policy when the connection is closed before the end of headers |
| 208 // has been read. This only applies to reading responses, and not writing | 170 // has been read. This only applies to reading responses, and not writing |
| 209 // requests. | 171 // requests. |
| 210 int HandleConnectionClosedBeforeEndOfHeaders(); | 172 int HandleConnectionClosedBeforeEndOfHeaders(); |
| 211 | 173 |
| 212 // Return true if based on the bytes read so far, the start of the | |
| 213 // status line is known. This is used to distingish between HTTP/0.9 | |
| 214 // responses (which have no status line) and HTTP/1.x responses. | |
| 215 bool has_found_status_line_start() const { | |
| 216 return header_buf_http_offset_ != -1; | |
| 217 } | |
| 218 | |
| 219 // Sets up the state machine to restart the transaction with auth. | 174 // Sets up the state machine to restart the transaction with auth. |
| 220 void PrepareForAuthRestart(HttpAuth::Target target); | 175 void PrepareForAuthRestart(HttpAuth::Target target); |
| 221 | 176 |
| 222 // Called when we don't need to drain the response body or have drained it. | 177 // Called when we don't need to drain the response body or have drained it. |
| 223 // Resets |connection_| unless |keep_alive| is true, then calls | 178 // Resets |connection_| unless |keep_alive| is true, then calls |
| 224 // ResetStateForRestart. Sets |next_state_| appropriately. | 179 // ResetStateForRestart. Sets |next_state_| appropriately. |
| 225 void DidDrainBodyForAuthRestart(bool keep_alive); | 180 void DidDrainBodyForAuthRestart(bool keep_alive); |
| 226 | 181 |
| 227 // Resets the members of the transaction so it can be restarted. | 182 // Resets the members of the transaction so it can be restarted. |
| 228 void ResetStateForRestart(); | 183 void ResetStateForRestart(); |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 // cleared by RestartWithAuth(). | 257 // cleared by RestartWithAuth(). |
| 303 HttpAuth::Target pending_auth_target_; | 258 HttpAuth::Target pending_auth_target_; |
| 304 | 259 |
| 305 CompletionCallbackImpl<HttpNetworkTransaction> io_callback_; | 260 CompletionCallbackImpl<HttpNetworkTransaction> io_callback_; |
| 306 CompletionCallback* user_callback_; | 261 CompletionCallback* user_callback_; |
| 307 | 262 |
| 308 scoped_refptr<HttpNetworkSession> session_; | 263 scoped_refptr<HttpNetworkSession> session_; |
| 309 | 264 |
| 310 scoped_refptr<LoadLog> load_log_; | 265 scoped_refptr<LoadLog> load_log_; |
| 311 const HttpRequestInfo* request_; | 266 const HttpRequestInfo* request_; |
| 312 HttpResponseInfo response_; | |
| 313 | 267 |
| 314 ProxyService::PacRequest* pac_request_; | 268 ProxyService::PacRequest* pac_request_; |
| 315 ProxyInfo proxy_info_; | 269 ProxyInfo proxy_info_; |
| 316 | 270 |
| 317 ClientSocketHandle connection_; | 271 ClientSocketHandle connection_; |
| 318 scoped_ptr<HttpStream> http_stream_; | 272 scoped_ptr<HttpStream> http_stream_; |
| 319 bool reused_socket_; | 273 bool reused_socket_; |
| 320 | 274 |
| 275 // True if we've validated the headers that the stream parser has returned. |
| 276 bool headers_valid_; |
| 277 |
| 278 // True if we've logged the time of the first response byte. Used to |
| 279 // prevent logging across authentication activity where we see multiple |
| 280 // responses. |
| 281 bool logged_response_time; |
| 282 |
| 321 bool using_ssl_; // True if handling a HTTPS request | 283 bool using_ssl_; // True if handling a HTTPS request |
| 322 ProxyMode proxy_mode_; | 284 ProxyMode proxy_mode_; |
| 323 | 285 |
| 324 // True while establishing a tunnel. This allows the HTTP CONNECT | 286 // True while establishing a tunnel. This allows the HTTP CONNECT |
| 325 // request/response to reuse the STATE_WRITE_HEADERS, | 287 // request/response to reuse the STATE_SEND_REQUEST, |
| 326 // STATE_WRITE_HEADERS_COMPLETE, STATE_READ_HEADERS, and | 288 // STATE_SEND_REQUEST_COMPLETE, STATE_READ_HEADERS, and |
| 327 // STATE_READ_HEADERS_COMPLETE states and allows us to tell them apart from | 289 // STATE_READ_HEADERS_COMPLETE states and allows us to tell them apart from |
| 328 // the real request/response of the transaction. | 290 // the real request/response of the transaction. |
| 329 bool establishing_tunnel_; | 291 bool establishing_tunnel_; |
| 330 | 292 |
| 331 // Only used between the states | |
| 332 // STATE_READ_BODY/STATE_DRAIN_BODY_FOR_AUTH and | |
| 333 // STATE_READ_BODY_COMPLETE/STATE_DRAIN_BODY_FOR_AUTH_COMPLETE. | |
| 334 // | |
| 335 // Set to true when DoReadBody or DoDrainBodyForAuthRestart starts to read | |
| 336 // the response body from the socket, and set to false when the socket read | |
| 337 // call completes. DoReadBodyComplete and DoDrainBodyForAuthRestartComplete | |
| 338 // use this boolean to disambiguate a |result| of 0 between a connection | |
| 339 // closure (EOF) and reaching the end of the response body (no more data). | |
| 340 // | |
| 341 // TODO(wtc): this is similar to the |ignore_ok_result_| member of the | |
| 342 // SSLClientSocketWin class. We may want to add an internal error code, say | |
| 343 // ERR_EOF, to indicate a connection closure, so that 0 simply means 0 bytes | |
| 344 // or OK. Note that we already have an ERR_CONNECTION_CLOSED error code, | |
| 345 // but it isn't really being used. | |
| 346 bool reading_body_from_socket_; | |
| 347 | |
| 348 // True if we've used the username/password embedded in the URL. This | 293 // True if we've used the username/password embedded in the URL. This |
| 349 // makes sure we use the embedded identity only once for the transaction, | 294 // makes sure we use the embedded identity only once for the transaction, |
| 350 // preventing an infinite auth restart loop. | 295 // preventing an infinite auth restart loop. |
| 351 bool embedded_identity_used_; | 296 bool embedded_identity_used_; |
| 352 | 297 |
| 353 SSLConfig ssl_config_; | 298 SSLConfig ssl_config_; |
| 354 | 299 |
| 355 scoped_refptr<RequestHeaders> request_headers_; | 300 std::string request_headers_; |
| 356 size_t request_headers_bytes_sent_; | |
| 357 scoped_ptr<UploadDataStream> request_body_stream_; | |
| 358 | |
| 359 // The read buffer |header_buf_| may be larger than it is full. The | |
| 360 // 'capacity' indicates the allocation size of the buffer, and the 'len' | |
| 361 // indicates how much data is in the buffer already. The 'body offset' | |
| 362 // indicates the offset of the start of the response body within the read | |
| 363 // buffer. | |
| 364 scoped_refptr<ResponseHeaders> header_buf_; | |
| 365 int header_buf_capacity_; | |
| 366 int header_buf_len_; | |
| 367 int header_buf_body_offset_; | |
| 368 | |
| 369 // The number of bytes by which the header buffer is grown when it reaches | |
| 370 // capacity. | |
| 371 enum { kHeaderBufInitialSize = 4096 }; | |
| 372 | |
| 373 // |kMaxHeaderBufSize| is the number of bytes that the response headers can | |
| 374 // grow to. If the body start is not found within this range of the | |
| 375 // response, the transaction will fail with ERR_RESPONSE_HEADERS_TOO_BIG. | |
| 376 // Note: |kMaxHeaderBufSize| should be a multiple of |kHeaderBufInitialSize|. | |
| 377 enum { kMaxHeaderBufSize = 256 * 1024 }; // 256 kilobytes. | |
| 378 | 301 |
| 379 // The size in bytes of the buffer we use to drain the response body that | 302 // The size in bytes of the buffer we use to drain the response body that |
| 380 // we want to throw away. The response body is typically a small error | 303 // we want to throw away. The response body is typically a small error |
| 381 // page just a few hundred bytes long. | 304 // page just a few hundred bytes long. |
| 382 enum { kDrainBodyBufferSize = 1024 }; | 305 enum { kDrainBodyBufferSize = 1024 }; |
| 383 | 306 |
| 384 // The position where status line starts; -1 if not found yet. | |
| 385 int header_buf_http_offset_; | |
| 386 | |
| 387 // Indicates the content length remaining to read. If this value is less | |
| 388 // than zero (and chunked_decoder_ is null), then we read until the server | |
| 389 // closes the connection. | |
| 390 int64 response_body_length_; | |
| 391 | |
| 392 // Keeps track of the number of response body bytes read so far. | |
| 393 int64 response_body_read_; | |
| 394 | |
| 395 scoped_ptr<HttpChunkedDecoder> chunked_decoder_; | |
| 396 | |
| 397 // User buffer and length passed to the Read method. | 307 // User buffer and length passed to the Read method. |
| 398 scoped_refptr<IOBuffer> read_buf_; | 308 scoped_refptr<IOBuffer> read_buf_; |
| 399 int read_buf_len_; | 309 int read_buf_len_; |
| 400 | 310 |
| 401 // The time the Start method was called. | 311 // The time the Start method was called. |
| 402 base::Time start_time_; | 312 base::Time start_time_; |
| 403 | 313 |
| 404 // The time the DoSSLConnect() method was called (if it got called). | 314 // The time the DoSSLConnect() method was called (if it got called). |
| 405 base::TimeTicks ssl_connect_start_time_; | 315 base::TimeTicks ssl_connect_start_time_; |
| 406 | 316 |
| 407 // The next state in the state machine. | 317 // The next state in the state machine. |
| 408 State next_state_; | 318 State next_state_; |
| 409 }; | 319 }; |
| 410 | 320 |
| 411 } // namespace net | 321 } // namespace net |
| 412 | 322 |
| 413 #endif // NET_HTTP_HTTP_NETWORK_TRANSACTION_H_ | 323 #endif // NET_HTTP_HTTP_NETWORK_TRANSACTION_H_ |
| OLD | NEW |