CCF
Loading...
Searching...
No Matches
http_etag.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the Apache 2.0 License.
3#pragma once
4
5#define FMT_HEADER_ONLY
6#include <regex>
7#include <set>
8#include <string>
9
10namespace ccf::http
11{
15 class Matcher
16 {
17 private:
19 bool any_value = false;
21 std::set<std::string> if_etags;
22
23 public:
28 Matcher(const std::string& match_header)
29 {
30 if (match_header == "*")
31 {
32 any_value = true;
33 return;
34 }
35
36 std::regex etag_rx(R"(\"([0-9a-f]+)\",?\s*)");
37 auto etags_begin =
38 std::sregex_iterator(match_header.begin(), match_header.end(), etag_rx);
39 auto etags_end = std::sregex_iterator();
40 ssize_t last_matched = 0;
41
42 for (std::sregex_iterator i = etags_begin; i != etags_end; ++i)
43 {
44 if (i->position() != last_matched)
45 {
46 throw std::runtime_error("Invalid If-Match header");
47 }
48 const std::smatch& match = *i;
49 if_etags.insert(match[1].str());
50 last_matched = match.position() + match.length();
51 }
52
53 ssize_t last_index_in_header = match_header.size();
54
55 if (last_matched != last_index_in_header || if_etags.empty())
56 {
57 throw std::runtime_error("Invalid If-Match header");
58 }
59 }
60
62 [[nodiscard]] bool matches(const std::string& etag) const
63 {
64 return any_value || if_etags.contains(etag);
65 }
66
68 [[nodiscard]] bool is_any() const
69 {
70 return any_value;
71 }
72 };
73}
Definition http_etag.h:16
bool matches(const std::string &etag) const
Check if a given ETag matches the If-Match/If-None-Match header.
Definition http_etag.h:62
Matcher(const std::string &match_header)
Definition http_etag.h:28
bool is_any() const
Check if the header will match any ETag (*)
Definition http_etag.h:68
Definition http_accept.h:13