RESTinio
uri_helpers.hpp
Go to the documentation of this file.
1 /*
2  restinio
3 */
4 
5 /*!
6  escape functions.
7 */
8 
9 #pragma once
10 
11 #include <string>
12 #include <unordered_map>
13 
14 #include <restinio/impl/include_fmtlib.hpp>
15 
16 #include <restinio/exception.hpp>
17 #include <restinio/utils/percent_encoding.hpp>
18 #include <restinio/optional.hpp>
19 
20 namespace restinio
21 {
22 
23 namespace impl
24 {
25 
26 inline const char *
27 modified_memchr( int chr , const char * from, const char * to )
28 {
29  const char * result = static_cast< const char * >(
30  std::memchr( from, chr, static_cast<std::size_t>(to - from) ) );
31 
32  return result ? result : to;
33 }
34 
35 } /* namespace impl */
36 
37 //
38 // query_string_params_t
39 //
40 
41 //! Parameters container for query strings parameters.
42 class query_string_params_t final
43 {
44  public:
45  using parameters_container_t = std::vector< std::pair< string_view_t, string_view_t > >;
46 
47  //! Constructor for the case when query string empty of
48  //! contains a set of key-value pairs.
50  std::unique_ptr< char[] > data_buffer,
51  parameters_container_t parameters )
54  {}
55 
56  //! Constructor for the case when query string contains only tag
57  //! (web beacon).
59  std::unique_ptr< char[] > data_buffer,
60  optional_t< string_view_t > tag )
62  , m_tag{ tag }
63  {}
64 
65  query_string_params_t( query_string_params_t && ) = default;
66  query_string_params_t & operator = ( query_string_params_t && ) = default;
67 
68  query_string_params_t( const query_string_params_t & ) = delete;
69  query_string_params_t & operator = ( const query_string_params_t & ) = delete;
70 
71  //! Get parameter.
73  operator [] ( string_view_t key ) const
74  {
75  return find_parameter_with_check( key ).second;
76  }
77 
78  //! Check parameter.
79  bool
80  has( string_view_t key ) const noexcept
81  {
82  return m_parameters.end() != find_parameter( key );
83  }
84 
85  //! Get the value of a parameter if it exists.
86  //! @since v.0.4.4
88  get_param( string_view_t key ) const noexcept
89  {
90  const auto it = find_parameter( key );
91 
92  return m_parameters.end() != it ?
93  optional_t< string_view_t >{ it->second } :
94  optional_t< string_view_t >{ nullopt };
95  }
96 
97  //! Get the size of parameters.
98  auto size() const noexcept { return m_parameters.size(); }
99 
100  //! Is there any parameters?
101  //! @since v.0.4.8
102  bool empty() const noexcept { return m_parameters.empty(); }
103 
104  //! @name Iterate parameters.
105  //! @{
107  begin() const noexcept
108  {
109  return m_parameters.begin();
110  }
111 
113  end() const noexcept
114  {
115  return m_parameters.end();
116  }
117  //! @}
118 
119  //! Get the tag (web beacon) part.
120  /*!
121  A value of "tag" (also known as web beacon) is available only
122  if URI looks like that:
123  \verbatim
124  http://example.com/resource?value
125  \endverbatim
126  In that case tag will contain `value`. For URI with different
127  formats tag() will return empty optional.
128 
129  @since v.0.4.9
130  */
131  auto tag() const noexcept { return m_tag; }
132 
133  private:
135  find_parameter( string_view_t key ) const noexcept
136  {
137  return
138  std::find_if(
139  m_parameters.begin(),
140  m_parameters.end(),
141  [&]( const auto p ){
142  return key == p.first;
143  } );
144  }
145 
148  {
149  auto it = find_parameter( key );
150 
151  if( m_parameters.end() == it )
152  {
153  throw exception_t{
154  fmt::format(
155  "unable to find parameter \"{}\"",
156  std::string{ key.data(), key.size() } ) };
157  }
158 
159  return *it;
160  }
161 
162  //! Shared buffer for string_view of named parameterts names.
165 
166  //! Tag (or web beacon) part.
167  /*! @since v.0.4.9 */
169 };
170 
171 //! Cast query string parameter to a given type.
172 template < typename Value_Type >
173 Value_Type
174 get( const query_string_params_t & params, string_view_t key )
175 {
176  return get< Value_Type >( params[ key ] );
177 }
178 
180 {
181 
182 namespace details
183 {
184 
185 /*!
186  * @brief Helper class to be reused in implementation of query-string
187  * parsing traits.
188  *
189  * Implements `find_next_separator` method that recongnizes `&` and
190  * `;` as `name=value` separators.
191  *
192  * @since v.0.6.5
193  */
195 {
196  static string_view_t::size_type
200  {
201  return where.find_first_of( "&;", start_from );
202  }
203 };
204 
205 /*!
206  * @brief Helper class to be reused in implementation of query-string
207  * parsing traits.
208  *
209  * Implements `find_next_separator` method that recongnizes only `&`
210  * `name=value` separator.
211  *
212  * @since v.0.6.5
213  */
215 {
216  static string_view_t::size_type
220  {
221  return where.find_first_of( '&', start_from );
222  }
223 };
224 
225 } /* namespace details */
226 
227 /*!
228  * @brief Traits for the default RESTinio parser for query string.
229  *
230  * The default RESTinio parser prohibit usage of unexcaped asterisk.
231  *
232  * @note
233  * This traits type is used by default. It means that a call:
234  * @code
235  * auto result = restinio::parse_query<restinio::parse_query_traits::restinio_defaults>("name=value");
236  * @endcode
237  * is equivalent to:
238  * @code
239  * auto result = restinio::parse_query("name=value");
240  * @endcode
241  *
242  * @since v.0.4.9.1
243  */
247 {};
248 
249 /*!
250  * @brief Traits for parsing a query string in JavaScript-compatible mode.
251  *
252  * In that mode several non-percent-encoded characters are allowed:
253  * `-`, `.`, `~`, `_`, `*`, `!`, `'`, `(`, `)`
254  *
255  * Usage example:
256  * @code
257  * auto result = restinio::parse_query<restinio::parse_query_traits::javascript_compatible>("name=A*");
258  * @endcode
259  *
260  * @since v.0.4.9.1
261  */
265 {};
266 
267 /*!
268  * @brief Traits for parsing a query string in
269  * application/x-www-form-urlencoded mode.
270  *
271  * In that mode:
272  *
273  * - `name=value` pairs can be concatenated only by `&`;
274  * - the following characters can only be used unescaped: `*` (0x2A), `-`
275  * (0x2D), `.` (0x2E), `_` (0x5F), `0`..`9` (0x30..0x39), `A`..`Z`
276  * (0x41..0x5A), `a`..`z` (0x61..0x7A);
277  * - space character (0x20) should be replaced by + (0x2B);
278  * - *all other characters should be represented as percent-encoded*.
279  *
280  * Reference for more details: https://url.spec.whatwg.org/#concept-urlencoded-byte-serializer
281  *
282  * Usage example:
283  * @code
284  * auto result = restinio::parse_query<restinio::parse_query_traits::x_www_form_urlencoded>("name=A*");
285  * @endcode
286  *
287  * @since v.0.6.5
288  */
292 {};
293 
294 /*!
295  * @brief Traits for parsing a query string in a very relaxed mode.
296  *
297  * In that mode all characters described in that rule from
298  * [RCF3986](https://tools.ietf.org/html/rfc3986) can be used as unexceped:
299 @verbatim
300 query = *( pchar / "/" / "?" )
301 pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
302 unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
303 reserved = gen-delims / sub-delims
304 gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"
305 sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
306  / "*" / "+" / "," / ";" / "="
307 @endverbatim
308  *
309  * Additionaly this traits allows to use unexcaped space character.
310  *
311  * Note that despite that fact that symbols like `#`, `+`, `=` and `&` can be
312  * used in non-percent-encoded form they play special role and are interpreted
313  * special way. So such symbols should be percent-encoded if they are used as
314  * part of name or value in query string.
315  *
316  * Only ampersand (`&`) can be used as `name=value` pairs separator.
317  *
318  * Usage example:
319  * @code
320  * auto result = restinio::parse_query<restinio::parse_query_traits::relaxed>("a=(&b=)&c=[&d=]&e=!&f=,&g=;");
321  * @endcode
322  *
323  * @since v.0.6.5
324  */
325 struct relaxed
328 {};
329 
330 } /* namespace parse_query_traits */
331 
332 /*!
333  * @brief Type that indicates a failure of an attempt of query-string parsing.
334  *
335  * @since v.0.6.5
336  */
338 {
339  //! Description of a failure.
341 
342 public:
343  parse_query_failure_t( std::string description )
345  {}
347  utils::unescape_percent_encoding_failure_t && failure )
349  {}
350 
351  //! Get a reference to the description of the failure.
353  const std::string &
354  description() const noexcept { return m_description; }
355 
356  //! Get out the value of the description of the failure.
357  /*!
358  * This method is intended for cases when this description should be move
359  * elsewhere (to another object like parse_query_failure_t or to some
360  * exception-like object).
361  */
363  std::string
364  giveout_description() noexcept { return m_description; }
365 };
366 
367 /*!
368  * @brief Helper function for parsing query string.
369  *
370  * Unlike parse_query() function the try_parse_query() doesn't throw if
371  * some unsupported character sequence is found.
372  *
373  * @note
374  * Parsing traits should be specified explicitly.
375  *
376  * Usage example:
377  * @code
378  * auto result = restinio::try_parse_query<
379  * restinio::parse_query_traits::javascript_compatible>("name=A*&flags=!");
380  * if(!result) {
381  * std::cerr << "Unable to parse query-string: " << result.error().description() << std::endl;
382  * }
383  * else {
384  * const restinio::query_string_params_t & params = *result;
385  * ...
386  * }
387  * @endcode
388  *
389  * @attention
390  * This function is not noexcept and can throw on other types of
391  * failures (like unability to allocate a memory).
392  *
393  * @since v.0.6.5
394  */
395 template< typename Parse_Traits >
396 RESTINIO_NODISCARD
399  //! Query part of the request target.
401 {
402  std::unique_ptr< char[] > data_buffer;
404 
406  {
407  // Because query string is not empty a new buffer should be
408  // allocated and query string should be copied to it.
409  data_buffer.reset( new char[ original_query_string.size() ] );
410  std::memcpy(
411  data_buffer.get(),
414 
415  // Work with created buffer:
417  data_buffer.get(),
419  };
422 
423  while( pos < end_pos )
424  {
425  const auto eq_pos = work_query_string.find_first_of( '=', pos );
426 
427  if( string_view_t::npos == eq_pos )
428  {
429  // Since v.0.4.9 we should check the presence of tag (web beacon)
430  // in query string.
431  // Tag can be the only item in query string.
432  if( pos != 0u )
433  // The query string has illegal format.
435  fmt::format(
436  "invalid format of key-value pairs in query_string, "
437  "no '=' symbol starting from position {}",
438  pos )
439  } );
440  else
441  {
442  // Query string contains only tag (web beacon).
443  auto tag_unescape_result =
445  &data_buffer[ pos ],
446  end_pos - pos );
447  if( !tag_unescape_result )
450  } );
451 
454 
456  }
457  }
458 
459  const auto eq_pos_next = eq_pos + 1u;
464 
465  // Handle next pair of parameters found.
466  auto key_unescape_result =
468  &data_buffer[ pos ],
469  eq_pos - pos );
470  if( !key_unescape_result )
473  } );
474 
475  auto value_unescape_result =
479  if( !value_unescape_result )
482  } );
483 
487 
488  pos = separator_pos + 1u;
489  }
490  }
491 
492  return query_string_params_t{
493  std::move( data_buffer ),
494  std::move( parameters )
495  };
496 }
497 
498 //! Parse query key-value parts.
499 /*!
500  Since v.0.4.9 this function correctly handles the following cases:
501 
502  - presence of tag (web beacon) in URI. For example, when URI looks like
503  `http://example.com/resource?tag`. In that case value of the tag (web
504  beacon) can be obtained via query_string_params_t::tag() method.
505  References: [web beacon](https://en.wikipedia.org/wiki/Web_beacon) and
506  [query-string-tracking](https://en.wikipedia.org/wiki/Query_string#Tracking);
507  - usage of `;` instead of `&` as parameter separator.
508 
509  Since v.0.4.9.1 this function can be parametrized by parser traits. For
510  example:
511  @code
512  auto result = restinio::parse_query<restinio::parse_query_traits::javascript_compatible>("name=A*");
513  @endcode
514 */
515 template< typename Parse_Traits = parse_query_traits::restinio_defaults >
516 RESTINIO_NODISCARD
517 query_string_params_t
519  //! Query part of the request target.
521 {
522  auto r = try_parse_query< Parse_Traits >( original_query_string );
523  if( !r )
524  throw exception_t{ std::move(r.error().giveout_description()) };
525 
526  return std::move(*r);
527 }
528 
529 } /* namespace restinio */
static string_view_t::size_type find_next_separator(string_view_t where, string_view_t::size_type start_from) noexcept
auto tag() const noexcept
Get the tag (web beacon) part.
Traits for parsing a query string in a very relaxed mode.
optional_t< string_view_t > get_param(string_view_t key) const noexcept
Get the value of a parameter if it exists.
Definition: uri_helpers.hpp:88
RESTINIO_NODISCARD query_string_params_t parse_query(string_view_t original_query_string)
Parse query key-value parts.
parameters_container_t::const_iterator find_parameter(string_view_t key) const noexcept
Helper class to be reused in implementation of query-string parsing traits.
parameters_container_t::const_reference find_parameter_with_check(string_view_t key) const
query_string_params_t & operator=(const query_string_params_t &)=delete
RESTINIO_NODISCARD std::string giveout_description() noexcept
Get out the value of the description of the failure.
std::unique_ptr< char[] > m_data_buffer
Shared buffer for string_view of named parameterts names.
parse_query_failure_t(std::string description)
std::string m_description
Description of a failure.
static string_view_t::size_type find_next_separator(string_view_t where, string_view_t::size_type start_from) noexcept
parameters_container_t m_parameters
query_string_params_t(const query_string_params_t &)=delete
parameters_container_t::const_iterator begin() const noexcept
Traits for parsing a query string in application/x-www-form-urlencoded mode.
auto size() const noexcept
Get the size of parameters.
Definition: uri_helpers.hpp:98
bool empty() const noexcept
Is there any parameters?
bool has(string_view_t key) const noexcept
Check parameter.
Definition: uri_helpers.hpp:80
query_string_params_t(std::unique_ptr< char[] > data_buffer, optional_t< string_view_t > tag)
Constructor for the case when query string contains only tag (web beacon).
Definition: uri_helpers.hpp:58
const char * modified_memchr(int chr, const char *from, const char *to)
Definition: uri_helpers.hpp:27
Traits for the default RESTinio parser for query string.
Value_Type get(const query_string_params_t &params, string_view_t key)
Cast query string parameter to a given type.
query_string_params_t(query_string_params_t &&)=default
string_view_t operator[](string_view_t key) const
Get parameter.
Definition: uri_helpers.hpp:73
RESTINIO_NODISCARD char to_lower_case(unsigned char ch)
parse_query_failure_t(utils::unescape_percent_encoding_failure_t &&failure)
Traits for parsing a query string in JavaScript-compatible mode.
query_string_params_t & operator=(query_string_params_t &&)=default
parameters_container_t::const_iterator end() const noexcept
Helper class to be reused in implementation of query-string parsing traits.
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
optional_t< string_view_t > m_tag
Tag (or web beacon) part.