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 | { |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 8 | inline bool requestPrefersHtml(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); |
| 15 | for (const std::string& encoding : encodings) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 16 | { |
Ed Tanous | 753d034 | 2021-07-01 07:57:30 -0700 | [diff] [blame] | 17 | if (encoding == "text/html") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 18 | { |
| 19 | return true; |
| 20 | } |
Ed Tanous | 753d034 | 2021-07-01 07:57:30 -0700 | [diff] [blame] | 21 | if (encoding == "application/json") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 22 | { |
| 23 | return false; |
| 24 | } |
Ed Tanous | 9bd21fc | 2018-04-26 16:08:56 -0700 | [diff] [blame] | 25 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 26 | return false; |
Ed Tanous | 9bd21fc | 2018-04-26 16:08:56 -0700 | [diff] [blame] | 27 | } |
Ed Tanous | 6b5e77d | 2018-11-16 14:52:56 -0800 | [diff] [blame] | 28 | |
Ed Tanous | 39e7750 | 2019-03-04 17:35:53 -0800 | [diff] [blame] | 29 | inline std::string urlEncode(const std::string_view value) |
Ed Tanous | 6b5e77d | 2018-11-16 14:52:56 -0800 | [diff] [blame] | 30 | { |
| 31 | std::ostringstream escaped; |
| 32 | escaped.fill('0'); |
| 33 | escaped << std::hex; |
| 34 | |
| 35 | for (const char c : value) |
| 36 | { |
| 37 | // Keep alphanumeric and other accepted characters intact |
| 38 | if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') |
| 39 | { |
| 40 | escaped << c; |
| 41 | continue; |
| 42 | } |
| 43 | |
| 44 | // Any other characters are percent-encoded |
| 45 | escaped << std::uppercase; |
| 46 | escaped << '%' << std::setw(2) |
| 47 | << static_cast<int>(static_cast<unsigned char>(c)); |
| 48 | escaped << std::nouppercase; |
| 49 | } |
| 50 | |
| 51 | return escaped.str(); |
| 52 | } |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 53 | } // namespace http_helpers |