12#include <unordered_map>
29 const char * result =
static_cast< const char *
>(
30 std::memchr( from, chr,
static_cast<std::size_t
>(to - from) ) );
32 return result ? result : to;
50 std::unique_ptr<
char[] > data_buffer,
59 std::unique_ptr<
char[] > data_buffer,
106 parameters_container_t::const_iterator
112 parameters_container_t::const_iterator
134 parameters_container_t::const_iterator
142 return key == p.first;
146 parameters_container_t::const_reference
155 "unable to find parameter \"{}\"",
156 std::string{ key.data(), key.size() } ) };
172template <
typename Value_Type >
176 return get< Value_Type >( params[ key ] );
179namespace parse_query_traits
196 static string_view_t::size_type
199 string_view_t::size_type start_from )
noexcept
201 return where.find_first_of(
"&;", start_from );
216 static string_view_t::size_type
219 string_view_t::size_type start_from )
noexcept
221 return where.find_first_of(
'&', start_from );
395template<
typename Parse_Traits >
397expected_t< query_string_params_t, parse_query_failure_t >
402 std::unique_ptr< char[] > data_buffer;
405 if( !original_query_string.empty() )
409 data_buffer.reset(
new char[ original_query_string.size() ] );
412 original_query_string.data(),
413 original_query_string.size() );
418 original_query_string.size()
420 string_view_t::size_type pos{ 0 };
421 const string_view_t::size_type end_pos = work_query_string.size();
423 while( pos < end_pos )
425 const auto eq_pos = work_query_string.find_first_of(
'=', pos );
427 if( string_view_t::npos == eq_pos )
436 "invalid format of key-value pairs in query_string, "
437 "no '=' symbol starting from position {}",
443 auto tag_unescape_result =
444 utils::try_inplace_unescape_percent_encoding< Parse_Traits >(
447 if( !tag_unescape_result )
453 pos, *tag_unescape_result );
459 const auto eq_pos_next = eq_pos + 1u;
460 auto separator_pos = Parse_Traits::find_next_separator(
461 work_query_string, eq_pos_next );
462 if( string_view_t::npos == separator_pos )
463 separator_pos = work_query_string.size();
466 auto key_unescape_result =
467 utils::try_inplace_unescape_percent_encoding< Parse_Traits >(
470 if( !key_unescape_result )
475 auto value_unescape_result =
476 utils::try_inplace_unescape_percent_encoding< Parse_Traits >(
477 &data_buffer[ eq_pos_next ],
478 separator_pos - eq_pos_next );
479 if( !value_unescape_result )
484 parameters.emplace_back(
486 string_view_t{ &data_buffer[ eq_pos_next ], *value_unescape_result } );
488 pos = separator_pos + 1u;
515template<
typename Parse_Traits = parse_query_traits::restinio_defaults >
522 auto r = try_parse_query< Parse_Traits >( original_query_string );
Exception class for all exceptions thrown by RESTinio.
Type that indicates a failure of an attempt of query-string parsing.
RESTINIO_NODISCARD const std::string & description() const noexcept
Get a reference to the description of the failure.
RESTINIO_NODISCARD std::string giveout_description() noexcept
Get out the value of the description of the failure.
std::string m_description
Description of a failure.
parse_query_failure_t(std::string description)
parse_query_failure_t(utils::unescape_percent_encoding_failure_t &&failure)
Parameters container for query strings parameters.
std::unique_ptr< char[] > m_data_buffer
Shared buffer for string_view of named parameterts names.
query_string_params_t(query_string_params_t &&)=default
auto tag() const noexcept
Get the tag (web beacon) part.
bool empty() const noexcept
Is there any parameters?
auto size() const noexcept
Get the size of parameters.
parameters_container_t::const_iterator begin() const noexcept
query_string_params_t & operator=(query_string_params_t &&)=default
string_view_t operator[](string_view_t key) const
Get parameter.
parameters_container_t m_parameters
std::vector< std::pair< string_view_t, string_view_t > > parameters_container_t
query_string_params_t(std::unique_ptr< char[] > data_buffer, parameters_container_t parameters)
Constructor for the case when query string empty of contains a set of key-value pairs.
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).
optional_t< string_view_t > get_param(string_view_t key) const noexcept
Get the value of a parameter if it exists.
parameters_container_t::const_iterator find_parameter(string_view_t key) const noexcept
optional_t< string_view_t > m_tag
Tag (or web beacon) part.
parameters_container_t::const_reference find_parameter_with_check(string_view_t key) const
parameters_container_t::const_iterator end() const noexcept
bool has(string_view_t key) const noexcept
Check parameter.
query_string_params_t(const query_string_params_t &)=delete
Type that indicates a failure of unescaping of percent-encoded symbols.
#define RESTINIO_NODISCARD
A special wrapper around fmtlib include files.
const nullopt_t nullopt((nullopt_t::init()))
const char * modified_memchr(int chr, const char *from, const char *to)
nonstd::string_view string_view_t
RESTINIO_NODISCARD query_string_params_t parse_query(string_view_t original_query_string)
Parse query key-value parts.
RESTINIO_NODISCARD expected_t< query_string_params_t, parse_query_failure_t > try_parse_query(string_view_t original_query_string)
Helper function for parsing query string.
Value_Type get(const router::route_params_t ¶ms, string_view_t key)
Cast named parameter value to a given type.
Helper class to be reused in implementation of query-string parsing traits.
static string_view_t::size_type find_next_separator(string_view_t where, string_view_t::size_type start_from) noexcept
Helper class to be reused in implementation of query-string parsing traits.
static string_view_t::size_type find_next_separator(string_view_t where, string_view_t::size_type start_from) noexcept
Traits for parsing a query string in JavaScript-compatible mode.
Traits for parsing a query string in a very relaxed mode.
Traits for the default RESTinio parser for query string.
The traits for escaping and unexcaping symbols in JavaScript-compatible mode.
Traits for escaping and unescaping symbols in a query string in very relaxed mode.
The default traits for escaping and unexcaping symbols in a query string.