| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // | ||
| 2 | // Copyright (c) 2021 Vinnie Falco (vinnie.falco@gmail.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 | // Official repository: https://github.com/cppalliance/http_proto | ||
| 8 | // | ||
| 9 | |||
| 10 | #ifndef BOOST_HTTP_PROTO_STRING_BODY_HPP | ||
| 11 | #define BOOST_HTTP_PROTO_STRING_BODY_HPP | ||
| 12 | |||
| 13 | #include <boost/http_proto/detail/config.hpp> | ||
| 14 | #include <boost/buffers/buffer.hpp> | ||
| 15 | #include <string> | ||
| 16 | #include <utility> | ||
| 17 | |||
| 18 | namespace boost { | ||
| 19 | namespace http_proto { | ||
| 20 | |||
| 21 | /** A ConstBufferSequence adapter for an owned `std::string`. | ||
| 22 | |||
| 23 | Takes ownership of a `std::string` and exposes | ||
| 24 | it via an interface conforming to the | ||
| 25 | ConstBufferSequence requirements. | ||
| 26 | |||
| 27 | @par Example | ||
| 28 | @code | ||
| 29 | serializer sr(ctx); | ||
| 30 | response res(status::not_found); | ||
| 31 | std::string body = | ||
| 32 | "<html>\n" | ||
| 33 | " <body>\n" | ||
| 34 | " <h1>404 Not Found</h1>\n" | ||
| 35 | " <p>Sorry, the page does not exist.</p>\n" | ||
| 36 | " </body>\n" | ||
| 37 | "</html>\n"; | ||
| 38 | res.set_payload_size(body.size()); | ||
| 39 | sr.start<string_body>(res, std::move(body)); | ||
| 40 | @endcode | ||
| 41 | |||
| 42 | @see | ||
| 43 | @ref serializer. | ||
| 44 | */ | ||
| 45 | class string_body | ||
| 46 | { | ||
| 47 | std::string s_; | ||
| 48 | buffers::const_buffer cb_; | ||
| 49 | |||
| 50 | public: | ||
| 51 | /// The type for each buffer. | ||
| 52 | using value_type = buffers::const_buffer; | ||
| 53 | |||
| 54 | /// The type of a const iterator. | ||
| 55 | using const_iterator = buffers::const_buffer const*; | ||
| 56 | |||
| 57 | ✗ | string_body( | |
| 58 | string_body&& other) noexcept | ||
| 59 | ✗ | : s_(std::move(other.s_)) | |
| 60 | ✗ | , cb_(s_.data(), s_.size()) | |
| 61 | { | ||
| 62 | ✗ | other.cb_ = {}; | |
| 63 | ✗ | } | |
| 64 | |||
| 65 | /** Constructor. | ||
| 66 | */ | ||
| 67 | string_body( | ||
| 68 | string_body const& other) = delete; | ||
| 69 | |||
| 70 | /** Constructor. | ||
| 71 | |||
| 72 | @param s The string to take ownership of. | ||
| 73 | */ | ||
| 74 | ✗ | string_body( | |
| 75 | std::string s) noexcept | ||
| 76 | ✗ | : s_(std::move(s)) | |
| 77 | ✗ | , cb_(s_.data(), s_.size()) | |
| 78 | { | ||
| 79 | ✗ | } | |
| 80 | |||
| 81 | /** Return an iterator to the beginning of the | ||
| 82 | buffer sequence. | ||
| 83 | */ | ||
| 84 | const_iterator | ||
| 85 | ✗ | begin() const noexcept | |
| 86 | { | ||
| 87 | ✗ | return &cb_; | |
| 88 | } | ||
| 89 | |||
| 90 | /** Return an iterator to the end of the | ||
| 91 | buffer sequence. | ||
| 92 | */ | ||
| 93 | const_iterator | ||
| 94 | ✗ | end() const noexcept | |
| 95 | { | ||
| 96 | ✗ | return &cb_ + 1; | |
| 97 | } | ||
| 98 | }; | ||
| 99 | |||
| 100 | } // http_proto | ||
| 101 | } // boost | ||
| 102 | |||
| 103 | #endif | ||
| 104 |