Skip to content

Commit 464d8ea

Browse files
committed
Add serializer design notes
1 parent c7056c4 commit 464d8ea

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
//
2+
// Copyright (c) 2024 Vinnie Falco ([email protected])
3+
//
4+
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5+
// file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
6+
//
7+
// Official repository: https://github.com/cppalliance/http_proto
8+
//
9+
10+
= Serializer Design Requirements
11+
12+
13+
== Use Cases and Interfaces
14+
15+
16+
=== Empty Body
17+
18+
Useful for sending bodiless requests or responses, it should be possible to reuse the existing `serializer` and `request`/`response` objects without any need for extra memory allocation.
19+
20+
21+
[source,cpp]
22+
----
23+
system::result<void>
24+
handle_request(
25+
serializer& sr,
26+
response& res,
27+
request_view req)
28+
{
29+
res.set_start_line(status::not_found, req.version());
30+
res.set_keep_alive(req.keep_alive());
31+
sr.start(res);
32+
return {};
33+
}
34+
----
35+
36+
37+
=== Source Body
38+
39+
A `source`-like body allows for algorithms that can generate body contents directly in the `serializer`'s internal buffer in one or multiple steps, for example for sending contents of a file. The `serializer` should take ownership of the `source` object and is responsible for driving the algorithm and offering a `MutableBufferSequence` (by calling the related virtual interfaces on the `source` object).
40+
41+
42+
[source,cpp]
43+
----
44+
system::result<void>
45+
handle_request(
46+
serializer& sr,
47+
response& res,
48+
request_view req)
49+
{
50+
res.set_start_line(status::ok, req.version());
51+
res.set_keep_alive(req.keep_alive());
52+
res.set_chunked(true);
53+
54+
http_proto::file file;
55+
system::error_code ec;
56+
file.open("./index.html", file_mode::scan, ec);
57+
if (ec.failed())
58+
return ec;
59+
60+
sr.start<file_body>(res, std::move(file));
61+
return {};
62+
}
63+
----
64+
65+
=== ConstBufferSequence Body
66+
67+
The `serializer` should be able to use body contents passed as a `ConstBufferSequence` without copying it into its internal buffer.
68+
69+
[source,cpp]
70+
----
71+
system::result<void>
72+
handle_request(
73+
serializer& sr,
74+
response& res,
75+
request_view req)
76+
{
77+
res.set_start_line(status::not_found, req.version());
78+
res.set_keep_alive(req.keep_alive());
79+
80+
// assume caller has an stable reference to static_pages
81+
sr.start(res, buffers::make_buffer(static_pages.not_found));
82+
return {};
83+
}
84+
----
85+
86+
87+
=== ConstBufferSequence Body with Ownership Transfer
88+
89+
This is useful when the caller wants to create a `ConstBufferSequence` from an object which lives on the caller's stack or for any reason it is inconvenient for the caller to keep the object alive until the send operation is complete.
90+
91+
[source,cpp]
92+
----
93+
system::result<void>
94+
handle_request(
95+
serializer& sr,
96+
response& res,
97+
request_view req)
98+
{
99+
res.set_start_line(status::ok, req.version());
100+
res.set_keep_alive(req.keep_alive());
101+
102+
std::string body;
103+
body += "<section>";
104+
body += "<b>";
105+
body += "HOWDY!";
106+
body += "</b>";
107+
body += "</section>";
108+
109+
sr.start(res, [body = std::move(body)]{ return buffers::make_buffer(body);});
110+
return {};
111+
}
112+
----

0 commit comments

Comments
 (0)