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 7 #include "base/basictypes.h" 8 #include "base/logging.h" 9 #include "net/base/io_buffer.h" 10 #include "net/http/http_request_info.h" 11 #include "net/http/http_response_body_drainer.h" 12 #include "net/http/http_stream_parser.h" 13 #include "net/http/http_util.h" 14 #include "net/socket/client_socket_handle.h" 15 #include "url/gurl.h" 16 17 namespace net { 18 19 HttpBasicState::HttpBasicState(ClientSocketHandle* connection, bool using_proxy) 20 : read_buf_(new GrowableIOBuffer()), 21 connection_(connection), 22 using_proxy_(using_proxy), 23 request_info_(NULL) {} 24 25 HttpBasicState::~HttpBasicState() {} 26 27 int HttpBasicState::Initialize(const HttpRequestInfo* request_info, 28 RequestPriority priority, 29 const BoundNetLog& net_log, 30 const CompletionCallback& callback) { 31 DCHECK(!parser_.get()); 32 request_info_ = request_info; 33 parser_.reset(new HttpStreamParser( 34 connection_.get(), request_info, read_buf_.get(), net_log)); 35 return OK; 36 } 37 38 scoped_ptr<ClientSocketHandle> HttpBasicState::ReleaseConnection() { 39 return connection_.Pass(); 40 } 41 42 scoped_refptr<GrowableIOBuffer> HttpBasicState::read_buf() const { 43 return read_buf_; 44 } 45 46 void HttpBasicState::DeleteParser() { parser_.reset(); } 47 48 std::string HttpBasicState::GenerateRequestLine() const { 49 static const char kSuffix[] = " HTTP/1.1\r\n"; 50 const size_t kSuffixLen = arraysize(kSuffix) - 1; 51 DCHECK(request_info_); 52 const GURL& url = request_info_->url; 53 const std::string path = using_proxy_ ? HttpUtil::SpecForRequest(url) 54 : HttpUtil::PathForRequest(url); 55 // Don't use StringPrintf for concatenation because it is very inefficient. 56 std::string request_line; 57 const size_t expected_size = request_info_->method.size() + 1 + path.size() + 58 kSuffixLen; 59 request_line.reserve(expected_size); 60 request_line.append(request_info_->method); 61 request_line.append(1, ' '); 62 request_line.append(path); 63 request_line.append(kSuffix, kSuffixLen); 64 DCHECK_EQ(expected_size, request_line.size()); 65 return request_line; 66 } 67 68 } // namespace net 69