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