userver: userver/http/content_type.hpp Source File
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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 <userver/logging/fwd.hpp>
12#include <userver/utils/str_icase.hpp>
13
14USERVER_NAMESPACE_BEGIN
15
16/// HTTP helpers
17namespace http {
18
19/// @brief Content-Type parsing error
20class MalformedContentType : public std::runtime_error {
21 using std::runtime_error::runtime_error;
22};
23
24/// @ingroup userver_universal userver_containers
25///
26/// @brief Content-Type representation
28public:
29 /// Constructor from a single content-type/media-range header value
30 /// as per RFC7231.
31 /// @{
32 /* implicit */ ContentType(std::string_view);
33 /* implicit */ ContentType(const std::string&);
34 /* implicit */ ContentType(const char*);
35 /// @}
36
37 /// Media type (application/json).
38 std::string MediaType() const;
39
40 /// "type" token of media type (application).
41 const std::string& TypeToken() const;
42
43 /// "subtype" token of media type (json).
44 const std::string& SubtypeToken() const;
45
46 /// Whether the "charset" option was explicitly specified.
47 bool HasExplicitCharset() const;
48
49 /// Charset (utf-8).
50 const std::string& Charset() const;
51
52 /// Value of "q" parameter in range 0--1000.
53 int Quality() const;
54
55 /// Whether this media range accepts specified content type.
56 /// Differs from equality comparison in wildcard support.
57 bool DoesAccept(const ContentType&) const;
58
59 /// Value of "boundary" parameter.
60 const std::string& Boundary() const;
61
62 /// Builds a string representation of content-type/media-range
63 std::string ToString() const;
64
65private:
66 friend logging::LogHelper& operator<<(logging::LogHelper&, const ContentType&);
67 friend std::ostream& operator<<(std::ostream&, const ContentType&);
68
69 void BuildStringRepresentation();
70
71 std::string type_;
72 std::string subtype_;
73 std::string charset_;
74 std::string boundary_;
75 int quality_;
76
77 std::string string_representation_;
78};
79
80bool operator==(const ContentType&, const ContentType&);
81bool operator!=(const ContentType&, const ContentType&);
82
83/// Weak ordering for Accept media-ranges checking.
84/// Positions less specific types before more specific, so that the most
85/// specific type can be matched first.
86bool operator<(const ContentType&, const ContentType&);
87
89public:
90 size_t operator()(const ContentType&) const;
91
92private:
93 utils::StrIcaseHash str_hasher_;
94};
95
96namespace content_type {
97
98extern const ContentType kApplicationOctetStream;
99extern const ContentType kApplicationJson;
100extern const ContentType kTextPlain;
101
102} // namespace content_type
103} // namespace http
104
105USERVER_NAMESPACE_END