RESTinio

media-type.hpp
Go to the documentation of this file.
1/*
2 * RESTinio
3 */
4
12#pragma once
13
14#include <restinio/helpers/http_field_parsers/basics.hpp>
15
16 namespace restinio
17{
18
19 namespace http_field_parsers
20{
21
22//
23// media_type_value_t
24//
41 struct media_type_value_t
42{
43 using parameter_t = parameter_with_mandatory_value_t;
44
45 using parameter_container_t = parameter_with_mandatory_value_container_t;
46
47 std::string type;
48 std::string subtype;
49 parameter_container_t parameters;
50
64 RESTINIO_NODISCARD
65 static auto
66 make_default_parser()
67 {
68 return produce< media_type_value_t >(
69 token_p() >> to_lower() >> &media_type_value_t::type,
70 symbol('/'),
71 token_p() >> to_lower() >> &media_type_value_t::subtype,
72 params_with_value_p() >> &media_type_value_t::parameters
73 );
74 }
75
91 RESTINIO_NODISCARD
92 static auto
93 make_weight_aware_parser()
94 {
95 return produce< media_type_value_t >(
96 token_p() >> to_lower() >> &media_type_value_t::type,
97 symbol('/'),
98 token_p() >> to_lower() >> &media_type_value_t::subtype,
99 produce< parameter_container_t >(
100 repeat( 0, N,
101 produce< parameter_t >(
102 not_clause( weight_p() >> skip() ),
103 ows(),
104 symbol(';'),
105 ows(),
106 token_p() >> to_lower() >> &parameter_t::first,
107 symbol('='),
108 alternatives(
109 token_p() >> &parameter_t::second,
110 quoted_string_p() >> &parameter_t::second
111 )
112 ) >> to_container()
113 )
114 ) >> &media_type_value_t::parameters
115 );
116 }
117
126 RESTINIO_NODISCARD
127 static expected_t< media_type_value_t, restinio::easy_parser::parse_error_t >
128 try_parse( string_view_t what )
129 {
130 return restinio::easy_parser::try_parse( what, make_default_parser() );
131 }
132};
133
134} /* namespace http_field_parsers */
135
136} /* namespace restinio */
137
basics.hpp
Utilities for parsing values of http-fields.
RESTINIO_NODISCARD
#define RESTINIO_NODISCARD
Definition: compiler_features.hpp:33
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::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::skip
RESTINIO_NODISCARD auto skip() noexcept
A factory function to create a skip_consumer.
Definition: easy_parser.hpp:3922
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::not_clause
RESTINIO_NODISCARD auto not_clause(Clauses &&... clauses)
A factory function to create a not_clause.
Definition: easy_parser.hpp:3676
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::params_with_value_p
RESTINIO_NODISCARD impl::params_with_value_producer_t params_with_value_p()
A factory of producer of parameter_with_mandatory_value_container.
Definition: basics.hpp:1685
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::http_field_parsers::weight_p
RESTINIO_NODISCARD auto weight_p() noexcept
A factory function to create a producer for weight parameter.
Definition: basics.hpp:1277
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
restinio::http_field_parsers::media_type_value_t
Tools for working with media-type in HTTP-fields.
Definition: media-type.hpp:42
restinio::http_field_parsers::media_type_value_t::make_weight_aware_parser
static RESTINIO_NODISCARD auto make_weight_aware_parser()
Definition: media-type.hpp:93
restinio::http_field_parsers::media_type_value_t::try_parse
static RESTINIO_NODISCARD expected_t< media_type_value_t, restinio::easy_parser::parse_error_t > try_parse(string_view_t what)
An attempt to parse media-type value.
Definition: media-type.hpp:128
restinio::http_field_parsers::media_type_value_t::make_default_parser
static RESTINIO_NODISCARD auto make_default_parser()
Definition: media-type.hpp:66
restinio::http_field_parsers::media_type_value_t::type
std::string type
Definition: media-type.hpp:47
restinio::http_field_parsers::media_type_value_t::parameter_container_t
parameter_with_mandatory_value_container_t parameter_container_t
Definition: media-type.hpp:45
restinio::http_field_parsers::media_type_value_t::parameters
parameter_container_t parameters
Definition: media-type.hpp:49
restinio::http_field_parsers::media_type_value_t::parameter_t
parameter_with_mandatory_value_t parameter_t
Definition: media-type.hpp:43
restinio::http_field_parsers::media_type_value_t::subtype
std::string subtype
Definition: media-type.hpp:48