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