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> |
Ed Tanous | 11ba397 | 2022-07-11 09:50:41 -0700 | [diff] [blame] | 5 | #include <boost/algorithm/string/split.hpp> |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 6 | #include <boost/iterator/iterator_facade.hpp> |
| 7 | #include <boost/type_index/type_index_facade.hpp> |
| 8 | |
| 9 | #include <cctype> |
| 10 | #include <iomanip> |
| 11 | #include <ostream> |
| 12 | #include <string> |
| 13 | #include <string_view> |
| 14 | #include <vector> |
| 15 | |
| 16 | // IWYU pragma: no_include <ctype.h> |
| 17 | // IWYU pragma: no_include <boost/algorithm/string/detail/classification.hpp> |
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 | inline std::vector<std::string> parseAccept(std::string_view header) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 22 | { |
Ed Tanous | 753d034 | 2021-07-01 07:57:30 -0700 | [diff] [blame] | 23 | std::vector<std::string> encodings; |
| 24 | // chrome currently sends 6 accepts headers, firefox sends 4. |
| 25 | encodings.reserve(6); |
| 26 | boost::split(encodings, header, boost::is_any_of(", "), |
| 27 | boost::token_compress_on); |
George Liu | 647b3cd | 2021-07-05 12:43:56 +0800 | [diff] [blame] | 28 | |
| 29 | return encodings; |
| 30 | } |
| 31 | |
| 32 | inline bool requestPrefersHtml(std::string_view header) |
| 33 | { |
| 34 | for (const std::string& encoding : parseAccept(header)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 35 | { |
Ed Tanous | 753d034 | 2021-07-01 07:57:30 -0700 | [diff] [blame] | 36 | if (encoding == "text/html") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 37 | { |
| 38 | return true; |
| 39 | } |
Ed Tanous | 753d034 | 2021-07-01 07:57:30 -0700 | [diff] [blame] | 40 | if (encoding == "application/json") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 41 | { |
| 42 | return false; |
| 43 | } |
Ed Tanous | 9bd21fc | 2018-04-26 16:08:56 -0700 | [diff] [blame] | 44 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 45 | return false; |
Ed Tanous | 9bd21fc | 2018-04-26 16:08:56 -0700 | [diff] [blame] | 46 | } |
Ed Tanous | 6b5e77d | 2018-11-16 14:52:56 -0800 | [diff] [blame] | 47 | |
George Liu | 647b3cd | 2021-07-05 12:43:56 +0800 | [diff] [blame] | 48 | inline bool isOctetAccepted(std::string_view header) |
| 49 | { |
Gunnar Mills | a3526fe | 2022-02-02 21:56:44 +0000 | [diff] [blame^] | 50 | for (std::string_view encoding : parseAccept(header)) |
George Liu | 647b3cd | 2021-07-05 12:43:56 +0800 | [diff] [blame] | 51 | { |
Gunnar Mills | a3526fe | 2022-02-02 21:56:44 +0000 | [diff] [blame^] | 52 | // ignore any q-factor weighting (;q=) |
| 53 | std::size_t separator = encoding.find(";q="); |
| 54 | |
| 55 | if (separator != std::string_view::npos) |
| 56 | { |
| 57 | encoding = encoding.substr(0, separator); |
| 58 | } |
George Liu | 647b3cd | 2021-07-05 12:43:56 +0800 | [diff] [blame] | 59 | if (encoding == "*/*" || encoding == "application/octet-stream") |
| 60 | { |
| 61 | return true; |
| 62 | } |
| 63 | } |
| 64 | return false; |
| 65 | } |
| 66 | |
Ed Tanous | 39e7750 | 2019-03-04 17:35:53 -0800 | [diff] [blame] | 67 | inline std::string urlEncode(const std::string_view value) |
Ed Tanous | 6b5e77d | 2018-11-16 14:52:56 -0800 | [diff] [blame] | 68 | { |
| 69 | std::ostringstream escaped; |
| 70 | escaped.fill('0'); |
| 71 | escaped << std::hex; |
| 72 | |
| 73 | for (const char c : value) |
| 74 | { |
| 75 | // Keep alphanumeric and other accepted characters intact |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 76 | if ((isalnum(c) != 0) || c == '-' || c == '_' || c == '.' || c == '~') |
Ed Tanous | 6b5e77d | 2018-11-16 14:52:56 -0800 | [diff] [blame] | 77 | { |
| 78 | escaped << c; |
| 79 | continue; |
| 80 | } |
| 81 | |
| 82 | // Any other characters are percent-encoded |
| 83 | escaped << std::uppercase; |
| 84 | escaped << '%' << std::setw(2) |
| 85 | << static_cast<int>(static_cast<unsigned char>(c)); |
| 86 | escaped << std::nouppercase; |
| 87 | } |
| 88 | |
| 89 | return escaped.str(); |
| 90 | } |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 91 | } // namespace http_helpers |