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, |
| 24 | CBOR, |
| 25 | HTML, |
| 26 | JSON, |
| 27 | OctetStream, |
| 28 | }; |
| 29 | |
| 30 | struct ContentTypePair |
| 31 | { |
| 32 | std::string_view contentTypeString; |
| 33 | ContentType contentTypeEnum; |
| 34 | }; |
| 35 | |
| 36 | constexpr std::array<ContentTypePair, 4> contentTypes{{ |
| 37 | {"application/cbor", ContentType::CBOR}, |
| 38 | {"application/json", ContentType::JSON}, |
| 39 | {"application/octet-stream", ContentType::OctetStream}, |
| 40 | {"text/html", ContentType::HTML}, |
| 41 | }}; |
| 42 | |
| 43 | inline ContentType getPreferedContentType(std::string_view header, |
| 44 | std::span<ContentType> preferedOrder) |
| 45 | { |
| 46 | size_t index = 0; |
| 47 | size_t lastIndex = 0; |
| 48 | while (lastIndex < header.size() + 1) |
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.find(',', lastIndex); |
| 51 | if (index == std::string_view::npos) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 52 | { |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 53 | index = header.size(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 54 | } |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 55 | std::string_view encoding = header.substr(lastIndex, index); |
Ed Tanous | 6b5e77d | 2018-11-16 14:52:56 -0800 | [diff] [blame] | 56 | |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 57 | if (!header.empty()) |
| 58 | { |
| 59 | header.remove_prefix(1); |
| 60 | } |
| 61 | lastIndex = index + 1; |
Gunnar Mills | a3526fe | 2022-02-02 21:56:44 +0000 | [diff] [blame] | 62 | // ignore any q-factor weighting (;q=) |
| 63 | std::size_t separator = encoding.find(";q="); |
| 64 | |
| 65 | if (separator != std::string_view::npos) |
| 66 | { |
| 67 | encoding = encoding.substr(0, separator); |
| 68 | } |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 69 | // If the client allows any encoding, given them the first one on the |
| 70 | // servers list |
| 71 | if (encoding == "*/*") |
George Liu | 647b3cd | 2021-07-05 12:43:56 +0800 | [diff] [blame] | 72 | { |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 73 | if (!preferedOrder.empty()) |
| 74 | { |
| 75 | return preferedOrder[0]; |
| 76 | } |
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 | |
| 101 | inline bool isContentTypeAllowed(std::string_view header, ContentType type) |
| 102 | { |
| 103 | auto types = std::to_array({type}); |
| 104 | return getPreferedContentType(header, types) == type; |
George Liu | 647b3cd | 2021-07-05 12:43:56 +0800 | [diff] [blame] | 105 | } |
| 106 | |
Ed Tanous | 39e7750 | 2019-03-04 17:35:53 -0800 | [diff] [blame] | 107 | inline std::string urlEncode(const std::string_view value) |
Ed Tanous | 6b5e77d | 2018-11-16 14:52:56 -0800 | [diff] [blame] | 108 | { |
| 109 | std::ostringstream escaped; |
| 110 | escaped.fill('0'); |
| 111 | escaped << std::hex; |
| 112 | |
| 113 | for (const char c : value) |
| 114 | { |
| 115 | // Keep alphanumeric and other accepted characters intact |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 116 | if ((isalnum(c) != 0) || c == '-' || c == '_' || c == '.' || c == '~') |
Ed Tanous | 6b5e77d | 2018-11-16 14:52:56 -0800 | [diff] [blame] | 117 | { |
| 118 | escaped << c; |
| 119 | continue; |
| 120 | } |
| 121 | |
| 122 | // Any other characters are percent-encoded |
| 123 | escaped << std::uppercase; |
| 124 | escaped << '%' << std::setw(2) |
| 125 | << static_cast<int>(static_cast<unsigned char>(c)); |
| 126 | escaped << std::nouppercase; |
| 127 | } |
| 128 | |
| 129 | return escaped.str(); |
| 130 | } |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 131 | } // namespace http_helpers |