Repository navigation
Expand file tree
/
Copy pathstatus.hpp
More file actions
114 lines (88 loc) · 3.86 KB
/
Copy pathstatus.hpp
File metadata and controls
114 lines (88 loc) · 3.86 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
This file is part of libhttpserver
Copyright (C) 2011-2026 Sebastiano Merlino
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the file
LICENSE in the distribution; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#ifndef SRC_HTTPSERVER_HTTP_STATUS_HPP_
#define SRC_HTTPSERVER_HTTP_STATUS_HPP_
#include <cstdint>
namespace httpserver {
namespace http {
// Semantic response status value (RFC 9110 §15). A status stores the
// numeric code exactly as given; valid() reports whether the code lies
// in the defined 100..599 range. The default-constructed value and any
// out-of-range code are invalid: every consumer of a response must
// check valid() before committing the value to the wire. Library-owned
// (DR-V3-001): no backend or OS status enumerations.
class status {
public:
// Constructs the invalid status (code 0).
constexpr status() noexcept = default;
// Wraps a numeric code. The value is stored as given even when it
// is outside the valid range, so callers can diagnose what they
// were handed; valid() gates use.
static constexpr status from_code(std::uint16_t code) noexcept {
return status(code);
}
// v3 equivalent of http_response::shoutCAST(): marks this copied
// response for the fixed ICY status-line token on HTTP/1 output.
// Numeric validity and category are unchanged. No audio metadata
// or streaming policy is implied; ordinary statuses remain HTTP.
constexpr status with_shoutcast() const noexcept {
status copy = *this;
copy.shoutcast_ = true;
return copy;
}
constexpr bool is_shoutcast() const noexcept { return shoutcast_; }
// The stored numeric code, valid or not.
constexpr std::uint16_t code() const noexcept { return code_; }
// True iff the code lies in 100..599 inclusive.
constexpr bool valid() const noexcept {
return code_ >= min_code && code_ <= max_code;
}
// RFC 9110 category predicates. Exactly one is true for a valid
// status; all five are false for an invalid one.
constexpr bool informational() const noexcept {
return valid() && code_ < 200;
}
constexpr bool success() const noexcept {
return valid() && code_ >= 200 && code_ < 300;
}
constexpr bool redirection() const noexcept {
return valid() && code_ >= 300 && code_ < 400;
}
constexpr bool client_error() const noexcept {
return valid() && code_ >= 400 && code_ < 500;
}
constexpr bool server_error() const noexcept {
return valid() && code_ >= 500;
}
// Equality compares the semantic numeric code, excluding wire metadata.
friend constexpr bool operator==(const status& a, const status& b) noexcept {
return a.code_ == b.code_;
}
friend constexpr bool operator!=(const status& a, const status& b) noexcept {
return !(a == b);
}
private:
static constexpr std::uint16_t min_code = 100;
static constexpr std::uint16_t max_code = 599;
constexpr explicit status(std::uint16_t code) noexcept : code_(code) { }
std::uint16_t code_ = 0;
bool shoutcast_ = false;
};
} // namespace http
} // namespace httpserver
#endif // SRC_HTTPSERVER_HTTP_STATUS_HPP_