1 // Copyright (c) 2009 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 // HttpStream is an interface for reading and writing data to an HttpStream that 6 // keeps the client agnostic of the actual underlying transport layer. This 7 // provides an abstraction for both a basic http stream as well as http 8 // pipelining implementations. 9 10 #ifndef NET_HTTP_HTTP_STREAM_H_ 11 #define NET_HTTP_HTTP_STREAM_H_ 12 13 #include <string> 14 15 #include "base/basictypes.h" 16 #include "net/socket/client_socket_handle.h" 17 18 namespace net { 19 20 class HttpRequestInfo; 21 class HttpResponseInfo; 22 class IOBuffer; 23 class UploadDataStream; 24 25 class HttpStream { 26 public: 27 HttpStream() {} 28 virtual ~HttpStream() {} 29 30 // Writes the headers and uploads body data to the underlying socket. 31 // ERR_IO_PENDING is returned if the operation could not be completed 32 // synchronously, in which case the result will be passed to the callback 33 // when available. Returns OK on success. The HttpStream takes ownership 34 // of the request_body. 35 virtual int SendRequest(const HttpRequestInfo* request, 36 const std::string& request_headers, 37 UploadDataStream* request_body, 38 HttpResponseInfo* response, 39 CompletionCallback* callback) = 0; 40 41 // Queries the UploadDataStream for its progress (bytes sent). 42 virtual uint64 GetUploadProgress() const = 0; 43 44 // Reads from the underlying socket until the response headers have been 45 // completely received. ERR_IO_PENDING is returned if the operation could 46 // not be completed synchronously, in which case the result will be passed 47 // to the callback when available. Returns OK on success. The response 48 // headers are available in the HttpResponseInfo returned by GetResponseInfo 49 virtual int ReadResponseHeaders(CompletionCallback* callback) = 0; 50 51 // Provides access to HttpResponseInfo (owned by HttpStream). 52 virtual HttpResponseInfo* GetResponseInfo() const = 0; 53 54 // Reads response body data, up to |buf_len| bytes. |buf_len| should be a 55 // reasonable size (<2MB). The number of bytes read is returned, or an 56 // error is returned upon failure. ERR_CONNECTION_CLOSED is returned to 57 // indicate end-of-connection. ERR_IO_PENDING is returned if the operation 58 // could not be completed synchronously, in which case the result will be 59 // passed to the callback when available. If the operation is not completed 60 // immediately, the socket acquires a reference to the provided buffer until 61 // the callback is invoked or the socket is destroyed. 62 virtual int ReadResponseBody(IOBuffer* buf, int buf_len, 63 CompletionCallback* callback) = 0; 64 65 // Indicates if the response body has been completely read. 66 virtual bool IsResponseBodyComplete() const = 0; 67 68 // Indicates that the end of the response is detectable. This means that 69 // the response headers indicate either chunked encoding or content length. 70 // If neither is sent, the server must close the connection for us to detect 71 // the end of the response. 72 virtual bool CanFindEndOfResponse() const = 0; 73 74 // After the response headers have been read and after the response body 75 // is complete, this function indicates if more data (either erroneous or 76 // as part of the next pipelined response) has been read from the socket. 77 virtual bool IsMoreDataBuffered() const = 0; 78 79 private: 80 DISALLOW_COPY_AND_ASSIGN(HttpStream); 81 }; 82 83 } // namespace net 84 85 #endif // NET_HTTP_HTTP_STREAM_H_ 86