]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com) | |
3 | // | |
4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
6 | // | |
7 | ||
8 | #ifndef BEAST_HTTP_BASIC_DYNABUF_BODY_HPP | |
9 | #define BEAST_HTTP_BASIC_DYNABUF_BODY_HPP | |
10 | ||
11 | #include <beast/config.hpp> | |
12 | #include <beast/core/error.hpp> | |
13 | #include <beast/http/message.hpp> | |
14 | #include <beast/core/detail/type_traits.hpp> | |
15 | #include <boost/asio/buffer.hpp> | |
16 | ||
17 | namespace beast { | |
18 | namespace http { | |
19 | ||
20 | /** A message body represented by a @b `DynamicBuffer` | |
21 | ||
22 | Meets the requirements of @b `Body`. | |
23 | */ | |
24 | template<class DynamicBuffer> | |
25 | struct basic_dynabuf_body | |
26 | { | |
27 | /// The type of the `message::body` member | |
28 | using value_type = DynamicBuffer; | |
29 | ||
30 | #if BEAST_DOXYGEN | |
31 | private: | |
32 | #endif | |
33 | ||
34 | class reader | |
35 | { | |
36 | value_type& body_; | |
37 | ||
38 | public: | |
39 | static bool constexpr is_direct = true; | |
40 | ||
41 | using mutable_buffers_type = | |
42 | typename DynamicBuffer::mutable_buffers_type; | |
43 | ||
44 | template<bool isRequest, class Fields> | |
45 | explicit | |
46 | reader(message<isRequest, | |
47 | basic_dynabuf_body, Fields>& msg) | |
48 | : body_(msg.body) | |
49 | { | |
50 | } | |
51 | ||
52 | void | |
53 | init() | |
54 | { | |
55 | } | |
56 | ||
57 | void | |
58 | init(std::uint64_t content_length) | |
59 | { | |
60 | } | |
61 | ||
62 | mutable_buffers_type | |
63 | prepare(std::size_t n) | |
64 | { | |
65 | return body_.prepare(n); | |
66 | } | |
67 | ||
68 | void | |
69 | commit(std::size_t n) | |
70 | { | |
71 | body_.commit(n); | |
72 | } | |
73 | ||
74 | void | |
75 | finish() | |
76 | { | |
77 | } | |
78 | }; | |
79 | ||
80 | class writer | |
81 | { | |
82 | DynamicBuffer const& body_; | |
83 | ||
84 | public: | |
85 | template<bool isRequest, class Fields> | |
86 | explicit | |
87 | writer(message< | |
88 | isRequest, basic_dynabuf_body, Fields> const& m) noexcept | |
89 | : body_(m.body) | |
90 | { | |
91 | } | |
92 | ||
93 | void | |
94 | init(error_code& ec) noexcept | |
95 | { | |
96 | beast::detail::ignore_unused(ec); | |
97 | } | |
98 | ||
99 | std::uint64_t | |
100 | content_length() const noexcept | |
101 | { | |
102 | return body_.size(); | |
103 | } | |
104 | ||
105 | template<class WriteFunction> | |
106 | bool | |
107 | write(error_code&, WriteFunction&& wf) noexcept | |
108 | { | |
109 | wf(body_.data()); | |
110 | return true; | |
111 | } | |
112 | }; | |
113 | }; | |
114 | ||
115 | } // http | |
116 | } // beast | |
117 | ||
118 | #endif |