blob: eb54d0b194ae42b508b48e726870bebfc617a889 [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,
Ed Tanous6fde95f2023-06-01 07:33:34 -070029 EventStream,
Ed Tanous99351cd2022-08-07 16:42:51 -070030};
31
32struct ContentTypePair
33{
34 std::string_view contentTypeString;
35 ContentType contentTypeEnum;
36};
37
Ed Tanous6fde95f2023-06-01 07:33:34 -070038constexpr std::array<ContentTypePair, 5> contentTypes{{
Ed Tanous99351cd2022-08-07 16:42:51 -070039 {"application/cbor", ContentType::CBOR},
40 {"application/json", ContentType::JSON},
41 {"application/octet-stream", ContentType::OctetStream},
42 {"text/html", ContentType::HTML},
Ed Tanous6fde95f2023-06-01 07:33:34 -070043 {"text/event-stream", ContentType::EventStream},
Ed Tanous99351cd2022-08-07 16:42:51 -070044}};
45
Ed Tanoused194be2022-08-07 16:50:11 -070046inline ContentType
47 getPreferedContentType(std::string_view header,
48 std::span<const ContentType> preferedOrder)
Ed Tanous99351cd2022-08-07 16:42:51 -070049{
Ed Tanous99351cd2022-08-07 16:42:51 -070050 size_t lastIndex = 0;
51 while (lastIndex < header.size() + 1)
Ed Tanous1abe55e2018-09-05 08:30:59 -070052 {
Ed Tanousf8fe53e2022-06-30 15:55:45 -070053 size_t index = header.find(',', lastIndex);
Ed Tanous99351cd2022-08-07 16:42:51 -070054 if (index == std::string_view::npos)
Ed Tanous1abe55e2018-09-05 08:30:59 -070055 {
Ed Tanous99351cd2022-08-07 16:42:51 -070056 index = header.size();
Ed Tanous1abe55e2018-09-05 08:30:59 -070057 }
Ed Tanous99351cd2022-08-07 16:42:51 -070058 std::string_view encoding = header.substr(lastIndex, index);
Ed Tanous6b5e77d2018-11-16 14:52:56 -080059
Ed Tanous99351cd2022-08-07 16:42:51 -070060 if (!header.empty())
61 {
62 header.remove_prefix(1);
63 }
64 lastIndex = index + 1;
Gunnar Millsa3526fe2022-02-02 21:56:44 +000065 // ignore any q-factor weighting (;q=)
66 std::size_t separator = encoding.find(";q=");
67
68 if (separator != std::string_view::npos)
69 {
70 encoding = encoding.substr(0, separator);
71 }
Ed Tanous99351cd2022-08-07 16:42:51 -070072 // If the client allows any encoding, given them the first one on the
73 // servers list
74 if (encoding == "*/*")
George Liu647b3cd2021-07-05 12:43:56 +080075 {
Ed Tanous4a0e1a02022-09-21 15:28:04 -070076 return ContentType::ANY;
George Liu647b3cd2021-07-05 12:43:56 +080077 }
Ed Tanous99351cd2022-08-07 16:42:51 -070078 const auto* knownContentType =
79 std::find_if(contentTypes.begin(), contentTypes.end(),
80 [encoding](const ContentTypePair& pair) {
81 return pair.contentTypeString == encoding;
82 });
83
84 if (knownContentType == contentTypes.end())
85 {
86 // not able to find content type in list
87 continue;
88 }
89
90 // Not one of the types requested
91 if (std::find(preferedOrder.begin(), preferedOrder.end(),
92 knownContentType->contentTypeEnum) == preferedOrder.end())
93 {
94 continue;
95 }
96 return knownContentType->contentTypeEnum;
George Liu647b3cd2021-07-05 12:43:56 +080097 }
Ed Tanous99351cd2022-08-07 16:42:51 -070098 return ContentType::NoMatch;
99}
100
Ed Tanous4a0e1a02022-09-21 15:28:04 -0700101inline bool isContentTypeAllowed(std::string_view header, ContentType type,
102 bool allowWildcard)
Ed Tanous99351cd2022-08-07 16:42:51 -0700103{
104 auto types = std::to_array({type});
Ed Tanous4a0e1a02022-09-21 15:28:04 -0700105 ContentType allowed = getPreferedContentType(header, types);
106 if (allowed == ContentType::ANY)
107 {
108 return allowWildcard;
109 }
110
111 return type == allowed;
George Liu647b3cd2021-07-05 12:43:56 +0800112}
113
Ed Tanous23a21a12020-07-25 04:45:05 +0000114} // namespace http_helpers