blob: ab679057094c282fc6173b7c2a421ec8226f4e20 [file] [log] [blame]
Alex Vakulenkoa3062c52014-04-21 17:05:51 -07001// Copyright 2014 The Chromium OS 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#ifndef BUFFET_HTTP_CONNECTION_H_
6#define BUFFET_HTTP_CONNECTION_H_
7
8#include <string>
9#include <vector>
10
11#include <base/basictypes.h>
12
Alex Vakulenkob3aac252014-05-07 17:35:24 -070013#include "buffet/error.h"
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070014#include "buffet/http_transport.h"
15
Alex Vakulenkoaf23b322014-05-08 16:25:45 -070016namespace buffet {
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070017namespace http {
18
19///////////////////////////////////////////////////////////////////////////////
Alex Vakulenkob3aac252014-05-07 17:35:24 -070020// Connection class is the base class for HTTP communication session.
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070021// It abstracts the implementation of underlying transport library (ex libcurl).
22// When the Connection-derived class is constructed, it is pre-set up with
23// basic initialization information necessary to initiate the server request
24// connection (such as the URL, request method, etc - see
25// Transport::CreateConnection() for more details). But most implementations
26// would not probably initiate the physical connection until SendHeaders
27// is called.
28// You normally shouldn't worry about using this class directly.
Alex Vakulenkob3aac252014-05-07 17:35:24 -070029// http::Request and http::Response classes use it for communication.
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070030///////////////////////////////////////////////////////////////////////////////
31class Connection {
32 public:
Alex Vakulenkob3aac252014-05-07 17:35:24 -070033 explicit Connection(std::shared_ptr<Transport> transport)
34 : transport_(transport) {}
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070035 virtual ~Connection() = default;
36
37 // Called by http::Request to initiate the connection with the server.
38 // This normally opens the socket and sends the request headers.
Alex Vakulenkob3aac252014-05-07 17:35:24 -070039 virtual bool SendHeaders(const HeaderList& headers, ErrorPtr* error) = 0;
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070040 // If needed, this function can be called to send the request body data.
41 // This function can be called repeatedly until all data is sent.
Alex Vakulenkob3aac252014-05-07 17:35:24 -070042 virtual bool WriteRequestData(const void* data, size_t size,
43 ErrorPtr* error) = 0;
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070044 // This function is called when all the data is sent off and it's time
45 // to receive the response data.
Alex Vakulenkob3aac252014-05-07 17:35:24 -070046 virtual bool FinishRequest(ErrorPtr* error) = 0;
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070047
48 // Returns the HTTP status code (e.g. 200 for success).
49 virtual int GetResponseStatusCode() const = 0;
50 // Returns the status text (e.g. for error 403 it could be "NOT AUTHORIZED").
51 virtual std::string GetResponseStatusText() const = 0;
52 // Returns the HTTP protocol version (e.g. "HTTP/1.1").
53 virtual std::string GetProtocolVersion() const = 0;
54 // Returns the value of particular response header, or empty string if the
55 // headers wasn't received.
56 virtual std::string GetResponseHeader(
57 const std::string& header_name) const = 0;
58 // Returns the response data size, if known. For chunked (streaming)
59 // transmission this might not be known until all the data is sent.
60 // In this case GetResponseDataSize() will return 0.
61 virtual uint64_t GetResponseDataSize() const = 0;
62 // This function is called to read a block of response data.
63 // It needs to be called repeatedly until it returns false or |size_read| is
64 // set to 0. |data| is the destination buffer to read the data into.
65 // |buffer_size| is the size of the buffer (amount of data to read).
66 // |read_size| is the amount of data actually read, which could be less than
67 // the size requested or 0 if there is no more data available.
68 virtual bool ReadResponseData(void* data, size_t buffer_size,
Alex Vakulenkob3aac252014-05-07 17:35:24 -070069 size_t* size_read, ErrorPtr* error) = 0;
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070070
71 protected:
72 // |transport_| is mainly used to keep the object alive as long as the
73 // connection exists. But some implementations of Connection could use
74 // the Transport-derived class for their own needs as well.
75 std::shared_ptr<Transport> transport_;
76
77 private:
78 DISALLOW_COPY_AND_ASSIGN(Connection);
79};
80
Alex Vakulenkob3aac252014-05-07 17:35:24 -070081} // namespace http
Alex Vakulenkoaf23b322014-05-08 16:25:45 -070082} // namespace buffet
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070083
Alex Vakulenkob3aac252014-05-07 17:35:24 -070084#endif // BUFFET_HTTP_CONNECTION_H_