1 // Copyright (c) 2012 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.
5 #ifndef NET_HTTP_HTTP_NETWORK_TRANSACTION_H_
6 #define NET_HTTP_HTTP_NETWORK_TRANSACTION_H_
10 #include "base/basictypes.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/time.h"
15 #include "net/base/net_log.h"
16 #include "net/base/request_priority.h"
17 #include "net/base/ssl_config_service.h"
18 #include "net/http/http_auth.h"
19 #include "net/http/http_request_headers.h"
20 #include "net/http/http_response_info.h"
21 #include "net/http/http_stream_factory.h"
22 #include "net/http/http_transaction.h"
23 #include "net/proxy/proxy_service.h"
27 class HttpAuthController
;
28 class HttpNetworkSession
;
30 class HttpStreamRequest
;
32 class UploadDataStream
;
33 struct HttpRequestInfo
;
35 class NET_EXPORT_PRIVATE HttpNetworkTransaction
36 : public HttpTransaction
,
37 public HttpStreamRequest::Delegate
{
39 explicit HttpNetworkTransaction(HttpNetworkSession
* session
);
41 virtual ~HttpNetworkTransaction();
43 // HttpTransaction methods:
44 virtual int Start(const HttpRequestInfo
* request_info
,
45 const CompletionCallback
& callback
,
46 const BoundNetLog
& net_log
) OVERRIDE
;
47 virtual int RestartIgnoringLastError(
48 const CompletionCallback
& callback
) OVERRIDE
;
49 virtual int RestartWithCertificate(
50 X509Certificate
* client_cert
,
51 const CompletionCallback
& callback
) OVERRIDE
;
52 virtual int RestartWithAuth(const AuthCredentials
& credentials
,
53 const CompletionCallback
& callback
) OVERRIDE
;
54 virtual bool IsReadyToRestartForAuth() OVERRIDE
;
56 virtual int Read(IOBuffer
* buf
,
58 const CompletionCallback
& callback
) OVERRIDE
;
59 virtual void StopCaching() OVERRIDE
{}
60 virtual void DoneReading() OVERRIDE
{}
61 virtual const HttpResponseInfo
* GetResponseInfo() const OVERRIDE
;
62 virtual LoadState
GetLoadState() const OVERRIDE
;
63 virtual uint64
GetUploadProgress() const OVERRIDE
;
65 // HttpStreamRequest::Delegate methods:
66 virtual void OnStreamReady(const SSLConfig
& used_ssl_config
,
67 const ProxyInfo
& used_proxy_info
,
68 HttpStream
* stream
) OVERRIDE
;
69 virtual void OnStreamFailed(int status
,
70 const SSLConfig
& used_ssl_config
) OVERRIDE
;
71 virtual void OnCertificateError(int status
,
72 const SSLConfig
& used_ssl_config
,
73 const SSLInfo
& ssl_info
) OVERRIDE
;
74 virtual void OnNeedsProxyAuth(
75 const HttpResponseInfo
& response_info
,
76 const SSLConfig
& used_ssl_config
,
77 const ProxyInfo
& used_proxy_info
,
78 HttpAuthController
* auth_controller
) OVERRIDE
;
79 virtual void OnNeedsClientAuth(const SSLConfig
& used_ssl_config
,
80 SSLCertRequestInfo
* cert_info
) OVERRIDE
;
81 virtual void OnHttpsProxyTunnelResponse(const HttpResponseInfo
& response_info
,
82 const SSLConfig
& used_ssl_config
,
83 const ProxyInfo
& used_proxy_info
,
84 HttpStream
* stream
) OVERRIDE
;
87 FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionSpdy2Test
,
88 ResetStateForRestart
);
89 FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionSpdy3Test
,
90 ResetStateForRestart
);
91 FRIEND_TEST_ALL_PREFIXES(SpdyNetworkTransactionSpdy2Test
,
92 WindowUpdateReceived
);
93 FRIEND_TEST_ALL_PREFIXES(SpdyNetworkTransactionSpdy2Test
,
95 FRIEND_TEST_ALL_PREFIXES(SpdyNetworkTransactionSpdy2Test
,
96 WindowUpdateOverflow
);
97 FRIEND_TEST_ALL_PREFIXES(SpdyNetworkTransactionSpdy2Test
,
98 FlowControlStallResume
);
99 FRIEND_TEST_ALL_PREFIXES(SpdyNetworkTransactionSpdy3Test
,
100 WindowUpdateReceived
);
101 FRIEND_TEST_ALL_PREFIXES(SpdyNetworkTransactionSpdy3Test
,
103 FRIEND_TEST_ALL_PREFIXES(SpdyNetworkTransactionSpdy3Test
,
104 WindowUpdateOverflow
);
105 FRIEND_TEST_ALL_PREFIXES(SpdyNetworkTransactionSpdy3Test
,
106 FlowControlStallResume
);
107 FRIEND_TEST_ALL_PREFIXES(SpdyNetworkTransactionSpdy3Test
,
108 FlowControlStallResumeAfterSettings
);
109 FRIEND_TEST_ALL_PREFIXES(SpdyNetworkTransactionSpdy3Test
,
110 FlowControlNegativeSendWindowSize
);
114 STATE_CREATE_STREAM_COMPLETE
,
116 STATE_INIT_STREAM_COMPLETE
,
117 STATE_GENERATE_PROXY_AUTH_TOKEN
,
118 STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE
,
119 STATE_GENERATE_SERVER_AUTH_TOKEN
,
120 STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE
,
122 STATE_BUILD_REQUEST_COMPLETE
,
124 STATE_SEND_REQUEST_COMPLETE
,
126 STATE_READ_HEADERS_COMPLETE
,
128 STATE_READ_BODY_COMPLETE
,
129 STATE_DRAIN_BODY_FOR_AUTH_RESTART
,
130 STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE
,
134 bool is_https_request() const;
136 void DoCallback(int result
);
137 void OnIOComplete(int result
);
139 // Runs the state transition loop.
140 int DoLoop(int result
);
142 // Each of these methods corresponds to a State value. Those with an input
143 // argument receive the result from the previous state. If a method returns
144 // ERR_IO_PENDING, then the result from OnIOComplete will be passed to the
145 // next state method as the result arg.
146 int DoCreateStream();
147 int DoCreateStreamComplete(int result
);
149 int DoInitStreamComplete(int result
);
150 int DoGenerateProxyAuthToken();
151 int DoGenerateProxyAuthTokenComplete(int result
);
152 int DoGenerateServerAuthToken();
153 int DoGenerateServerAuthTokenComplete(int result
);
154 int DoBuildRequest();
155 int DoBuildRequestComplete(int result
);
157 int DoSendRequestComplete(int result
);
159 int DoReadHeadersComplete(int result
);
161 int DoReadBodyComplete(int result
);
162 int DoDrainBodyForAuthRestart();
163 int DoDrainBodyForAuthRestartComplete(int result
);
165 void BuildRequestHeaders(bool using_proxy
);
167 // Record histogram of time until first byte of header is received.
168 void LogTransactionConnectedMetrics();
170 // Record histogram of latency (durations until last byte received).
171 void LogTransactionMetrics() const;
173 // Writes a log message to help debugging in the field when we block a proxy
174 // response to a CONNECT request.
175 void LogBlockedTunnelResponse(int response_code
) const;
177 // Called to handle a client certificate request.
178 int HandleCertificateRequest(int error
);
180 // Called to possibly recover from an SSL handshake error. Sets next_state_
181 // and returns OK if recovering from the error. Otherwise, the same error
183 int HandleSSLHandshakeError(int error
);
185 // Called to possibly recover from the given error. Sets next_state_ and
186 // returns OK if recovering from the error. Otherwise, the same error code
188 int HandleIOError(int error
);
190 // Gets the response headers from the HttpStream.
191 HttpResponseHeaders
* GetResponseHeaders() const;
193 // Called when we reached EOF or got an error. Returns true if we should
194 // resend the request. |error| is OK when we reached EOF.
195 bool ShouldResendRequest(int error
) const;
197 // Resets the connection and the request headers for resend. Called when
198 // ShouldResendRequest() is true.
199 void ResetConnectionAndRequestForResend();
201 // Decides the policy when the connection is closed before the end of headers
202 // has been read. This only applies to reading responses, and not writing
204 int HandleConnectionClosedBeforeEndOfHeaders();
206 // Sets up the state machine to restart the transaction with auth.
207 void PrepareForAuthRestart(HttpAuth::Target target
);
209 // Called when we don't need to drain the response body or have drained it.
210 // Resets |connection_| unless |keep_alive| is true, then calls
211 // ResetStateForRestart. Sets |next_state_| appropriately.
212 void DidDrainBodyForAuthRestart(bool keep_alive
);
214 // Resets the members of the transaction so it can be restarted.
215 void ResetStateForRestart();
217 // Resets the members of the transaction, except |stream_|, which needs
218 // to be maintained for multi-round auth.
219 void ResetStateForAuthRestart();
221 // Returns true if we should try to add a Proxy-Authorization header
222 bool ShouldApplyProxyAuth() const;
224 // Returns true if we should try to add an Authorization header.
225 bool ShouldApplyServerAuth() const;
227 // Handles HTTP status code 401 or 407.
228 // HandleAuthChallenge() returns a network error code, or OK on success.
229 // May update |pending_auth_target_| or |response_.auth_challenge|.
230 int HandleAuthChallenge();
232 // Returns true if we have auth credentials for the given target.
233 bool HaveAuth(HttpAuth::Target target
) const;
235 // Get the {scheme, host, path, port} for the authentication target
236 GURL
AuthURL(HttpAuth::Target target
) const;
239 static std::string
DescribeState(State state
);
241 scoped_refptr
<HttpAuthController
>
242 auth_controllers_
[HttpAuth::AUTH_NUM_TARGETS
];
244 // Whether this transaction is waiting for proxy auth, server auth, or is
245 // not waiting for any auth at all. |pending_auth_target_| is read and
246 // cleared by RestartWithAuth().
247 HttpAuth::Target pending_auth_target_
;
249 CompletionCallback io_callback_
;
250 CompletionCallback callback_
;
251 scoped_ptr
<UploadDataStream
> request_body_
;
253 scoped_refptr
<HttpNetworkSession
> session_
;
255 BoundNetLog net_log_
;
256 const HttpRequestInfo
* request_
;
257 HttpResponseInfo response_
;
259 // |proxy_info_| is the ProxyInfo used by the HttpStreamRequest.
260 ProxyInfo proxy_info_
;
262 scoped_ptr
<HttpStreamRequest
> stream_request_
;
263 scoped_ptr
<HttpStream
> stream_
;
265 // True if we've validated the headers that the stream parser has returned.
268 // True if we've logged the time of the first response byte. Used to
269 // prevent logging across authentication activity where we see multiple
271 bool logged_response_time_
;
273 SSLConfig server_ssl_config_
;
274 SSLConfig proxy_ssl_config_
;
276 HttpRequestHeaders request_headers_
;
278 // The size in bytes of the buffer we use to drain the response body that
279 // we want to throw away. The response body is typically a small error
280 // page just a few hundred bytes long.
281 static const int kDrainBodyBufferSize
= 1024;
283 // User buffer and length passed to the Read method.
284 scoped_refptr
<IOBuffer
> read_buf_
;
287 // The time the Start method was called.
288 base::Time start_time_
;
290 // The next state in the state machine.
293 // True when the tunnel is in the process of being established - we can't
294 // read from the socket until the tunnel is done.
295 bool establishing_tunnel_
;
297 DISALLOW_COPY_AND_ASSIGN(HttpNetworkTransaction
);
302 #endif // NET_HTTP_HTTP_NETWORK_TRANSACTION_H_