summaryrefslogtreecommitdiff
path: root/http/verb.hpp
blob: b96008de18aef5312124d359a332765f4074e474 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#pragma once

#include <boost/beast/http/verb.hpp>

#include <optional>
#include <string_view>

enum class HttpVerb
{
    Delete = 0,
    Get,
    Head,
    Options,
    Patch,
    Post,
    Put,
    Max,
};

static constexpr size_t maxVerbIndex = static_cast<size_t>(HttpVerb::Max) - 1U;

// MaxVerb + 1 is designated as the "not found" verb.  It is done this way
// to keep the BaseRule as a single bitfield (thus keeping the struct small)
// while still having a way to declare a route a "not found" route.
static constexpr const size_t notFoundIndex = maxVerbIndex + 1;
static constexpr const size_t methodNotAllowedIndex = notFoundIndex + 1;

inline std::optional<HttpVerb> httpVerbFromBoost(boost::beast::http::verb bv)
{
    switch (bv)
    {
        case boost::beast::http::verb::delete_:
            return HttpVerb::Delete;
        case boost::beast::http::verb::get:
            return HttpVerb::Get;
        case boost::beast::http::verb::head:
            return HttpVerb::Head;
        case boost::beast::http::verb::options:
            return HttpVerb::Options;
        case boost::beast::http::verb::patch:
            return HttpVerb::Patch;
        case boost::beast::http::verb::post:
            return HttpVerb::Post;
        case boost::beast::http::verb::put:
            return HttpVerb::Put;
        default:
            return std::nullopt;
    }
}

inline std::string_view httpVerbToString(HttpVerb verb)
{
    switch (verb)
    {
        case HttpVerb::Delete:
            return "DELETE";
        case HttpVerb::Get:
            return "GET";
        case HttpVerb::Head:
            return "HEAD";
        case HttpVerb::Patch:
            return "PATCH";
        case HttpVerb::Post:
            return "POST";
        case HttpVerb::Put:
            return "PUT";
        case HttpVerb::Options:
            return "OPTIONS";
        default:
            return "";
    }

    // Should never reach here
    return "";
}