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
11{
12 namespace http
13 {
17 class Matcher
18 {
19 private:
21 bool any_value = false;
23 std::set<std::string> if_etags;
24
25 public:
30 Matcher(const std::string& match_header)
31 {
32 if (match_header == "*")
33 {
34 any_value = true;
35 return;
36 }
37
38 std::regex etag_rx("\\\"([0-9a-f]+)\\\",?\\s*");
39 auto etags_begin = std::sregex_iterator(
40 match_header.begin(), match_header.end(), etag_rx);
41 auto etags_end = std::sregex_iterator();
42 ssize_t last_matched = 0;
43
44 for (std::sregex_iterator i = etags_begin; i != etags_end; ++i)
45 {
46 if (i->position() != last_matched)
47 {
48 throw std::runtime_error("Invalid If-Match header");
49 }
50 std::smatch match = *i;
51 if_etags.insert(match[1].str());
52 last_matched = match.position() + match.length();
53 }
54
55 ssize_t last_index_in_header = match_header.size();
56
57 if (last_matched != last_index_in_header || if_etags.empty())
58 {
59 throw std::runtime_error("Invalid If-Match header");
60 }
61 }
62
64 bool matches(const std::string& etag) const
65 {
66 return any_value || if_etags.contains(etag);
67 }
68
70 bool is_any() const
71 {
72 return any_value;
73 }
74 };
75 }
76}
Definition http_etag.h:18
bool matches(const std::string &etag) const
Check if a given ETag matches the If-Match/If-None-Match header.
Definition http_etag.h:64
Matcher(const std::string &match_header)
Definition http_etag.h:30
bool is_any() const
Check if the header will match any ETag (*)
Definition http_etag.h:70
Definition app_interface.h:14
Definition error_reporter.h:6