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.
4
5#include "net/http/http_basic_stream.h"
6
7#include "base/memory/scoped_ptr.h"
8#include "net/http/http_request_info.h"
9#include "net/http/http_response_body_drainer.h"
10#include "net/http/http_stream_parser.h"
11#include "net/socket/client_socket_handle.h"
12
13namespace net {
14
15HttpBasicStream::HttpBasicStream(ClientSocketHandle* connection,
16                                 bool using_proxy)
17    : state_(connection, using_proxy) {}
18
19HttpBasicStream::~HttpBasicStream() {}
20
21int HttpBasicStream::InitializeStream(const HttpRequestInfo* request_info,
22                                      RequestPriority priority,
23                                      const BoundNetLog& net_log,
24                                      const CompletionCallback& callback) {
25  state_.Initialize(request_info, priority, net_log, callback);
26  return OK;
27}
28
29int HttpBasicStream::SendRequest(const HttpRequestHeaders& headers,
30                                 HttpResponseInfo* response,
31                                 const CompletionCallback& callback) {
32  DCHECK(parser());
33  return parser()->SendRequest(
34      state_.GenerateRequestLine(), headers, response, callback);
35}
36
37UploadProgress HttpBasicStream::GetUploadProgress() const {
38  return parser()->GetUploadProgress();
39}
40
41int HttpBasicStream::ReadResponseHeaders(const CompletionCallback& callback) {
42  return parser()->ReadResponseHeaders(callback);
43}
44
45int HttpBasicStream::ReadResponseBody(IOBuffer* buf,
46                                      int buf_len,
47                                      const CompletionCallback& callback) {
48  return parser()->ReadResponseBody(buf, buf_len, callback);
49}
50
51void HttpBasicStream::Close(bool not_reusable) {
52  parser()->Close(not_reusable);
53}
54
55HttpStream* HttpBasicStream::RenewStreamForAuth() {
56  DCHECK(IsResponseBodyComplete());
57  DCHECK(!parser()->IsMoreDataBuffered());
58  // The HttpStreamParser object still has a pointer to the connection. Just to
59  // be extra-sure it doesn't touch the connection again, delete it here rather
60  // than leaving it until the destructor is called.
61  state_.DeleteParser();
62  return new HttpBasicStream(state_.ReleaseConnection().release(),
63                             state_.using_proxy());
64}
65
66bool HttpBasicStream::IsResponseBodyComplete() const {
67  return parser()->IsResponseBodyComplete();
68}
69
70bool HttpBasicStream::CanFindEndOfResponse() const {
71  return parser()->CanFindEndOfResponse();
72}
73
74bool HttpBasicStream::IsConnectionReused() const {
75  return parser()->IsConnectionReused();
76}
77
78void HttpBasicStream::SetConnectionReused() { parser()->SetConnectionReused(); }
79
80bool HttpBasicStream::IsConnectionReusable() const {
81  return parser()->IsConnectionReusable();
82}
83
84int64 HttpBasicStream::GetTotalReceivedBytes() const {
85  if (parser())
86    return parser()->received_bytes();
87  return 0;
88}
89
90bool HttpBasicStream::GetLoadTimingInfo(
91    LoadTimingInfo* load_timing_info) const {
92  return state_.connection()->GetLoadTimingInfo(IsConnectionReused(),
93                                                load_timing_info);
94}
95
96void HttpBasicStream::GetSSLInfo(SSLInfo* ssl_info) {
97  parser()->GetSSLInfo(ssl_info);
98}
99
100void HttpBasicStream::GetSSLCertRequestInfo(
101    SSLCertRequestInfo* cert_request_info) {
102  parser()->GetSSLCertRequestInfo(cert_request_info);
103}
104
105bool HttpBasicStream::IsSpdyHttpStream() const { return false; }
106
107void HttpBasicStream::Drain(HttpNetworkSession* session) {
108  HttpResponseBodyDrainer* drainer = new HttpResponseBodyDrainer(this);
109  drainer->Start(session);
110  // |drainer| will delete itself.
111}
112
113void HttpBasicStream::SetPriority(RequestPriority priority) {
114  // TODO(akalin): Plumb this through to |connection_|.
115}
116
117}  // namespace net
118