blob: 9ea7ca4afcbc5d335c6062bb25c596a6012e1bbe [file] [log] [blame]
Ed Tanous40e9b922024-09-10 13:50:16 -07001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
Ed Tanous9bd21fc2018-04-26 16:08:56 -07003#pragma once
Tanousf00032d2018-11-05 01:18:10 -03004
Ed Tanousd7857202025-01-28 15:32:26 -08005#include <boost/spirit/home/x3/char/char.hpp>
6#include <boost/spirit/home/x3/char/char_class.hpp>
7#include <boost/spirit/home/x3/core/parse.hpp>
8#include <boost/spirit/home/x3/directive/no_case.hpp>
9#include <boost/spirit/home/x3/directive/omit.hpp>
10#include <boost/spirit/home/x3/numeric/uint.hpp>
11#include <boost/spirit/home/x3/operator/alternative.hpp>
12#include <boost/spirit/home/x3/operator/kleene.hpp>
13#include <boost/spirit/home/x3/operator/optional.hpp>
14#include <boost/spirit/home/x3/operator/plus.hpp>
15#include <boost/spirit/home/x3/operator/sequence.hpp>
16#include <boost/spirit/home/x3/string/literal_string.hpp>
17#include <boost/spirit/home/x3/string/symbols.hpp>
Ed Tanous463b2932024-07-16 17:02:42 -070018
Ed Tanous18f8f602023-07-18 10:07:23 -070019#include <algorithm>
Ed Tanousd7857202025-01-28 15:32:26 -080020#include <array>
Ed Tanous3544d2a2023-08-06 18:12:20 -070021#include <ranges>
Ed Tanous99351cd2022-08-07 16:42:51 -070022#include <span>
Nan Zhoud5c80ad2022-07-11 01:16:31 +000023#include <string_view>
24#include <vector>
25
Ed Tanous1abe55e2018-09-05 08:30:59 -070026namespace http_helpers
27{
George Liu647b3cd2021-07-05 12:43:56 +080028
Ed Tanous99351cd2022-08-07 16:42:51 -070029enum class ContentType
George Liu647b3cd2021-07-05 12:43:56 +080030{
Ed Tanous99351cd2022-08-07 16:42:51 -070031 NoMatch,
Ed Tanous4a0e1a02022-09-21 15:28:04 -070032 ANY, // Accepts: */*
Ed Tanous99351cd2022-08-07 16:42:51 -070033 CBOR,
34 HTML,
35 JSON,
36 OctetStream,
Ed Tanous6fde95f2023-06-01 07:33:34 -070037 EventStream,
Ed Tanous99351cd2022-08-07 16:42:51 -070038};
39
Ed Tanouse4628c82024-12-16 10:57:04 -080040inline ContentType getContentType(std::string_view contentTypeHeader)
Ed Tanous99351cd2022-08-07 16:42:51 -070041{
Ed Tanous463b2932024-07-16 17:02:42 -070042 using boost::spirit::x3::char_;
43 using boost::spirit::x3::lit;
Ed Tanouse4628c82024-12-16 10:57:04 -080044 using boost::spirit::x3::no_case;
45 using boost::spirit::x3::omit;
46 using boost::spirit::x3::parse;
47 using boost::spirit::x3::space;
48 using boost::spirit::x3::symbols;
Ed Tanouse4628c82024-12-16 10:57:04 -080049
50 const symbols<ContentType> knownMimeType{
51 {"application/cbor", ContentType::CBOR},
52 {"application/json", ContentType::JSON},
53 {"application/octet-stream", ContentType::OctetStream},
54 {"text/event-stream", ContentType::EventStream},
55 {"text/html", ContentType::HTML}};
56
57 ContentType ct = ContentType::NoMatch;
58
59 auto typeCharset = +(char_("a-zA-Z0-9.+-"));
60
61 auto parameters =
62 *(lit(';') >> *space >> typeCharset >> lit("=") >> typeCharset);
63 auto parser = no_case[knownMimeType] >> omit[parameters];
64 std::string_view::iterator begin = contentTypeHeader.begin();
65 if (!parse(begin, contentTypeHeader.end(), parser, ct))
66 {
67 return ContentType::NoMatch;
68 }
69 if (begin != contentTypeHeader.end())
70 {
71 return ContentType::NoMatch;
72 }
73
74 return ct;
75}
76
77inline ContentType getPreferredContentType(
78 std::string_view acceptsHeader, std::span<const ContentType> preferredOrder)
79{
80 using boost::spirit::x3::char_;
81 using boost::spirit::x3::lit;
82 using boost::spirit::x3::no_case;
Ed Tanous463b2932024-07-16 17:02:42 -070083 using boost::spirit::x3::omit;
84 using boost::spirit::x3::parse;
85 using boost::spirit::x3::space;
86 using boost::spirit::x3::symbols;
Ed Tanous463b2932024-07-16 17:02:42 -070087
88 const symbols<ContentType> knownMimeType{
89 {"application/cbor", ContentType::CBOR},
90 {"application/json", ContentType::JSON},
91 {"application/octet-stream", ContentType::OctetStream},
92 {"text/html", ContentType::HTML},
93 {"text/event-stream", ContentType::EventStream},
94 {"*/*", ContentType::ANY}};
95
96 std::vector<ContentType> ct;
97
Ed Tanous80e6e252024-12-11 11:28:39 -080098 auto typeCharset = +(char_("a-zA-Z0-9.+-"));
99
100 auto parameters = *(lit(';') >> typeCharset >> lit("=") >> typeCharset);
Ed Tanouse4628c82024-12-16 10:57:04 -0800101 auto mimeType = no_case[knownMimeType] |
Ed Tanous463b2932024-07-16 17:02:42 -0700102 omit[+typeCharset >> lit('/') >> +typeCharset];
103 auto parser = +(mimeType >> omit[parameters >> -char_(',') >> *space]);
Ed Tanouse4628c82024-12-16 10:57:04 -0800104 if (!parse(acceptsHeader.begin(), acceptsHeader.end(), parser, ct))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700105 {
Ed Tanous463b2932024-07-16 17:02:42 -0700106 return ContentType::NoMatch;
George Liu647b3cd2021-07-05 12:43:56 +0800107 }
Ed Tanous463b2932024-07-16 17:02:42 -0700108
109 for (const ContentType parsedType : ct)
110 {
111 if (parsedType == ContentType::ANY)
112 {
113 return parsedType;
114 }
Ed Tanous276ede52024-08-28 15:57:45 -0700115 auto it = std::ranges::find(preferredOrder, parsedType);
116 if (it != preferredOrder.end())
Ed Tanous463b2932024-07-16 17:02:42 -0700117 {
118 return *it;
119 }
120 }
121
Ed Tanous99351cd2022-08-07 16:42:51 -0700122 return ContentType::NoMatch;
123}
124
Ed Tanous4a0e1a02022-09-21 15:28:04 -0700125inline bool isContentTypeAllowed(std::string_view header, ContentType type,
126 bool allowWildcard)
Ed Tanous99351cd2022-08-07 16:42:51 -0700127{
128 auto types = std::to_array({type});
Ed Tanous8ece0e42024-01-02 13:16:50 -0800129 ContentType allowed = getPreferredContentType(header, types);
Ed Tanous4a0e1a02022-09-21 15:28:04 -0700130 if (allowed == ContentType::ANY)
131 {
132 return allowWildcard;
133 }
134
135 return type == allowed;
George Liu647b3cd2021-07-05 12:43:56 +0800136}
137
Ed Tanous276ede52024-08-28 15:57:45 -0700138enum class Encoding
139{
140 ParseError,
141 NoMatch,
142 UnencodedBytes,
143 GZIP,
144 ZSTD,
145 ANY, // represents *. Never returned. Only used for string matching
146};
147
Patrick Williams504af5a2025-02-03 14:29:03 -0500148inline Encoding getPreferredEncoding(
149 std::string_view acceptEncoding,
150 const std::span<const Encoding> availableEncodings)
Ed Tanous276ede52024-08-28 15:57:45 -0700151{
152 if (acceptEncoding.empty())
153 {
154 return Encoding::UnencodedBytes;
155 }
156
157 using boost::spirit::x3::char_;
158 using boost::spirit::x3::lit;
159 using boost::spirit::x3::omit;
160 using boost::spirit::x3::parse;
161 using boost::spirit::x3::space;
162 using boost::spirit::x3::symbols;
163 using boost::spirit::x3::uint_;
164
165 const symbols<Encoding> knownAcceptEncoding{{"gzip", Encoding::GZIP},
166 {"zstd", Encoding::ZSTD},
167 {"*", Encoding::ANY}};
168
169 std::vector<Encoding> ct;
170
171 auto parameters = *(lit(';') >> lit("q=") >> uint_ >> -(lit('.') >> uint_));
172 auto typeCharset = char_("a-zA-Z.+-");
173 auto encodeType = knownAcceptEncoding | omit[+typeCharset];
174 auto parser = +(encodeType >> omit[parameters >> -char_(',') >> *space]);
175 if (!parse(acceptEncoding.begin(), acceptEncoding.end(), parser, ct))
176 {
177 return Encoding::ParseError;
178 }
179
180 for (const Encoding parsedType : ct)
181 {
182 if (parsedType == Encoding::ANY)
183 {
184 if (!availableEncodings.empty())
185 {
186 return *availableEncodings.begin();
187 }
188 }
189 auto it = std::ranges::find(availableEncodings, parsedType);
190 if (it != availableEncodings.end())
191 {
192 return *it;
193 }
194 }
195
196 // Fall back to raw bytes if it was allowed
197 auto it = std::ranges::find(availableEncodings, Encoding::UnencodedBytes);
198 if (it != availableEncodings.end())
199 {
200 return *it;
201 }
202
203 return Encoding::NoMatch;
204}
205
Ed Tanous23a21a12020-07-25 04:45:05 +0000206} // namespace http_helpers