blob: bac14511eb99a6d133bf8834dd889f55ab0d21c5 [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]511f6f52010-12-17 03:58:2968 virtual void OnHttpsProxyTunnelResponse(const HttpResponseInfo& response_info,
69 HttpStream* stream);
[email protected]8e6441ca2010-08-19 05:56:3870
initial.commit586acc5fe2008-07-26 22:42:5271 private:
[email protected]8822f382010-07-30 21:49:0372 FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionTest, ResetStateForRestart);
[email protected]6abf5372010-08-17 15:44:2573 FRIEND_TEST_ALL_PREFIXES(SpdyNetworkTransactionTest, WindowUpdateReceived);
[email protected]450c5022010-08-26 02:38:2874 FRIEND_TEST_ALL_PREFIXES(SpdyNetworkTransactionTest, WindowUpdateSent);
[email protected]310240592010-08-05 21:04:1975 FRIEND_TEST_ALL_PREFIXES(SpdyNetworkTransactionTest, WindowUpdateOverflow);
76 FRIEND_TEST_ALL_PREFIXES(SpdyNetworkTransactionTest, FlowControlStallResume);
[email protected]89ceba9a2009-03-21 03:46:0677
[email protected]d207a5f2009-06-04 05:28:4078 enum State {
[email protected]82918cc2010-08-25 17:24:5079 STATE_CREATE_STREAM,
80 STATE_CREATE_STREAM_COMPLETE,
[email protected]351ab642010-08-05 16:55:3181 STATE_INIT_STREAM,
82 STATE_INIT_STREAM_COMPLETE,
[email protected]044de0642010-06-17 10:42:1583 STATE_GENERATE_PROXY_AUTH_TOKEN,
84 STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE,
85 STATE_GENERATE_SERVER_AUTH_TOKEN,
86 STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE,
[email protected]0877e3d2009-10-17 22:29:5787 STATE_SEND_REQUEST,
88 STATE_SEND_REQUEST_COMPLETE,
[email protected]d207a5f2009-06-04 05:28:4089 STATE_READ_HEADERS,
90 STATE_READ_HEADERS_COMPLETE,
91 STATE_READ_BODY,
92 STATE_READ_BODY_COMPLETE,
93 STATE_DRAIN_BODY_FOR_AUTH_RESTART,
94 STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE,
95 STATE_NONE
96 };
97
[email protected]8e6441ca2010-08-19 05:56:3898 bool is_https_request() const;
[email protected]564b4912010-03-09 16:30:4299
initial.commit586acc5fe2008-07-26 22:42:52100 void DoCallback(int result);
101 void OnIOComplete(int result);
102
103 // Runs the state transition loop.
104 int DoLoop(int result);
105
106 // Each of these methods corresponds to a State value. Those with an input
107 // argument receive the result from the previous state. If a method returns
108 // ERR_IO_PENDING, then the result from OnIOComplete will be passed to the
109 // next state method as the result arg.
[email protected]82918cc2010-08-25 17:24:50110 int DoCreateStream();
111 int DoCreateStreamComplete(int result);
[email protected]351ab642010-08-05 16:55:31112 int DoInitStream();
113 int DoInitStreamComplete(int result);
[email protected]044de0642010-06-17 10:42:15114 int DoGenerateProxyAuthToken();
115 int DoGenerateProxyAuthTokenComplete(int result);
116 int DoGenerateServerAuthToken();
117 int DoGenerateServerAuthTokenComplete(int result);
[email protected]0877e3d2009-10-17 22:29:57118 int DoSendRequest();
119 int DoSendRequestComplete(int result);
initial.commit586acc5fe2008-07-26 22:42:52120 int DoReadHeaders();
121 int DoReadHeadersComplete(int result);
122 int DoReadBody();
123 int DoReadBodyComplete(int result);
[email protected]2d2697f92009-02-18 21:00:32124 int DoDrainBodyForAuthRestart();
125 int DoDrainBodyForAuthRestartComplete(int result);
initial.commit586acc5fe2008-07-26 22:42:52126
[email protected]9a0a55f2009-04-13 23:23:03127 // Record histogram of time until first byte of header is received.
[email protected]8e3d2d32010-06-13 18:46:23128 void LogTransactionConnectedMetrics();
[email protected]9a0a55f2009-04-13 23:23:03129
130 // Record histogram of latency (durations until last byte received).
[email protected]56300172008-11-06 18:42:55131 void LogTransactionMetrics() const;
132
[email protected]9f9f86c2009-03-12 22:32:42133 // Writes a log message to help debugging in the field when we block a proxy
134 // response to a CONNECT request.
[email protected]af89ba62009-03-16 20:26:38135 void LogBlockedTunnelResponse(int response_code) const;
[email protected]9f9f86c2009-03-12 22:32:42136
[email protected]0b45559b2009-06-12 21:45:11137 // Called to handle a client certificate request.
[email protected]5e363962009-06-19 19:57:01138 int HandleCertificateRequest(int error);
[email protected]0b45559b2009-06-12 21:45:11139
[email protected]bd0b6772011-01-11 19:59:30140 // Called to possibly recover from an SSL handshake error. Sets next_state_
141 // and returns OK if recovering from the error. Otherwise, the same error
142 // code is returned.
143 int HandleSSLHandshakeError(int error);
144
initial.commit586acc5fe2008-07-26 22:42:52145 // Called to possibly recover from the given error. Sets next_state_ and
146 // returns OK if recovering from the error. Otherwise, the same error code
147 // is returned.
148 int HandleIOError(int error);
149
[email protected]0877e3d2009-10-17 22:29:57150 // Gets the response headers from the HttpStream.
151 HttpResponseHeaders* GetResponseHeaders() const;
152
[email protected]1c773ea12009-04-28 19:58:42153 // Called when we reached EOF or got an error. Returns true if we should
[email protected]a19f1c602009-08-24 21:35:28154 // resend the request. |error| is OK when we reached EOF.
155 bool ShouldResendRequest(int error) const;
[email protected]1c773ea12009-04-28 19:58:42156
157 // Resets the connection and the request headers for resend. Called when
158 // ShouldResendRequest() is true.
159 void ResetConnectionAndRequestForResend();
[email protected]2a5c76b2008-09-25 22:15:16160
[email protected]aecfbf22008-10-16 02:02:47161 // Decides the policy when the connection is closed before the end of headers
[email protected]0e75a732008-10-16 20:36:09162 // has been read. This only applies to reading responses, and not writing
163 // requests.
164 int HandleConnectionClosedBeforeEndOfHeaders();
[email protected]aecfbf22008-10-16 02:02:47165
[email protected]f9ee6b52008-11-08 06:46:23166 // Sets up the state machine to restart the transaction with auth.
167 void PrepareForAuthRestart(HttpAuth::Target target);
168
[email protected]2d2697f92009-02-18 21:00:32169 // Called when we don't need to drain the response body or have drained it.
170 // Resets |connection_| unless |keep_alive| is true, then calls
171 // ResetStateForRestart. Sets |next_state_| appropriately.
172 void DidDrainBodyForAuthRestart(bool keep_alive);
173
[email protected]f9ee6b52008-11-08 06:46:23174 // Resets the members of the transaction so it can be restarted.
[email protected]c3b35c22008-09-27 03:19:42175 void ResetStateForRestart();
176
[email protected]697ef4c2010-10-14 16:38:58177 // Resets the members of the transaction, except |stream_|, which needs
178 // to be maintained for multi-round auth.
179 void ResetStateForAuthRestart();
180
[email protected]1c773ea12009-04-28 19:58:42181 // Returns true if we should try to add a Proxy-Authorization header
182 bool ShouldApplyProxyAuth() const;
[email protected]c3b35c22008-09-27 03:19:42183
[email protected]1c773ea12009-04-28 19:58:42184 // Returns true if we should try to add an Authorization header.
185 bool ShouldApplyServerAuth() const;
186
[email protected]f9ee6b52008-11-08 06:46:23187 // Handles HTTP status code 401 or 407.
[email protected]0757e7702009-03-27 04:00:22188 // HandleAuthChallenge() returns a network error code, or OK on success.
189 // May update |pending_auth_target_| or |response_.auth_challenge|.
[email protected]e772db3f2010-07-12 18:11:13190 int HandleAuthChallenge();
[email protected]f9ee6b52008-11-08 06:46:23191
[email protected]8e6441ca2010-08-19 05:56:38192 // Returns true if we have auth credentials for the given target.
193 bool HaveAuth(HttpAuth::Target target) const;
[email protected]c3b35c22008-09-27 03:19:42194
[email protected]228404f2010-06-24 04:31:41195 // Get the {scheme, host, path, port} for the authentication target
196 GURL AuthURL(HttpAuth::Target target) const;
[email protected]044de0642010-06-17 10:42:15197
[email protected]aef04272010-06-28 18:03:04198 // Debug helper.
199 static std::string DescribeState(State state);
200
[email protected]e772db3f2010-07-12 18:11:13201 scoped_refptr<HttpAuthController>
202 auth_controllers_[HttpAuth::AUTH_NUM_TARGETS];
[email protected]c3b35c22008-09-27 03:19:42203
[email protected]0757e7702009-03-27 04:00:22204 // Whether this transaction is waiting for proxy auth, server auth, or is
205 // not waiting for any auth at all. |pending_auth_target_| is read and
206 // cleared by RestartWithAuth().
207 HttpAuth::Target pending_auth_target_;
208
initial.commit586acc5fe2008-07-26 22:42:52209 CompletionCallbackImpl<HttpNetworkTransaction> io_callback_;
210 CompletionCallback* user_callback_;
211
212 scoped_refptr<HttpNetworkSession> session_;
213
[email protected]9e743cd2010-03-16 07:03:53214 BoundNetLog net_log_;
initial.commit586acc5fe2008-07-26 22:42:52215 const HttpRequestInfo* request_;
[email protected]a7e41312009-12-16 23:18:14216 HttpResponseInfo response_;
initial.commit586acc5fe2008-07-26 22:42:52217
[email protected]928fb582008-08-11 15:40:23218 ProxyInfo proxy_info_;
initial.commit586acc5fe2008-07-26 22:42:52219
[email protected]26816882010-10-14 18:03:09220 scoped_ptr<StreamRequest> stream_request_;
[email protected]4d4a5162010-09-21 22:44:04221 scoped_ptr<HttpStream> stream_;
[email protected]8e6441ca2010-08-19 05:56:38222
[email protected]0877e3d2009-10-17 22:29:57223 // True if we've validated the headers that the stream parser has returned.
224 bool headers_valid_;
225
226 // True if we've logged the time of the first response byte. Used to
227 // prevent logging across authentication activity where we see multiple
228 // responses.
[email protected]8e3d2d32010-06-13 18:46:23229 bool logged_response_time_;
[email protected]0877e3d2009-10-17 22:29:57230
[email protected]aaead502008-10-15 00:20:11231 SSLConfig ssl_config_;
[email protected]c5949a32008-10-08 17:28:23232
[email protected]b94f92d2010-10-27 16:45:20233 HttpRequestHeaders request_headers_;
[email protected]4ddaf2502008-10-23 18:26:19234
[email protected]2d2697f92009-02-18 21:00:32235 // The size in bytes of the buffer we use to drain the response body that
236 // we want to throw away. The response body is typically a small error
237 // page just a few hundred bytes long.
[email protected]81854c42010-09-22 03:39:36238 static const int kDrainBodyBufferSize = 1024;
[email protected]2d2697f92009-02-18 21:00:32239
initial.commit586acc5fe2008-07-26 22:42:52240 // User buffer and length passed to the Read method.
[email protected]9dea9e1f2009-01-29 00:30:47241 scoped_refptr<IOBuffer> read_buf_;
initial.commit586acc5fe2008-07-26 22:42:52242 int read_buf_len_;
243
[email protected]21b316a2009-03-23 18:25:06244 // The time the Start method was called.
245 base::Time start_time_;
246
[email protected]d207a5f2009-06-04 05:28:40247 // The next state in the state machine.
initial.commit586acc5fe2008-07-26 22:42:52248 State next_state_;
[email protected]2ff8b312010-04-26 22:20:54249
[email protected]a7ea8832010-07-12 17:54:54250 // True when the tunnel is in the process of being established - we can't
251 // read from the socket until the tunnel is done.
252 bool establishing_tunnel_;
253
[email protected]2ff8b312010-04-26 22:20:54254 DISALLOW_COPY_AND_ASSIGN(HttpNetworkTransaction);
initial.commit586acc5fe2008-07-26 22:42:52255};
256
[email protected]c9c6f5c2010-07-31 01:30:03257int ConvertRequestPriorityToSpdyPriority(RequestPriority priority);
258
initial.commit586acc5fe2008-07-26 22:42:52259} // namespace net
260
261#endif // NET_HTTP_HTTP_NETWORK_TRANSACTION_H_