Loading...
Searching...
No Matches
uri_helpers.hpp
Go to the documentation of this file.
/*
restinio
*/
#pragma once
#include <string>
#include <unordered_map>
#include <restinio/impl/include_fmtlib.hpp>
#include <restinio/exception.hpp>
#include <restinio/utils/percent_encoding.hpp>
#include <restinio/optional.hpp>
namespace restinio
{
namespace impl
{
inline const char *
modified_memchr( int chr , const char * from, const char * to )
{
const char * result = static_cast< const char * >(
std::memchr( from, chr, static_cast<std::size_t>(to - from) ) );
return result ? result : to;
}
} /* namespace impl */
//
// query_string_params_t
//
class query_string_params_t final
{
public:
using parameters_container_t = std::vector< std::pair< string_view_t, string_view_t > >;
query_string_params_t(
std::unique_ptr< char[] > data_buffer,
parameters_container_t parameters )
: m_data_buffer{ std::move( data_buffer ) }
, m_parameters{ std::move( parameters ) }
{}
query_string_params_t(
std::unique_ptr< char[] > data_buffer,
optional_t< string_view_t > tag )
: m_data_buffer{ std::move( data_buffer ) }
, m_tag{ tag }
{}
query_string_params_t( query_string_params_t && ) = default;
query_string_params_t & operator = ( query_string_params_t && ) = default;
query_string_params_t( const query_string_params_t & ) = delete;
query_string_params_t & operator = ( const query_string_params_t & ) = delete;
string_view_t
operator [] ( string_view_t key ) const
{
return find_parameter_with_check( key ).second;
}
bool
has( string_view_t key ) const noexcept
{
return m_parameters.end() != find_parameter( key );
}
optional_t< string_view_t >
get_param( string_view_t key ) const noexcept
{
const auto it = find_parameter( key );
return m_parameters.end() != it ?
optional_t< string_view_t >{ it->second } :
optional_t< string_view_t >{ nullopt };
}
auto size() const noexcept { return m_parameters.size(); }
bool empty() const noexcept { return m_parameters.empty(); }
parameters_container_t::const_iterator
begin() const noexcept
{
return m_parameters.begin();
}
parameters_container_t::const_iterator
end() const noexcept
{
return m_parameters.end();
}
auto tag() const noexcept { return m_tag; }
private:
parameters_container_t::const_iterator
find_parameter( string_view_t key ) const noexcept
{
return
std::find_if(
m_parameters.begin(),
m_parameters.end(),
[&]( const auto p ){
return key == p.first;
} );
}
parameters_container_t::const_reference
find_parameter_with_check( string_view_t key ) const
{
auto it = find_parameter( key );
if( m_parameters.end() == it )
{
throw exception_t{
fmt::format(
RESTINIO_FMT_FORMAT_STRING(
"unable to find parameter \"{}\"" ),
std::string{ key.data(), key.size() } ) };
}
return *it;
}
std::unique_ptr< char[] > m_data_buffer;
parameters_container_t m_parameters;
optional_t< string_view_t > m_tag;
};
template < typename Value_Type >
Value_Type
get( const query_string_params_t & params, string_view_t key )
{
return get< Value_Type >( params[ key ] );
}
namespace parse_query_traits
{
namespace details
{
struct ampersand_and_semicolon_as_separators
{
static string_view_t::size_type
find_next_separator(
string_view_t where,
string_view_t::size_type start_from ) noexcept
{
return where.find_first_of( "&;", start_from );
}
};
struct ampersand_only_as_separators
{
static string_view_t::size_type
find_next_separator(
string_view_t where,
string_view_t::size_type start_from ) noexcept
{
return where.find_first_of( '&', start_from );
}
};
} /* namespace details */
struct restinio_defaults
: public restinio::utils::restinio_default_unescape_traits
, public details::ampersand_and_semicolon_as_separators
{};
struct javascript_compatible
: public restinio::utils::javascript_compatible_unescape_traits
, public details::ampersand_and_semicolon_as_separators
{};
struct x_www_form_urlencoded
: public restinio::utils::x_www_form_urlencoded_unescape_traits
, public details::ampersand_only_as_separators
{};
struct relaxed
: public restinio::utils::relaxed_unescape_traits
, public details::ampersand_only_as_separators
{};
} /* namespace parse_query_traits */
class parse_query_failure_t
{
std::string m_description;
public:
parse_query_failure_t( std::string description )
: m_description{ std::move(description) }
{}
parse_query_failure_t(
utils::unescape_percent_encoding_failure_t && failure )
: m_description{ failure.giveout_description() }
{}
RESTINIO_NODISCARD
const std::string &
description() const noexcept { return m_description; }
RESTINIO_NODISCARD
std::string
giveout_description() noexcept { return m_description; }
};
template< typename Parse_Traits >
RESTINIO_NODISCARD
expected_t< query_string_params_t, parse_query_failure_t >
try_parse_query(
string_view_t original_query_string )
{
std::unique_ptr< char[] > data_buffer;
query_string_params_t::parameters_container_t parameters;
if( !original_query_string.empty() )
{
// Because query string is not empty a new buffer should be
// allocated and query string should be copied to it.
data_buffer.reset( new char[ original_query_string.size() ] );
std::memcpy(
data_buffer.get(),
original_query_string.data(),
original_query_string.size() );
// Work with created buffer:
string_view_t work_query_string{
data_buffer.get(),
original_query_string.size()
};
string_view_t::size_type pos{ 0 };
const string_view_t::size_type end_pos = work_query_string.size();
while( pos < end_pos )
{
const auto eq_pos = work_query_string.find_first_of( '=', pos );
if( string_view_t::npos == eq_pos )
{
// Since v.0.4.9 we should check the presence of tag (web beacon)
// in query string.
// Tag can be the only item in query string.
if( pos != 0u )
// The query string has illegal format.
return make_unexpected( parse_query_failure_t{
fmt::format(
RESTINIO_FMT_FORMAT_STRING(
"invalid format of key-value pairs in query_string, "
"no '=' symbol starting from position {}" ),
pos )
} );
else
{
// Query string contains only tag (web beacon).
auto tag_unescape_result =
utils::try_inplace_unescape_percent_encoding< Parse_Traits >(
&data_buffer[ pos ],
end_pos - pos );
if( !tag_unescape_result )
return make_unexpected( parse_query_failure_t{
std::move(tag_unescape_result.error())
} );
const string_view_t tag = work_query_string.substr(
pos, *tag_unescape_result );
return query_string_params_t{ std::move( data_buffer ), tag };
}
}
const auto eq_pos_next = eq_pos + 1u;
auto separator_pos = Parse_Traits::find_next_separator(
work_query_string, eq_pos_next );
if( string_view_t::npos == separator_pos )
separator_pos = work_query_string.size();
// Handle next pair of parameters found.
auto key_unescape_result =
utils::try_inplace_unescape_percent_encoding< Parse_Traits >(
&data_buffer[ pos ],
eq_pos - pos );
if( !key_unescape_result )
return make_unexpected( parse_query_failure_t{
std::move(key_unescape_result.error())
} );
auto value_unescape_result =
utils::try_inplace_unescape_percent_encoding< Parse_Traits >(
&data_buffer[ eq_pos_next ],
separator_pos - eq_pos_next );
if( !value_unescape_result )
return make_unexpected( parse_query_failure_t{
std::move(value_unescape_result.error())
} );
parameters.emplace_back(
string_view_t{ &data_buffer[ pos ], *key_unescape_result },
string_view_t{ &data_buffer[ eq_pos_next ], *value_unescape_result } );
pos = separator_pos + 1u;
}
}
return query_string_params_t{
std::move( data_buffer ),
std::move( parameters )
};
}
template< typename Parse_Traits = parse_query_traits::restinio_defaults >
RESTINIO_NODISCARD
query_string_params_t
parse_query(
string_view_t original_query_string )
{
auto r = try_parse_query< Parse_Traits >( original_query_string );
if( !r )
throw exception_t{ std::move(r.error().giveout_description()) };
return std::move(*r);
}
} /* namespace restinio */
nonstd::optional_lite::optional
class optional
Definition: optional.hpp:932
restinio::exception_t
Exception class for all exceptions thrown by RESTinio.
Definition: exception.hpp:26
restinio::parse_query_failure_t
Type that indicates a failure of an attempt of query-string parsing.
Definition: uri_helpers.hpp:339
restinio::parse_query_failure_t::description
RESTINIO_NODISCARD const std::string & description() const noexcept
Get a reference to the description of the failure.
Definition: uri_helpers.hpp:355
restinio::parse_query_failure_t::giveout_description
RESTINIO_NODISCARD std::string giveout_description() noexcept
Get out the value of the description of the failure.
Definition: uri_helpers.hpp:365
restinio::parse_query_failure_t::m_description
std::string m_description
Description of a failure.
Definition: uri_helpers.hpp:341
restinio::parse_query_failure_t::parse_query_failure_t
parse_query_failure_t(std::string description)
Definition: uri_helpers.hpp:344
restinio::parse_query_failure_t::parse_query_failure_t
parse_query_failure_t(utils::unescape_percent_encoding_failure_t &&failure)
Definition: uri_helpers.hpp:347
restinio::query_string_params_t
Parameters container for query strings parameters.
Definition: uri_helpers.hpp:43
restinio::query_string_params_t::m_data_buffer
std::unique_ptr< char[] > m_data_buffer
Shared buffer for string_view of named parameterts names.
Definition: uri_helpers.hpp:164
restinio::query_string_params_t::query_string_params_t
query_string_params_t(query_string_params_t &&)=default
restinio::query_string_params_t::tag
auto tag() const noexcept
Get the tag (web beacon) part.
Definition: uri_helpers.hpp:131
restinio::query_string_params_t::empty
bool empty() const noexcept
Is there any parameters?
Definition: uri_helpers.hpp:102
restinio::query_string_params_t::size
auto size() const noexcept
Get the size of parameters.
Definition: uri_helpers.hpp:98
restinio::query_string_params_t::begin
parameters_container_t::const_iterator begin() const noexcept
Definition: uri_helpers.hpp:107
restinio::query_string_params_t::operator=
query_string_params_t & operator=(query_string_params_t &&)=default
restinio::query_string_params_t::operator[]
string_view_t operator[](string_view_t key) const
Get parameter.
Definition: uri_helpers.hpp:73
restinio::query_string_params_t::m_parameters
parameters_container_t m_parameters
Definition: uri_helpers.hpp:165
restinio::query_string_params_t::parameters_container_t
std::vector< std::pair< string_view_t, string_view_t > > parameters_container_t
Definition: uri_helpers.hpp:45
restinio::query_string_params_t::query_string_params_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.
Definition: uri_helpers.hpp:49
restinio::query_string_params_t::query_string_params_t
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
restinio::query_string_params_t::get_param
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::query_string_params_t::find_parameter
parameters_container_t::const_iterator find_parameter(string_view_t key) const noexcept
Definition: uri_helpers.hpp:135
restinio::query_string_params_t::m_tag
optional_t< string_view_t > m_tag
Tag (or web beacon) part.
Definition: uri_helpers.hpp:169
restinio::query_string_params_t::find_parameter_with_check
parameters_container_t::const_reference find_parameter_with_check(string_view_t key) const
Definition: uri_helpers.hpp:147
restinio::query_string_params_t::end
parameters_container_t::const_iterator end() const noexcept
Definition: uri_helpers.hpp:113
restinio::query_string_params_t::has
bool has(string_view_t key) const noexcept
Check parameter.
Definition: uri_helpers.hpp:80
restinio::query_string_params_t::query_string_params_t
query_string_params_t(const query_string_params_t &)=delete
restinio::utils::unescape_percent_encoding_failure_t
Type that indicates a failure of unescaping of percent-encoded symbols.
Definition: percent_encoding.hpp:160
RESTINIO_NODISCARD
#define RESTINIO_NODISCARD
Definition: compiler_features.hpp:36
exception.hpp
include_fmtlib.hpp
A special wrapper around fmtlib include files.
RESTINIO_FMT_FORMAT_STRING
#define RESTINIO_FMT_FORMAT_STRING(s)
Definition: include_fmtlib.hpp:92
restinio::impl::modified_memchr
const char * modified_memchr(int chr, const char *from, const char *to)
Definition: uri_helpers.hpp:27
restinio
Definition: asio_include.hpp:21
restinio::string_view_t
nonstd::string_view string_view_t
Definition: string_view.hpp:19
restinio::parse_query
RESTINIO_NODISCARD query_string_params_t parse_query(string_view_t original_query_string)
Parse query key-value parts.
Definition: uri_helpers.hpp:520
restinio::try_parse_query
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.
Definition: uri_helpers.hpp:399
restinio::get
Value_Type get(const router::route_params_t &params, string_view_t key)
Cast named parameter value to a given type.
Definition: express.hpp:854
std
STL namespace.
optional.hpp
percent_encoding.hpp
restinio::parse_query_traits::details::ampersand_and_semicolon_as_separators
Helper class to be reused in implementation of query-string parsing traits.
Definition: uri_helpers.hpp:196
restinio::parse_query_traits::details::ampersand_and_semicolon_as_separators::find_next_separator
static string_view_t::size_type find_next_separator(string_view_t where, string_view_t::size_type start_from) noexcept
Definition: uri_helpers.hpp:198
restinio::parse_query_traits::details::ampersand_only_as_separators
Helper class to be reused in implementation of query-string parsing traits.
Definition: uri_helpers.hpp:216
restinio::parse_query_traits::details::ampersand_only_as_separators::find_next_separator
static string_view_t::size_type find_next_separator(string_view_t where, string_view_t::size_type start_from) noexcept
Definition: uri_helpers.hpp:218
restinio::parse_query_traits::javascript_compatible
Traits for parsing a query string in JavaScript-compatible mode.
Definition: uri_helpers.hpp:266
restinio::parse_query_traits::relaxed
Traits for parsing a query string in a very relaxed mode.
Definition: uri_helpers.hpp:329
restinio::parse_query_traits::restinio_defaults
Traits for the default RESTinio parser for query string.
Definition: uri_helpers.hpp:248
restinio::parse_query_traits::x_www_form_urlencoded
Traits for parsing a query string in application/x-www-form-urlencoded mode.
Definition: uri_helpers.hpp:293
restinio::utils::javascript_compatible_unescape_traits
The traits for escaping and unexcaping symbols in JavaScript-compatible mode.
Definition: percent_encoding.hpp:125
restinio::utils::relaxed_unescape_traits
Traits for escaping and unescaping symbols in a query string in very relaxed mode.
Definition: percent_encoding.hpp:97
restinio::utils::restinio_default_unescape_traits
The default traits for escaping and unexcaping symbols in a query string.
Definition: percent_encoding.hpp:36
restinio::utils::x_www_form_urlencoded_unescape_traits
Traits for escaping and unexcaping symbols in a query string in correspondence with application/x-www...
Definition: percent_encoding.hpp:61
const
#define const
Definition: zconf.h:230