Repository navigation
Expand file tree
/
Copy pathresponse_sources.cpp
More file actions
235 lines (215 loc) · 9.33 KB
/
Copy pathresponse_sources.cpp
File metadata and controls
235 lines (215 loc) · 9.33 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/* Copyright (C) 2026 Sebastiano Merlino; SPDX-License-Identifier: LGPL-2.1-or-later */
#include <algorithm>
#include <cstdio>
#include <utility>
#include <httpserver/response_sources.hpp>
namespace httpserver::detail {
http::outcome parse_content_length(const http::fields& f, std::uint64_t& out) {
out = 0;
const std::optional<std::string_view> value = f.first("content-length");
if (!value.has_value()) return http::outcome::okay();
if (value->empty()) {
return http::outcome(http::outcome_code::invalid_argument, "response fields: Content-Length is not a number");
}
for (const char c : *value) {
if (c < '0' || c > '9') {
return http::outcome(http::outcome_code::invalid_argument, "response fields: Content-Length is not a number");
}
if (out > (std::numeric_limits<std::uint64_t>::max() - static_cast<std::uint64_t>(c - '0')) / 10) {
return http::outcome(http::outcome_code::invalid_argument, "response fields: Content-Length is out of range");
}
out = out * 10 + static_cast<std::uint64_t>(c - '0');
}
return http::outcome::okay();
}
auto transferred_handle::claim() noexcept -> bool {
bool idle = false;
return claimed_.compare_exchange_strong(idle, true, std::memory_order_acq_rel, std::memory_order_relaxed);
}
auto transferred_handle::close() noexcept -> void {
if (closed_.exchange(true, std::memory_order_acq_rel)) return;
if (close_) close_(handle_);
}
void fclose_owned(std::FILE* handle) noexcept {
if (handle != nullptr) std::fclose(handle);
}
send_cursor::~send_cursor() {
if (owner_ && owns_handle_) owner_->handle->close();
}
auto send_cursor::prepare(std::shared_ptr<const response_body_source> source, const http::fields& framing) -> http::outcome {
owner_ = std::move(source);
switch (owner_->kind) {
case response_source_kind::owned_bytes:
bind_span(owner_->bytes);
return http::outcome::okay();
case response_source_kind::reopen_file:
return prepare_file(framing);
case response_source_kind::factory:
return prepare_factory();
case response_source_kind::borrowed:
bind_span(owner_->view);
return http::outcome::okay();
case response_source_kind::owned_file:
return prepare_owned_file(framing);
case response_source_kind::owned_pipe:
return prepare_owned_pipe(framing);
}
return http::outcome::okay();
}
auto send_cursor::pull() -> body_chunk {
switch (owner_->kind) {
case response_source_kind::owned_bytes:
case response_source_kind::borrowed:
return pull_span();
case response_source_kind::reopen_file:
return pull_file();
case response_source_kind::factory:
return pull_factory();
case response_source_kind::owned_file:
return pull_handle();
case response_source_kind::owned_pipe:
return pull_handle();
}
return body_chunk{http::outcome::okay(), {}, true};
}
auto send_cursor::bind_span(std::span<const std::byte> body) noexcept -> void {
view_ = body;
offset_ = 0;
total_ = body.size();
}
auto send_cursor::prepare_file(const http::fields& framing) -> http::outcome {
file_.open(owner_->path, std::ios::binary);
if (!file_.is_open()) {
return http::outcome(http::outcome_code::invalid_argument, "send_definition: cannot reopen file '" + owner_->path + "'");
}
file_.seekg(0, std::ios::end);
const std::streamoff end = file_.tellg();
file_.seekg(0, std::ios::beg);
file_size_ = end < 0 ? 0 : static_cast<std::uint64_t>(end);
remaining_ = file_size_;
if (framing.count("content-length") > 0) {
std::uint64_t declared = 0;
const http::outcome length = parse_content_length(framing, declared);
if (!length.ok()) return length;
remaining_ = declared;
}
buffer_.assign(k_file_chunk_bytes, std::byte{0});
return http::outcome::okay();
}
auto send_cursor::prepare_factory() -> http::outcome {
producer_ = owner_->make();
if (!producer_) {
return http::outcome(http::outcome_code::invalid_argument, "send_definition: factory returned an empty producer");
}
return http::outcome::okay();
}
auto send_cursor::prepare_owned_file(const http::fields& framing) -> http::outcome {
if (!owner_->handle->claim()) {
return http::outcome(http::outcome_code::invalid_state, "send_definition: one-shot source already used");
}
owns_handle_ = true;
std::FILE* const stream = owner_->handle->get();
// std::ftell is the ISO C probe and returns long by contract —
// the documented LONG_MAX bound of the probed size.
const long start = std::ftell(stream); // NOLINT(runtime/int)
if (start < 0 || std::fseek(stream, 0, SEEK_END) != 0) {
return unseekable();
}
const long end = std::ftell(stream); // NOLINT(runtime/int)
if (end < 0 || std::fseek(stream, start, SEEK_SET) != 0) {
return unseekable();
}
file_size_ = static_cast<std::uint64_t>(end - start);
remaining_ = file_size_;
if (framing.count("content-length") > 0) {
std::uint64_t declared = 0;
const http::outcome length = parse_content_length(framing, declared);
if (!length.ok()) return length;
remaining_ = declared;
}
buffer_.assign(k_file_chunk_bytes, std::byte{0});
return http::outcome::okay();
}
auto send_cursor::prepare_owned_pipe(const http::fields& framing) -> http::outcome {
if (!owner_->handle->claim()) {
return http::outcome(http::outcome_code::invalid_state, "send_definition: one-shot source already used");
}
owns_handle_ = true;
remaining_ = k_read_to_eof;
eof_ends_ = true;
if (framing.count("content-length") > 0) {
std::uint64_t declared = 0;
const http::outcome length = parse_content_length(framing, declared);
if (!length.ok()) return length;
remaining_ = declared;
eof_ends_ = false;
}
buffer_.assign(k_file_chunk_bytes, std::byte{0});
return http::outcome::okay();
}
auto send_cursor::pull_span() -> body_chunk {
if (offset_ >= total_) {
return body_chunk{http::outcome::okay(), {}, true};
}
const std::byte* const base = view_.data() + offset_;
const std::size_t rest = static_cast<std::size_t>(total_ - offset_);
offset_ = total_;
return body_chunk{http::outcome::okay(), std::span<const std::byte>(base, rest), true};
}
auto send_cursor::pull_file() -> body_chunk {
if (remaining_ == 0) {
return body_chunk{http::outcome::okay(), {}, true};
}
file_.read(reinterpret_cast<char*>(buffer_.data()), static_cast<std::streamsize>(std::min<std::uint64_t>(remaining_, buffer_.size())));
const std::streamsize got = file_.gcount();
// eofbit is set only when fewer characters than requested were
// available, and the request never exceeds the bound -- so eof
// (or a zero count) means the file ran out before the pinned
// length: a short body, typed like the engine's.
if (got <= 0 || file_.eof()) {
return body_chunk{http::outcome(http::outcome_code::protocol_error,
"send_definition: body shorter than the declared "
"Content-Length"),
{},
false};
}
remaining_ -= static_cast<std::uint64_t>(got);
return body_chunk{http::outcome::okay(), std::span<const std::byte>(buffer_.data(), static_cast<std::size_t>(got)), remaining_ == 0};
}
auto send_cursor::pull_factory() -> body_chunk {
const body_chunk chunk = producer_();
if (chunk.status.ok() && !chunk.end && chunk.data.empty()) {
return body_chunk{http::outcome(http::outcome_code::invalid_argument,
"send_definition: producer returned an empty "
"non-end chunk"),
{},
false};
}
return chunk;
}
auto send_cursor::pull_handle() -> body_chunk {
if (remaining_ == 0) {
return body_chunk{http::outcome::okay(), {}, true};
}
std::FILE* const stream = owner_->handle->get();
const std::size_t want = static_cast<std::size_t>(std::min<std::uint64_t>(remaining_, buffer_.size()));
const std::size_t got = std::fread(buffer_.data(), 1, want, stream);
if (got == want) {
remaining_ -= got;
return body_chunk{http::outcome::okay(), std::span<const std::byte>(buffer_.data(), got), remaining_ == 0};
}
if (std::ferror(stream) != 0) {
return body_chunk{http::outcome(http::outcome_code::invalid_state, "send_definition: handle read failed"), {}, false};
}
if (eof_ends_ && std::feof(stream) != 0) {
remaining_ = 0;
return body_chunk{http::outcome::okay(), std::span<const std::byte>(buffer_.data(), got), true};
}
return body_chunk{http::outcome(http::outcome_code::protocol_error,
"send_definition: body shorter than the declared "
"Content-Length"),
{},
false};
}
auto send_cursor::unseekable() -> http::outcome { return http::outcome(http::outcome_code::invalid_argument, "send_definition: owned_file handle is not seekable"); }
} // namespace httpserver::detail