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