userver: userver/http/content_type.hpp Source File
Loading...
Searching...
No Matches
content_type.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/http/content_type.hpp
4/// @brief @copybrief http::ContentType
5
6#include <iosfwd>
7#include <stdexcept>
8#include <string>
9#include <string_view>
10
11#include <fmt/core.h>
12
13#include <userver/logging/fwd.hpp>
14#include <userver/utils/str_icase.hpp>
15
16USERVER_NAMESPACE_BEGIN
17
18/// HTTP helpers
19namespace http {
20
21/// @brief Content-Type parsing error
22class MalformedContentType : public std::runtime_error {
23 using std::runtime_error::runtime_error;
24};
25
26/// @ingroup userver_universal userver_containers
27///
28/// @brief Content-Type representation
30public:
31 /// Constructor from a single content-type/media-range header value
32 /// as per RFC7231.
33 /// @{
34 /* implicit */ ContentType(std::string_view);
35 /* implicit */ ContentType(const std::string&);
36 /* implicit */ ContentType(const char*);
37 /// @}
38
39 /// Media type (application/json).
40 std::string MediaType() const;
41
42 /// "type" token of media type (application).
43 const std::string& TypeToken() const;
44
45 /// "subtype" token of media type (json).
46 const std::string& SubtypeToken() const;
47
48 /// Whether the "charset" option was explicitly specified.
49 bool HasExplicitCharset() const;
50
51 /// Charset (utf-8).
52 const std::string& Charset() const;
53
54 /// Value of "q" parameter in range 0--1000.
55 int Quality() const;
56
57 /// Whether this media range accepts specified content type.
58 /// Differs from equality comparison in wildcard support.
59 bool DoesAccept(const ContentType&) const;
60
61 /// Value of "boundary" parameter.
62 const std::string& Boundary() const;
63
64 /// Builds a string representation of content-type/media-range
65 std::string ToString() const;
66
67private:
68 friend logging::LogHelper& operator<<(logging::LogHelper&, const ContentType&);
69 friend std::ostream& operator<<(std::ostream&, const ContentType&);
70
71 void BuildStringRepresentation();
72
73 std::string type_;
74 std::string subtype_;
75 std::string charset_;
76 std::string boundary_;
77 int quality_;
78
79 std::string string_representation_;
80};
81
82bool operator==(const ContentType&, const ContentType&);
83
84/// Weak ordering for Accept media-ranges checking.
85/// Positions less specific types before more specific, so that the most
86/// specific type can be matched first.
87bool operator<(const ContentType&, const ContentType&);
88
90public:
91 size_t operator()(const ContentType&) const;
92
93private:
94 utils::StrIcaseHash str_hasher_;
95};
96
97namespace content_type {
98
99extern const ContentType kApplicationOctetStream;
100extern const ContentType kApplicationJson;
101extern const ContentType kTextPlain;
102
103} // namespace content_type
104} // namespace http
105
106USERVER_NAMESPACE_END
107
108template <>
109struct fmt::formatter<USERVER_NAMESPACE::http::ContentType> {
110 constexpr static auto parse(format_parse_context& ctx) { return ctx.begin(); }
111
112 template <typename FormatContext>
113 auto format(const USERVER_NAMESPACE::http::ContentType& value, FormatContext& ctx) const -> decltype(ctx.out()) {
114 return fmt::format_to(ctx.out(), "{}", value.ToString());
115 }
116};