1 // Copyright (c) 2012 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_stream.h"
7 #include "base/strings/stringprintf.h"
8 #include "net/base/io_buffer.h"
9 #include "net/base/net_errors.h"
10 #include "net/http/http_request_headers.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"
19 HttpBasicStream::HttpBasicStream(ClientSocketHandle
* connection
,
20 HttpStreamParser
* parser
,
22 : read_buf_(new GrowableIOBuffer()),
24 connection_(connection
),
25 using_proxy_(using_proxy
),
29 HttpBasicStream::~HttpBasicStream() {}
31 int HttpBasicStream::InitializeStream(
32 const HttpRequestInfo
* request_info
,
33 RequestPriority priority
,
34 const BoundNetLog
& net_log
,
35 const CompletionCallback
& callback
) {
36 DCHECK(!parser_
.get());
37 request_info_
= request_info
;
38 parser_
.reset(new HttpStreamParser(
39 connection_
.get(), request_info
, read_buf_
.get(), net_log
));
44 int HttpBasicStream::SendRequest(const HttpRequestHeaders
& headers
,
45 HttpResponseInfo
* response
,
46 const CompletionCallback
& callback
) {
47 DCHECK(parser_
.get());
48 DCHECK(request_info_
);
49 const std::string path
= using_proxy_
?
50 HttpUtil::SpecForRequest(request_info_
->url
) :
51 HttpUtil::PathForRequest(request_info_
->url
);
52 request_line_
= base::StringPrintf("%s %s HTTP/1.1\r\n",
53 request_info_
->method
.c_str(),
55 return parser_
->SendRequest(request_line_
, headers
, response
, callback
);
58 UploadProgress
HttpBasicStream::GetUploadProgress() const {
59 return parser_
->GetUploadProgress();
62 int HttpBasicStream::ReadResponseHeaders(const CompletionCallback
& callback
) {
63 return parser_
->ReadResponseHeaders(callback
);
66 const HttpResponseInfo
* HttpBasicStream::GetResponseInfo() const {
67 return parser_
->GetResponseInfo();
70 int HttpBasicStream::ReadResponseBody(IOBuffer
* buf
, int buf_len
,
71 const CompletionCallback
& callback
) {
72 return parser_
->ReadResponseBody(buf
, buf_len
, callback
);
75 void HttpBasicStream::Close(bool not_reusable
) {
76 parser_
->Close(not_reusable
);
79 HttpStream
* HttpBasicStream::RenewStreamForAuth() {
80 DCHECK(IsResponseBodyComplete());
81 DCHECK(!parser_
->IsMoreDataBuffered());
83 return new HttpBasicStream(connection_
.release(), NULL
, using_proxy_
);
86 bool HttpBasicStream::IsResponseBodyComplete() const {
87 return parser_
->IsResponseBodyComplete();
90 bool HttpBasicStream::CanFindEndOfResponse() const {
91 return parser_
->CanFindEndOfResponse();
94 bool HttpBasicStream::IsConnectionReused() const {
95 return parser_
->IsConnectionReused();
98 void HttpBasicStream::SetConnectionReused() {
99 parser_
->SetConnectionReused();
102 bool HttpBasicStream::IsConnectionReusable() const {
103 return parser_
->IsConnectionReusable();
106 bool HttpBasicStream::GetLoadTimingInfo(
107 LoadTimingInfo
* load_timing_info
) const {
108 return connection_
->GetLoadTimingInfo(IsConnectionReused(), load_timing_info
);
111 void HttpBasicStream::GetSSLInfo(SSLInfo
* ssl_info
) {
112 parser_
->GetSSLInfo(ssl_info
);
115 void HttpBasicStream::GetSSLCertRequestInfo(
116 SSLCertRequestInfo
* cert_request_info
) {
117 parser_
->GetSSLCertRequestInfo(cert_request_info
);
120 bool HttpBasicStream::IsSpdyHttpStream() const {
124 void HttpBasicStream::Drain(HttpNetworkSession
* session
) {
125 HttpResponseBodyDrainer
* drainer
= new HttpResponseBodyDrainer(this);
126 drainer
->Start(session
);
127 // |drainer| will delete itself.