265 lines
7.5 KiB
C++
265 lines
7.5 KiB
C++
#include "networked_kv/application.hpp"
|
|
|
|
#include <Void/Net/Http.hpp>
|
|
|
|
#include <arpa/inet.h>
|
|
#include <netinet/in.h>
|
|
#include <poll.h>
|
|
#include <sys/socket.h>
|
|
#include <unistd.h>
|
|
|
|
#include <cassert>
|
|
#include <chrono>
|
|
#include <cstdio>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <thread>
|
|
|
|
namespace
|
|
{
|
|
using namespace NetworkedKv;
|
|
|
|
/// Minimal raw HTTP peer so framing/overload behavior is observable
|
|
/// without the library's own client normalizing it.
|
|
class RawPeer
|
|
{
|
|
public:
|
|
|
|
explicit RawPeer(std::uint16_t port)
|
|
{
|
|
m_fd = ::socket(AF_INET, SOCK_STREAM, 0);
|
|
assert(m_fd >= 0);
|
|
sockaddr_in addr{};
|
|
addr.sin_family = AF_INET;
|
|
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
|
addr.sin_port = htons(port);
|
|
assert(::connect(m_fd, reinterpret_cast<sockaddr*>(&addr),
|
|
sizeof(addr)) == 0);
|
|
}
|
|
|
|
~RawPeer()
|
|
{
|
|
if (m_fd >= 0) {
|
|
(void)::close(m_fd);
|
|
}
|
|
}
|
|
|
|
RawPeer(const RawPeer&) = delete;
|
|
RawPeer& operator=(const RawPeer&) = delete;
|
|
|
|
void send(std::string_view bytes) const
|
|
{
|
|
std::size_t offset = 0;
|
|
while (offset < bytes.size()) {
|
|
const ssize_t written =
|
|
::send(m_fd, bytes.data() + offset,
|
|
bytes.size() - offset, MSG_NOSIGNAL);
|
|
assert(written > 0);
|
|
offset += static_cast<std::size_t>(written);
|
|
}
|
|
}
|
|
|
|
/// Read until the server closes or `timeout` elapses.
|
|
std::string read_until_closed(
|
|
std::chrono::milliseconds timeout = std::chrono::seconds{5})
|
|
{
|
|
std::string out;
|
|
const auto deadline =
|
|
std::chrono::steady_clock::now() + timeout;
|
|
for (;;) {
|
|
const auto remaining = deadline - std::chrono::steady_clock::now();
|
|
if (remaining <= std::chrono::milliseconds::zero()) {
|
|
break;
|
|
}
|
|
pollfd pfd{.fd = m_fd, .events = POLLIN, .revents = 0};
|
|
const int ready = ::poll(
|
|
&pfd, 1,
|
|
static_cast<int>(std::min<long long>(
|
|
std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
remaining)
|
|
.count(),
|
|
1000)));
|
|
if (ready <= 0) {
|
|
if (ready == 0) {
|
|
continue;
|
|
}
|
|
break;
|
|
}
|
|
char buffer[4096];
|
|
const ssize_t read = ::recv(m_fd, buffer, sizeof(buffer), 0);
|
|
if (read <= 0) {
|
|
break;
|
|
}
|
|
out.append(buffer, static_cast<std::size_t>(read));
|
|
}
|
|
return out;
|
|
}
|
|
|
|
[[nodiscard]] int fd() const noexcept { return m_fd; }
|
|
|
|
private:
|
|
|
|
int m_fd{-1};
|
|
};
|
|
|
|
std::string raw_exchange(std::uint16_t port, std::string_view raw)
|
|
{
|
|
RawPeer peer{port};
|
|
peer.send(raw);
|
|
(void)::shutdown(peer.fd(), SHUT_WR);
|
|
return peer.read_until_closed();
|
|
}
|
|
|
|
NetworkedConfig config()
|
|
{
|
|
NetworkedConfig cfg;
|
|
cfg.listen_address = "127.0.0.1:0";
|
|
cfg.admin_address = "127.0.0.1:0";
|
|
cfg.bearer_token = "secret";
|
|
cfg.read_timeout = std::chrono::milliseconds{200};
|
|
cfg.write_timeout = std::chrono::milliseconds{200};
|
|
return cfg;
|
|
}
|
|
|
|
ApplicationResources managed()
|
|
{
|
|
ApplicationResources resources;
|
|
resources.ownership = ResourceOwnership::Managed;
|
|
return resources;
|
|
}
|
|
|
|
void malformed_framing_is_rejected()
|
|
{
|
|
auto app = NetworkedApplication::create(managed(), config());
|
|
assert(app.has_value());
|
|
assert(app->start().has_value());
|
|
const auto port = app->health().bound_port;
|
|
|
|
assert(raw_exchange(port, "NOT-HTTP\r\n\r\n")
|
|
.starts_with("HTTP/1.1 400"));
|
|
// Conflicting framing (smuggling shape) is rejected before routing.
|
|
assert(raw_exchange(port,
|
|
"POST /v1/kv/a HTTP/1.1\r\nHost: x\r\n"
|
|
"Content-Length: 4\r\nTransfer-Encoding: chunked\r\n\r\n"
|
|
"0\r\n\r\n")
|
|
.starts_with("HTTP/1.1 400"));
|
|
// A request line without a version is malformed.
|
|
assert(raw_exchange(port, "GET /v1/kv/a\r\n\r\n")
|
|
.starts_with("HTTP/1.1 400"));
|
|
|
|
assert(app->stop().has_value());
|
|
}
|
|
|
|
void oversized_header_and_body_are_bounded()
|
|
{
|
|
NetworkedConfig cfg = config();
|
|
cfg.max_header_bytes = 512;
|
|
cfg.max_request_bytes = 64;
|
|
auto app = NetworkedApplication::create(managed(), cfg);
|
|
assert(app.has_value());
|
|
assert(app->start().has_value());
|
|
const auto port = app->health().bound_port;
|
|
|
|
std::string huge = "GET /v1/kv/a HTTP/1.1\r\nHost: x\r\nX-Huge: ";
|
|
huge += std::string(2000, 'H');
|
|
huge += "\r\n\r\n";
|
|
assert(raw_exchange(port, huge).starts_with("HTTP/1.1 431"));
|
|
|
|
std::string body = "PUT /v1/kv/a HTTP/1.1\r\nHost: x\r\n";
|
|
body += "Authorization: Bearer secret\r\nX-Request-Id: r\r\n";
|
|
body += "If-None-Match: *\r\nContent-Length: 100\r\n\r\n";
|
|
body += std::string(100, 'B');
|
|
assert(raw_exchange(port, body).starts_with("HTTP/1.1 413"));
|
|
|
|
assert(app->stop().has_value());
|
|
}
|
|
|
|
void pipelining_and_keep_alive_preserve_order()
|
|
{
|
|
auto app = NetworkedApplication::create(managed(), config());
|
|
assert(app.has_value());
|
|
assert(app->start().has_value());
|
|
const auto port = app->health().bound_port;
|
|
|
|
RawPeer peer{port};
|
|
const std::string first =
|
|
"GET /v1/kv/missing HTTP/1.1\r\nHost: x\r\n"
|
|
"Authorization: Bearer secret\r\n\r\n";
|
|
// Two pipelined requests: the 404 for the missing key, then a 401 for
|
|
// the unauthenticated one, in that order.
|
|
peer.send(first +
|
|
"GET /v1/kv/missing HTTP/1.1\r\nHost: x\r\n\r\n");
|
|
(void)::shutdown(peer.fd(), SHUT_WR);
|
|
const std::string responses = peer.read_until_closed();
|
|
const auto first_response = responses.find("HTTP/1.1 404");
|
|
const auto second_response = responses.find("HTTP/1.1 401");
|
|
assert(first_response != std::string::npos);
|
|
assert(second_response != std::string::npos);
|
|
assert(first_response < second_response);
|
|
|
|
assert(app->stop().has_value());
|
|
}
|
|
|
|
void slow_header_is_bounded_by_the_read_timeout()
|
|
{
|
|
auto app = NetworkedApplication::create(managed(), config());
|
|
assert(app.has_value());
|
|
assert(app->start().has_value());
|
|
const auto port = app->health().bound_port;
|
|
|
|
RawPeer peer{port};
|
|
peer.send("GET /v1/kv/a HTTP/1.1\r\nHost: x\r\n");
|
|
const auto begin = std::chrono::steady_clock::now();
|
|
const std::string response =
|
|
peer.read_until_closed(std::chrono::seconds{5});
|
|
const auto elapsed = std::chrono::steady_clock::now() - begin;
|
|
// Bounded: the 200 ms read timeout ends the connection well before
|
|
// the client-side 5 second cap.
|
|
assert(response.empty());
|
|
assert(elapsed < std::chrono::seconds{4});
|
|
|
|
assert(app->stop().has_value());
|
|
}
|
|
|
|
void overload_returns_503_while_admin_stays_responsive()
|
|
{
|
|
NetworkedConfig cfg = config();
|
|
cfg.max_connections = 1;
|
|
auto app = NetworkedApplication::create(managed(), cfg);
|
|
assert(app.has_value());
|
|
assert(app->start().has_value());
|
|
const auto port = app->health().bound_port;
|
|
const auto admin_port = app->admin_bound_port();
|
|
|
|
// Occupy the only admitted connection with a partial request.
|
|
RawPeer held{port};
|
|
held.send("GET /v1/kv/a HTTP/1.1\r\nHost: x\r\n");
|
|
std::this_thread::sleep_for(std::chrono::milliseconds{50});
|
|
|
|
RawPeer second{port};
|
|
second.send("GET /v1/kv/a HTTP/1.1\r\nHost: x\r\n\r\n");
|
|
const std::string rejected = second.read_until_closed();
|
|
assert(rejected.starts_with("HTTP/1.1 503"));
|
|
|
|
// The administrative listener has its own admission reserve.
|
|
Void::Net::HttpClient client;
|
|
auto live = client.get("http://127.0.0.1:" +
|
|
std::to_string(admin_port) + "/live");
|
|
assert(live.has_value());
|
|
assert(live->status == 200);
|
|
|
|
assert(app->stop().has_value());
|
|
}
|
|
} // namespace
|
|
|
|
int main()
|
|
{
|
|
malformed_framing_is_rejected();
|
|
oversized_header_and_body_are_bounded();
|
|
pipelining_and_keep_alive_preserve_order();
|
|
slow_header_is_bounded_by_the_read_timeout();
|
|
overload_returns_503_while_admin_stays_responsive();
|
|
std::puts("networked-kv protocol matrix ok");
|
|
return 0;
|
|
}
|