Ed Tanous | 40e9b92 | 2024-09-10 13:50:16 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: Copyright OpenBMC Authors |
Ed Tanous | 9bd21fc | 2018-04-26 16:08:56 -0700 | [diff] [blame] | 3 | #pragma once |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 4 | |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 5 | #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 Tanous | 463b293 | 2024-07-16 17:02:42 -0700 | [diff] [blame] | 18 | |
Ed Tanous | 18f8f60 | 2023-07-18 10:07:23 -0700 | [diff] [blame] | 19 | #include <algorithm> |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 20 | #include <array> |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 21 | #include <cctype> |
Ed Tanous | 3544d2a | 2023-08-06 18:12:20 -0700 | [diff] [blame] | 22 | #include <ranges> |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 23 | #include <span> |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 24 | #include <string_view> |
| 25 | #include <vector> |
| 26 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 27 | namespace http_helpers |
| 28 | { |
George Liu | 647b3cd | 2021-07-05 12:43:56 +0800 | [diff] [blame] | 29 | |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 30 | enum class ContentType |
George Liu | 647b3cd | 2021-07-05 12:43:56 +0800 | [diff] [blame] | 31 | { |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 32 | NoMatch, |
Ed Tanous | 4a0e1a0 | 2022-09-21 15:28:04 -0700 | [diff] [blame] | 33 | ANY, // Accepts: */* |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 34 | CBOR, |
| 35 | HTML, |
| 36 | JSON, |
| 37 | OctetStream, |
Ed Tanous | 6fde95f | 2023-06-01 07:33:34 -0700 | [diff] [blame] | 38 | EventStream, |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 39 | }; |
| 40 | |
Ed Tanous | e4628c8 | 2024-12-16 10:57:04 -0800 | [diff] [blame] | 41 | inline ContentType getContentType(std::string_view contentTypeHeader) |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 42 | { |
Ed Tanous | 463b293 | 2024-07-16 17:02:42 -0700 | [diff] [blame] | 43 | using boost::spirit::x3::char_; |
| 44 | using boost::spirit::x3::lit; |
Ed Tanous | e4628c8 | 2024-12-16 10:57:04 -0800 | [diff] [blame] | 45 | using boost::spirit::x3::no_case; |
| 46 | using boost::spirit::x3::omit; |
| 47 | using boost::spirit::x3::parse; |
| 48 | using boost::spirit::x3::space; |
| 49 | using boost::spirit::x3::symbols; |
| 50 | using boost::spirit::x3::uint_; |
| 51 | |
| 52 | const symbols<ContentType> knownMimeType{ |
| 53 | {"application/cbor", ContentType::CBOR}, |
| 54 | {"application/json", ContentType::JSON}, |
| 55 | {"application/octet-stream", ContentType::OctetStream}, |
| 56 | {"text/event-stream", ContentType::EventStream}, |
| 57 | {"text/html", ContentType::HTML}}; |
| 58 | |
| 59 | ContentType ct = ContentType::NoMatch; |
| 60 | |
| 61 | auto typeCharset = +(char_("a-zA-Z0-9.+-")); |
| 62 | |
| 63 | auto parameters = |
| 64 | *(lit(';') >> *space >> typeCharset >> lit("=") >> typeCharset); |
| 65 | auto parser = no_case[knownMimeType] >> omit[parameters]; |
| 66 | std::string_view::iterator begin = contentTypeHeader.begin(); |
| 67 | if (!parse(begin, contentTypeHeader.end(), parser, ct)) |
| 68 | { |
| 69 | return ContentType::NoMatch; |
| 70 | } |
| 71 | if (begin != contentTypeHeader.end()) |
| 72 | { |
| 73 | return ContentType::NoMatch; |
| 74 | } |
| 75 | |
| 76 | return ct; |
| 77 | } |
| 78 | |
| 79 | inline ContentType getPreferredContentType( |
| 80 | std::string_view acceptsHeader, std::span<const ContentType> preferredOrder) |
| 81 | { |
| 82 | using boost::spirit::x3::char_; |
| 83 | using boost::spirit::x3::lit; |
| 84 | using boost::spirit::x3::no_case; |
Ed Tanous | 463b293 | 2024-07-16 17:02:42 -0700 | [diff] [blame] | 85 | using boost::spirit::x3::omit; |
| 86 | using boost::spirit::x3::parse; |
| 87 | using boost::spirit::x3::space; |
| 88 | using boost::spirit::x3::symbols; |
| 89 | using boost::spirit::x3::uint_; |
| 90 | |
| 91 | const symbols<ContentType> knownMimeType{ |
| 92 | {"application/cbor", ContentType::CBOR}, |
| 93 | {"application/json", ContentType::JSON}, |
| 94 | {"application/octet-stream", ContentType::OctetStream}, |
| 95 | {"text/html", ContentType::HTML}, |
| 96 | {"text/event-stream", ContentType::EventStream}, |
| 97 | {"*/*", ContentType::ANY}}; |
| 98 | |
| 99 | std::vector<ContentType> ct; |
| 100 | |
Ed Tanous | 80e6e25 | 2024-12-11 11:28:39 -0800 | [diff] [blame] | 101 | auto typeCharset = +(char_("a-zA-Z0-9.+-")); |
| 102 | |
| 103 | auto parameters = *(lit(';') >> typeCharset >> lit("=") >> typeCharset); |
Ed Tanous | e4628c8 | 2024-12-16 10:57:04 -0800 | [diff] [blame] | 104 | auto mimeType = no_case[knownMimeType] | |
Ed Tanous | 463b293 | 2024-07-16 17:02:42 -0700 | [diff] [blame] | 105 | omit[+typeCharset >> lit('/') >> +typeCharset]; |
| 106 | auto parser = +(mimeType >> omit[parameters >> -char_(',') >> *space]); |
Ed Tanous | e4628c8 | 2024-12-16 10:57:04 -0800 | [diff] [blame] | 107 | if (!parse(acceptsHeader.begin(), acceptsHeader.end(), parser, ct)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 108 | { |
Ed Tanous | 463b293 | 2024-07-16 17:02:42 -0700 | [diff] [blame] | 109 | return ContentType::NoMatch; |
George Liu | 647b3cd | 2021-07-05 12:43:56 +0800 | [diff] [blame] | 110 | } |
Ed Tanous | 463b293 | 2024-07-16 17:02:42 -0700 | [diff] [blame] | 111 | |
| 112 | for (const ContentType parsedType : ct) |
| 113 | { |
| 114 | if (parsedType == ContentType::ANY) |
| 115 | { |
| 116 | return parsedType; |
| 117 | } |
Ed Tanous | 276ede5 | 2024-08-28 15:57:45 -0700 | [diff] [blame] | 118 | auto it = std::ranges::find(preferredOrder, parsedType); |
| 119 | if (it != preferredOrder.end()) |
Ed Tanous | 463b293 | 2024-07-16 17:02:42 -0700 | [diff] [blame] | 120 | { |
| 121 | return *it; |
| 122 | } |
| 123 | } |
| 124 | |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 125 | return ContentType::NoMatch; |
| 126 | } |
| 127 | |
Ed Tanous | 4a0e1a0 | 2022-09-21 15:28:04 -0700 | [diff] [blame] | 128 | inline bool isContentTypeAllowed(std::string_view header, ContentType type, |
| 129 | bool allowWildcard) |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 130 | { |
| 131 | auto types = std::to_array({type}); |
Ed Tanous | 8ece0e4 | 2024-01-02 13:16:50 -0800 | [diff] [blame] | 132 | ContentType allowed = getPreferredContentType(header, types); |
Ed Tanous | 4a0e1a0 | 2022-09-21 15:28:04 -0700 | [diff] [blame] | 133 | if (allowed == ContentType::ANY) |
| 134 | { |
| 135 | return allowWildcard; |
| 136 | } |
| 137 | |
| 138 | return type == allowed; |
George Liu | 647b3cd | 2021-07-05 12:43:56 +0800 | [diff] [blame] | 139 | } |
| 140 | |
Ed Tanous | 276ede5 | 2024-08-28 15:57:45 -0700 | [diff] [blame] | 141 | enum class Encoding |
| 142 | { |
| 143 | ParseError, |
| 144 | NoMatch, |
| 145 | UnencodedBytes, |
| 146 | GZIP, |
| 147 | ZSTD, |
| 148 | ANY, // represents *. Never returned. Only used for string matching |
| 149 | }; |
| 150 | |
Patrick Williams | 504af5a | 2025-02-03 14:29:03 -0500 | [diff] [blame] | 151 | inline Encoding getPreferredEncoding( |
| 152 | std::string_view acceptEncoding, |
| 153 | const std::span<const Encoding> availableEncodings) |
Ed Tanous | 276ede5 | 2024-08-28 15:57:45 -0700 | [diff] [blame] | 154 | { |
| 155 | if (acceptEncoding.empty()) |
| 156 | { |
| 157 | return Encoding::UnencodedBytes; |
| 158 | } |
| 159 | |
| 160 | using boost::spirit::x3::char_; |
| 161 | using boost::spirit::x3::lit; |
| 162 | using boost::spirit::x3::omit; |
| 163 | using boost::spirit::x3::parse; |
| 164 | using boost::spirit::x3::space; |
| 165 | using boost::spirit::x3::symbols; |
| 166 | using boost::spirit::x3::uint_; |
| 167 | |
| 168 | const symbols<Encoding> knownAcceptEncoding{{"gzip", Encoding::GZIP}, |
| 169 | {"zstd", Encoding::ZSTD}, |
| 170 | {"*", Encoding::ANY}}; |
| 171 | |
| 172 | std::vector<Encoding> ct; |
| 173 | |
| 174 | auto parameters = *(lit(';') >> lit("q=") >> uint_ >> -(lit('.') >> uint_)); |
| 175 | auto typeCharset = char_("a-zA-Z.+-"); |
| 176 | auto encodeType = knownAcceptEncoding | omit[+typeCharset]; |
| 177 | auto parser = +(encodeType >> omit[parameters >> -char_(',') >> *space]); |
| 178 | if (!parse(acceptEncoding.begin(), acceptEncoding.end(), parser, ct)) |
| 179 | { |
| 180 | return Encoding::ParseError; |
| 181 | } |
| 182 | |
| 183 | for (const Encoding parsedType : ct) |
| 184 | { |
| 185 | if (parsedType == Encoding::ANY) |
| 186 | { |
| 187 | if (!availableEncodings.empty()) |
| 188 | { |
| 189 | return *availableEncodings.begin(); |
| 190 | } |
| 191 | } |
| 192 | auto it = std::ranges::find(availableEncodings, parsedType); |
| 193 | if (it != availableEncodings.end()) |
| 194 | { |
| 195 | return *it; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | // Fall back to raw bytes if it was allowed |
| 200 | auto it = std::ranges::find(availableEncodings, Encoding::UnencodedBytes); |
| 201 | if (it != availableEncodings.end()) |
| 202 | { |
| 203 | return *it; |
| 204 | } |
| 205 | |
| 206 | return Encoding::NoMatch; |
| 207 | } |
| 208 | |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 209 | } // namespace http_helpers |