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 std::string type_;
63 std::string subtype_;
64 std::string charset_;
65 int quality_;
66};
67
68bool operator==(const ContentType&, const ContentType&);
69bool operator!=(const ContentType&, const ContentType&);
70
71/// Weak ordering for Accept media-ranges checking.
72/// Positions less specific types before more specific, so that the most
73/// specific type can be matched first.
74bool operator<(const ContentType&, const ContentType&);
75
77 public:
78 size_t operator()(const ContentType&) const;
79
80 private:
81 utils::StrIcaseHash str_hasher_;
82};
83
84std::ostream& operator<<(std::ostream&, const ContentType&);
85
86namespace content_type {
87
88extern const ContentType kApplicationOctetStream;
89extern const ContentType kApplicationJson;
90extern const ContentType kTextPlain;
91
92} // namespace content_type
93} // namespace http
94
95USERVER_NAMESPACE_END