RESTinio

chunked_input_info.hpp
Go to the documentation of this file.
1/*
2 restinio
3*/
4
11#pragma once
12
13#include <restinio/exception.hpp>
14#include <restinio/http_headers.hpp>
15
16#include <restinio/impl/include_fmtlib.hpp>
17
18#include <vector>
19#include <memory>
20
21 namespace restinio
22{
23
24//
25// chunk_info_t
26//
40 class chunk_info_t
41{
42 std::size_t m_started_at;
43 std::size_t m_size;
44
45 public:
47 chunk_info_t(
48 std::size_t started_at,
49 std::size_t size )
50 : m_started_at{ started_at }
51 , m_size{ size }
52 {}
53
55 RESTINIO_NODISCARD
56 std::size_t
57 started_at() const noexcept { return m_started_at; }
58
60 RESTINIO_NODISCARD
61 std::size_t
62 size() const noexcept { return m_size; }
63
65
71 RESTINIO_NODISCARD
72 string_view_t
73 make_string_view_nonchecked( string_view_t full_body ) const noexcept
74 {
75 return full_body.substr( m_started_at, m_size );
76 }
77
79
84 RESTINIO_NODISCARD
85 string_view_t
86 make_string_view( string_view_t full_body ) const
87 {
88 if( m_started_at >= full_body.size() ||
89 m_started_at + m_size > full_body.size() )
90 {
91 throw exception_t{
92 fmt::format( "unable to make a chunk (started_at:{}, size: {}) "
93 "from a body with length:{}",
94 m_started_at,
95 m_size,
96 full_body.size() )
97 };
98 }
99
100 return make_string_view_nonchecked( full_body );
101 }
102};
103
104 namespace impl
105{
106
107//
108// chunked_input_info_block_t
109//
115 struct chunked_input_info_block_t
116{
118 std::vector< chunk_info_t > m_chunks;
119
121
125 http_header_fields_t m_trailing_fields;
126};
127
128} /* namespace impl */
129
130//
131// chunked_input_info_t
132//
142 class chunked_input_info_t
143{
145 impl::chunked_input_info_block_t m_info;
146
147 public:
149 chunked_input_info_t() = default;
151
156 chunked_input_info_t(
157 impl::chunked_input_info_block_t info )
158 : m_info{ std::move(info) }
159 {}
160
162
165 RESTINIO_NODISCARD
166 std::size_t
167 chunk_count() const noexcept { return m_info.m_chunks.size(); }
168
170
175 RESTINIO_NODISCARD
176 const chunk_info_t &
177 chunk_at_nochecked( std::size_t index ) const noexcept
178 {
179 return m_info.m_chunks[ index ];
180 }
181
183
186 RESTINIO_NODISCARD
187 const chunk_info_t &
188 chunk_at( std::size_t index ) const
189 {
190 return m_info.m_chunks.at( index );
191 }
192
194
200 RESTINIO_NODISCARD
201 const auto &
202 chunks() const noexcept
203 {
204 return m_info.m_chunks;
205 }
206
208
213 RESTINIO_NODISCARD
214 const http_header_fields_t &
215 trailing_fields() const noexcept
216 {
217 return m_info.m_trailing_fields;
218 }
219};
220
221//
222// chunked_input_info_unique_ptr_t
223//
229 using chunked_input_info_unique_ptr_t =
230 std::unique_ptr< chunked_input_info_t >;
231
232} /* namespace restinio */
233
restinio::chunk_info_t
Information about one chunk in an incoming request with chunked encoding.
Definition: chunked_input_info.hpp:41
restinio::chunk_info_t::make_string_view
RESTINIO_NODISCARD string_view_t make_string_view(string_view_t full_body) const
Extract the chunk value from the whole body.
Definition: chunked_input_info.hpp:86
restinio::chunk_info_t::chunk_info_t
chunk_info_t(std::size_t started_at, std::size_t size)
Initializing constructor.
Definition: chunked_input_info.hpp:47
restinio::chunk_info_t::m_size
std::size_t m_size
Definition: chunked_input_info.hpp:43
restinio::chunk_info_t::started_at
RESTINIO_NODISCARD std::size_t started_at() const noexcept
Get the starting offset of chunk.
Definition: chunked_input_info.hpp:57
restinio::chunk_info_t::size
RESTINIO_NODISCARD std::size_t size() const noexcept
Get the size of chunk.
Definition: chunked_input_info.hpp:62
restinio::chunk_info_t::make_string_view_nonchecked
RESTINIO_NODISCARD string_view_t make_string_view_nonchecked(string_view_t full_body) const noexcept
Extract the chunk value from the whole body.
Definition: chunked_input_info.hpp:73
restinio::chunk_info_t::m_started_at
std::size_t m_started_at
Definition: chunked_input_info.hpp:42
restinio::chunked_input_info_t
An information about chunks and trailing fields in the incoming request.
Definition: chunked_input_info.hpp:143
restinio::chunked_input_info_t::chunk_at
RESTINIO_NODISCARD const chunk_info_t & chunk_at(std::size_t index) const
Get reference to the description of a chunk by index.
Definition: chunked_input_info.hpp:188
restinio::chunked_input_info_t::chunk_count
RESTINIO_NODISCARD std::size_t chunk_count() const noexcept
Get the count of chunks.
Definition: chunked_input_info.hpp:167
restinio::chunked_input_info_t::chunk_at_nochecked
RESTINIO_NODISCARD const chunk_info_t & chunk_at_nochecked(std::size_t index) const noexcept
Get reference to the description of a chunk by index.
Definition: chunked_input_info.hpp:177
restinio::chunked_input_info_t::m_info
impl::chunked_input_info_block_t m_info
Actual data.
Definition: chunked_input_info.hpp:145
restinio::chunked_input_info_t::chunks
RESTINIO_NODISCARD const auto & chunks() const noexcept
Get access to the container with description of chunks.
Definition: chunked_input_info.hpp:202
restinio::chunked_input_info_t::chunked_input_info_t
chunked_input_info_t(impl::chunked_input_info_block_t info)
Initializing constructor.
Definition: chunked_input_info.hpp:156
restinio::chunked_input_info_t::chunked_input_info_t
chunked_input_info_t()=default
Default constructor. Makes empty object.
restinio::chunked_input_info_t::trailing_fields
RESTINIO_NODISCARD const http_header_fields_t & trailing_fields() const noexcept
Get access to the container with trailing fields.
Definition: chunked_input_info.hpp:215
restinio::exception_t
Exception class for all exceptions thrown by RESTinio.
Definition: exception.hpp:26
restinio::http_header_fields_t
Header fields map.
Definition: http_headers.hpp:703
RESTINIO_NODISCARD
#define RESTINIO_NODISCARD
Definition: compiler_features.hpp:33
exception.hpp
http_headers.hpp
include_fmtlib.hpp
A special wrapper around fmtlib include files.
nonstd::optional_lite::std11::move
T & move(T &t)
Definition: optional.hpp:465
restinio
Definition: asio_include.hpp:21
restinio::chunked_input_info_unique_ptr_t
std::unique_ptr< chunked_input_info_t > chunked_input_info_unique_ptr_t
Alias of unique_ptr for chunked_input_info.
Definition: chunked_input_info.hpp:230
restinio::string_view_t
nonstd::string_view string_view_t
Definition: string_view.hpp:19
std
STL namespace.
restinio::impl::chunked_input_info_block_t
Bunch of data related to chunked input.
Definition: chunked_input_info.hpp:116
restinio::impl::chunked_input_info_block_t::m_chunks
std::vector< chunk_info_t > m_chunks
All non-empty chunks from the input.
Definition: chunked_input_info.hpp:118
restinio::impl::chunked_input_info_block_t::m_trailing_fields
http_header_fields_t m_trailing_fields
Trailing fields found in the input.
Definition: chunked_input_info.hpp:125
const
#define const
Definition: zconf.h:230