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 | 18f8f60 | 2023-07-18 10:07:23 -0700 | [diff] [blame] | 3 | #include <algorithm> |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 4 | #include <cctype> |
| 5 | #include <iomanip> |
| 6 | #include <ostream> |
Ed Tanous | 3544d2a | 2023-08-06 18:12:20 -0700 | [diff] [blame] | 7 | #include <ranges> |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 8 | #include <span> |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <string_view> |
| 11 | #include <vector> |
| 12 | |
| 13 | // IWYU pragma: no_include <ctype.h> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 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 | |
| 29 | struct ContentTypePair |
| 30 | { |
| 31 | std::string_view contentTypeString; |
| 32 | ContentType contentTypeEnum; |
| 33 | }; |
| 34 | |
Ed Tanous | 6fde95f | 2023-06-01 07:33:34 -0700 | [diff] [blame] | 35 | constexpr std::array<ContentTypePair, 5> contentTypes{{ |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 36 | {"application/cbor", ContentType::CBOR}, |
| 37 | {"application/json", ContentType::JSON}, |
| 38 | {"application/octet-stream", ContentType::OctetStream}, |
| 39 | {"text/html", ContentType::HTML}, |
Ed Tanous | 6fde95f | 2023-06-01 07:33:34 -0700 | [diff] [blame] | 40 | {"text/event-stream", ContentType::EventStream}, |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 41 | }}; |
| 42 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame^] | 43 | inline ContentType getPreferredContentType( |
| 44 | std::string_view header, std::span<const ContentType> preferedOrder) |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 45 | { |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 46 | size_t lastIndex = 0; |
| 47 | while (lastIndex < header.size() + 1) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 48 | { |
Ed Tanous | f8fe53e | 2022-06-30 15:55:45 -0700 | [diff] [blame] | 49 | size_t index = header.find(',', lastIndex); |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 50 | if (index == std::string_view::npos) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 51 | { |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 52 | index = header.size(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 53 | } |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 54 | std::string_view encoding = header.substr(lastIndex, index); |
Ed Tanous | 6b5e77d | 2018-11-16 14:52:56 -0800 | [diff] [blame] | 55 | |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 56 | if (!header.empty()) |
| 57 | { |
| 58 | header.remove_prefix(1); |
| 59 | } |
| 60 | lastIndex = index + 1; |
Gunnar Mills | a3526fe | 2022-02-02 21:56:44 +0000 | [diff] [blame] | 61 | // ignore any q-factor weighting (;q=) |
| 62 | std::size_t separator = encoding.find(";q="); |
| 63 | |
| 64 | if (separator != std::string_view::npos) |
| 65 | { |
| 66 | encoding = encoding.substr(0, separator); |
| 67 | } |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 68 | // If the client allows any encoding, given them the first one on the |
| 69 | // servers list |
| 70 | if (encoding == "*/*") |
George Liu | 647b3cd | 2021-07-05 12:43:56 +0800 | [diff] [blame] | 71 | { |
Ed Tanous | 4a0e1a0 | 2022-09-21 15:28:04 -0700 | [diff] [blame] | 72 | return ContentType::ANY; |
George Liu | 647b3cd | 2021-07-05 12:43:56 +0800 | [diff] [blame] | 73 | } |
Ed Tanous | 3544d2a | 2023-08-06 18:12:20 -0700 | [diff] [blame] | 74 | const auto* knownContentType = std::ranges::find_if( |
| 75 | contentTypes, [encoding](const ContentTypePair& pair) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame^] | 76 | return pair.contentTypeString == encoding; |
| 77 | }); |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 78 | |
| 79 | if (knownContentType == contentTypes.end()) |
| 80 | { |
| 81 | // not able to find content type in list |
| 82 | continue; |
| 83 | } |
| 84 | |
| 85 | // Not one of the types requested |
Ed Tanous | 3544d2a | 2023-08-06 18:12:20 -0700 | [diff] [blame] | 86 | if (std::ranges::find(preferedOrder, |
| 87 | knownContentType->contentTypeEnum) == |
| 88 | preferedOrder.end()) |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 89 | { |
| 90 | continue; |
| 91 | } |
| 92 | return knownContentType->contentTypeEnum; |
George Liu | 647b3cd | 2021-07-05 12:43:56 +0800 | [diff] [blame] | 93 | } |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 94 | return ContentType::NoMatch; |
| 95 | } |
| 96 | |
Ed Tanous | 4a0e1a0 | 2022-09-21 15:28:04 -0700 | [diff] [blame] | 97 | inline bool isContentTypeAllowed(std::string_view header, ContentType type, |
| 98 | bool allowWildcard) |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 99 | { |
| 100 | auto types = std::to_array({type}); |
Ed Tanous | 8ece0e4 | 2024-01-02 13:16:50 -0800 | [diff] [blame] | 101 | ContentType allowed = getPreferredContentType(header, types); |
Ed Tanous | 4a0e1a0 | 2022-09-21 15:28:04 -0700 | [diff] [blame] | 102 | if (allowed == ContentType::ANY) |
| 103 | { |
| 104 | return allowWildcard; |
| 105 | } |
| 106 | |
| 107 | return type == allowed; |
George Liu | 647b3cd | 2021-07-05 12:43:56 +0800 | [diff] [blame] | 108 | } |
| 109 | |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 110 | } // namespace http_helpers |