[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 1 | // Copyright 2013 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. |
| 4 | |
| 5 | #include "net/http/http_basic_state.h" |
| 6 | |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 9 | #include "base/logging.h" |
| 10 | #include "net/base/io_buffer.h" |
| 11 | #include "net/http/http_request_info.h" |
| 12 | #include "net/http/http_response_body_drainer.h" |
| 13 | #include "net/http/http_stream_parser.h" |
| 14 | #include "net/http/http_util.h" |
| 15 | #include "net/socket/client_socket_handle.h" |
| 16 | #include "url/gurl.h" |
| 17 | |
| 18 | namespace net { |
| 19 | |
bnc | 3faa41e | 2016-08-18 14:16:18 | [diff] [blame] | 20 | HttpBasicState::HttpBasicState(std::unique_ptr<ClientSocketHandle> connection, |
mmenke | a7da6da | 2016-09-01 21:56:52 | [diff] [blame] | 21 | bool using_proxy, |
| 22 | bool http_09_on_non_default_ports_enabled) |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 23 | : read_buf_(new GrowableIOBuffer()), |
bnc | 3faa41e | 2016-08-18 14:16:18 | [diff] [blame] | 24 | connection_(std::move(connection)), |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 25 | using_proxy_(using_proxy), |
mmenke | a7da6da | 2016-09-01 21:56:52 | [diff] [blame] | 26 | http_09_on_non_default_ports_enabled_( |
shivanisha | 0b44085 | 2016-10-18 15:48:15 | [diff] [blame] | 27 | http_09_on_non_default_ports_enabled) {} |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 28 | |
| 29 | HttpBasicState::~HttpBasicState() {} |
| 30 | |
| 31 | int HttpBasicState::Initialize(const HttpRequestInfo* request_info, |
| 32 | RequestPriority priority, |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 33 | const NetLogWithSource& net_log, |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 34 | const CompletionCallback& callback) { |
| 35 | DCHECK(!parser_.get()); |
shivanisha | 0b44085 | 2016-10-18 15:48:15 | [diff] [blame] | 36 | url_ = request_info->url; |
| 37 | request_method_ = request_info->method; |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 38 | parser_.reset(new HttpStreamParser( |
| 39 | connection_.get(), request_info, read_buf_.get(), net_log)); |
mmenke | a7da6da | 2016-09-01 21:56:52 | [diff] [blame] | 40 | parser_->set_http_09_on_non_default_ports_enabled( |
| 41 | http_09_on_non_default_ports_enabled_); |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 42 | return OK; |
| 43 | } |
| 44 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 45 | std::unique_ptr<ClientSocketHandle> HttpBasicState::ReleaseConnection() { |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 46 | return std::move(connection_); |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 47 | } |
| 48 | |
[email protected] | d51365e | 2013-11-27 10:46:52 | [diff] [blame] | 49 | scoped_refptr<GrowableIOBuffer> HttpBasicState::read_buf() const { |
| 50 | return read_buf_; |
| 51 | } |
| 52 | |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 53 | void HttpBasicState::DeleteParser() { parser_.reset(); } |
| 54 | |
| 55 | std::string HttpBasicState::GenerateRequestLine() const { |
| 56 | static const char kSuffix[] = " HTTP/1.1\r\n"; |
| 57 | const size_t kSuffixLen = arraysize(kSuffix) - 1; |
nharper | 834c49b | 2015-06-03 23:46:43 | [diff] [blame] | 58 | const std::string path = |
shivanisha | 0b44085 | 2016-10-18 15:48:15 | [diff] [blame] | 59 | using_proxy_ ? HttpUtil::SpecForRequest(url_) : url_.PathForRequest(); |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 60 | // Don't use StringPrintf for concatenation because it is very inefficient. |
| 61 | std::string request_line; |
shivanisha | 0b44085 | 2016-10-18 15:48:15 | [diff] [blame] | 62 | const size_t expected_size = |
| 63 | request_method_.size() + 1 + path.size() + kSuffixLen; |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 64 | request_line.reserve(expected_size); |
shivanisha | 0b44085 | 2016-10-18 15:48:15 | [diff] [blame] | 65 | request_line.append(request_method_); |
[email protected] | f7c2173 | 2013-10-27 09:38:58 | [diff] [blame] | 66 | request_line.append(1, ' '); |
| 67 | request_line.append(path); |
| 68 | request_line.append(kSuffix, kSuffixLen); |
| 69 | DCHECK_EQ(expected_size, request_line.size()); |
| 70 | return request_line; |
| 71 | } |
| 72 | |
| 73 | } // namespace net |