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.
5 #include "net/http/http_basic_state.h"
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"
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
) {}
25 HttpBasicState::~HttpBasicState() {}
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
));
38 scoped_ptr
<ClientSocketHandle
> HttpBasicState::ReleaseConnection() {
39 return connection_
.Pass();
42 void HttpBasicState::DeleteParser() { parser_
.reset(); }
44 std::string
HttpBasicState::GenerateRequestLine() const {
45 static const char kSuffix
[] = " HTTP/1.1\r\n";
46 const size_t kSuffixLen
= arraysize(kSuffix
) - 1;
47 DCHECK(request_info_
);
48 const GURL
& url
= request_info_
->url
;
49 const std::string path
= using_proxy_
? HttpUtil::SpecForRequest(url
)
50 : HttpUtil::PathForRequest(url
);
51 // Don't use StringPrintf for concatenation because it is very inefficient.
52 std::string request_line
;
53 const size_t expected_size
= request_info_
->method
.size() + 1 + path
.size() +
55 request_line
.reserve(expected_size
);
56 request_line
.append(request_info_
->method
);
57 request_line
.append(1, ' ');
58 request_line
.append(path
);
59 request_line
.append(kSuffix
, kSuffixLen
);
60 DCHECK_EQ(expected_size
, request_line
.size());