Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,7 @@ if(ONE_TESTS)
add_subdirectory(test/unit/self)
endif()
add_subdirectory(test/cli)
if(ONE_SERVER)
add_subdirectory(test/cli/http)
endif()
endif()
16 changes: 10 additions & 6 deletions src/http/include/sourcemeta/one/http_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <latch> // std::latch
#include <memory> // std::unique_ptr, std::make_unique
#include <mutex> // std::mutex, std::scoped_lock
#include <string> // std::string
#include <string_view> // std::string_view
#include <thread> // std::thread, std::thread::hardware_concurrency
#include <unistd.h> // write, STDERR_FILENO
Expand All @@ -36,8 +37,11 @@ class HTTPServer {
template <typename RequestHandler, typename ListenCallback,
typename ErrorCallback>
HTTPServer(const std::uint16_t port, RequestHandler on_request,
ListenCallback on_listen, ErrorCallback on_error) {
HTTPServer::concurrency = std::max(1u, std::thread::hardware_concurrency());
ListenCallback on_listen, ErrorCallback on_error,
const std::string &host = {}) {
// An ephemeral listener has one bound port to report.
HTTPServer::concurrency =
port == 0 ? 1u : std::max(1u, std::thread::hardware_concurrency());
HTTPServer::round_robin.store(0, std::memory_order_relaxed);
HTTPServer::should_stop.store(false, std::memory_order_relaxed);
HTTPServer::workers_alive.store(HTTPServer::concurrency,
Expand Down Expand Up @@ -89,8 +93,8 @@ class HTTPServer {
threads.reserve(HTTPServer::concurrency);
for (unsigned int index{0}; index < HTTPServer::concurrency; ++index) {
threads.emplace_back(std::make_unique<std::thread>(
[index, port, &on_request, &on_listen, &on_error, &setup_barrier,
&setup_mutex]() -> void {
[index, port, &host, &on_request, &on_listen, &on_error,
&setup_barrier, &setup_mutex]() -> void {
// uWS provides slow-loris protection out of the box, so
// we don't have to configure any of it ourselves.
uWS::SocketContextOptions options{};
Expand Down Expand Up @@ -118,7 +122,7 @@ class HTTPServer {
{
std::scoped_lock guard{setup_mutex};
app->listen(
static_cast<int>(port),
host, static_cast<int>(port),
[port, &on_listen,
&on_error](us_listen_socket_t *const socket) -> void {
if (socket) {
Expand All @@ -127,7 +131,7 @@ class HTTPServer {
true,
reinterpret_cast<struct us_socket_t *>(socket)))};
assert(bound_port > 0);
assert(port == bound_port);
assert(port == 0 || port == bound_port);
on_listen(bound_port);
} else {
on_error(port);
Expand Down
11 changes: 11 additions & 0 deletions test/cli/http/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
find_program(NODE_BIN NAMES node REQUIRED)

sourcemeta_executable(NAMESPACE sourcemeta PROJECT one NAME http
VARIANT wire_echo SOURCES echo.cc)
target_link_libraries(sourcemeta_one_http_wire_echo
PRIVATE sourcemeta::one::http)

add_test(NAME one.http.wire COMMAND "${NODE_BIN}"
"${CMAKE_CURRENT_SOURCE_DIR}/wire.test.cjs"
"$<TARGET_FILE:sourcemeta_one_http_wire_echo>")
set_tests_properties(one.http.wire PROPERTIES TIMEOUT 30)
35 changes: 35 additions & 0 deletions test/cli/http/README.md

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general try to avoid these markdown files. Its unnecessary documentation that can easily go out of date.

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# HTTP/1.1 wire tests

The CTest target `one.http.wire` starts a test-only echo handler using
One's HTTPServer, HTTPRequest, and HTTPResponse. It binds IPv4 loopback
at an OS-selected port and announces the port on stdout. The runner waits
for the announcement, bounds socket and process operations, and shuts down
the child on success and failure. The existing Docker CTest run picks up
this test when ONE_TESTS and ONE_SERVER are enabled.

Run from a One build configured with `ONE_TESTS=ON` and `ONE_SERVER=ON`,
with Node available:

```sh
cmake --build build --target sourcemeta_one_http_wire_echo
ctest --test-dir build -R '^one.http.wire$' --output-on-failure
```

The cases are independently written from RFC 9112 and motivated by the
reviewed categories in the [h1spec revision pinned by uWebSockets v20.81.0](https://github.com/uNetworking/h1spec/tree/f0a5650a20c575fbea0f7179a3a9cfa50f20ba6e).
The test takes no runtime dependency on h1spec or Deno.

| Case | Expected outcome | Basis |
| --- | --- | --- |
| Origin-form GET and a request split across writes | 200 and exact path | RFC 9112 sections 2.2 and 3.2.1 |
| Fixed-length binary body | 200 and exact bytes | RFC 9112 section 6.3 |
| Chunked body | 200 and decoded bytes | RFC 9112 section 7.1 |
| Missing Host | 400 | RFC 9112 section 3.2 |
| Bodies of 64 and 65 bytes | Exact echo at the cap and 413 above it | One HTTPRequest::body fixture policy |

The remaining h1spec cases are outside this initial subset. In particular,
this gate excludes timeout-only cases, special method or interim-response
handling, and cases whose broad status ranges need separate RFC review.
The split-write checks complete the requests and verify the response.
TCP is a byte stream, so the test does not assume that each client write
maps to a separate server read.
43 changes: 43 additions & 0 deletions test/cli/http/echo.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <sourcemeta/core/http.h>

#include <sourcemeta/one/http.h>

#include <cstdint> // std::uint16_t
#include <exception> // std::exception_ptr
#include <iostream> // std::cout, std::endl
#include <string> // std::string

auto main() -> int {
using namespace sourcemeta::one;
const HTTPServer server{
0,
[](HTTPRequest &request, HTTPResponse &response) {
if (request.method() == "get") {
response.send(request, request.path(), Encoding::Identity);
} else {
request.body(
[](HTTPRequest &snapshot, HTTPResponse &reply, std::string body,
const bool too_big) {
if (too_big) {
reply.write_status(
sourcemeta::core::HTTP_STATUS_CONTENT_TOO_LARGE);
reply.send_without_content();
} else {
reply.send(snapshot, body, Encoding::Identity);
}
},
[](HTTPRequest &, HTTPResponse &reply, std::exception_ptr) {
reply.write_status(
sourcemeta::core::HTTP_STATUS_INTERNAL_SERVER_ERROR);
reply.send_without_content();
},
64);
}
},
[](const std::uint16_t port) { std::cout << port << std::endl; },
[](const std::uint16_t port) {
std::cerr << "Failed to listen on 127.0.0.1:" << port << '\n';
},
"127.0.0.1"};
return server.stopped_gracefully() ? 0 : 1;
}
122 changes: 122 additions & 0 deletions test/cli/http/wire.test.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
const assert = require("node:assert/strict");
const { spawn } = require("node:child_process");
const { once } = require("node:events");
const { createConnection } = require("node:net");
const { createInterface } = require("node:readline");
const { setTimeout: delay } = require("node:timers/promises");

const HOST = "127.0.0.1";
const DEADLINE = 3000;

function request(port, parts) {
return new Promise((resolve, reject) => {
const socket = createConnection({ host: HOST, port });
const chunks = [];
const deadline = setTimeout(() => socket.destroy(new Error("Response deadline exceeded")), DEADLINE);
socket.on("data", (chunk) => chunks.push(chunk));
socket.once("error", reject);
socket.once("close", (failed) => {
clearTimeout(deadline);
if (!failed) resolve(Buffer.concat(chunks));
});
socket.once("connect", async () => {
try {
for (const [index, part] of parts.entries()) {
if (index > 0) {
await delay(40);
assert.equal(chunks.length, 0, "response arrived before the request was complete");
assert.equal(socket.destroyed, false, "connection closed before the request was complete");
}
socket.write(part);
}
} catch (error) {
socket.destroy(error);
}
});
});
}

function checkResponse(raw, status, expectedBody) {
const split = raw.indexOf("\r\n\r\n");
assert.ok(split > 0, "expected a complete response header block");
const [line, ...headers] = raw.subarray(0, split).toString("latin1").split("\r\n");
assert.equal(Number(/^HTTP\/1\.1 (\d{3})(?: |$)/.exec(line)?.[1]), status, line);
if (expectedBody !== undefined) {
const body = Buffer.from(expectedBody);
assert.deepEqual(raw.subarray(split + 4), body);
assert.deepEqual(headers.filter((header) => /^content-length:/i.test(header))
.map((header) => header.split(":")[1].trim()), [String(body.length)]);
}
}

async function run(port) {
const get = "GET /example?query=1 HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n";
const post = "POST /echo HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n";
const binary = Buffer.from([0, 0x41, 0xff, 0x0a]);
// These cases re-express behaviors covered by h1spec. See README.md.
const cases = [
["GET request target", [get], 200, "/example"],
["request line and header split across writes", [get.slice(0, 9), get.slice(9, 30), get.slice(30)], 200, "/example"],
["fixed-length binary body", [post + "Content-Length: 4\r\n\r\n", binary], 200, binary],
["chunked body", [post + "Transfer-Encoding: chunked\r\n\r\n2\r\nhe\r\n3\r\nllo\r\n0\r\n\r\n"], 200, "hello"],
["missing Host", ["GET /example HTTP/1.1\r\nConnection: close\r\n\r\n"], 400],
// The body limit is specific to the test fixture and exercises One's wrapper.
["body at the limit", [post + "Content-Length: 64\r\n\r\n" + "a".repeat(64)], 200, "a".repeat(64)],
["body above the limit", [post + "Content-Length: 65\r\n\r\n" + "a".repeat(65)], 413],
];
for (const [name, parts, status, body] of cases) {
try {
checkResponse(await request(port, parts), status, body);
console.log("PASS " + name);
} catch (error) {
throw new Error(name + ": " + error.message, { cause: error });
}
}
}

async function main() {
const child = spawn(process.argv[2], [], { stdio: ["ignore", "pipe", "inherit"] });
const lines = createInterface({ input: child.stdout });
const ready = once(lines, "line").then(([line]) => Number(line));
const closed = once(child, "close");
let timeout;
let failed = false;
const readinessDeadline = new Promise((_, reject) => {
timeout = setTimeout(() => reject(new Error(
"Echo server did not announce readiness before the deadline"
)), DEADLINE);
});
try {
const port = await Promise.race([
ready,
readinessDeadline,
closed.then(([code, signal]) => { throw new Error("Echo server exited before readiness: " + (signal || code)); }),
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
]);
clearTimeout(timeout);
assert.ok(Number.isInteger(port) && port > 0 && port <= 65535, "expected an ephemeral port");
await run(port);
} catch (error) {
failed = true;
throw error;
} finally {
clearTimeout(timeout);
lines.close();
child.kill("SIGTERM");
const kill = setTimeout(() => child.kill("SIGKILL"), DEADLINE);
try {
const [code, signal] = await closed;
assert.equal(signal, null, "echo server terminated by " + signal);
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
assert.equal(code, 0, "expected a graceful server exit");
} catch (shutdownError) {
if (!failed) throw shutdownError;
console.error("Echo server cleanup also failed:", shutdownError.message);
} finally {
clearTimeout(kill);
}
}
}

main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
Loading