Skip to content
Merged
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
19 changes: 11 additions & 8 deletions benchmarks/hello/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
// Copyright, 2026, by Samuel Williams.

use base_http::{
InterimHandler, InterimResponses, Request, Response, Status,
Request, Response, Status,
body::{BufferedBody, ReadableBody},
};
use base_http_server::Handler;
use base_http_server::{Handler, ServerContext};
use base_scheduler::{
Cancellation, SocketAccept, SocketClose, SocketRegistration, SocketSetOption,
SocketStreamReading, SocketStreamWriting, Spawn, TcpNoDelay,
Cancellation, SocketAccept, SocketClose, SocketLocalAddress, SocketPeerAddress,
SocketRegistration, SocketSetOption, SocketStreamReading, SocketStreamWriting, Spawn,
TcpNoDelay,
};
use base_scheduler_native::{NativeOptions, NativeScheduler};
use base_scheduler_tokio::TokioScheduler;
Expand All @@ -30,15 +31,15 @@ const MAX_CONNECTIONS: usize = 1024;
const STALL_RESCUE: bool = true;

struct Hello;
impl<'h> Handler<'h> for Hello {
impl<'h> Handler<'h, ServerContext> for Hello {
type Error = Infallible;
type Body<B: ReadableBody + 'h> = BufferedBody<&'static [u8]>;

#[allow(clippy::manual_async_fn)] // Drop the unused request at the service-call boundary.
fn call<B: ReadableBody + 'h, I: InterimHandler>(
fn call<B: ReadableBody + 'h>(
&self,
_: Request<B>,
_: &mut InterimResponses<I>,
_: &mut ServerContext,
_: &Cancellation,
) -> impl std::future::Future<Output = std::result::Result<Response<Self::Body<B>>, Infallible>> + Send
{
Expand Down Expand Up @@ -76,7 +77,9 @@ where
+ SocketSetOption<TcpNoDelay>
+ SocketStreamReading
+ SocketStreamWriting
+ SocketClose,
+ SocketClose
+ SocketPeerAddress
+ SocketLocalAddress,
S::Socket: 'static,
{
let listener = TcpListener::bind("127.0.0.1:0")?;
Expand Down
10 changes: 5 additions & 5 deletions crates/client/examples/client_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@
//! through the pooled client, and joins the HTTP and transport cleanup.
use base_endpoint::{AddressEndpoint, BindEndpoint, BindOptions};
use base_http::{
InterimHandler, InterimResponses, Method, Request, RequestMetadata, Response, Status,
Method, Request, RequestMetadata, Response, Status,
body::{BodyFrame, BufferedBody, ReadableBody},
};
use base_http_client::{Client, Endpoint};
use base_http_server::Handler;
use base_http_server::{Handler, ServerContext};
use base_scheduler::{Cancellation, SocketAccept, SocketClose, SocketLocalAddress};
use base_scheduler_native::NativeScheduler;
use futures_lite::future::{block_on, zip};
use std::{convert::Infallible, future::Future, net::SocketAddr};

type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
struct Echo;
impl<'h> Handler<'h> for Echo {
impl<'h> Handler<'h, ServerContext> for Echo {
type Error = Infallible;
type Body<B: ReadableBody + 'h> = B;
async fn call<B: ReadableBody + 'h, I: InterimHandler>(
async fn call<B: ReadableBody + 'h>(
&self,
request: Request<B>,
_: &mut InterimResponses<I>,
_: &mut ServerContext,
_: &Cancellation,
) -> std::result::Result<Response<B>, Infallible> {
Ok(Response::new(Status::OK, request.body))
Expand Down
6 changes: 3 additions & 3 deletions crates/client/tests/client/idle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
//! Pooled connections the server closes or writes to while idle are retired before reuse, and the request runs on a fresh connection.
use base_http::{Method, Request, RequestMetadata, Status, body::EmptyBody};
use base_http_client::{Client, Options};
use base_http_fixtures::{ClientContext, Latch, RawServer, body::payload};
use base_http_fixtures::{ClientFixture, Latch, RawServer, body::payload};
use base_scheduler::Cancellation;

const FIRST: &[u8] = b"HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nfirst";
const NEXT: &[u8] = b"HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\nnext";

// One pooled connection, so the second request must either reuse the idle connection or retire it first.
fn client(server: &RawServer) -> ClientContext {
ClientContext::new(server.endpoint()).options(Options {
fn client(server: &RawServer) -> ClientFixture {
ClientFixture::new(server.endpoint()).options(Options {
pool: base_pool::Options {
max_resources: 1,
..Default::default()
Expand Down
11 changes: 5 additions & 6 deletions crates/client/tests/https.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
// Copyright, 2026, by Samuel Williams.

#![cfg(feature = "rustls")]

use base_endpoint::{AddressEndpoint, BindEndpoint, BindOptions};
use base_http::{
InterimHandler, InterimResponses, Method, Request, Response, Status,
Method, Request, Response, Status,
body::{BodyFrame, BufferedBody, EmptyBody, ReadableBody},
};
use base_http_client::{Client, Endpoint, Error, Options};
use base_http_fixtures::socket::wait_acknowledged;
use base_http_server::Handler;
use base_http_server::{Handler, ServerContext};
use base_scheduler::{Cancellation, Clock, SocketLocalAddress};
use base_scheduler_native::NativeScheduler;
use futures_lite::future::{block_on, zip};
Expand Down Expand Up @@ -58,13 +57,13 @@ async fn payload(body: &mut impl ReadableBody) -> Vec<u8> {
}
}
struct Echo;
impl<'h> Handler<'h> for Echo {
impl<'h> Handler<'h, ServerContext> for Echo {
type Error = Infallible;
type Body<B: ReadableBody + 'h> = B;
async fn call<B: ReadableBody + 'h, I: InterimHandler>(
async fn call<B: ReadableBody + 'h>(
&self,
request: Request<B>,
_: &mut InterimResponses<I>,
_: &mut ServerContext,
_: &Cancellation,
) -> Result<Response<B>, Infallible> {
assert_eq!(request.scheme.as_deref(), Some("https"));
Expand Down
6 changes: 3 additions & 3 deletions crates/client/tests/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

//! The shared protocol cases, run between this client and the Base server for each HTTP version and transport.
use base_http::Version;
use base_http_fixtures::{Context, ServerContext, protocol::App};
use base_http_fixtures::{Context, ServerFixture, protocol::App};
use base_scheduler_native::NativeScheduler;

fn serve(app: App, version: Version) -> ServerContext<App> {
ServerContext::new().handler(app).version(version)
fn serve(app: App, version: Version) -> ServerFixture<App> {
ServerFixture::new().handler(app).version(version)
}

fn http11(app: App, test: impl AsyncFnOnce(&Context<'_, NativeScheduler, App>)) {
Expand Down
7 changes: 4 additions & 3 deletions crates/client/tests/writable/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use base_http::{
body::{BodyFrame, EmptyBody, ReadableBody, WritableBody},
};
use base_http_client::{Client, Endpoint, Error, Options};
use base_http_server::ServerContext;
use base_scheduler::Cancellation;
use base_scheduler_native::NativeScheduler;
use futures_lite::future::{block_on, poll_once, zip};
Expand Down Expand Up @@ -839,13 +840,13 @@ fn cancelled_and_dropped_open_waiters_do_not_abandon_active_exchange() {
}

struct Echo;
impl<'h> base_http_server::Handler<'h> for Echo {
impl<'h> base_http_server::Handler<'h, ServerContext> for Echo {
type Error = std::convert::Infallible;
type Body<B: ReadableBody + 'h> = B;
async fn call<B: ReadableBody + 'h, I: base_http::InterimHandler>(
async fn call<B: ReadableBody + 'h>(
&self,
request: Request<B>,
_: &mut base_http::InterimResponses<I>,
_: &mut ServerContext,
_: &Cancellation,
) -> Result<base_http::Response<B>, Self::Error> {
Ok(base_http::Response::new(
Expand Down
15 changes: 7 additions & 8 deletions crates/content/tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
// Copyright, 2026, by Samuel Williams.

//! Reading content in a `base-http-server` handler, over real connections.

use base_http::{
InterimHandler, InterimResponses, Request, Response, Status,
Request, Response, Status,
body::{BufferedBody, ReadableBody},
};
use base_http_content::{FileStore, read_content};
use base_http_fixtures::{RawPeer, ServerContext};
use base_http_server::Handler;
use base_http_fixtures::{RawPeer, ServerFixture};
use base_http_server::{Handler, ServerContext};
use base_scheduler::Cancellation;
use base_scheduler_native::NativeScheduler;
use base_web_content::{Content, ContentFormat, FieldPath, Upload};
Expand Down Expand Up @@ -40,14 +39,14 @@ struct Profiles {
next: Arc<AtomicUsize>,
}

impl<'h> Handler<'h> for Profiles {
impl<'h> Handler<'h, ServerContext> for Profiles {
type Error = Infallible;
type Body<B: ReadableBody + 'h> = BufferedBody<Vec<u8>>;

async fn call<B: ReadableBody + 'h, I: InterimHandler>(
async fn call<B: ReadableBody + 'h>(
&self,
mut request: Request<B>,
_: &mut InterimResponses<I>,
_: &mut ServerContext,
cancellation: &Cancellation,
) -> Result<Response<Self::Body<B>>, Infallible> {
let mut store = FileStore::new(&*self.scheduler, |_: &FieldPath| {
Expand Down Expand Up @@ -83,7 +82,7 @@ fn serve<T: Send + 'static>(
directory: directory.path().to_owned(),
next: next.clone(),
};
let output = ServerContext::new()
let output = ServerFixture::new()
.handler(profiles)
.run_spawned(async |context| context.raw(peer).await);
futures_lite::future::block_on(scheduler.shutdown());
Expand Down
10 changes: 5 additions & 5 deletions crates/fixtures/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

Shared test fixtures and behaviour suites for Base HTTP, following Socketry's `sus-fixtures-async-http` and the `AProtocol` and `AReadableBody` shared contexts of `async-http` and `protocol-http`. This crate is unpublished; the workspace's crates use it as a development dependency.

[`ServerContext`] serves a handler on an OS-selected loopback port and runs a test beside the server and a connected client, then tears both down in order within a bounded time:
[`ServerFixture`] serves a handler on an OS-selected loopback port and runs a test beside the server and a connected client, then tears both down in order within a bounded time:

```rust
use base_http::{Method, Status, body::EmptyBody};
use base_http_fixtures::{ServerContext, body::payload};
use base_http_fixtures::{ServerFixture, body::payload};
use base_scheduler::Cancellation;

ServerContext::new().run(async |context| {
ServerFixture::new().run(async |context| {
let request = context.request(Method::GET, "/", EmptyBody);
let mut response = context
.client()
Expand All @@ -21,8 +21,8 @@ ServerContext::new().run(async |context| {
});
```

[`ServerContext::run`] polls the server, the client and the test together on the test's thread, which makes interleavings reproducible but cannot run them in parallel. [`ServerContext::run_spawned`] instead spawns the server onto the workers of its own Native scheduler and drives the client from the test's thread, with the same bounds and teardown, so cross-thread wake-ups and races can show up.
[`ServerFixture::run`] polls the server, the client and the test together on the test's thread, which makes interleavings reproducible but cannot run them in parallel. [`ServerFixture::run_spawned`] instead spawns the server onto the workers of its own Native scheduler and drives the client from the test's thread, with the same bounds and teardown, so cross-thread wake-ups and races can show up.

[`ClientContext`] runs a client of any endpoint, such as a [`RawServer`]. Raw peers in [`raw`] speak HTTP/1 over blocking `std::net` sockets, so they can misbehave in ways the Base server cannot: responding then closing, stalling after a head, truncating a body, or closing or writing to an idle connection. [`RawPeer::split`] gives a peer separate reading and writing halves, for uploading and downloading at once from two threads. [`socket`] waits until a peer's kernel has acknowledged what was sent, so a test observes a close or unsolicited input without sleeping.
[`ClientFixture`] runs a client of any endpoint, such as a [`RawServer`]. Raw peers in [`raw`] speak HTTP/1 over blocking `std::net` sockets, so they can misbehave in ways the Base server cannot: responding then closing, stalling after a head, truncating a body, or closing or writing to an idle connection. [`RawPeer::split`] gives a peer separate reading and writing halves, for uploading and downloading at once from two threads. [`socket`] waits until a peer's kernel has acknowledged what was sent, so a test observes a close or unsolicited input without sleeping.

[`protocol_tests!`] and [`protocol_trailer_tests!`] generate the shared HTTP protocol cases for a runner, and [`readable_body_tests!`] generates the `ReadableBody` contract cases for a body type.
Loading