[email protected] | 5f9205f | 2012-06-14 19:55:23 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/http/http_basic_stream.h" |
| 6 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame^] | 7 | #include <memory> |
| 8 | |
[email protected] | b94f92d | 2010-10-27 16:45:20 | [diff] [blame] | 9 | #include "net/http/http_request_info.h" |
[email protected] | 5a60c8b | 2011-10-19 20:14:29 | [diff] [blame] | 10 | #include "net/http/http_response_body_drainer.h" |
[email protected] | 277d594 | 2010-08-11 21:02:35 | [diff] [blame] | 11 | #include "net/http/http_stream_parser.h" |
[email protected] | 4d4a516 | 2010-09-21 22:44:04 | [diff] [blame] | 12 | #include "net/socket/client_socket_handle.h" |
[email protected] | 277d594 | 2010-08-11 21:02:35 | [diff] [blame] | 13 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 14 | namespace net { |
| 15 | |
[email protected] | b94f92d | 2010-10-27 16:45:20 | [diff] [blame] | 16 | HttpBasicStream::HttpBasicStream(ClientSocketHandle* connection, |
| 17 | bool using_proxy) |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 18 | : state_(connection, using_proxy) {} |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 19 | |
[email protected] | d100e44f | 2011-01-26 22:47:11 | [diff] [blame] | 20 | HttpBasicStream::~HttpBasicStream() {} |
| 21 | |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 22 | int HttpBasicStream::InitializeStream(const HttpRequestInfo* request_info, |
| 23 | RequestPriority priority, |
| 24 | const BoundNetLog& net_log, |
| 25 | const CompletionCallback& callback) { |
| 26 | state_.Initialize(request_info, priority, net_log, callback); |
[email protected] | c638a85a | 2010-07-29 18:41:40 | [diff] [blame] | 27 | return OK; |
| 28 | } |
| 29 | |
[email protected] | b94f92d | 2010-10-27 16:45:20 | [diff] [blame] | 30 | int HttpBasicStream::SendRequest(const HttpRequestHeaders& headers, |
[email protected] | a7e4131 | 2009-12-16 23:18:14 | [diff] [blame] | 31 | HttpResponseInfo* response, |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 32 | const CompletionCallback& callback) { |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 33 | DCHECK(parser()); |
| 34 | return parser()->SendRequest( |
| 35 | state_.GenerateRequestLine(), headers, response, callback); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 36 | } |
| 37 | |
[email protected] | 196d18a | 2012-08-30 03:47:31 | [diff] [blame] | 38 | UploadProgress HttpBasicStream::GetUploadProgress() const { |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 39 | return parser()->GetUploadProgress(); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 40 | } |
| 41 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 42 | int HttpBasicStream::ReadResponseHeaders(const CompletionCallback& callback) { |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 43 | return parser()->ReadResponseHeaders(callback); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 44 | } |
| 45 | |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 46 | int HttpBasicStream::ReadResponseBody(IOBuffer* buf, |
| 47 | int buf_len, |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 48 | const CompletionCallback& callback) { |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 49 | return parser()->ReadResponseBody(buf, buf_len, callback); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 50 | } |
| 51 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 52 | void HttpBasicStream::Close(bool not_reusable) { |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 53 | parser()->Close(not_reusable); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 54 | } |
| 55 | |
[email protected] | 697ef4c | 2010-10-14 16:38:58 | [diff] [blame] | 56 | HttpStream* HttpBasicStream::RenewStreamForAuth() { |
| 57 | DCHECK(IsResponseBodyComplete()); |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 58 | DCHECK(!parser()->IsMoreDataBuffered()); |
[email protected] | b0c3514 | 2013-11-02 03:37:06 | [diff] [blame] | 59 | // The HttpStreamParser object still has a pointer to the connection. Just to |
| 60 | // be extra-sure it doesn't touch the connection again, delete it here rather |
| 61 | // than leaving it until the destructor is called. |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 62 | state_.DeleteParser(); |
| 63 | return new HttpBasicStream(state_.ReleaseConnection().release(), |
| 64 | state_.using_proxy()); |
[email protected] | 697ef4c | 2010-10-14 16:38:58 | [diff] [blame] | 65 | } |
| 66 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 67 | bool HttpBasicStream::IsResponseBodyComplete() const { |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 68 | return parser()->IsResponseBodyComplete(); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 69 | } |
| 70 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 71 | bool HttpBasicStream::IsConnectionReused() const { |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 72 | return parser()->IsConnectionReused(); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 73 | } |
| 74 | |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 75 | void HttpBasicStream::SetConnectionReused() { parser()->SetConnectionReused(); } |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 76 | |
mmenke | bd84c39 | 2015-09-02 14:12:34 | [diff] [blame] | 77 | bool HttpBasicStream::CanReuseConnection() const { |
| 78 | return parser()->CanReuseConnection(); |
[email protected] | 2d0a4f9 | 2011-05-05 16:38:46 | [diff] [blame] | 79 | } |
| 80 | |
sclittle | 4de1bab9 | 2015-09-22 21:28:24 | [diff] [blame] | 81 | int64_t HttpBasicStream::GetTotalReceivedBytes() const { |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 82 | if (parser()) |
| 83 | return parser()->received_bytes(); |
| 84 | return 0; |
[email protected] | bc92bc97 | 2013-12-13 08:32:59 | [diff] [blame] | 85 | } |
| 86 | |
sclittle | be1ccf6 | 2015-09-02 19:40:36 | [diff] [blame] | 87 | int64_t HttpBasicStream::GetTotalSentBytes() const { |
| 88 | if (parser()) |
| 89 | return parser()->sent_bytes(); |
| 90 | return 0; |
| 91 | } |
| 92 | |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 93 | bool HttpBasicStream::GetLoadTimingInfo( |
| 94 | LoadTimingInfo* load_timing_info) const { |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 95 | return state_.connection()->GetLoadTimingInfo(IsConnectionReused(), |
| 96 | load_timing_info); |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 97 | } |
| 98 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 99 | void HttpBasicStream::GetSSLInfo(SSLInfo* ssl_info) { |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 100 | parser()->GetSSLInfo(ssl_info); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void HttpBasicStream::GetSSLCertRequestInfo( |
| 104 | SSLCertRequestInfo* cert_request_info) { |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 105 | parser()->GetSSLCertRequestInfo(cert_request_info); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 106 | } |
| 107 | |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 108 | bool HttpBasicStream::GetRemoteEndpoint(IPEndPoint* endpoint) { |
| 109 | if (!state_.connection() || !state_.connection()->socket()) |
| 110 | return false; |
| 111 | |
| 112 | return state_.connection()->socket()->GetPeerAddress(endpoint) == OK; |
| 113 | } |
| 114 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 115 | Error HttpBasicStream::GetSignedEKMForTokenBinding(crypto::ECPrivateKey* key, |
| 116 | std::vector<uint8_t>* out) { |
| 117 | return parser()->GetSignedEKMForTokenBinding(key, out); |
| 118 | } |
| 119 | |
[email protected] | 5a60c8b | 2011-10-19 20:14:29 | [diff] [blame] | 120 | void HttpBasicStream::Drain(HttpNetworkSession* session) { |
| 121 | HttpResponseBodyDrainer* drainer = new HttpResponseBodyDrainer(this); |
| 122 | drainer->Start(session); |
| 123 | // |drainer| will delete itself. |
| 124 | } |
| 125 | |
zhongyi | 2ed4a6a | 2016-02-26 19:45:42 | [diff] [blame] | 126 | void HttpBasicStream::PopulateNetErrorDetails(NetErrorDetails* details) { |
| 127 | details->connection_info = HttpResponseInfo::CONNECTION_INFO_HTTP1; |
zhongyi | ca364fbb | 2015-12-12 03:39:12 | [diff] [blame] | 128 | return; |
| 129 | } |
| 130 | |
[email protected] | e86839fd | 2013-08-14 18:29:03 | [diff] [blame] | 131 | void HttpBasicStream::SetPriority(RequestPriority priority) { |
| 132 | // TODO(akalin): Plumb this through to |connection_|. |
| 133 | } |
| 134 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 135 | } // namespace net |