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