Ed Tanous | 9bd21fc | 2018-04-26 16:08:56 -0700 | [diff] [blame] | 1 | #pragma once |
Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 2 | #include "http_request.hpp" |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 3 | |
Ed Tanous | 11ba397 | 2022-07-11 09:50:41 -0700 | [diff] [blame^] | 4 | #include <boost/algorithm/string/classification.hpp> |
| 5 | #include <boost/algorithm/string/split.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 6 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 7 | namespace http_helpers |
| 8 | { |
George Liu | 647b3cd | 2021-07-05 12:43:56 +0800 | [diff] [blame] | 9 | inline std::vector<std::string> parseAccept(std::string_view header) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 10 | { |
Ed Tanous | 753d034 | 2021-07-01 07:57:30 -0700 | [diff] [blame] | 11 | std::vector<std::string> encodings; |
| 12 | // chrome currently sends 6 accepts headers, firefox sends 4. |
| 13 | encodings.reserve(6); |
| 14 | boost::split(encodings, header, boost::is_any_of(", "), |
| 15 | boost::token_compress_on); |
George Liu | 647b3cd | 2021-07-05 12:43:56 +0800 | [diff] [blame] | 16 | |
| 17 | return encodings; |
| 18 | } |
| 19 | |
| 20 | inline bool requestPrefersHtml(std::string_view header) |
| 21 | { |
| 22 | for (const std::string& encoding : parseAccept(header)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 23 | { |
Ed Tanous | 753d034 | 2021-07-01 07:57:30 -0700 | [diff] [blame] | 24 | if (encoding == "text/html") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 25 | { |
| 26 | return true; |
| 27 | } |
Ed Tanous | 753d034 | 2021-07-01 07:57:30 -0700 | [diff] [blame] | 28 | if (encoding == "application/json") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 29 | { |
| 30 | return false; |
| 31 | } |
Ed Tanous | 9bd21fc | 2018-04-26 16:08:56 -0700 | [diff] [blame] | 32 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 33 | return false; |
Ed Tanous | 9bd21fc | 2018-04-26 16:08:56 -0700 | [diff] [blame] | 34 | } |
Ed Tanous | 6b5e77d | 2018-11-16 14:52:56 -0800 | [diff] [blame] | 35 | |
George Liu | 647b3cd | 2021-07-05 12:43:56 +0800 | [diff] [blame] | 36 | inline bool isOctetAccepted(std::string_view header) |
| 37 | { |
| 38 | for (const std::string& encoding : parseAccept(header)) |
| 39 | { |
| 40 | if (encoding == "*/*" || encoding == "application/octet-stream") |
| 41 | { |
| 42 | return true; |
| 43 | } |
| 44 | } |
| 45 | return false; |
| 46 | } |
| 47 | |
Ed Tanous | 39e7750 | 2019-03-04 17:35:53 -0800 | [diff] [blame] | 48 | inline std::string urlEncode(const std::string_view value) |
Ed Tanous | 6b5e77d | 2018-11-16 14:52:56 -0800 | [diff] [blame] | 49 | { |
| 50 | std::ostringstream escaped; |
| 51 | escaped.fill('0'); |
| 52 | escaped << std::hex; |
| 53 | |
| 54 | for (const char c : value) |
| 55 | { |
| 56 | // Keep alphanumeric and other accepted characters intact |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 57 | if ((isalnum(c) != 0) || c == '-' || c == '_' || c == '.' || c == '~') |
Ed Tanous | 6b5e77d | 2018-11-16 14:52:56 -0800 | [diff] [blame] | 58 | { |
| 59 | escaped << c; |
| 60 | continue; |
| 61 | } |
| 62 | |
| 63 | // Any other characters are percent-encoded |
| 64 | escaped << std::uppercase; |
| 65 | escaped << '%' << std::setw(2) |
| 66 | << static_cast<int>(static_cast<unsigned char>(c)); |
| 67 | escaped << std::nouppercase; |
| 68 | } |
| 69 | |
| 70 | return escaped.str(); |
| 71 | } |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 72 | } // namespace http_helpers |