769 lines
22 KiB
C++
769 lines
22 KiB
C++
#include <Void/Net/Http.hpp>
|
|
#include <Void/Net/HttpServer.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <arpa/inet.h>
|
|
#include <netinet/in.h>
|
|
#include <poll.h>
|
|
#include <sys/socket.h>
|
|
#include <unistd.h>
|
|
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <atomic>
|
|
#include <chrono>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <span>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
namespace
|
|
{
|
|
using namespace std::chrono_literals;
|
|
using Void::Net::BodyReader;
|
|
using Void::Net::DuplexRouteHandler;
|
|
using Void::Net::Header;
|
|
using Void::Net::HttpServer;
|
|
using Void::Net::HttpServerOptions;
|
|
using Void::Net::Method;
|
|
using Void::Net::ResponseWriter;
|
|
using Void::Net::ServerRequest;
|
|
using Void::Result::ErrorKind;
|
|
using Void::Result::Result;
|
|
|
|
int connect_loopback(std::uint16_t port)
|
|
{
|
|
const int fd = ::socket(AF_INET, SOCK_STREAM, 0);
|
|
Void::Testing::require(fd >= 0);
|
|
|
|
timeval tv{};
|
|
tv.tv_sec = 3;
|
|
tv.tv_usec = 0;
|
|
(void)::setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
|
|
(void)::setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
|
|
|
|
sockaddr_in addr{};
|
|
addr.sin_family = AF_INET;
|
|
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
|
addr.sin_port = htons(port);
|
|
Void::Testing::require(
|
|
::connect(fd, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) ==
|
|
0);
|
|
return fd;
|
|
}
|
|
|
|
void send_bytes(int fd, std::string_view bytes)
|
|
{
|
|
std::size_t off = 0;
|
|
while (off < bytes.size()) {
|
|
const ssize_t n =
|
|
::send(fd, bytes.data() + off, bytes.size() - off, MSG_NOSIGNAL);
|
|
Void::Testing::require(n > 0);
|
|
off += static_cast<std::size_t>(n);
|
|
}
|
|
}
|
|
|
|
std::string recv_exact(int fd, std::size_t count)
|
|
{
|
|
std::string out;
|
|
out.reserve(count);
|
|
std::array<char, 4096> buf{};
|
|
while (out.size() < count) {
|
|
const std::size_t want = std::min(buf.size(), count - out.size());
|
|
const ssize_t n = ::recv(fd, buf.data(), want, 0);
|
|
if (n <= 0) {
|
|
break;
|
|
}
|
|
out.append(buf.data(), static_cast<std::size_t>(n));
|
|
}
|
|
return out;
|
|
}
|
|
|
|
std::string recv_until(int fd, std::string_view marker)
|
|
{
|
|
// Read one byte at a time: the caller reads the body right after the
|
|
// header, so consuming bytes past the marker would lose body data.
|
|
std::string out;
|
|
char c = 0;
|
|
while (out.find(marker) == std::string::npos) {
|
|
const ssize_t n = ::recv(fd, &c, 1, 0);
|
|
if (n <= 0) {
|
|
break;
|
|
}
|
|
out.push_back(c);
|
|
if (out.size() > 64 * 1024) {
|
|
break;
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
std::string recv_to_eof(int fd)
|
|
{
|
|
std::string out;
|
|
std::array<char, 4096> buf{};
|
|
for (;;) {
|
|
const ssize_t n = ::recv(fd, buf.data(), buf.size(), 0);
|
|
if (n <= 0) {
|
|
break;
|
|
}
|
|
out.append(buf.data(), static_cast<std::size_t>(n));
|
|
}
|
|
return out;
|
|
}
|
|
|
|
std::string get_request(std::string_view path, bool head = false,
|
|
std::string_view extra_headers = {})
|
|
{
|
|
std::string req = head ? "HEAD " : "GET ";
|
|
req += path;
|
|
req += " HTTP/1.1\r\nHost: 127.0.0.1\r\nConnection: close\r\n";
|
|
req += extra_headers;
|
|
req += "\r\n";
|
|
return req;
|
|
}
|
|
|
|
bool wait_flag(const std::atomic<bool>& flag,
|
|
std::chrono::milliseconds timeout = 5s)
|
|
{
|
|
const auto deadline = std::chrono::steady_clock::now() + timeout;
|
|
while (!flag.load(std::memory_order_acquire)) {
|
|
if (std::chrono::steady_clock::now() >= deadline) {
|
|
return false;
|
|
}
|
|
std::this_thread::sleep_for(1ms);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
std::size_t count_occurrences(std::string_view haystack,
|
|
std::string_view needle)
|
|
{
|
|
std::size_t count = 0;
|
|
std::size_t pos = 0;
|
|
while ((pos = haystack.find(needle, pos)) != std::string_view::npos) {
|
|
++count;
|
|
pos += needle.size();
|
|
}
|
|
return count;
|
|
}
|
|
} // namespace
|
|
|
|
Void::Testing::Test http_duplex_upload_while_reading_fixed_response()
|
|
{
|
|
HttpServerOptions opts;
|
|
opts.write_timeout = 2s;
|
|
opts.read_timeout = 2s;
|
|
opts.duplex_write_buffer_bytes = 4 * 1024;
|
|
|
|
HttpServer server{opts};
|
|
Void::Testing::require(
|
|
server
|
|
.add_duplex_route(
|
|
Method::Post, "/echo-fixed",
|
|
[](const ServerRequest&, BodyReader& body,
|
|
ResponseWriter& writer) -> Result<void> {
|
|
const std::uint64_t total = body.content_length().value_or(0);
|
|
auto began = writer.begin(
|
|
200, {Header{"Content-Length", std::to_string(total)}});
|
|
if (!began.has_value()) {
|
|
return began;
|
|
}
|
|
std::array<std::byte, 8> buf{};
|
|
for (;;) {
|
|
auto n = body.read_some(buf);
|
|
if (!n.has_value()) {
|
|
return std::unexpected{std::move(n.error())};
|
|
}
|
|
if (*n == 0) {
|
|
break;
|
|
}
|
|
std::size_t off = 0;
|
|
while (off < *n) {
|
|
auto accepted = writer.write_some(
|
|
std::span<const std::byte>{buf.data() + off,
|
|
*n - off});
|
|
if (!accepted.has_value()) {
|
|
return std::unexpected{
|
|
std::move(accepted.error())};
|
|
}
|
|
off += *accepted;
|
|
}
|
|
}
|
|
return writer.finish();
|
|
})
|
|
.has_value());
|
|
Void::Testing::require(server.start().has_value());
|
|
|
|
const int fd = connect_loopback(server.bound_port());
|
|
const std::string head =
|
|
"POST /echo-fixed HTTP/1.1\r\n"
|
|
"Host: 127.0.0.1\r\n"
|
|
"Content-Length: 12\r\n"
|
|
"Connection: close\r\n\r\n";
|
|
send_bytes(fd, head);
|
|
|
|
// Upload the first half, then read the echoed half before uploading the
|
|
// rest: the server must be reading and writing the same exchange.
|
|
send_bytes(fd, "abcd");
|
|
const std::string resp_head = recv_until(fd, "\r\n\r\n");
|
|
Void::Testing::require(resp_head.find("HTTP/1.1 200") != std::string::npos);
|
|
Void::Testing::require(resp_head.find("Content-Length: 12") !=
|
|
std::string::npos);
|
|
Void::Testing::expect_eq(recv_exact(fd, 4), std::string{"abcd"});
|
|
|
|
send_bytes(fd, "efghijkl");
|
|
Void::Testing::expect_eq(recv_exact(fd, 8), std::string{"efghijkl"});
|
|
(void)::close(fd);
|
|
|
|
server.shutdown();
|
|
}
|
|
|
|
Void::Testing::Test http_duplex_chunked_bodyless_and_head_framing()
|
|
{
|
|
HttpServer server;
|
|
Void::Testing::require(
|
|
server
|
|
.add_duplex_route(
|
|
Method::Get, "/chunked",
|
|
[](const ServerRequest&, BodyReader&,
|
|
ResponseWriter& writer) -> Result<void> {
|
|
auto began = writer.begin(200);
|
|
if (!began.has_value()) {
|
|
return began;
|
|
}
|
|
const std::string_view text = "hello";
|
|
auto accepted = writer.write_some(std::as_bytes(
|
|
std::span{text.data(), text.size()}));
|
|
if (!accepted.has_value()) {
|
|
return std::unexpected{std::move(accepted.error())};
|
|
}
|
|
Void::Testing::expect_eq(*accepted, std::size_t{5});
|
|
return writer.finish();
|
|
})
|
|
.has_value());
|
|
Void::Testing::require(
|
|
server
|
|
.add_duplex_route(
|
|
Method::Get, "/bodyless",
|
|
[](const ServerRequest&, BodyReader&,
|
|
ResponseWriter& writer) -> Result<void> {
|
|
auto began = writer.begin(204);
|
|
if (!began.has_value()) {
|
|
return began;
|
|
}
|
|
std::array<std::byte, 1> one{std::byte{'x'}};
|
|
auto rejected = writer.write_some(one);
|
|
Void::Testing::expect(!rejected.has_value());
|
|
Void::Testing::expect(rejected.error().kind() ==
|
|
ErrorKind::invalid_argument);
|
|
return writer.finish();
|
|
})
|
|
.has_value());
|
|
Void::Testing::require(
|
|
server
|
|
.add_duplex_route(
|
|
Method::Head, "/head",
|
|
[](const ServerRequest&, BodyReader&,
|
|
ResponseWriter& writer) -> Result<void> {
|
|
auto began =
|
|
writer.begin(200, {Header{"Content-Length", "123"}});
|
|
if (!began.has_value()) {
|
|
return began;
|
|
}
|
|
std::array<std::byte, 1> one{std::byte{'x'}};
|
|
Void::Testing::expect(!writer.write_some(one).has_value());
|
|
return writer.finish();
|
|
})
|
|
.has_value());
|
|
Void::Testing::require(server.start().has_value());
|
|
|
|
{
|
|
const int fd = connect_loopback(server.bound_port());
|
|
send_bytes(fd, get_request("/chunked"));
|
|
const std::string raw = recv_to_eof(fd);
|
|
(void)::close(fd);
|
|
Void::Testing::expect(raw.starts_with("HTTP/1.1 200"));
|
|
Void::Testing::expect(raw.find("Transfer-Encoding: chunked") !=
|
|
std::string::npos);
|
|
Void::Testing::expect(raw.find("5\r\nhello\r\n0\r\n\r\n") !=
|
|
std::string::npos);
|
|
}
|
|
{
|
|
const int fd = connect_loopback(server.bound_port());
|
|
send_bytes(fd, get_request("/bodyless"));
|
|
const std::string raw = recv_to_eof(fd);
|
|
(void)::close(fd);
|
|
Void::Testing::expect(raw.starts_with("HTTP/1.1 204"));
|
|
Void::Testing::expect(raw.find("Transfer-Encoding") ==
|
|
std::string::npos);
|
|
Void::Testing::expect(raw.ends_with("\r\n\r\n"));
|
|
}
|
|
{
|
|
const int fd = connect_loopback(server.bound_port());
|
|
send_bytes(fd, get_request("/head", true));
|
|
const std::string raw = recv_to_eof(fd);
|
|
(void)::close(fd);
|
|
Void::Testing::expect(raw.starts_with("HTTP/1.1 200"));
|
|
Void::Testing::expect(raw.find("Content-Length: 123") !=
|
|
std::string::npos);
|
|
Void::Testing::expect(raw.ends_with("\r\n\r\n"));
|
|
}
|
|
|
|
server.shutdown();
|
|
}
|
|
|
|
Void::Testing::Test http_duplex_disconnect_cancellation_and_retained_writer()
|
|
{
|
|
HttpServerOptions opts;
|
|
opts.write_timeout = 300ms;
|
|
|
|
HttpServer server{opts};
|
|
std::atomic<bool> disconnect_done{false};
|
|
std::atomic<int> disconnect_kind{-1};
|
|
std::atomic<bool> cancel_done{false};
|
|
std::atomic<int> cancel_kind{-1};
|
|
std::atomic<int> cancel_write_kind{-1};
|
|
|
|
std::mutex retained_mutex;
|
|
std::shared_ptr<ResponseWriter> retained;
|
|
|
|
Void::Testing::require(
|
|
server
|
|
.add_duplex_route(
|
|
Method::Get, "/disconnect",
|
|
[&](const ServerRequest&, BodyReader&,
|
|
ResponseWriter& writer) -> Result<void> {
|
|
auto began = writer.begin(200);
|
|
if (!began.has_value()) {
|
|
return began;
|
|
}
|
|
std::vector<std::byte> chunk(4096, std::byte{'d'});
|
|
for (;;) {
|
|
auto accepted = writer.write_some(chunk);
|
|
if (!accepted.has_value()) {
|
|
disconnect_kind.store(
|
|
static_cast<int>(accepted.error().kind()),
|
|
std::memory_order_release);
|
|
break;
|
|
}
|
|
}
|
|
disconnect_done.store(true, std::memory_order_release);
|
|
return std::unexpected{Void::Net::make_net_error(
|
|
Void::Net::NetErrorCode::io_error, "disconnected")};
|
|
})
|
|
.has_value());
|
|
Void::Testing::require(
|
|
server
|
|
.add_duplex_route(
|
|
Method::Get, "/cancel",
|
|
[&](const ServerRequest&, BodyReader&,
|
|
ResponseWriter& writer) -> Result<void> {
|
|
auto began = writer.begin(200);
|
|
if (!began.has_value()) {
|
|
return began;
|
|
}
|
|
const std::string_view text = "abc";
|
|
(void)writer.write_some(std::as_bytes(
|
|
std::span{text.data(), text.size()}));
|
|
writer.cancel();
|
|
Void::Testing::expect(writer.cancelled());
|
|
std::array<std::byte, 1> one{std::byte{'z'}};
|
|
auto rejected = writer.write_some(one);
|
|
cancel_write_kind.store(
|
|
rejected.has_value()
|
|
? -1
|
|
: static_cast<int>(rejected.error().kind()),
|
|
std::memory_order_release);
|
|
auto fin = writer.finish();
|
|
cancel_kind.store(
|
|
fin.has_value()
|
|
? -1
|
|
: static_cast<int>(fin.error().kind()),
|
|
std::memory_order_release);
|
|
cancel_done.store(true, std::memory_order_release);
|
|
return std::unexpected{Void::Net::make_net_error(
|
|
Void::Net::NetErrorCode::cancelled, "cancelled")};
|
|
})
|
|
.has_value());
|
|
Void::Testing::require(
|
|
server
|
|
.add_duplex_route(
|
|
Method::Get, "/retain",
|
|
[&](const ServerRequest&, BodyReader&,
|
|
ResponseWriter& writer) -> Result<void> {
|
|
auto began = writer.begin(
|
|
200, {Header{"Content-Length", "2"}});
|
|
if (!began.has_value()) {
|
|
return began;
|
|
}
|
|
const std::string_view text = "ok";
|
|
(void)writer.write_some(std::as_bytes(
|
|
std::span{text.data(), text.size()}));
|
|
auto fin = writer.finish();
|
|
if (!fin.has_value()) {
|
|
return fin;
|
|
}
|
|
std::lock_guard lock{retained_mutex};
|
|
retained =
|
|
std::make_shared<ResponseWriter>(std::move(writer));
|
|
return {};
|
|
})
|
|
.has_value());
|
|
Void::Testing::require(server.start().has_value());
|
|
|
|
{
|
|
const int fd = connect_loopback(server.bound_port());
|
|
send_bytes(fd, get_request("/disconnect"));
|
|
(void)recv_until(fd, "\r\n\r\n");
|
|
(void)recv_exact(fd, 128);
|
|
(void)::close(fd);
|
|
}
|
|
Void::Testing::require(wait_flag(disconnect_done));
|
|
Void::Testing::expect(
|
|
disconnect_kind.load() == static_cast<int>(ErrorKind::io) ||
|
|
disconnect_kind.load() == static_cast<int>(ErrorKind::cancelled));
|
|
|
|
{
|
|
const int fd = connect_loopback(server.bound_port());
|
|
send_bytes(fd, get_request("/cancel"));
|
|
const std::string raw = recv_to_eof(fd);
|
|
(void)::close(fd);
|
|
Void::Testing::expect(raw.starts_with("HTTP/1.1 200"));
|
|
Void::Testing::expect(raw.find("abc") != std::string::npos);
|
|
Void::Testing::expect(raw.find("0\r\n\r\n") == std::string::npos);
|
|
}
|
|
Void::Testing::require(wait_flag(cancel_done));
|
|
Void::Testing::expect(cancel_write_kind.load() ==
|
|
static_cast<int>(ErrorKind::cancelled));
|
|
Void::Testing::expect(cancel_kind.load() ==
|
|
static_cast<int>(ErrorKind::cancelled));
|
|
|
|
{
|
|
const int fd = connect_loopback(server.bound_port());
|
|
send_bytes(fd, get_request("/retain"));
|
|
const std::string raw = recv_to_eof(fd);
|
|
(void)::close(fd);
|
|
Void::Testing::expect(raw.find("ok") != std::string::npos);
|
|
}
|
|
|
|
std::shared_ptr<ResponseWriter> kept;
|
|
{
|
|
std::lock_guard lock{retained_mutex};
|
|
kept = retained;
|
|
}
|
|
Void::Testing::require(kept != nullptr);
|
|
const auto deadline = std::chrono::steady_clock::now() + 2s;
|
|
while (kept->valid() && std::chrono::steady_clock::now() < deadline) {
|
|
std::this_thread::sleep_for(1ms);
|
|
}
|
|
Void::Testing::expect(!kept->valid());
|
|
std::array<std::byte, 1> one{std::byte{'q'}};
|
|
auto after = kept->write_some(one);
|
|
Void::Testing::expect(!after.has_value());
|
|
Void::Testing::expect(after.error().kind() ==
|
|
ErrorKind::invalid_argument);
|
|
|
|
server.shutdown();
|
|
}
|
|
|
|
Void::Testing::Test http_duplex_stalled_consumer_bounded_memory()
|
|
{
|
|
HttpServerOptions opts;
|
|
opts.write_timeout = 150ms;
|
|
opts.duplex_write_buffer_bytes = 4 * 1024;
|
|
|
|
HttpServer server{opts};
|
|
std::atomic<bool> done{false};
|
|
std::atomic<int> kind{-1};
|
|
std::atomic<std::uint64_t> accepted_total{0};
|
|
std::atomic<std::size_t> max_buffered{0};
|
|
|
|
Void::Testing::require(
|
|
server
|
|
.add_duplex_route(
|
|
Method::Get, "/stall",
|
|
[&](const ServerRequest&, BodyReader&,
|
|
ResponseWriter& writer) -> Result<void> {
|
|
writer.set_write_timeout(150ms);
|
|
auto began = writer.begin(200);
|
|
if (!began.has_value()) {
|
|
return began;
|
|
}
|
|
std::vector<std::byte> chunk(4096, std::byte{'s'});
|
|
for (;;) {
|
|
auto accepted = writer.write_some(chunk);
|
|
max_buffered.store(
|
|
std::max(max_buffered.load(),
|
|
writer.buffered_bytes()),
|
|
std::memory_order_release);
|
|
if (!accepted.has_value()) {
|
|
kind.store(
|
|
static_cast<int>(accepted.error().kind()),
|
|
std::memory_order_release);
|
|
break;
|
|
}
|
|
accepted_total.fetch_add(*accepted,
|
|
std::memory_order_acq_rel);
|
|
}
|
|
done.store(true, std::memory_order_release);
|
|
return std::unexpected{Void::Net::make_net_error(
|
|
Void::Net::NetErrorCode::timeout, "stalled")};
|
|
})
|
|
.has_value());
|
|
Void::Testing::require(server.start().has_value());
|
|
|
|
const int fd = connect_loopback(server.bound_port());
|
|
send_bytes(fd, get_request("/stall"));
|
|
// Deliberately never read the response.
|
|
std::this_thread::sleep_for(400ms);
|
|
(void)::close(fd);
|
|
|
|
Void::Testing::require(wait_flag(done, 5s));
|
|
Void::Testing::expect(kind.load() == static_cast<int>(ErrorKind::timeout) ||
|
|
kind.load() == static_cast<int>(ErrorKind::io) ||
|
|
kind.load() ==
|
|
static_cast<int>(ErrorKind::cancelled));
|
|
Void::Testing::expect(max_buffered.load() <= opts.duplex_write_buffer_bytes);
|
|
Void::Testing::expect(accepted_total.load() < 64ULL * 1024 * 1024);
|
|
|
|
server.shutdown();
|
|
}
|
|
|
|
Void::Testing::Test http_duplex_concurrent_writers_are_rejected()
|
|
{
|
|
HttpServerOptions opts;
|
|
opts.write_timeout = 500ms;
|
|
opts.duplex_write_buffer_bytes = 4 * 1024;
|
|
|
|
HttpServer server{opts};
|
|
std::atomic<bool> done{false};
|
|
std::atomic<int> overloaded{0};
|
|
|
|
Void::Testing::require(
|
|
server
|
|
.add_duplex_route(
|
|
Method::Get, "/concurrent",
|
|
[&](const ServerRequest&, BodyReader&,
|
|
ResponseWriter& writer) -> Result<void> {
|
|
writer.set_write_timeout(500ms);
|
|
auto began = writer.begin(200);
|
|
if (!began.has_value()) {
|
|
return began;
|
|
}
|
|
std::vector<std::byte> chunk(4096, std::byte{'c'});
|
|
auto writer_fn = [&]() {
|
|
for (int i = 0; i < 100000; ++i) {
|
|
auto accepted = writer.write_some(chunk);
|
|
if (!accepted.has_value()) {
|
|
if (accepted.error().kind() ==
|
|
ErrorKind::overloaded) {
|
|
overloaded.fetch_add(
|
|
1, std::memory_order_acq_rel);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
};
|
|
std::thread first{writer_fn};
|
|
std::thread second{writer_fn};
|
|
first.join();
|
|
second.join();
|
|
done.store(true, std::memory_order_release);
|
|
writer.cancel();
|
|
return std::unexpected{Void::Net::make_net_error(
|
|
Void::Net::NetErrorCode::cancelled, "done")};
|
|
})
|
|
.has_value());
|
|
Void::Testing::require(server.start().has_value());
|
|
|
|
const int fd = connect_loopback(server.bound_port());
|
|
send_bytes(fd, get_request("/concurrent"));
|
|
// Never read, so the socket backpressures the writers.
|
|
Void::Testing::require(wait_flag(done, 10s));
|
|
Void::Testing::expect(overloaded.load() >= 1);
|
|
(void)::close(fd);
|
|
|
|
server.shutdown();
|
|
}
|
|
|
|
Void::Testing::Test http_duplex_malformed_framing_and_error_phases()
|
|
{
|
|
HttpServer server;
|
|
std::atomic<int> bad_begin_kind{-1};
|
|
std::atomic<int> bad_header_kind{-1};
|
|
std::atomic<int> overflow_kind{-1};
|
|
|
|
Void::Testing::require(
|
|
server
|
|
.add_duplex_route(
|
|
Method::Get, "/bad-begin",
|
|
[&](const ServerRequest&, BodyReader&,
|
|
ResponseWriter& writer) -> Result<void> {
|
|
auto began = writer.begin(
|
|
200, {Header{"Content-Length", "5"},
|
|
Header{"Transfer-Encoding", "chunked"}});
|
|
Void::Testing::expect(!began.has_value());
|
|
bad_begin_kind.store(
|
|
static_cast<int>(began.error().kind()),
|
|
std::memory_order_release);
|
|
return std::unexpected{std::move(began.error())};
|
|
})
|
|
.has_value());
|
|
Void::Testing::require(
|
|
server
|
|
.add_duplex_route(
|
|
Method::Get, "/bad-header",
|
|
[&](const ServerRequest&, BodyReader&,
|
|
ResponseWriter& writer) -> Result<void> {
|
|
auto began =
|
|
writer.begin(200, {Header{"X-Bad", "a\r\nb"}});
|
|
Void::Testing::expect(!began.has_value());
|
|
bad_header_kind.store(
|
|
static_cast<int>(began.error().kind()),
|
|
std::memory_order_release);
|
|
return std::unexpected{std::move(began.error())};
|
|
})
|
|
.has_value());
|
|
Void::Testing::require(
|
|
server
|
|
.add_duplex_route(
|
|
Method::Get, "/bad-fixed",
|
|
[&](const ServerRequest&, BodyReader&,
|
|
ResponseWriter& writer) -> Result<void> {
|
|
auto began = writer.begin(
|
|
200, {Header{"Content-Length", "3"}});
|
|
if (!began.has_value()) {
|
|
return began;
|
|
}
|
|
std::array<std::byte, 4> four{};
|
|
auto accepted = writer.write_some(four);
|
|
Void::Testing::expect(!accepted.has_value());
|
|
overflow_kind.store(
|
|
static_cast<int>(accepted.error().kind()),
|
|
std::memory_order_release);
|
|
// Error after headers: the server must terminate the
|
|
// stream without a successful terminal chunk.
|
|
return std::unexpected{std::move(accepted.error())};
|
|
})
|
|
.has_value());
|
|
Void::Testing::require(
|
|
server
|
|
.add_duplex_route(
|
|
Method::Get, "/ok",
|
|
[](const ServerRequest&, BodyReader&,
|
|
ResponseWriter& writer) -> Result<void> {
|
|
auto began = writer.begin(
|
|
200, {Header{"Content-Length", "2"}});
|
|
if (!began.has_value()) {
|
|
return began;
|
|
}
|
|
const std::string_view text = "ok";
|
|
auto accepted = writer.write_some(std::as_bytes(
|
|
std::span{text.data(), text.size()}));
|
|
if (!accepted.has_value()) {
|
|
return std::unexpected{std::move(accepted.error())};
|
|
}
|
|
return writer.finish();
|
|
})
|
|
.has_value());
|
|
Void::Testing::require(server.start().has_value());
|
|
|
|
{
|
|
const int fd = connect_loopback(server.bound_port());
|
|
send_bytes(fd, get_request("/bad-begin"));
|
|
const std::string raw = recv_to_eof(fd);
|
|
(void)::close(fd);
|
|
Void::Testing::expect(raw.starts_with("HTTP/1.1 500"));
|
|
}
|
|
{
|
|
const int fd = connect_loopback(server.bound_port());
|
|
send_bytes(fd, get_request("/bad-header"));
|
|
const std::string raw = recv_to_eof(fd);
|
|
(void)::close(fd);
|
|
Void::Testing::expect(raw.starts_with("HTTP/1.1 500"));
|
|
}
|
|
{
|
|
const int fd = connect_loopback(server.bound_port());
|
|
send_bytes(fd, get_request("/bad-fixed"));
|
|
const std::string raw = recv_to_eof(fd);
|
|
(void)::close(fd);
|
|
Void::Testing::expect(raw.starts_with("HTTP/1.1 200"));
|
|
Void::Testing::expect(raw.find("Content-Length: 3") !=
|
|
std::string::npos);
|
|
Void::Testing::expect(raw.find("ok") == std::string::npos);
|
|
}
|
|
Void::Testing::expect(bad_begin_kind.load() ==
|
|
static_cast<int>(ErrorKind::invalid_argument));
|
|
Void::Testing::expect(bad_header_kind.load() ==
|
|
static_cast<int>(ErrorKind::invalid_argument));
|
|
Void::Testing::expect(overflow_kind.load() ==
|
|
static_cast<int>(ErrorKind::invalid_argument));
|
|
|
|
// The server survives malformed exchanges and still serves a valid route.
|
|
{
|
|
const int fd = connect_loopback(server.bound_port());
|
|
send_bytes(fd, get_request("/ok"));
|
|
const std::string raw = recv_to_eof(fd);
|
|
(void)::close(fd);
|
|
Void::Testing::expect(raw.starts_with("HTTP/1.1 200"));
|
|
Void::Testing::expect(raw.ends_with("ok"));
|
|
}
|
|
|
|
server.shutdown();
|
|
}
|
|
|
|
Void::Testing::Test http_duplex_server_shutdown_cancels_exchange()
|
|
{
|
|
HttpServerOptions opts;
|
|
opts.write_timeout = 200ms;
|
|
|
|
HttpServer server{opts};
|
|
std::atomic<bool> started{false};
|
|
std::atomic<bool> exited{false};
|
|
|
|
Void::Testing::require(
|
|
server
|
|
.add_duplex_route(
|
|
Method::Get, "/shutdown",
|
|
[&](const ServerRequest&, BodyReader&,
|
|
ResponseWriter& writer) -> Result<void> {
|
|
writer.set_write_timeout(200ms);
|
|
auto began = writer.begin(200);
|
|
if (!began.has_value()) {
|
|
return began;
|
|
}
|
|
started.store(true, std::memory_order_release);
|
|
std::array<std::byte, 1> one{std::byte{'x'}};
|
|
while (!writer.cancelled()) {
|
|
auto accepted = writer.write_some(one);
|
|
if (!accepted.has_value()) {
|
|
break;
|
|
}
|
|
std::this_thread::sleep_for(5ms);
|
|
}
|
|
exited.store(true, std::memory_order_release);
|
|
return std::unexpected{Void::Net::make_net_error(
|
|
Void::Net::NetErrorCode::cancelled, "shutdown")};
|
|
})
|
|
.has_value());
|
|
Void::Testing::require(server.start().has_value());
|
|
|
|
const int fd = connect_loopback(server.bound_port());
|
|
send_bytes(fd, get_request("/shutdown"));
|
|
Void::Testing::require(wait_flag(started));
|
|
|
|
const auto begin = std::chrono::steady_clock::now();
|
|
server.shutdown(2s);
|
|
const auto elapsed = std::chrono::steady_clock::now() - begin;
|
|
Void::Testing::expect(elapsed < 3s);
|
|
Void::Testing::expect(exited.load());
|
|
(void)::close(fd);
|
|
}
|