blob: 255fcdf90831781aba5262e7ab15278f56c8ecd3 [file] [log] [blame]
[email protected]a796bcec2010-03-22 17:17:261// Copyright (c) 2010 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit586acc5fe2008-07-26 22:42:524
5#ifndef NET_HTTP_HTTP_NETWORK_TRANSACTION_H_
6#define NET_HTTP_HTTP_NETWORK_TRANSACTION_H_
[email protected]32b76ef2010-07-26 23:08:247#pragma once
initial.commit586acc5fe2008-07-26 22:42:528
[email protected]96d570e42008-08-05 22:43:049#include <string>
10
[email protected]85c0ed82009-12-15 23:14:1411#include "base/basictypes.h"
[email protected]8822f382010-07-30 21:49:0312#include "base/gtest_prod_util.h"
initial.commit586acc5fe2008-07-26 22:42:5213#include "base/ref_counted.h"
[email protected]9a0a55f2009-04-13 23:23:0314#include "base/scoped_ptr.h"
[email protected]21b316a2009-03-23 18:25:0615#include "base/time.h"
[email protected]9e743cd2010-03-16 07:03:5316#include "net/base/net_log.h"
[email protected]8e6441ca2010-08-19 05:56:3817#include "net/base/request_priority.h"
[email protected]aaead502008-10-15 00:20:1118#include "net/base/ssl_config_service.h"
[email protected]c3b35c22008-09-27 03:19:4219#include "net/http/http_auth.h"
[email protected]b94f92d2010-10-27 16:45:2020#include "net/http/http_request_headers.h"
initial.commit586acc5fe2008-07-26 22:42:5221#include "net/http/http_response_info.h"
22#include "net/http/http_transaction.h"
[email protected]8e6441ca2010-08-19 05:56:3823#include "net/http/stream_factory.h"
[email protected]928fb582008-08-11 15:40:2324#include "net/proxy/proxy_service.h"
initial.commit586acc5fe2008-07-26 22:42:5225
26namespace net {
27
[email protected]8e6441ca2010-08-19 05:56:3828class HttpAuthController;
initial.commit586acc5fe2008-07-26 22:42:5229class HttpNetworkSession;
[email protected]8d5a34e2009-06-11 21:21:3630class HttpStream;
[email protected]4d4a5162010-09-21 22:44:0431class HttpStreamRequest;
[email protected]8e6441ca2010-08-19 05:56:3832class IOBuffer;
[email protected]82918cc2010-08-25 17:24:5033struct HttpRequestInfo;
initial.commit586acc5fe2008-07-26 22:42:5234
[email protected]8e6441ca2010-08-19 05:56:3835class HttpNetworkTransaction : public HttpTransaction,
[email protected]26816882010-10-14 18:03:0936 public StreamRequest::Delegate {
initial.commit586acc5fe2008-07-26 22:42:5237 public:
[email protected]5695b8c2009-09-30 21:36:4338 explicit HttpNetworkTransaction(HttpNetworkSession* session);
initial.commit586acc5fe2008-07-26 22:42:5239
[email protected]af4876d2008-10-21 23:10:5740 virtual ~HttpNetworkTransaction();
41
initial.commit586acc5fe2008-07-26 22:42:5242 // HttpTransaction methods:
[email protected]684970b2009-08-14 04:54:4643 virtual int Start(const HttpRequestInfo* request_info,
44 CompletionCallback* callback,
[email protected]9e743cd2010-03-16 07:03:5345 const BoundNetLog& net_log);
initial.commit586acc5fe2008-07-26 22:42:5246 virtual int RestartIgnoringLastError(CompletionCallback* callback);
[email protected]0b45559b2009-06-12 21:45:1147 virtual int RestartWithCertificate(X509Certificate* client_cert,
48 CompletionCallback* callback);
[email protected]13c8a092010-07-29 06:15:4449 virtual int RestartWithAuth(const string16& username,
50 const string16& password,
initial.commit586acc5fe2008-07-26 22:42:5251 CompletionCallback* callback);
[email protected]8e6441ca2010-08-19 05:56:3852 virtual bool IsReadyToRestartForAuth();
[email protected]0757e7702009-03-27 04:00:2253
[email protected]9dea9e1f2009-01-29 00:30:4754 virtual int Read(IOBuffer* buf, int buf_len, CompletionCallback* callback);
[email protected]9dd90e52010-02-23 19:15:0155 virtual void StopCaching() {}
initial.commit586acc5fe2008-07-26 22:42:5256 virtual const HttpResponseInfo* GetResponseInfo() const;
57 virtual LoadState GetLoadState() const;
58 virtual uint64 GetUploadProgress() const;
59
[email protected]26816882010-10-14 18:03:0960 // StreamRequest::Delegate methods:
[email protected]4d4a5162010-09-21 22:44:0461 virtual void OnStreamReady(HttpStream* stream);
[email protected]8e6441ca2010-08-19 05:56:3862 virtual void OnStreamFailed(int status);
63 virtual void OnCertificateError(int status, const SSLInfo& ssl_info);
64 virtual void OnNeedsProxyAuth(
[email protected]6dc476da2010-09-01 04:43:5065 const HttpResponseInfo& response_info,
66 HttpAuthController* auth_controller);
67 virtual void OnNeedsClientAuth(SSLCertRequestInfo* cert_info);
[email protected]8e6441ca2010-08-19 05:56:3868
initial.commit586acc5fe2008-07-26 22:42:5269 private:
[email protected]8822f382010-07-30 21:49:0370 FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionTest, ResetStateForRestart);
[email protected]6abf5372010-08-17 15:44:2571 FRIEND_TEST_ALL_PREFIXES(SpdyNetworkTransactionTest, WindowUpdateReceived);
[email protected]450c5022010-08-26 02:38:2872 FRIEND_TEST_ALL_PREFIXES(SpdyNetworkTransactionTest, WindowUpdateSent);
[email protected]310240592010-08-05 21:04:1973 FRIEND_TEST_ALL_PREFIXES(SpdyNetworkTransactionTest, WindowUpdateOverflow);
74 FRIEND_TEST_ALL_PREFIXES(SpdyNetworkTransactionTest, FlowControlStallResume);
[email protected]89ceba9a2009-03-21 03:46:0675
[email protected]d207a5f2009-06-04 05:28:4076 enum State {
[email protected]82918cc2010-08-25 17:24:5077 STATE_CREATE_STREAM,
78 STATE_CREATE_STREAM_COMPLETE,
[email protected]351ab642010-08-05 16:55:3179 STATE_INIT_STREAM,
80 STATE_INIT_STREAM_COMPLETE,
[email protected]044de0642010-06-17 10:42:1581 STATE_GENERATE_PROXY_AUTH_TOKEN,
82 STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE,
83 STATE_GENERATE_SERVER_AUTH_TOKEN,
84 STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE,
[email protected]0877e3d2009-10-17 22:29:5785 STATE_SEND_REQUEST,
86 STATE_SEND_REQUEST_COMPLETE,
[email protected]d207a5f2009-06-04 05:28:4087 STATE_READ_HEADERS,
88 STATE_READ_HEADERS_COMPLETE,
89 STATE_READ_BODY,
90 STATE_READ_BODY_COMPLETE,
91 STATE_DRAIN_BODY_FOR_AUTH_RESTART,
92 STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE,
93 STATE_NONE
94 };
95
[email protected]8e6441ca2010-08-19 05:56:3896 bool is_https_request() const;
[email protected]564b4912010-03-09 16:30:4297
initial.commit586acc5fe2008-07-26 22:42:5298 void DoCallback(int result);
99 void OnIOComplete(int result);
100
101 // Runs the state transition loop.
102 int DoLoop(int result);
103
104 // Each of these methods corresponds to a State value. Those with an input
105 // argument receive the result from the previous state. If a method returns
106 // ERR_IO_PENDING, then the result from OnIOComplete will be passed to the
107 // next state method as the result arg.
[email protected]82918cc2010-08-25 17:24:50108 int DoCreateStream();
109 int DoCreateStreamComplete(int result);
[email protected]351ab642010-08-05 16:55:31110 int DoInitStream();
111 int DoInitStreamComplete(int result);
[email protected]044de0642010-06-17 10:42:15112 int DoGenerateProxyAuthToken();
113 int DoGenerateProxyAuthTokenComplete(int result);
114 int DoGenerateServerAuthToken();
115 int DoGenerateServerAuthTokenComplete(int result);
[email protected]0877e3d2009-10-17 22:29:57116 int DoSendRequest();
117 int DoSendRequestComplete(int result);
initial.commit586acc5fe2008-07-26 22:42:52118 int DoReadHeaders();
119 int DoReadHeadersComplete(int result);
120 int DoReadBody();
121 int DoReadBodyComplete(int result);
[email protected]2d2697f92009-02-18 21:00:32122 int DoDrainBodyForAuthRestart();
123 int DoDrainBodyForAuthRestartComplete(int result);
initial.commit586acc5fe2008-07-26 22:42:52124
[email protected]9a0a55f2009-04-13 23:23:03125 // Record histogram of time until first byte of header is received.
[email protected]8e3d2d32010-06-13 18:46:23126 void LogTransactionConnectedMetrics();
[email protected]9a0a55f2009-04-13 23:23:03127
128 // Record histogram of latency (durations until last byte received).
[email protected]56300172008-11-06 18:42:55129 void LogTransactionMetrics() const;
130
[email protected]9f9f86c2009-03-12 22:32:42131 // Writes a log message to help debugging in the field when we block a proxy
132 // response to a CONNECT request.
[email protected]af89ba62009-03-16 20:26:38133 void LogBlockedTunnelResponse(int response_code) const;
[email protected]9f9f86c2009-03-12 22:32:42134
[email protected]0b45559b2009-06-12 21:45:11135 // Called to handle a client certificate request.
[email protected]5e363962009-06-19 19:57:01136 int HandleCertificateRequest(int error);
[email protected]0b45559b2009-06-12 21:45:11137
initial.commit586acc5fe2008-07-26 22:42:52138 // Called to possibly recover from the given error. Sets next_state_ and
139 // returns OK if recovering from the error. Otherwise, the same error code
140 // is returned.
141 int HandleIOError(int error);
142
[email protected]0877e3d2009-10-17 22:29:57143 // Gets the response headers from the HttpStream.
144 HttpResponseHeaders* GetResponseHeaders() const;
145
[email protected]1c773ea12009-04-28 19:58:42146 // Called when we reached EOF or got an error. Returns true if we should
[email protected]a19f1c602009-08-24 21:35:28147 // resend the request. |error| is OK when we reached EOF.
148 bool ShouldResendRequest(int error) const;
[email protected]1c773ea12009-04-28 19:58:42149
150 // Resets the connection and the request headers for resend. Called when
151 // ShouldResendRequest() is true.
152 void ResetConnectionAndRequestForResend();
[email protected]2a5c76b2008-09-25 22:15:16153
[email protected]aecfbf22008-10-16 02:02:47154 // Decides the policy when the connection is closed before the end of headers
[email protected]0e75a732008-10-16 20:36:09155 // has been read. This only applies to reading responses, and not writing
156 // requests.
157 int HandleConnectionClosedBeforeEndOfHeaders();
[email protected]aecfbf22008-10-16 02:02:47158
[email protected]f9ee6b52008-11-08 06:46:23159 // Sets up the state machine to restart the transaction with auth.
160 void PrepareForAuthRestart(HttpAuth::Target target);
161
[email protected]2d2697f92009-02-18 21:00:32162 // Called when we don't need to drain the response body or have drained it.
163 // Resets |connection_| unless |keep_alive| is true, then calls
164 // ResetStateForRestart. Sets |next_state_| appropriately.
165 void DidDrainBodyForAuthRestart(bool keep_alive);
166
[email protected]f9ee6b52008-11-08 06:46:23167 // Resets the members of the transaction so it can be restarted.
[email protected]c3b35c22008-09-27 03:19:42168 void ResetStateForRestart();
169
[email protected]697ef4c2010-10-14 16:38:58170 // Resets the members of the transaction, except |stream_|, which needs
171 // to be maintained for multi-round auth.
172 void ResetStateForAuthRestart();
173
[email protected]1c773ea12009-04-28 19:58:42174 // Returns true if we should try to add a Proxy-Authorization header
175 bool ShouldApplyProxyAuth() const;
[email protected]c3b35c22008-09-27 03:19:42176
[email protected]1c773ea12009-04-28 19:58:42177 // Returns true if we should try to add an Authorization header.
178 bool ShouldApplyServerAuth() const;
179
[email protected]f9ee6b52008-11-08 06:46:23180 // Handles HTTP status code 401 or 407.
[email protected]0757e7702009-03-27 04:00:22181 // HandleAuthChallenge() returns a network error code, or OK on success.
182 // May update |pending_auth_target_| or |response_.auth_challenge|.
[email protected]e772db3f2010-07-12 18:11:13183 int HandleAuthChallenge();
[email protected]f9ee6b52008-11-08 06:46:23184
[email protected]8e6441ca2010-08-19 05:56:38185 // Returns true if we have auth credentials for the given target.
186 bool HaveAuth(HttpAuth::Target target) const;
[email protected]c3b35c22008-09-27 03:19:42187
[email protected]228404f2010-06-24 04:31:41188 // Get the {scheme, host, path, port} for the authentication target
189 GURL AuthURL(HttpAuth::Target target) const;
[email protected]044de0642010-06-17 10:42:15190
[email protected]aef04272010-06-28 18:03:04191 // Debug helper.
192 static std::string DescribeState(State state);
193
[email protected]e772db3f2010-07-12 18:11:13194 scoped_refptr<HttpAuthController>
195 auth_controllers_[HttpAuth::AUTH_NUM_TARGETS];
[email protected]c3b35c22008-09-27 03:19:42196
[email protected]0757e7702009-03-27 04:00:22197 // Whether this transaction is waiting for proxy auth, server auth, or is
198 // not waiting for any auth at all. |pending_auth_target_| is read and
199 // cleared by RestartWithAuth().
200 HttpAuth::Target pending_auth_target_;
201
initial.commit586acc5fe2008-07-26 22:42:52202 CompletionCallbackImpl<HttpNetworkTransaction> io_callback_;
203 CompletionCallback* user_callback_;
204
205 scoped_refptr<HttpNetworkSession> session_;
206
[email protected]9e743cd2010-03-16 07:03:53207 BoundNetLog net_log_;
initial.commit586acc5fe2008-07-26 22:42:52208 const HttpRequestInfo* request_;
[email protected]a7e41312009-12-16 23:18:14209 HttpResponseInfo response_;
initial.commit586acc5fe2008-07-26 22:42:52210
[email protected]928fb582008-08-11 15:40:23211 ProxyInfo proxy_info_;
initial.commit586acc5fe2008-07-26 22:42:52212
[email protected]26816882010-10-14 18:03:09213 scoped_ptr<StreamRequest> stream_request_;
[email protected]4d4a5162010-09-21 22:44:04214 scoped_ptr<HttpStream> stream_;
[email protected]8e6441ca2010-08-19 05:56:38215
[email protected]0877e3d2009-10-17 22:29:57216 // True if we've validated the headers that the stream parser has returned.
217 bool headers_valid_;
218
219 // True if we've logged the time of the first response byte. Used to
220 // prevent logging across authentication activity where we see multiple
221 // responses.
[email protected]8e3d2d32010-06-13 18:46:23222 bool logged_response_time_;
[email protected]0877e3d2009-10-17 22:29:57223
[email protected]aaead502008-10-15 00:20:11224 SSLConfig ssl_config_;
[email protected]c5949a32008-10-08 17:28:23225
[email protected]b94f92d2010-10-27 16:45:20226 HttpRequestHeaders request_headers_;
[email protected]4ddaf2502008-10-23 18:26:19227
[email protected]2d2697f92009-02-18 21:00:32228 // The size in bytes of the buffer we use to drain the response body that
229 // we want to throw away. The response body is typically a small error
230 // page just a few hundred bytes long.
[email protected]81854c42010-09-22 03:39:36231 static const int kDrainBodyBufferSize = 1024;
[email protected]2d2697f92009-02-18 21:00:32232
initial.commit586acc5fe2008-07-26 22:42:52233 // User buffer and length passed to the Read method.
[email protected]9dea9e1f2009-01-29 00:30:47234 scoped_refptr<IOBuffer> read_buf_;
initial.commit586acc5fe2008-07-26 22:42:52235 int read_buf_len_;
236
[email protected]21b316a2009-03-23 18:25:06237 // The time the Start method was called.
238 base::Time start_time_;
239
[email protected]d207a5f2009-06-04 05:28:40240 // The next state in the state machine.
initial.commit586acc5fe2008-07-26 22:42:52241 State next_state_;
[email protected]2ff8b312010-04-26 22:20:54242
[email protected]a7ea8832010-07-12 17:54:54243 // True when the tunnel is in the process of being established - we can't
244 // read from the socket until the tunnel is done.
245 bool establishing_tunnel_;
246
[email protected]2ff8b312010-04-26 22:20:54247 DISALLOW_COPY_AND_ASSIGN(HttpNetworkTransaction);
initial.commit586acc5fe2008-07-26 22:42:52248};
249
[email protected]c9c6f5c2010-07-31 01:30:03250int ConvertRequestPriorityToSpdyPriority(RequestPriority priority);
251
initial.commit586acc5fe2008-07-26 22:42:52252} // namespace net
253
254#endif // NET_HTTP_HTTP_NETWORK_TRANSACTION_H_