Home | History | Annotate | Download | only in http
      1 // Copyright (c) 2011 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/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_stream_parser.h"
     13 #include "net/http/http_util.h"
     14 #include "net/socket/client_socket_handle.h"
     15 
     16 namespace net {
     17 
     18 HttpBasicStream::HttpBasicStream(ClientSocketHandle* connection,
     19                                  HttpStreamParser* parser,
     20                                  bool using_proxy)
     21     : read_buf_(new GrowableIOBuffer()),
     22       parser_(parser),
     23       connection_(connection),
     24       using_proxy_(using_proxy),
     25       request_info_(NULL) {
     26 }
     27 
     28 HttpBasicStream::~HttpBasicStream() {}
     29 
     30 int HttpBasicStream::InitializeStream(const HttpRequestInfo* request_info,
     31                                       const BoundNetLog& net_log,
     32                                       CompletionCallback* callback) {
     33   DCHECK(!parser_.get());
     34   request_info_ = request_info;
     35   parser_.reset(new HttpStreamParser(connection_.get(), request_info,
     36                                      read_buf_, net_log));
     37   return OK;
     38 }
     39 
     40 
     41 int HttpBasicStream::SendRequest(const HttpRequestHeaders& headers,
     42                                  UploadDataStream* request_body,
     43                                  HttpResponseInfo* response,
     44                                  CompletionCallback* callback) {
     45   DCHECK(parser_.get());
     46   DCHECK(request_info_);
     47   const std::string path = using_proxy_ ?
     48                            HttpUtil::SpecForRequest(request_info_->url) :
     49                            HttpUtil::PathForRequest(request_info_->url);
     50   request_line_ = base::StringPrintf("%s %s HTTP/1.1\r\n",
     51                                      request_info_->method.c_str(),
     52                                      path.c_str());
     53   return parser_->SendRequest(request_line_, headers, request_body, response,
     54                               callback);
     55 }
     56 
     57 uint64 HttpBasicStream::GetUploadProgress() const {
     58   return parser_->GetUploadProgress();
     59 }
     60 
     61 int HttpBasicStream::ReadResponseHeaders(CompletionCallback* callback) {
     62   return parser_->ReadResponseHeaders(callback);
     63 }
     64 
     65 const HttpResponseInfo* HttpBasicStream::GetResponseInfo() const {
     66   return parser_->GetResponseInfo();
     67 }
     68 
     69 int HttpBasicStream::ReadResponseBody(IOBuffer* buf, int buf_len,
     70                                       CompletionCallback* callback) {
     71   return parser_->ReadResponseBody(buf, buf_len, callback);
     72 }
     73 
     74 void HttpBasicStream::Close(bool not_reusable) {
     75   parser_->Close(not_reusable);
     76 }
     77 
     78 HttpStream* HttpBasicStream::RenewStreamForAuth() {
     79   DCHECK(IsResponseBodyComplete());
     80   DCHECK(!IsMoreDataBuffered());
     81   parser_.reset();
     82   return new HttpBasicStream(connection_.release(), NULL, using_proxy_);
     83 }
     84 
     85 bool HttpBasicStream::IsResponseBodyComplete() const {
     86   return parser_->IsResponseBodyComplete();
     87 }
     88 
     89 bool HttpBasicStream::CanFindEndOfResponse() const {
     90   return parser_->CanFindEndOfResponse();
     91 }
     92 
     93 bool HttpBasicStream::IsMoreDataBuffered() const {
     94   return parser_->IsMoreDataBuffered();
     95 }
     96 
     97 bool HttpBasicStream::IsConnectionReused() const {
     98   return parser_->IsConnectionReused();
     99 }
    100 
    101 void HttpBasicStream::SetConnectionReused() {
    102   parser_->SetConnectionReused();
    103 }
    104 
    105 bool HttpBasicStream::IsConnectionReusable() const {
    106   return parser_->IsConnectionReusable();
    107 }
    108 
    109 void HttpBasicStream::GetSSLInfo(SSLInfo* ssl_info) {
    110   parser_->GetSSLInfo(ssl_info);
    111 }
    112 
    113 void HttpBasicStream::GetSSLCertRequestInfo(
    114     SSLCertRequestInfo* cert_request_info) {
    115   parser_->GetSSLCertRequestInfo(cert_request_info);
    116 }
    117 
    118 bool HttpBasicStream::IsSpdyHttpStream() const {
    119   return false;
    120 }
    121 
    122 }  // namespace net
    123