CCF
Loading...
Searching...
No Matches
rest_verb.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#include "ccf/ds/hash.h"
6
7#include <llhttp/llhttp.h>
8#include <string>
9
10namespace ccf
11{
12 static inline llhttp_method http_method_from_str(const std::string_view& s)
13 {
14 const auto hashed_name = ccf::ds::fnv_1a<size_t>(s);
15
16#define XX(num, name, string) \
17 case (ccf::ds::fnv_1a<size_t>(#string)): \
18 { \
19 return llhttp_method(num); \
20 }
21
22 switch (hashed_name)
23 {
24 HTTP_METHOD_MAP(XX)
25
26 default:
27 {
28 throw std::logic_error(fmt::format("Unknown HTTP method '{}'", s));
29 }
30 }
31#undef XX
32 }
33
45 {
46 private:
47 int verb;
48
49 public:
50 RESTVerb() : verb(std::numeric_limits<int>::min()) {}
51 RESTVerb(const llhttp_method& hm) : verb(hm) {}
52 RESTVerb(const std::string& s)
53 {
54 verb = http_method_from_str(s.c_str());
55 }
56
57 std::optional<llhttp_method> get_http_method() const
58 {
59 return static_cast<llhttp_method>(verb);
60 }
61
62 const char* c_str() const
63 {
64 return llhttp_method_name(static_cast<llhttp_method>(verb));
65 }
66
67 bool operator<(const RESTVerb& o) const
68 {
69 return verb < o.verb;
70 }
71
72 bool operator==(const RESTVerb& o) const
73 {
74 return verb == o.verb;
75 }
76
77 bool operator!=(const RESTVerb& o) const
78 {
79 return !(*this == o);
80 }
81 };
82
83 // Custom to_json and from_json specializations which encode RESTVerb in a
84 // lower-cased string, so it can be used in OpenAPI and similar documents
85 inline void to_json(nlohmann::json& j, const RESTVerb& verb)
86 {
87 std::string s(verb.c_str());
88 ccf::nonstd::to_lower(s);
89 j = s;
90 }
91
92 inline void from_json(const nlohmann::json& j, RESTVerb& verb)
93 {
94 if (!j.is_string())
95 {
96 throw std::runtime_error(fmt::format(
97 "Cannot parse RESTVerb from non-string JSON value: {}", j.dump()));
98 }
99
100 std::string s = j.get<std::string>();
101 ccf::nonstd::to_upper(s);
102
103 verb = RESTVerb(s.c_str());
104 }
105
106 inline std::string schema_name(const RESTVerb*)
107 {
108 return "HttpMethod";
109 }
110
111 inline void fill_json_schema(nlohmann::json& schema, const RESTVerb*)
112 {
113 schema["type"] = "string";
114 }
115}
Definition rest_verb.h:45
RESTVerb()
Definition rest_verb.h:50
bool operator<(const RESTVerb &o) const
Definition rest_verb.h:67
RESTVerb(const llhttp_method &hm)
Definition rest_verb.h:51
RESTVerb(const std::string &s)
Definition rest_verb.h:52
bool operator!=(const RESTVerb &o) const
Definition rest_verb.h:77
std::optional< llhttp_method > get_http_method() const
Definition rest_verb.h:57
const char * c_str() const
Definition rest_verb.h:62
bool operator==(const RESTVerb &o) const
Definition rest_verb.h:72
Definition app_interface.h:14
void fill_json_schema(nlohmann::json &schema, const ClaimsDigest *)
Definition claims_digest.h:64
std::string schema_name(const ClaimsDigest *)
Definition claims_digest.h:59
void from_json(const nlohmann::json &j, ClaimsDigest &hash)
Definition claims_digest.h:54
void to_json(nlohmann::json &j, const ClaimsDigest &hash)
Definition claims_digest.h:49
STL namespace.
#define XX(num, name, string)