Ed Tanous | 9bd21fc | 2018-04-26 16:08:56 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | #include <boost/algorithm/string.hpp> |
| 3 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 4 | namespace http_helpers |
| 5 | { |
| 6 | inline bool requestPrefersHtml(const crow::Request& req) |
| 7 | { |
| 8 | boost::string_view header = req.getHeaderValue("accept"); |
| 9 | std::vector<std::string> encodings; |
| 10 | // chrome currently sends 6 accepts headers, firefox sends 4. |
| 11 | encodings.reserve(6); |
| 12 | boost::split(encodings, header, boost::is_any_of(", "), |
| 13 | boost::token_compress_on); |
| 14 | for (const std::string& encoding : encodings) |
| 15 | { |
| 16 | if (encoding == "text/html") |
| 17 | { |
| 18 | return true; |
| 19 | } |
| 20 | else if (encoding == "application/json") |
| 21 | { |
| 22 | return false; |
| 23 | } |
Ed Tanous | 9bd21fc | 2018-04-26 16:08:56 -0700 | [diff] [blame] | 24 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 25 | return false; |
Ed Tanous | 9bd21fc | 2018-04-26 16:08:56 -0700 | [diff] [blame] | 26 | } |
Ed Tanous | 6b5e77d | 2018-11-16 14:52:56 -0800 | [diff] [blame] | 27 | |
| 28 | inline std::string urlEncode(const boost::string_view value) |
| 29 | { |
| 30 | std::ostringstream escaped; |
| 31 | escaped.fill('0'); |
| 32 | escaped << std::hex; |
| 33 | |
| 34 | for (const char c : value) |
| 35 | { |
| 36 | // Keep alphanumeric and other accepted characters intact |
| 37 | if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') |
| 38 | { |
| 39 | escaped << c; |
| 40 | continue; |
| 41 | } |
| 42 | |
| 43 | // Any other characters are percent-encoded |
| 44 | escaped << std::uppercase; |
| 45 | escaped << '%' << std::setw(2) |
| 46 | << static_cast<int>(static_cast<unsigned char>(c)); |
| 47 | escaped << std::nouppercase; |
| 48 | } |
| 49 | |
| 50 | return escaped.str(); |
| 51 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 52 | } // namespace http_helpers |