blob: e1a8e2a9eaef3c811f607d7a4327d4e426a33442 [file] [log] [blame]
Ed Tanous9bd21fc2018-04-26 16:08:56 -07001#pragma once
Tanousf00032d2018-11-05 01:18:10 -03002
Ed Tanous11ba3972022-07-11 09:50:41 -07003#include <boost/algorithm/string/classification.hpp>
Nan Zhoud5c80ad2022-07-11 01:16:31 +00004#include <boost/algorithm/string/constants.hpp>
Nan Zhoud5c80ad2022-07-11 01:16:31 +00005#include <boost/iterator/iterator_facade.hpp>
6#include <boost/type_index/type_index_facade.hpp>
7
8#include <cctype>
9#include <iomanip>
10#include <ostream>
Ed Tanous99351cd2022-08-07 16:42:51 -070011#include <span>
Nan Zhoud5c80ad2022-07-11 01:16:31 +000012#include <string>
13#include <string_view>
14#include <vector>
15
16// IWYU pragma: no_include <ctype.h>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050017
Ed Tanous1abe55e2018-09-05 08:30:59 -070018namespace http_helpers
19{
George Liu647b3cd2021-07-05 12:43:56 +080020
Ed Tanous99351cd2022-08-07 16:42:51 -070021enum class ContentType
George Liu647b3cd2021-07-05 12:43:56 +080022{
Ed Tanous99351cd2022-08-07 16:42:51 -070023 NoMatch,
Ed Tanous4a0e1a02022-09-21 15:28:04 -070024 ANY, // Accepts: */*
Ed Tanous99351cd2022-08-07 16:42:51 -070025 CBOR,
26 HTML,
27 JSON,
28 OctetStream,
29};
30
31struct ContentTypePair
32{
33 std::string_view contentTypeString;
34 ContentType contentTypeEnum;
35};
36
37constexpr std::array<ContentTypePair, 4> contentTypes{{
38 {"application/cbor", ContentType::CBOR},
39 {"application/json", ContentType::JSON},
40 {"application/octet-stream", ContentType::OctetStream},
41 {"text/html", ContentType::HTML},
42}};
43
44inline ContentType getPreferedContentType(std::string_view header,
45 std::span<ContentType> preferedOrder)
46{
Ed Tanous99351cd2022-08-07 16:42:51 -070047 size_t lastIndex = 0;
48 while (lastIndex < header.size() + 1)
Ed Tanous1abe55e2018-09-05 08:30:59 -070049 {
Ed Tanousf8fe53e2022-06-30 15:55:45 -070050 size_t index = header.find(',', lastIndex);
Ed Tanous99351cd2022-08-07 16:42:51 -070051 if (index == std::string_view::npos)
Ed Tanous1abe55e2018-09-05 08:30:59 -070052 {
Ed Tanous99351cd2022-08-07 16:42:51 -070053 index = header.size();
Ed Tanous1abe55e2018-09-05 08:30:59 -070054 }
Ed Tanous99351cd2022-08-07 16:42:51 -070055 std::string_view encoding = header.substr(lastIndex, index);
Ed Tanous6b5e77d2018-11-16 14:52:56 -080056
Ed Tanous99351cd2022-08-07 16:42:51 -070057 if (!header.empty())
58 {
59 header.remove_prefix(1);
60 }
61 lastIndex = index + 1;
Gunnar Millsa3526fe2022-02-02 21:56:44 +000062 // ignore any q-factor weighting (;q=)
63 std::size_t separator = encoding.find(";q=");
64
65 if (separator != std::string_view::npos)
66 {
67 encoding = encoding.substr(0, separator);
68 }
Ed Tanous99351cd2022-08-07 16:42:51 -070069 // If the client allows any encoding, given them the first one on the
70 // servers list
71 if (encoding == "*/*")
George Liu647b3cd2021-07-05 12:43:56 +080072 {
Ed Tanous4a0e1a02022-09-21 15:28:04 -070073 return ContentType::ANY;
George Liu647b3cd2021-07-05 12:43:56 +080074 }
Ed Tanous99351cd2022-08-07 16:42:51 -070075 const auto* knownContentType =
76 std::find_if(contentTypes.begin(), contentTypes.end(),
77 [encoding](const ContentTypePair& pair) {
78 return pair.contentTypeString == encoding;
79 });
80
81 if (knownContentType == contentTypes.end())
82 {
83 // not able to find content type in list
84 continue;
85 }
86
87 // Not one of the types requested
88 if (std::find(preferedOrder.begin(), preferedOrder.end(),
89 knownContentType->contentTypeEnum) == preferedOrder.end())
90 {
91 continue;
92 }
93 return knownContentType->contentTypeEnum;
George Liu647b3cd2021-07-05 12:43:56 +080094 }
Ed Tanous99351cd2022-08-07 16:42:51 -070095 return ContentType::NoMatch;
96}
97
Ed Tanous4a0e1a02022-09-21 15:28:04 -070098inline bool isContentTypeAllowed(std::string_view header, ContentType type,
99 bool allowWildcard)
Ed Tanous99351cd2022-08-07 16:42:51 -0700100{
101 auto types = std::to_array({type});
Ed Tanous4a0e1a02022-09-21 15:28:04 -0700102 ContentType allowed = getPreferedContentType(header, types);
103 if (allowed == ContentType::ANY)
104 {
105 return allowWildcard;
106 }
107
108 return type == allowed;
George Liu647b3cd2021-07-05 12:43:56 +0800109}
110
Ed Tanous39e77502019-03-04 17:35:53 -0800111inline std::string urlEncode(const std::string_view value)
Ed Tanous6b5e77d2018-11-16 14:52:56 -0800112{
113 std::ostringstream escaped;
114 escaped.fill('0');
115 escaped << std::hex;
116
117 for (const char c : value)
118 {
119 // Keep alphanumeric and other accepted characters intact
Ed Tanouse662eae2022-01-25 10:39:19 -0800120 if ((isalnum(c) != 0) || c == '-' || c == '_' || c == '.' || c == '~')
Ed Tanous6b5e77d2018-11-16 14:52:56 -0800121 {
122 escaped << c;
123 continue;
124 }
125
126 // Any other characters are percent-encoded
127 escaped << std::uppercase;
128 escaped << '%' << std::setw(2)
129 << static_cast<int>(static_cast<unsigned char>(c));
130 escaped << std::nouppercase;
131 }
132
133 return escaped.str();
134}
Ed Tanous23a21a12020-07-25 04:45:05 +0000135} // namespace http_helpers