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