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, |
| 29 | }; |
| 30 | |
| 31 | struct ContentTypePair |
| 32 | { |
| 33 | std::string_view contentTypeString; |
| 34 | ContentType contentTypeEnum; |
| 35 | }; |
| 36 | |
| 37 | constexpr std::array<ContentTypePair, 4> contentTypes{{ |
| 38 | {"application/cbor", ContentType::CBOR}, |
| 39 | {"application/json", ContentType::JSON}, |
| 40 | {"application/octet-stream", ContentType::OctetStream}, |
| 41 | {"text/html", ContentType::HTML}, |
| 42 | }}; |
| 43 | |
Ed Tanous | ed194be | 2022-08-07 16:50:11 -0700 | [diff] [blame] | 44 | inline ContentType |
| 45 | getPreferedContentType(std::string_view header, |
| 46 | std::span<const ContentType> preferedOrder) |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 47 | { |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 48 | size_t lastIndex = 0; |
| 49 | while (lastIndex < header.size() + 1) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 50 | { |
Ed Tanous | f8fe53e | 2022-06-30 15:55:45 -0700 | [diff] [blame] | 51 | size_t index = header.find(',', lastIndex); |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 52 | if (index == std::string_view::npos) |
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 | index = header.size(); |
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 | std::string_view encoding = header.substr(lastIndex, index); |
Ed Tanous | 6b5e77d | 2018-11-16 14:52:56 -0800 | [diff] [blame] | 57 | |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 58 | if (!header.empty()) |
| 59 | { |
| 60 | header.remove_prefix(1); |
| 61 | } |
| 62 | lastIndex = index + 1; |
Gunnar Mills | a3526fe | 2022-02-02 21:56:44 +0000 | [diff] [blame] | 63 | // ignore any q-factor weighting (;q=) |
| 64 | std::size_t separator = encoding.find(";q="); |
| 65 | |
| 66 | if (separator != std::string_view::npos) |
| 67 | { |
| 68 | encoding = encoding.substr(0, separator); |
| 69 | } |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 70 | // If the client allows any encoding, given them the first one on the |
| 71 | // servers list |
| 72 | if (encoding == "*/*") |
George Liu | 647b3cd | 2021-07-05 12:43:56 +0800 | [diff] [blame] | 73 | { |
Ed Tanous | 4a0e1a0 | 2022-09-21 15:28:04 -0700 | [diff] [blame] | 74 | return ContentType::ANY; |
George Liu | 647b3cd | 2021-07-05 12:43:56 +0800 | [diff] [blame] | 75 | } |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 76 | const auto* knownContentType = |
| 77 | std::find_if(contentTypes.begin(), contentTypes.end(), |
| 78 | [encoding](const ContentTypePair& pair) { |
| 79 | return pair.contentTypeString == encoding; |
| 80 | }); |
| 81 | |
| 82 | if (knownContentType == contentTypes.end()) |
| 83 | { |
| 84 | // not able to find content type in list |
| 85 | continue; |
| 86 | } |
| 87 | |
| 88 | // Not one of the types requested |
| 89 | if (std::find(preferedOrder.begin(), preferedOrder.end(), |
| 90 | knownContentType->contentTypeEnum) == preferedOrder.end()) |
| 91 | { |
| 92 | continue; |
| 93 | } |
| 94 | return knownContentType->contentTypeEnum; |
George Liu | 647b3cd | 2021-07-05 12:43:56 +0800 | [diff] [blame] | 95 | } |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 96 | return ContentType::NoMatch; |
| 97 | } |
| 98 | |
Ed Tanous | 4a0e1a0 | 2022-09-21 15:28:04 -0700 | [diff] [blame] | 99 | inline bool isContentTypeAllowed(std::string_view header, ContentType type, |
| 100 | bool allowWildcard) |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 101 | { |
| 102 | auto types = std::to_array({type}); |
Ed Tanous | 4a0e1a0 | 2022-09-21 15:28:04 -0700 | [diff] [blame] | 103 | ContentType allowed = getPreferedContentType(header, types); |
| 104 | if (allowed == ContentType::ANY) |
| 105 | { |
| 106 | return allowWildcard; |
| 107 | } |
| 108 | |
| 109 | return type == allowed; |
George Liu | 647b3cd | 2021-07-05 12:43:56 +0800 | [diff] [blame] | 110 | } |
| 111 | |
Ed Tanous | 26ccae3 | 2023-02-16 10:28:44 -0800 | [diff] [blame] | 112 | inline std::string urlEncode(std::string_view value) |
Ed Tanous | 6b5e77d | 2018-11-16 14:52:56 -0800 | [diff] [blame] | 113 | { |
| 114 | std::ostringstream escaped; |
| 115 | escaped.fill('0'); |
| 116 | escaped << std::hex; |
| 117 | |
| 118 | for (const char c : value) |
| 119 | { |
| 120 | // Keep alphanumeric and other accepted characters intact |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 121 | if ((isalnum(c) != 0) || c == '-' || c == '_' || c == '.' || c == '~') |
Ed Tanous | 6b5e77d | 2018-11-16 14:52:56 -0800 | [diff] [blame] | 122 | { |
| 123 | escaped << c; |
| 124 | continue; |
| 125 | } |
| 126 | |
| 127 | // Any other characters are percent-encoded |
| 128 | escaped << std::uppercase; |
| 129 | escaped << '%' << std::setw(2) |
| 130 | << static_cast<int>(static_cast<unsigned char>(c)); |
| 131 | escaped << std::nouppercase; |
| 132 | } |
| 133 | |
| 134 | return escaped.str(); |
| 135 | } |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 136 | } // namespace http_helpers |