[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 | |
bnc | 3faa41e | 2016-08-18 14:16:18 | [diff] [blame] | 7 | #include <utility> |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 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 | |
bnc | 3faa41e | 2016-08-18 14:16:18 | [diff] [blame] | 16 | HttpBasicStream::HttpBasicStream(std::unique_ptr<ClientSocketHandle> connection, |
mmenke | a7da6da | 2016-09-01 21:56:52 | [diff] [blame] | 17 | bool using_proxy, |
| 18 | bool http_09_on_non_default_ports_enabled) |
| 19 | : state_(std::move(connection), |
| 20 | using_proxy, |
| 21 | http_09_on_non_default_ports_enabled) {} |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 22 | |
[email protected] | d100e44f | 2011-01-26 22:47:11 | [diff] [blame] | 23 | HttpBasicStream::~HttpBasicStream() {} |
| 24 | |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 25 | int HttpBasicStream::InitializeStream(const HttpRequestInfo* request_info, |
| 26 | RequestPriority priority, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 27 | const NetLogWithSource& net_log, |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 28 | const CompletionCallback& callback) { |
| 29 | state_.Initialize(request_info, priority, net_log, callback); |
[email protected] | c638a85a | 2010-07-29 18:41:40 | [diff] [blame] | 30 | return OK; |
| 31 | } |
| 32 | |
[email protected] | b94f92d | 2010-10-27 16:45:20 | [diff] [blame] | 33 | int HttpBasicStream::SendRequest(const HttpRequestHeaders& headers, |
[email protected] | a7e4131 | 2009-12-16 23:18:14 | [diff] [blame] | 34 | HttpResponseInfo* response, |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 35 | const CompletionCallback& callback) { |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 36 | DCHECK(parser()); |
| 37 | return parser()->SendRequest( |
| 38 | state_.GenerateRequestLine(), headers, response, callback); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 39 | } |
| 40 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 41 | int HttpBasicStream::ReadResponseHeaders(const CompletionCallback& callback) { |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 42 | return parser()->ReadResponseHeaders(callback); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 43 | } |
| 44 | |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 45 | int HttpBasicStream::ReadResponseBody(IOBuffer* buf, |
| 46 | int buf_len, |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 47 | const CompletionCallback& callback) { |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 48 | return parser()->ReadResponseBody(buf, buf_len, callback); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 49 | } |
| 50 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 51 | void HttpBasicStream::Close(bool not_reusable) { |
xunjieli | e368398 | 2017-01-10 12:14:48 | [diff] [blame] | 52 | // parser() is null if |this| is created by an orphaned |
| 53 | // HttpStreamFactoryImpl::Job in which case InitializeStream() will not have |
| 54 | // been called. |
| 55 | if (parser()) |
| 56 | parser()->Close(not_reusable); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 57 | } |
| 58 | |
[email protected] | 697ef4c | 2010-10-14 16:38:58 | [diff] [blame] | 59 | HttpStream* HttpBasicStream::RenewStreamForAuth() { |
| 60 | DCHECK(IsResponseBodyComplete()); |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 61 | DCHECK(!parser()->IsMoreDataBuffered()); |
[email protected] | b0c3514 | 2013-11-02 03:37:06 | [diff] [blame] | 62 | // The HttpStreamParser object still has a pointer to the connection. Just to |
| 63 | // be extra-sure it doesn't touch the connection again, delete it here rather |
| 64 | // than leaving it until the destructor is called. |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 65 | state_.DeleteParser(); |
mmenke | a7da6da | 2016-09-01 21:56:52 | [diff] [blame] | 66 | return new HttpBasicStream(state_.ReleaseConnection(), state_.using_proxy(), |
| 67 | state_.http_09_on_non_default_ports_enabled()); |
[email protected] | 697ef4c | 2010-10-14 16:38:58 | [diff] [blame] | 68 | } |
| 69 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 70 | bool HttpBasicStream::IsResponseBodyComplete() const { |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 71 | return parser()->IsResponseBodyComplete(); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 72 | } |
| 73 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 74 | bool HttpBasicStream::IsConnectionReused() const { |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 75 | return parser()->IsConnectionReused(); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 76 | } |
| 77 | |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 78 | void HttpBasicStream::SetConnectionReused() { parser()->SetConnectionReused(); } |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 79 | |
mmenke | bd84c39 | 2015-09-02 14:12:34 | [diff] [blame] | 80 | bool HttpBasicStream::CanReuseConnection() const { |
| 81 | return parser()->CanReuseConnection(); |
[email protected] | 2d0a4f9 | 2011-05-05 16:38:46 | [diff] [blame] | 82 | } |
| 83 | |
sclittle | 4de1bab9 | 2015-09-22 21:28:24 | [diff] [blame] | 84 | int64_t HttpBasicStream::GetTotalReceivedBytes() const { |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 85 | if (parser()) |
| 86 | return parser()->received_bytes(); |
| 87 | return 0; |
[email protected] | bc92bc97 | 2013-12-13 08:32:59 | [diff] [blame] | 88 | } |
| 89 | |
sclittle | be1ccf6 | 2015-09-02 19:40:36 | [diff] [blame] | 90 | int64_t HttpBasicStream::GetTotalSentBytes() const { |
| 91 | if (parser()) |
| 92 | return parser()->sent_bytes(); |
| 93 | return 0; |
| 94 | } |
| 95 | |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 96 | bool HttpBasicStream::GetLoadTimingInfo( |
| 97 | LoadTimingInfo* load_timing_info) const { |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 98 | return state_.connection()->GetLoadTimingInfo(IsConnectionReused(), |
| 99 | load_timing_info); |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 100 | } |
| 101 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 102 | void HttpBasicStream::GetSSLInfo(SSLInfo* ssl_info) { |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 103 | parser()->GetSSLInfo(ssl_info); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | void HttpBasicStream::GetSSLCertRequestInfo( |
| 107 | SSLCertRequestInfo* cert_request_info) { |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 108 | parser()->GetSSLCertRequestInfo(cert_request_info); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 109 | } |
| 110 | |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 111 | bool HttpBasicStream::GetRemoteEndpoint(IPEndPoint* endpoint) { |
| 112 | if (!state_.connection() || !state_.connection()->socket()) |
| 113 | return false; |
| 114 | |
| 115 | return state_.connection()->socket()->GetPeerAddress(endpoint) == OK; |
| 116 | } |
| 117 | |
nharper | 78e6d2b | 2016-09-21 05:42:35 | [diff] [blame] | 118 | Error HttpBasicStream::GetTokenBindingSignature(crypto::ECPrivateKey* key, |
| 119 | TokenBindingType tb_type, |
| 120 | std::vector<uint8_t>* out) { |
| 121 | return parser()->GetTokenBindingSignature(key, tb_type, out); |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 122 | } |
| 123 | |
[email protected] | 5a60c8b | 2011-10-19 20:14:29 | [diff] [blame] | 124 | void HttpBasicStream::Drain(HttpNetworkSession* session) { |
| 125 | HttpResponseBodyDrainer* drainer = new HttpResponseBodyDrainer(this); |
| 126 | drainer->Start(session); |
| 127 | // |drainer| will delete itself. |
| 128 | } |
| 129 | |
zhongyi | 2ed4a6a | 2016-02-26 19:45:42 | [diff] [blame] | 130 | void HttpBasicStream::PopulateNetErrorDetails(NetErrorDetails* details) { |
mmenke | 8210acc | 2016-07-11 16:34:52 | [diff] [blame] | 131 | // TODO(mmenke): Consumers don't actually care about HTTP version, but seems |
| 132 | // like the right version should be reported, if headers were received. |
| 133 | details->connection_info = HttpResponseInfo::CONNECTION_INFO_HTTP1_1; |
zhongyi | ca364fbb | 2015-12-12 03:39:12 | [diff] [blame] | 134 | return; |
| 135 | } |
| 136 | |
[email protected] | e86839fd | 2013-08-14 18:29:03 | [diff] [blame] | 137 | void HttpBasicStream::SetPriority(RequestPriority priority) { |
| 138 | // TODO(akalin): Plumb this through to |connection_|. |
| 139 | } |
| 140 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 141 | } // namespace net |