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 | { |
| 50 | for (const std::string& encoding : parseAccept(header)) |
| 51 | { |
| 52 | if (encoding == "*/*" || encoding == "application/octet-stream") |
| 53 | { |
| 54 | return true; |
| 55 | } |
| 56 | } |
| 57 | return false; |
| 58 | } |
| 59 | |
Ed Tanous | 39e7750 | 2019-03-04 17:35:53 -0800 | [diff] [blame] | 60 | inline std::string urlEncode(const std::string_view value) |
Ed Tanous | 6b5e77d | 2018-11-16 14:52:56 -0800 | [diff] [blame] | 61 | { |
| 62 | std::ostringstream escaped; |
| 63 | escaped.fill('0'); |
| 64 | escaped << std::hex; |
| 65 | |
| 66 | for (const char c : value) |
| 67 | { |
| 68 | // Keep alphanumeric and other accepted characters intact |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 69 | if ((isalnum(c) != 0) || c == '-' || c == '_' || c == '.' || c == '~') |
Ed Tanous | 6b5e77d | 2018-11-16 14:52:56 -0800 | [diff] [blame] | 70 | { |
| 71 | escaped << c; |
| 72 | continue; |
| 73 | } |
| 74 | |
| 75 | // Any other characters are percent-encoded |
| 76 | escaped << std::uppercase; |
| 77 | escaped << '%' << std::setw(2) |
| 78 | << static_cast<int>(static_cast<unsigned char>(c)); |
| 79 | escaped << std::nouppercase; |
| 80 | } |
| 81 | |
| 82 | return escaped.str(); |
| 83 | } |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 84 | } // namespace http_helpers |