blob: 74ab15a86e1d4b27f9d5142e05dae2dc9d630981 [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
13// IWYU pragma: no_include <ctype.h>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050014
Ed Tanous1abe55e2018-09-05 08:30:59 -070015namespace http_helpers
16{
George Liu647b3cd2021-07-05 12:43:56 +080017
Ed Tanous99351cd2022-08-07 16:42:51 -070018enum class ContentType
George Liu647b3cd2021-07-05 12:43:56 +080019{
Ed Tanous99351cd2022-08-07 16:42:51 -070020 NoMatch,
Ed Tanous4a0e1a02022-09-21 15:28:04 -070021 ANY, // Accepts: */*
Ed Tanous99351cd2022-08-07 16:42:51 -070022 CBOR,
23 HTML,
24 JSON,
25 OctetStream,
Ed Tanous6fde95f2023-06-01 07:33:34 -070026 EventStream,
Ed Tanous99351cd2022-08-07 16:42:51 -070027};
28
29struct ContentTypePair
30{
31 std::string_view contentTypeString;
32 ContentType contentTypeEnum;
33};
34
Ed Tanous6fde95f2023-06-01 07:33:34 -070035constexpr std::array<ContentTypePair, 5> contentTypes{{
Ed Tanous99351cd2022-08-07 16:42:51 -070036 {"application/cbor", ContentType::CBOR},
37 {"application/json", ContentType::JSON},
38 {"application/octet-stream", ContentType::OctetStream},
39 {"text/html", ContentType::HTML},
Ed Tanous6fde95f2023-06-01 07:33:34 -070040 {"text/event-stream", ContentType::EventStream},
Ed Tanous99351cd2022-08-07 16:42:51 -070041}};
42
Ed Tanoused194be2022-08-07 16:50:11 -070043inline ContentType
Ed Tanous8ece0e42024-01-02 13:16:50 -080044 getPreferredContentType(std::string_view header,
45 std::span<const ContentType> preferedOrder)
Ed Tanous99351cd2022-08-07 16:42:51 -070046{
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 Tanous3544d2a2023-08-06 18:12:20 -070075 const auto* knownContentType = std::ranges::find_if(
76 contentTypes, [encoding](const ContentTypePair& pair) {
Patrick Williams5a39f772023-10-20 11:20:21 -050077 return pair.contentTypeString == encoding;
78 });
Ed Tanous99351cd2022-08-07 16:42:51 -070079
80 if (knownContentType == contentTypes.end())
81 {
82 // not able to find content type in list
83 continue;
84 }
85
86 // Not one of the types requested
Ed Tanous3544d2a2023-08-06 18:12:20 -070087 if (std::ranges::find(preferedOrder,
88 knownContentType->contentTypeEnum) ==
89 preferedOrder.end())
Ed Tanous99351cd2022-08-07 16:42:51 -070090 {
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 Tanous8ece0e42024-01-02 13:16:50 -0800102 ContentType allowed = getPreferredContentType(header, types);
Ed Tanous4a0e1a02022-09-21 15:28:04 -0700103 if (allowed == ContentType::ANY)
104 {
105 return allowWildcard;
106 }
107
108 return type == allowed;
George Liu647b3cd2021-07-05 12:43:56 +0800109}
110
Ed Tanous23a21a12020-07-25 04:45:05 +0000111} // namespace http_helpers