RESTinio
chunked_input_info.hpp
Go to the documentation of this file.
1 /*
2  restinio
3 */
4 
5 /*!
6  * @file
7  * @brief Information about chunked encoded body.
8  * @since v.0.6.9
9  */
10 
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 //
27 /*!
28  * @brief Information about one chunk in an incoming request with
29  * chunked encoding.
30  *
31  * In RESTinio v.0.6.9 all chunks are concatenated into the one body.
32  * The information about individual chunks preserved in the form
33  * of vector of chunk_info_t objects. Every object contains
34  * the offset from the begining of the concatenated body and the size
35  * of the chunk. This information allows to extract the corresponding
36  * fragment from the whole body.
37  *
38  * @since v.0.6.9
39  */
41 {
44 
45 public:
46  //! Initializing constructor.
48  std::size_t started_at,
49  std::size_t size )
51  , m_size{ size }
52  {}
53 
54  //! Get the starting offset of chunk.
56  std::size_t
57  started_at() const noexcept { return m_started_at; }
58 
59  //! Get the size of chunk.
61  std::size_t
62  size() const noexcept { return m_size; }
63 
64  //! Extract the chunk value from the whole body.
65  /*!
66  * @attention
67  * This method doesn't check the possibility of the extraction.
68  * An attempt of extraction of chunk from a body that is too small
69  * is undefined behavior.
70  */
74  {
76  }
77 
78  //! Extract the chunk value from the whole body.
79  /*!
80  * A check of possibility of extraction is performed.
81  *
82  * @throw exception_t if @a full_body is too small to hold the chunk.
83  */
87  {
88  if( m_started_at >= 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:{}",
95  m_size,
96  full_body.size() )
97  };
98  }
99 
101  }
102 };
103 
104 namespace impl
105 {
106 
107 //
108 // chunked_input_info_block_t
109 //
110 /*!
111  * @brief Bunch of data related to chunked input.
112  *
113  * @since v.0.6.9
114  */
116 {
117  //! All non-empty chunks from the input.
119 
120  //! Trailing fields found in the input.
121  /*!
122  * @note
123  * Can be empty.
124  */
126 };
127 
128 } /* namespace impl */
129 
130 //
131 // chunked_input_info_t
132 //
133 /*!
134  * @brief An information about chunks and trailing fields in the
135  * incoming request.
136  *
137  * This information is collected if chunked encoding is used in the
138  * incoming request.
139  *
140  * @since v.0.6.9
141  */
143 {
144  //! Actual data.
146 
147 public:
148  //! Default constructor. Makes empty object.
149  chunked_input_info_t() = default;
150  //! Initializing constructor.
151  /*!
152  * @note
153  * This constrictor is intended to be used inside RESTinio and
154  * can be changed in future versions without any notice.
155  */
158  : m_info{ std::move(info) }
159  {}
160 
161  //! Get the count of chunks.
162  /*!
163  * @retval 0 if there is no chunks in the incoming request.
164  */
166  std::size_t
167  chunk_count() const noexcept { return m_info.m_chunks.size(); }
168 
169  //! Get reference to the description of a chunk by index.
170  /*!
171  * @attention
172  * This method doesn't check the validity of @a index.
173  * An attempt to access non-existent chunk is undefined behavior.
174  */
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 
182  //! Get reference to the description of a chunk by index.
183  /*!
184  * @throw std::exception if @a index is invalid.
185  */
187  const chunk_info_t &
189  {
190  return m_info.m_chunks.at( index );
191  }
192 
193  //! Get access to the container with description of chunks.
194  /*!
195  * @note
196  * The actual type of the container is not specified and can
197  * be changed from version to version. But this container
198  * can be sequentially enumerated from begin() to the end().
199  */
201  const auto &
202  chunks() const noexcept
203  {
204  return m_info.m_chunks;
205  }
206 
207  //! Get access to the container with trailing fields.
208  /*!
209  * @note
210  * This can be an empty container if there is no trailing fields
211  * in the incoming request.
212  */
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 //
224 /*!
225  * @brief Alias of unique_ptr for chunked_input_info.
226  *
227  * @since v.0.6.9
228  */
231 
232 } /* namespace restinio */
chunk_info_t(std::size_t started_at, std::size_t size)
Initializing constructor.
Bunch of data related to chunked input.
impl::chunked_input_info_block_t m_info
Actual data.
std::vector< chunk_info_t > m_chunks
All non-empty chunks from the input.
chunked_input_info_t()=default
Default constructor. Makes empty object.
RESTINIO_NODISCARD string_view_t make_string_view(string_view_t full_body) const
Extract the chunk value from the whole body.
http_header_fields_t m_trailing_fields
Trailing fields found in the input.
RESTINIO_NODISCARD char to_lower_case(unsigned char ch)
chunked_input_info_t(impl::chunked_input_info_block_t info)
Initializing constructor.
RESTINIO_NODISCARD const http_header_fields_t & trailing_fields() const noexcept
Get access to the container with trailing fields.
std::enable_if< std::is_same< Parameter_Container, query_string_params_t >::value||std::is_same< Parameter_Container, router::route_params_t >::value, optional_t< Value_Type > >::type opt_value(const Parameter_Container &params, string_view_t key)
Gets the value of a parameter specified by key wrapped in optional_t<Value_Type> if parameter exists ...
Definition: value_or.hpp:64