Repository navigation
Expand file tree
/
Copy pathconsumer_v3_forms.cpp
More file actions
147 lines (133 loc) · 5.89 KB
/
Copy pathconsumer_v3_forms.cpp
File metadata and controls
147 lines (133 loc) · 5.89 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
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
*/
// TASK-116: a consumer including
// <httpserver/forms/urlencoded.hpp> WITHOUT HTTPSERVER_COMPILATION
// (or any other build/TLS configuration macro) must compile and link
// cleanly. The decoder and the adapters live in the library
// (detail/forms_urlencoded.cpp in v3core), so the empty LDADD of this
// target proves the vocabulary surface needs no library linkage to
// consume; every check is a static_assert over unevaluated
// expressions only -- no object whose members live in the library is
// ever constructed (make_urlencoded_route would run library code, so
// it is only name-checked through decltype).
#include <cstddef>
#include <cstdint>
#include <optional>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>
#include <vector>
#include <httpserver/concurrency/executor.hpp>
#include <httpserver/concurrency/task.hpp>
#include <httpserver/exchange.hpp>
#include <httpserver/forms/urlencoded.hpp>
#include <httpserver/http/fields.hpp>
#include <httpserver/http/outcome.hpp>
#include <httpserver/http/request_head.hpp>
#include <httpserver/http/status.hpp>
#include <httpserver/server/routes.hpp>
namespace {
using httpserver::exchange;
using httpserver::task;
using httpserver::forms::decode_urlencoded;
using httpserver::forms::form_fields;
using httpserver::forms::form_read;
using httpserver::forms::form_route_handler;
using httpserver::forms::make_urlencoded_route;
using httpserver::forms::read_urlencoded;
using httpserver::forms::urlencoded_limits;
namespace http = httpserver::http;
namespace srv = httpserver::server;
// The limits are a plain aggregate mirroring v2's GET-arg budgets;
// create() validates both caps.
static_assert(std::is_default_constructible_v<urlencoded_limits>);
static_assert(std::is_copy_assignable_v<urlencoded_limits>);
static_assert(urlencoded_limits{}.max_total_bytes
== static_cast<std::uint64_t>(65536));
static_assert(urlencoded_limits{}.max_fields
== static_cast<std::uint64_t>(64));
static_assert(std::is_same_v<
decltype(&urlencoded_limits::create),
http::outcome (*)(std::uint64_t, std::uint64_t,
urlencoded_limits&)>);
// The decoded fields: a copyable ordered value type with
// const-correct accessors; the adoption bridge is explicit.
static_assert(std::is_default_constructible_v<form_fields>);
static_assert(std::is_copy_constructible_v<form_fields>);
static_assert(std::is_move_constructible_v<form_fields>);
static_assert(std::is_constructible_v<
form_fields,
std::vector<std::pair<std::string, std::string>>>);
static_assert(!std::is_convertible_v<
std::vector<std::pair<std::string, std::string>>, form_fields>);
static_assert(std::is_same_v<
decltype(std::declval<const form_fields&>().entries()),
const std::vector<std::pair<std::string, std::string>>&>);
static_assert(std::is_same_v<
decltype(std::declval<const form_fields&>().value(
std::declval<std::string_view>())),
std::optional<std::string_view>>);
static_assert(noexcept(std::declval<const form_fields&>().value(
std::declval<std::string_view>())));
static_assert(std::is_same_v<
decltype(std::declval<const form_fields&>().all(
std::declval<std::string_view>())),
std::vector<std::string_view>>);
static_assert(std::is_same_v<
decltype(std::declval<const form_fields&>().size()),
std::size_t>);
static_assert(noexcept(std::declval<const form_fields&>().size()));
// The read verdict: a plain value carrying the typed outcome, the
// fields, and the ready-made rejection vocabulary.
static_assert(std::is_default_constructible_v<form_read>);
static_assert(std::is_copy_constructible_v<form_read>);
static_assert(std::is_move_constructible_v<form_read>);
static_assert(std::is_same_v<
decltype(std::declval<const form_read&>().ok()), bool>);
static_assert(noexcept(std::declval<const form_read&>().ok()));
static_assert(std::is_same_v<
decltype(std::declval<const form_read&>().reject_status()),
http::status>);
static_assert(noexcept(
std::declval<const form_read&>().reject_status()));
static_assert(std::is_same_v<
decltype(std::declval<const form_read&>().reject_fields()),
http::fields>);
// The handler callback is a unique_function (move-only); the adapter
// and the two read forms have the declared shapes.
static_assert(!std::is_copy_constructible_v<form_route_handler>);
static_assert(std::is_move_constructible_v<form_route_handler>);
static_assert(std::is_same_v<
decltype(make_urlencoded_route),
srv::route_handler(urlencoded_limits, form_route_handler)>);
static_assert(std::is_same_v<
decltype(read_urlencoded(std::declval<exchange&>(),
std::declval<const urlencoded_limits&>())),
task<form_read>>);
static_assert(std::is_same_v<
decltype(decode_urlencoded(
std::declval<std::span<const std::byte>>(),
std::declval<const urlencoded_limits&>(),
std::declval<form_fields&>())),
http::outcome>);
} // namespace
int main() {
// Nothing runs: compiling and linking is the contract.
return 0;
}