blob: 3eef8890387ebcffd4acbf6e040cebbc54dae57e [file] [log] [blame]
Ed Tanous9bd21fc2018-04-26 16:08:56 -07001#pragma once
Tanousf00032d2018-11-05 01:18:10 -03002
Ed Tanous18f8f602023-07-18 10:07:23 -07003#include <algorithm>
Nan Zhoud5c80ad2022-07-11 01:16:31 +00004#include <cctype>
5#include <iomanip>
6#include <ostream>
Ed Tanous3544d2a2023-08-06 18:12:20 -07007#include <ranges>
Ed Tanous99351cd2022-08-07 16:42:51 -07008#include <span>
Nan Zhoud5c80ad2022-07-11 01:16:31 +00009#include <string>
10#include <string_view>
11#include <vector>
12
Ed Tanous1abe55e2018-09-05 08:30:59 -070013namespace http_helpers
14{
George Liu647b3cd2021-07-05 12:43:56 +080015
Ed Tanous99351cd2022-08-07 16:42:51 -070016enum class ContentType
George Liu647b3cd2021-07-05 12:43:56 +080017{
Ed Tanous99351cd2022-08-07 16:42:51 -070018 NoMatch,
Ed Tanous4a0e1a02022-09-21 15:28:04 -070019 ANY, // Accepts: */*
Ed Tanous99351cd2022-08-07 16:42:51 -070020 CBOR,
21 HTML,
22 JSON,
23 OctetStream,
Ed Tanous6fde95f2023-06-01 07:33:34 -070024 EventStream,
Ed Tanous99351cd2022-08-07 16:42:51 -070025};
26
27struct ContentTypePair
28{
29 std::string_view contentTypeString;
30 ContentType contentTypeEnum;
31};
32
Ed Tanous6fde95f2023-06-01 07:33:34 -070033constexpr std::array<ContentTypePair, 5> contentTypes{{
Ed Tanous99351cd2022-08-07 16:42:51 -070034 {"application/cbor", ContentType::CBOR},
35 {"application/json", ContentType::JSON},
36 {"application/octet-stream", ContentType::OctetStream},
37 {"text/html", ContentType::HTML},
Ed Tanous6fde95f2023-06-01 07:33:34 -070038 {"text/event-stream", ContentType::EventStream},
Ed Tanous99351cd2022-08-07 16:42:51 -070039}};
40
Patrick Williamsbd79bce2024-08-16 15:22:20 -040041inline ContentType getPreferredContentType(
42 std::string_view header, std::span<const ContentType> preferedOrder)
Ed Tanous99351cd2022-08-07 16:42:51 -070043{
Ed Tanous99351cd2022-08-07 16:42:51 -070044 size_t lastIndex = 0;
45 while (lastIndex < header.size() + 1)
Ed Tanous1abe55e2018-09-05 08:30:59 -070046 {
Ed Tanousf8fe53e2022-06-30 15:55:45 -070047 size_t index = header.find(',', lastIndex);
Ed Tanous99351cd2022-08-07 16:42:51 -070048 if (index == std::string_view::npos)
Ed Tanous1abe55e2018-09-05 08:30:59 -070049 {
Ed Tanous99351cd2022-08-07 16:42:51 -070050 index = header.size();
Ed Tanous1abe55e2018-09-05 08:30:59 -070051 }
Ed Tanous99351cd2022-08-07 16:42:51 -070052 std::string_view encoding = header.substr(lastIndex, index);
Ed Tanous6b5e77d2018-11-16 14:52:56 -080053
Ed Tanous99351cd2022-08-07 16:42:51 -070054 if (!header.empty())
55 {
56 header.remove_prefix(1);
57 }
58 lastIndex = index + 1;
Gunnar Millsa3526fe2022-02-02 21:56:44 +000059 // ignore any q-factor weighting (;q=)
60 std::size_t separator = encoding.find(";q=");
61
62 if (separator != std::string_view::npos)
63 {
64 encoding = encoding.substr(0, separator);
65 }
Ed Tanous99351cd2022-08-07 16:42:51 -070066 // If the client allows any encoding, given them the first one on the
67 // servers list
68 if (encoding == "*/*")
George Liu647b3cd2021-07-05 12:43:56 +080069 {
Ed Tanous4a0e1a02022-09-21 15:28:04 -070070 return ContentType::ANY;
George Liu647b3cd2021-07-05 12:43:56 +080071 }
Ed Tanous3544d2a2023-08-06 18:12:20 -070072 const auto* knownContentType = std::ranges::find_if(
73 contentTypes, [encoding](const ContentTypePair& pair) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -040074 return pair.contentTypeString == encoding;
75 });
Ed Tanous99351cd2022-08-07 16:42:51 -070076
77 if (knownContentType == contentTypes.end())
78 {
79 // not able to find content type in list
80 continue;
81 }
82
83 // Not one of the types requested
Ed Tanous3544d2a2023-08-06 18:12:20 -070084 if (std::ranges::find(preferedOrder,
85 knownContentType->contentTypeEnum) ==
86 preferedOrder.end())
Ed Tanous99351cd2022-08-07 16:42:51 -070087 {
88 continue;
89 }
90 return knownContentType->contentTypeEnum;
George Liu647b3cd2021-07-05 12:43:56 +080091 }
Ed Tanous99351cd2022-08-07 16:42:51 -070092 return ContentType::NoMatch;
93}
94
Ed Tanous4a0e1a02022-09-21 15:28:04 -070095inline bool isContentTypeAllowed(std::string_view header, ContentType type,
96 bool allowWildcard)
Ed Tanous99351cd2022-08-07 16:42:51 -070097{
98 auto types = std::to_array({type});
Ed Tanous8ece0e42024-01-02 13:16:50 -080099 ContentType allowed = getPreferredContentType(header, types);
Ed Tanous4a0e1a02022-09-21 15:28:04 -0700100 if (allowed == ContentType::ANY)
101 {
102 return allowWildcard;
103 }
104
105 return type == allowed;
George Liu647b3cd2021-07-05 12:43:56 +0800106}
107
Ed Tanous23a21a12020-07-25 04:45:05 +0000108} // namespace http_helpers