Ed Tanous | 9bd21fc | 2018-04-26 16:08:56 -0700 | [diff] [blame] | 1 | #pragma once |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 2 | |
Ed Tanous | 463b293 | 2024-07-16 17:02:42 -0700 | [diff] [blame] | 3 | #include <boost/spirit/home/x3.hpp> |
| 4 | |
Ed Tanous | 18f8f60 | 2023-07-18 10:07:23 -0700 | [diff] [blame] | 5 | #include <algorithm> |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 6 | #include <cctype> |
| 7 | #include <iomanip> |
| 8 | #include <ostream> |
Ed Tanous | 3544d2a | 2023-08-06 18:12:20 -0700 | [diff] [blame] | 9 | #include <ranges> |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 10 | #include <span> |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 11 | #include <string> |
| 12 | #include <string_view> |
| 13 | #include <vector> |
| 14 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 15 | namespace http_helpers |
| 16 | { |
George Liu | 647b3cd | 2021-07-05 12:43:56 +0800 | [diff] [blame] | 17 | |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 18 | enum class ContentType |
George Liu | 647b3cd | 2021-07-05 12:43:56 +0800 | [diff] [blame] | 19 | { |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 20 | NoMatch, |
Ed Tanous | 4a0e1a0 | 2022-09-21 15:28:04 -0700 | [diff] [blame] | 21 | ANY, // Accepts: */* |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 22 | CBOR, |
| 23 | HTML, |
| 24 | JSON, |
| 25 | OctetStream, |
Ed Tanous | 6fde95f | 2023-06-01 07:33:34 -0700 | [diff] [blame] | 26 | EventStream, |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 27 | }; |
| 28 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 29 | inline ContentType getPreferredContentType( |
Ed Tanous | 276ede5 | 2024-08-28 15:57:45 -0700 | [diff] [blame] | 30 | std::string_view header, std::span<const ContentType> preferredOrder) |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 31 | { |
Ed Tanous | 463b293 | 2024-07-16 17:02:42 -0700 | [diff] [blame] | 32 | using boost::spirit::x3::char_; |
| 33 | using boost::spirit::x3::lit; |
| 34 | using boost::spirit::x3::omit; |
| 35 | using boost::spirit::x3::parse; |
| 36 | using boost::spirit::x3::space; |
| 37 | using boost::spirit::x3::symbols; |
| 38 | using boost::spirit::x3::uint_; |
| 39 | |
| 40 | const symbols<ContentType> knownMimeType{ |
| 41 | {"application/cbor", ContentType::CBOR}, |
| 42 | {"application/json", ContentType::JSON}, |
| 43 | {"application/octet-stream", ContentType::OctetStream}, |
| 44 | {"text/html", ContentType::HTML}, |
| 45 | {"text/event-stream", ContentType::EventStream}, |
| 46 | {"*/*", ContentType::ANY}}; |
| 47 | |
| 48 | std::vector<ContentType> ct; |
| 49 | |
| 50 | auto parameters = *(lit(';') >> lit("q=") >> uint_ >> -(lit('.') >> uint_)); |
| 51 | auto typeCharset = char_("a-zA-Z.+-"); |
| 52 | auto mimeType = knownMimeType | |
| 53 | omit[+typeCharset >> lit('/') >> +typeCharset]; |
| 54 | auto parser = +(mimeType >> omit[parameters >> -char_(',') >> *space]); |
| 55 | if (!parse(header.begin(), header.end(), parser, ct)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 56 | { |
Ed Tanous | 463b293 | 2024-07-16 17:02:42 -0700 | [diff] [blame] | 57 | return ContentType::NoMatch; |
George Liu | 647b3cd | 2021-07-05 12:43:56 +0800 | [diff] [blame] | 58 | } |
Ed Tanous | 463b293 | 2024-07-16 17:02:42 -0700 | [diff] [blame] | 59 | |
| 60 | for (const ContentType parsedType : ct) |
| 61 | { |
| 62 | if (parsedType == ContentType::ANY) |
| 63 | { |
| 64 | return parsedType; |
| 65 | } |
Ed Tanous | 276ede5 | 2024-08-28 15:57:45 -0700 | [diff] [blame] | 66 | auto it = std::ranges::find(preferredOrder, parsedType); |
| 67 | if (it != preferredOrder.end()) |
Ed Tanous | 463b293 | 2024-07-16 17:02:42 -0700 | [diff] [blame] | 68 | { |
| 69 | return *it; |
| 70 | } |
| 71 | } |
| 72 | |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 73 | return ContentType::NoMatch; |
| 74 | } |
| 75 | |
Ed Tanous | 4a0e1a0 | 2022-09-21 15:28:04 -0700 | [diff] [blame] | 76 | inline bool isContentTypeAllowed(std::string_view header, ContentType type, |
| 77 | bool allowWildcard) |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 78 | { |
| 79 | auto types = std::to_array({type}); |
Ed Tanous | 8ece0e4 | 2024-01-02 13:16:50 -0800 | [diff] [blame] | 80 | ContentType allowed = getPreferredContentType(header, types); |
Ed Tanous | 4a0e1a0 | 2022-09-21 15:28:04 -0700 | [diff] [blame] | 81 | if (allowed == ContentType::ANY) |
| 82 | { |
| 83 | return allowWildcard; |
| 84 | } |
| 85 | |
| 86 | return type == allowed; |
George Liu | 647b3cd | 2021-07-05 12:43:56 +0800 | [diff] [blame] | 87 | } |
| 88 | |
Ed Tanous | 276ede5 | 2024-08-28 15:57:45 -0700 | [diff] [blame] | 89 | enum class Encoding |
| 90 | { |
| 91 | ParseError, |
| 92 | NoMatch, |
| 93 | UnencodedBytes, |
| 94 | GZIP, |
| 95 | ZSTD, |
| 96 | ANY, // represents *. Never returned. Only used for string matching |
| 97 | }; |
| 98 | |
| 99 | inline Encoding |
| 100 | getPreferredEncoding(std::string_view acceptEncoding, |
| 101 | const std::span<const Encoding> availableEncodings) |
| 102 | { |
| 103 | if (acceptEncoding.empty()) |
| 104 | { |
| 105 | return Encoding::UnencodedBytes; |
| 106 | } |
| 107 | |
| 108 | using boost::spirit::x3::char_; |
| 109 | using boost::spirit::x3::lit; |
| 110 | using boost::spirit::x3::omit; |
| 111 | using boost::spirit::x3::parse; |
| 112 | using boost::spirit::x3::space; |
| 113 | using boost::spirit::x3::symbols; |
| 114 | using boost::spirit::x3::uint_; |
| 115 | |
| 116 | const symbols<Encoding> knownAcceptEncoding{{"gzip", Encoding::GZIP}, |
| 117 | {"zstd", Encoding::ZSTD}, |
| 118 | {"*", Encoding::ANY}}; |
| 119 | |
| 120 | std::vector<Encoding> ct; |
| 121 | |
| 122 | auto parameters = *(lit(';') >> lit("q=") >> uint_ >> -(lit('.') >> uint_)); |
| 123 | auto typeCharset = char_("a-zA-Z.+-"); |
| 124 | auto encodeType = knownAcceptEncoding | omit[+typeCharset]; |
| 125 | auto parser = +(encodeType >> omit[parameters >> -char_(',') >> *space]); |
| 126 | if (!parse(acceptEncoding.begin(), acceptEncoding.end(), parser, ct)) |
| 127 | { |
| 128 | return Encoding::ParseError; |
| 129 | } |
| 130 | |
| 131 | for (const Encoding parsedType : ct) |
| 132 | { |
| 133 | if (parsedType == Encoding::ANY) |
| 134 | { |
| 135 | if (!availableEncodings.empty()) |
| 136 | { |
| 137 | return *availableEncodings.begin(); |
| 138 | } |
| 139 | } |
| 140 | auto it = std::ranges::find(availableEncodings, parsedType); |
| 141 | if (it != availableEncodings.end()) |
| 142 | { |
| 143 | return *it; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | // Fall back to raw bytes if it was allowed |
| 148 | auto it = std::ranges::find(availableEncodings, Encoding::UnencodedBytes); |
| 149 | if (it != availableEncodings.end()) |
| 150 | { |
| 151 | return *it; |
| 152 | } |
| 153 | |
| 154 | return Encoding::NoMatch; |
| 155 | } |
| 156 | |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 157 | } // namespace http_helpers |