Line data Source code
1 : #ifndef THINGER_HTTP_DATA_OUT_DATA_HPP
2 : #define THINGER_HTTP_DATA_OUT_DATA_HPP
3 :
4 : #include <memory>
5 : #include <functional>
6 : #include <boost/asio.hpp>
7 : #include "../../asio/sockets/socket.hpp"
8 : #include "../../util/types.hpp"
9 :
10 : namespace thinger::http::data {
11 :
12 : class out_data {
13 :
14 : public:
15 597288 : out_data() = default;
16 599885 : virtual ~out_data() = default;
17 :
18 8332 : virtual awaitable<io_result> to_socket(std::shared_ptr<thinger::asio::socket> socket) {
19 : std::vector<boost::asio::const_buffer> buffer;
20 : // enough for the status line, a few headers and the body of a response at once
21 : buffer.reserve(32);
22 : fill_buffer(buffer);
23 : co_return co_await socket->write(buffer);
24 16664 : }
25 :
26 : void set_next_data(std::shared_ptr<out_data> data) {
27 : data_ = data;
28 : }
29 :
30 : virtual size_t get_size() = 0;
31 : virtual void to_buffer(std::vector<boost::asio::const_buffer>& buffer) const = 0;
32 :
33 : // Buffers of this data and the data following it (see set_next_data)
34 47449 : void fill_buffer(std::vector<boost::asio::const_buffer>& buffer) {
35 47449 : to_buffer(buffer);
36 47449 : if (data_) {
37 0 : data_->fill_buffer(buffer);
38 : }
39 47449 : }
40 :
41 : protected:
42 0 : virtual bool supports_buffer() {
43 0 : return true;
44 : }
45 :
46 : private:
47 : std::shared_ptr<out_data> data_;
48 : };
49 :
50 : }
51 :
52 : #endif
|