content-disposition.hpp
Go to the documentation of this file.
/*
* RESTinio
*/
#pragma once
#include <restinio/helpers/http_field_parsers/basics.hpp>
#include <restinio/helpers/http_field_parsers/details/pct_encoded_symbols.hpp>
namespace restinio
{
namespace http_field_parsers
{
namespace content_disposition_details
{
namespace ep_impl = restinio::easy_parser::impl;
namespace hfp_impl = restinio::http_field_parsers::impl;
namespace hfp_details = restinio::http_field_parsers::details;
//
// regular_token_producer_t
//
class regular_token_producer_t
: public hfp_impl::token_producer_t
{
public:
RESTINIO_NODISCARD
expected_t< result_type, parse_error_t >
try_parse( ep_impl::source_t & from ) const
{
ep_impl::source_t::content_consumer_t consumer{ from };
const auto result = hfp_impl::token_producer_t::try_parse( from );
if( result )
{
if( '*' == *(result->rbegin()) )
{
// Regular token can't have the trailing '*'.
return make_unexpected( parse_error_t{
consumer.started_at() + result->size() - 1,
error_reason_t::unexpected_character
} );
}
consumer.commit();
}
return result;
}
};
//
// ext_token_producer_t
//
class ext_token_producer_t
: public hfp_impl::token_producer_t
{
public:
RESTINIO_NODISCARD
expected_t< result_type, parse_error_t >
try_parse( ep_impl::source_t & from ) const
{
ep_impl::source_t::content_consumer_t consumer{ from };
const auto result = hfp_impl::token_producer_t::try_parse( from );
if( result )
{
if( '*' != *(result->rbegin()) )
{
// Extended token should have the trailing '*'.
return make_unexpected( parse_error_t{
consumer.started_at(),
error_reason_t::pattern_not_found
} );
}
consumer.commit();
}
return result;
}
};
//
// mime_charsetc_predicate_t
//
struct mime_charsetc_predicate_t
{
RESTINIO_NODISCARD
bool
operator()( const char actual ) const noexcept
{
return hfp_impl::is_alpha(actual)
|| hfp_impl::is_digit(actual)
|| '!' == actual
|| '#' == actual
|| '$' == actual
|| '%' == actual
|| '&' == actual
|| '+' == actual
|| '-' == actual
|| '^' == actual
|| '_' == actual
|| '`' == actual
|| '{' == actual
|| '}' == actual
|| '~' == actual
;
}
};
//
// mime_charsetc_symbol_producer
//
RESTINIO_NODISCARD
inline auto
mime_charsetc_symbol_p()
{
return ep_impl::symbol_producer_template_t< mime_charsetc_predicate_t >{};
}
//
// language_predicate_t
//
struct language_predicate_t
{
RESTINIO_NODISCARD
bool
operator()( const char actual ) const noexcept
{
return hfp_impl::is_alpha(actual)
|| hfp_impl::is_digit(actual)
|| '-' == actual
;
}
};
//
// language_symbol_producer
//
RESTINIO_NODISCARD
inline auto
language_symbol_p()
{
return ep_impl::symbol_producer_template_t< language_predicate_t >{};
}
//
// attr_char_predicate_t
//
struct attr_char_predicate_t
{
RESTINIO_NODISCARD
bool
operator()( const char actual ) const noexcept
{
return hfp_impl::is_alpha(actual)
|| hfp_impl::is_digit(actual)
|| '!' == actual
|| '#' == actual
|| '$' == actual
|| '&' == actual
|| '+' == actual
|| '-' == actual
|| '.' == actual
|| '^' == actual
|| '_' == actual
|| '`' == actual
|| '|' == actual
|| '~' == actual
;
}
};
//
// attr_char_symbol_producer
//
RESTINIO_NODISCARD
inline auto
attr_char_symbol_p()
{
return ep_impl::symbol_producer_template_t< attr_char_predicate_t >{};
}
//
// ext_parameter_value_producer
//
RESTINIO_NODISCARD
inline auto
ext_parameter_value_p()
{
return produce< std::string >(
repeat( 1, N, mime_charsetc_symbol_p() >> to_container() ),
symbol_p( '\'' ) >> to_container(),
repeat( 0, N, language_symbol_p() >> to_container() ),
symbol_p( '\'' ) >> to_container(),
repeat( 0, N,
alternatives(
attr_char_symbol_p() >> to_container(),
hfp_details::pct_encoded_symbols_p() >>
hfp_details::pct_encoded_symbols_consumer_t{} )
)
);
}
} /* namespace content_disposition_details */
//
// content_disposition_value_t
//
struct content_disposition_value_t
{
using parameter_t = parameter_with_mandatory_value_t;
using parameter_container_t = parameter_with_mandatory_value_container_t;
std::string value;
parameter_container_t parameters;
RESTINIO_NODISCARD
static auto
make_parser()
{
using namespace content_disposition_details;
return produce< content_disposition_value_t >(
token_p() >> to_lower()
>> &content_disposition_value_t::value,
produce< parameter_container_t >(
repeat( 0, N,
produce< parameter_t >(
ows(),
symbol(';'),
ows(),
alternatives(
sequence(
regular_token_producer_t{}
>> to_lower() >> &parameter_t::first,
symbol('='),
alternatives(
token_p() >> &parameter_t::second,
quoted_string_p() >> &parameter_t::second
)
),
sequence(
ext_token_producer_t{}
>> to_lower() >> &parameter_t::first,
symbol('='),
ext_parameter_value_p() >> &parameter_t::second
)
)
) >> to_container()
)
) >> &content_disposition_value_t::parameters
);
}
RESTINIO_NODISCARD
static expected_t< content_disposition_value_t, restinio::easy_parser::parse_error_t >
try_parse( string_view_t what )
{
return restinio::easy_parser::try_parse( what, make_parser() );
}
};
} /* namespace http_field_parsers */
} /* namespace restinio */
basics.hpp
Utilities for parsing values of http-fields.
restinio::easy_parser::impl::source_t
The class that implements "input stream".
Definition: easy_parser.hpp:740
restinio::easy_parser::impl::symbol_producer_template_t
A template for producer of charachers that satisfy some predicate.
Definition: easy_parser.hpp:2133
restinio::easy_parser::parse_error_t
Information about parsing error.
Definition: easy_parser.hpp:93
restinio::http_field_parsers::content_disposition_details::ext_token_producer_t
A producer for token that is an "extended parameter name" in sense of RCF6266 and RCF5987.
Definition: content-disposition.hpp:89
restinio::http_field_parsers::content_disposition_details::ext_token_producer_t::try_parse
RESTINIO_NODISCARD expected_t< result_type, parse_error_t > try_parse(ep_impl::source_t &from) const
Definition: content-disposition.hpp:93
restinio::http_field_parsers::content_disposition_details::regular_token_producer_t
A producer for token that is a "regular parameter name" in sense of RCF6266 and RCF5987.
Definition: content-disposition.hpp:47
restinio::http_field_parsers::content_disposition_details::regular_token_producer_t::try_parse
RESTINIO_NODISCARD expected_t< result_type, parse_error_t > try_parse(ep_impl::source_t &from) const
Definition: content-disposition.hpp:51
restinio::http_field_parsers::impl::token_producer_t
A producer for token.
Definition: basics.hpp:523
restinio::http_field_parsers::impl::token_producer_t::try_parse
RESTINIO_NODISCARD expected_t< result_type, parse_error_t > try_parse(source_t &from) const
Definition: basics.hpp:568
RESTINIO_NODISCARD
#define RESTINIO_NODISCARD
Definition: compiler_features.hpp:33
restinio::easy_parser::impl
Definition: easy_parser.hpp:280
restinio::easy_parser::impl::is_digit
RESTINIO_NODISCARD constexpr bool is_digit(const char ch) noexcept
Is a character a decimal digit?
Definition: easy_parser.hpp:669
restinio::easy_parser::symbol
RESTINIO_NODISCARD auto symbol(char expected) noexcept
A factory function to create a clause that expects the speficied symbol, extracts it and then skips i...
Definition: easy_parser.hpp:4032
restinio::easy_parser::sequence
RESTINIO_NODISCARD auto sequence(Clauses &&... clauses)
A factory function to create a sequence of subclauses.
Definition: easy_parser.hpp:3758
restinio::easy_parser::try_parse
RESTINIO_NODISCARD expected_t< typename Producer::result_type, parse_error_t > try_parse(string_view_t from, Producer producer)
Perform the parsing of the specified content by using specified value producer.
Definition: easy_parser.hpp:5042
restinio::easy_parser::repeat
RESTINIO_NODISCARD auto repeat(std::size_t min_occurences, std::size_t max_occurences, Clauses &&... clauses)
A factory function to create repetitor of subclauses.
Definition: easy_parser.hpp:3876
restinio::easy_parser::N
constexpr std::size_t N
A special marker that means infinite repetitions.
Definition: easy_parser.hpp:455
restinio::easy_parser::to_container
RESTINIO_NODISCARD auto to_container()
A factory function to create a to_container_consumer.
Definition: easy_parser.hpp:4583
restinio::easy_parser::symbol_p
RESTINIO_NODISCARD auto symbol_p(char expected) noexcept
A factory function to create a symbol_producer.
Definition: easy_parser.hpp:3955
restinio::easy_parser::to_lower
RESTINIO_NODISCARD auto to_lower() noexcept
A factory function to create a to_lower_transformer.
Definition: easy_parser.hpp:4610
restinio::easy_parser::alternatives
RESTINIO_NODISCARD auto alternatives(Clauses &&... clauses)
A factory function to create an alternatives clause.
Definition: easy_parser.hpp:3595
restinio::http_field_parsers::content_disposition_details::mime_charsetc_symbol_p
RESTINIO_NODISCARD auto mime_charsetc_symbol_p()
A factory for producer that extracts mime-charsetc symbols.
Definition: content-disposition.hpp:163
restinio::http_field_parsers::content_disposition_details::language_symbol_p
RESTINIO_NODISCARD auto language_symbol_p()
A factory for producer that extracts language symbols.
Definition: content-disposition.hpp:207
restinio::http_field_parsers::content_disposition_details::attr_char_symbol_p
RESTINIO_NODISCARD auto attr_char_symbol_p()
A factory for producer that extracts attr-char symbols.
Definition: content-disposition.hpp:259
restinio::http_field_parsers::content_disposition_details::ext_parameter_value_p
RESTINIO_NODISCARD auto ext_parameter_value_p()
A producer for an "extended parameter value" in sense of RCF6266 and RCF5987.
Definition: content-disposition.hpp:299
restinio::http_field_parsers::details
Definition: pct_encoded_symbols.hpp:23
restinio::http_field_parsers::details::pct_encoded_symbols_p
RESTINIO_NODISCARD auto pct_encoded_symbols_p()
A producer that extract a sequence of symbols represented a percent-encoded character.
Definition: pct_encoded_symbols.hpp:57
restinio::http_field_parsers::impl
Definition: basics.hpp:247
restinio::http_field_parsers::impl::is_alpha
RESTINIO_NODISCARD constexpr bool is_alpha(const char ch) noexcept
Is a character an ALPHA?
Definition: basics.hpp:263
restinio::http_field_parsers::parameter_with_mandatory_value_container_t
std::vector< parameter_with_mandatory_value_t > parameter_with_mandatory_value_container_t
A type of container for parameters with mandatory values.
Definition: basics.hpp:1532
restinio::http_field_parsers::ows
RESTINIO_NODISCARD auto ows() noexcept
A factory function to create an OWS clause.
Definition: basics.hpp:939
restinio::http_field_parsers::token_p
RESTINIO_NODISCARD auto token_p() noexcept
A factory function to create a token_producer.
Definition: basics.hpp:985
restinio::http_field_parsers::quoted_string_p
RESTINIO_NODISCARD auto quoted_string_p() noexcept
A factory function to create a quoted_string_producer.
Definition: basics.hpp:1012
restinio::http_field_parsers::parameter_with_mandatory_value_t
std::pair< std::string, std::string > parameter_with_mandatory_value_t
A type that describes a parameter with mandatory value.
Definition: basics.hpp:1521
restinio
Definition: asio_include.hpp:21
restinio::string_view_t
nonstd::string_view string_view_t
Definition: string_view.hpp:19
restinio::expected_t
nonstd::expected< T, E > expected_t
Definition: expected.hpp:22
pct_encoded_symbols.hpp
Stuff related to percent-encoded symbols.
restinio::http_field_parsers::content_disposition_details::attr_char_predicate_t
A preducate for symbol_producer_template that checks that a symbol is attr-char symbol from RCF5987.
Definition: content-disposition.hpp:224
restinio::http_field_parsers::content_disposition_details::attr_char_predicate_t::operator()
RESTINIO_NODISCARD bool operator()(const char actual) const noexcept
Definition: content-disposition.hpp:227
restinio::http_field_parsers::content_disposition_details::language_predicate_t
A preducate for symbol_producer_template that checks that a symbol is language symbol from RCF5646.
Definition: content-disposition.hpp:185
restinio::http_field_parsers::content_disposition_details::language_predicate_t::operator()
RESTINIO_NODISCARD bool operator()(const char actual) const noexcept
Definition: content-disposition.hpp:188
restinio::http_field_parsers::content_disposition_details::mime_charsetc_predicate_t
A preducate for symbol_producer_template that checks that a symbol is mime-charsetc symbol from RCF59...
Definition: content-disposition.hpp:127
restinio::http_field_parsers::content_disposition_details::mime_charsetc_predicate_t::operator()
RESTINIO_NODISCARD bool operator()(const char actual) const noexcept
Definition: content-disposition.hpp:130
restinio::http_field_parsers::content_disposition_value_t
Tools for working with the value of Content-Disposition HTTP-field.
Definition: content-disposition.hpp:337
restinio::http_field_parsers::content_disposition_value_t::try_parse
static RESTINIO_NODISCARD expected_t< content_disposition_value_t, restinio::easy_parser::parse_error_t > try_parse(string_view_t what)
An attempt to parse Content-Disposition HTTP-field.
Definition: content-disposition.hpp:395
restinio::http_field_parsers::content_disposition_value_t::parameter_container_t
parameter_with_mandatory_value_container_t parameter_container_t
Definition: content-disposition.hpp:340
restinio::http_field_parsers::content_disposition_value_t::parameters
parameter_container_t parameters
Definition: content-disposition.hpp:343
restinio::http_field_parsers::content_disposition_value_t::parameter_t
parameter_with_mandatory_value_t parameter_t
Definition: content-disposition.hpp:338
restinio::http_field_parsers::content_disposition_value_t::value
std::string value
Definition: content-disposition.hpp:342
restinio::http_field_parsers::content_disposition_value_t::make_parser
static RESTINIO_NODISCARD auto make_parser()
A factory function for a parser of Content-Disposition value.
Definition: content-disposition.hpp:352
restinio::http_field_parsers::details::pct_encoded_symbols_consumer_t
A special consumer that inserts an extracted sequence of symbols into the result string.
Definition: pct_encoded_symbols.hpp:80