ndjson-rpc 0.0.2

Line-delimited JSON-RPC framing with Unix socket and HTTP/1.1 chunked transports, supporting one-shot and streaming responses.
Documentation
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
//! HTTP-over-TCP transport for the same NDJSON frame shapes the Unix
//! socket [`crate::server`] speaks.
//!
//! Plaintext HTTP/1.1 only — TLS termination is the operator's concern.
//!
//! Wire shape:
//! - request: `POST /` with a JSON body matching [`Request`]; any other
//!   method or path returns `405` / `404`.
//! - one-shot reply: `200 OK` + `Content-Type: application/json` + a
//!   single [`Response`] body.
//! - streaming reply: `200 OK` + `Content-Type: application/x-ndjson` +
//!   one JSON [`Response`] frame per chunk, terminated by an `End`
//!   frame. The client cancels by closing the TCP connection.
//!
//! Auth: `Authorization: Bearer <token>`, constant-time compared
//! against the configured token via the `subtle` crate. Boot
//! validation (e.g. "anonymous access only on loopback") lives in the
//! caller.

use std::net::SocketAddr;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
use std::time::Duration;

use bytes::Bytes;
use http_body_util::{BodyExt, Full, Limited, combinators::BoxBody};
use hyper::body::{Body, Frame, Incoming};
use hyper::header::{AUTHORIZATION, CONTENT_TYPE, WWW_AUTHENTICATE};
use hyper::service::service_fn;
use hyper::{HeaderMap, Method, StatusCode};
use hyper_util::rt::{TokioIo, TokioTimer};
use tokio::net::TcpListener;
use tokio::sync::{Semaphore, mpsc};
use tokio::task::JoinHandle;
use tokio_util::sync::CancellationToken;

use crate::protocol::{EndMarker, Request, Response, ResponseOutcome, encode_line};
use crate::server::{DispatchOutcome, Handler};

/// Hard cap on request body size. Mgmt requests are tiny (a verb +
/// arg blob); 1 MiB is generous and lets us reject pathological clients
/// before they pin RAM.
const MAX_REQUEST_BODY_BYTES: usize = 1024 * 1024;

/// Channel depth for streaming responses. Each slot holds one already-
/// encoded NDJSON frame; backpressure flows naturally from a slow client
/// (TCP buffer fills → hyper stops draining → channel fills → producer
/// awaits).
const STREAM_CHANNEL_DEPTH: usize = 64;

/// Per-listener cap on concurrent live HTTP connections. Mgmt is a
/// control plane, not a data plane — a handful of concurrent operators
/// is the realistic ceiling, and capping here turns connection-flood
/// attacks into deterministic 503-ish backpressure (new connects sit
/// in the OS accept queue) instead of unbounded task spawning.
const MAX_CONCURRENT_CONNECTIONS: usize = 64;

/// Hard cap on the size of one HTTP/1 header-section read buffer.
/// Mgmt requests carry one `Authorization` header plus a small POST
/// body framing; 64 KiB is generous and matches the L1 listener cap.
const HTTP1_MAX_BUF_SIZE: usize = 64 * 1024;

/// Header-section read timeout. A slowloris client that opens a TCP
/// connection and dribbles header bytes one per second will be
/// disconnected here instead of pinning a hyper task indefinitely.
const HTTP1_HEADER_READ_TIMEOUT: Duration = Duration::from_secs(10);

#[derive(Clone, Debug)]
pub struct HttpServerConfig {
	/// Bind addresses. Empty = HTTP transport disabled; the caller
	/// should not call [`spawn_http_server`] in that case.
	pub binds: Vec<SocketAddr>,
	/// `Some(token)` enforces bearer auth; `None` means anonymous
	/// access (typically only safe on loopback — the caller is
	/// responsible for that policy decision).
	pub bearer_token: Option<Arc<str>>,
}

#[derive(thiserror::Error, Debug)]
pub enum HttpServerError {
	#[error("ndjson-rpc http: bind {addr} failed: {source}")]
	Bind { addr: SocketAddr, source: std::io::Error },
}

/// Spawn one accept loop per bind address. Returns the spawned task
/// handles; each task runs until `cancel` fires or the listener errors
/// fatally.
///
/// # Errors
/// On the first bind failure, returns the error and aborts any tasks
/// spawned for earlier (already-bound) addresses so the daemon does not
/// end up serving a partial bind set.
pub async fn spawn_http_server<H: Handler>(
	cfg: HttpServerConfig,
	handler: Arc<H>,
	cancel: CancellationToken,
) -> Result<Vec<JoinHandle<()>>, HttpServerError> {
	let mut tasks: Vec<JoinHandle<()>> = Vec::with_capacity(cfg.binds.len());
	for addr in &cfg.binds {
		let listener = match TcpListener::bind(addr).await {
			Ok(l) => l,
			Err(source) => {
				// Roll back any earlier successful binds. Cancellation
				// is the contract; we honor it for partial-failure too.
				for t in &tasks {
					t.abort();
				}
				return Err(HttpServerError::Bind { addr: *addr, source });
			}
		};
		let handler = Arc::clone(&handler);
		let cancel = cancel.clone();
		let token = cfg.bearer_token.clone();
		let bind_addr = *addr;
		tasks.push(tokio::spawn(async move {
			run_accept_loop(listener, handler, token, cancel, bind_addr).await;
		}));
	}
	Ok(tasks)
}

async fn run_accept_loop<H: Handler>(
	listener: TcpListener,
	handler: Arc<H>,
	token: Option<Arc<str>>,
	cancel: CancellationToken,
	bind_addr: SocketAddr,
) {
	tracing::info!(%bind_addr, auth = if token.is_some() { "bearer" } else { "anonymous" }, "mgmt http listening");
	let semaphore = Arc::new(Semaphore::new(MAX_CONCURRENT_CONNECTIONS));
	loop {
		// Reserve a connection slot BEFORE accept so a flood of
		// connects backs up in the OS accept queue rather than
		// causing us to spawn an unbounded number of hyper drivers
		// that all then have to serialize behind the cap.
		let permit = tokio::select! {
			biased;
			() = cancel.cancelled() => return,
			p = Arc::clone(&semaphore).acquire_owned() => match p {
				Ok(p) => p,
				// Semaphore::close is the only way `acquire_owned`
				// errors. We never close, so this is unreachable in
				// practice; treat it as a hard stop to avoid a spin
				// loop if the invariant ever breaks.
				Err(_) => return,
			},
		};

		tokio::select! {
			biased;
			() = cancel.cancelled() => return,
			res = listener.accept() => {
				let (stream, peer) = match res {
					Ok(v) => v,
					Err(e) => {
						tracing::debug!(?e, %bind_addr, "mgmt http accept error");
						drop(permit);
						continue;
					}
				};
				let handler = Arc::clone(&handler);
				let token = token.clone();
				tokio::spawn(async move {
					let _permit = permit; // released when this task exits
					let io = TokioIo::new(stream);
					let svc = service_fn(move |req| {
						let handler = Arc::clone(&handler);
						let token = token.clone();
						async move { handle_request(req, handler, token, peer).await }
					});
					let mut builder = hyper::server::conn::http1::Builder::new();
					builder
						.keep_alive(false)
						.max_buf_size(HTTP1_MAX_BUF_SIZE)
						.timer(TokioTimer::new())
						.header_read_timeout(HTTP1_HEADER_READ_TIMEOUT);
					if let Err(e) = builder.serve_connection(io, svc).await {
						tracing::debug!(?e, %peer, "mgmt http connection ended");
					}
				});
			}
		}
	}
}

type RespBody = BoxBody<Bytes, std::io::Error>;

async fn handle_request<H: Handler>(
	req: hyper::Request<Incoming>,
	handler: Arc<H>,
	token: Option<Arc<str>>,
	_peer: SocketAddr,
) -> Result<hyper::Response<RespBody>, std::convert::Infallible> {
	// Method / path gating happens before auth so a misrouted client
	// gets a deterministic 4xx instead of an auth failure that masks
	// the real problem.
	if req.uri().path() != "/" {
		return Ok(simple_status(StatusCode::NOT_FOUND));
	}
	if req.method() != Method::POST {
		return Ok(simple_status(StatusCode::METHOD_NOT_ALLOWED));
	}
	if let Some(expected) = &token
		&& !verify_bearer(req.headers(), expected)
	{
		return Ok(unauthorized());
	}
	let body_bytes = match read_request_body(req.into_body()).await {
		Ok(b) => b,
		Err(BodyReadError::TooLarge) => {
			return Ok(text_status(
				StatusCode::PAYLOAD_TOO_LARGE,
				"request body exceeds management transport limit",
			));
		}
		Err(BodyReadError::Io(e)) => {
			return Ok(text_status(StatusCode::BAD_REQUEST, &format!("body read failed: {e}")));
		}
	};
	let request = match serde_json::from_slice::<Request>(&body_bytes) {
		Ok(r) => r,
		Err(e) => return Ok(text_status(StatusCode::BAD_REQUEST, &format!("json parse: {e}"))),
	};
	let id = request.id;
	match handler.dispatch(request).await {
		DispatchOutcome::OneShot(Ok(value)) => {
			Ok(oneshot_response(&Response { id, outcome: ResponseOutcome::Result { result: value } }))
		}
		DispatchOutcome::OneShot(Err(error)) => {
			Ok(oneshot_response(&Response { id, outcome: ResponseOutcome::Error { error } }))
		}
		DispatchOutcome::Stream(stream) => Ok(streaming_response(id, stream)),
	}
}

/// Constant-time bearer-token check.
///
/// `subtle::ConstantTimeEq` runs in time independent of where the
/// mismatch is, defeating timing-side-channel guesses against the
/// token. A length mismatch short-circuits to `false` but still touches
/// the expected slice once so the call shape stays uniform across
/// equal- and unequal-length inputs.
fn verify_bearer(headers: &HeaderMap, expected: &Arc<str>) -> bool {
	use subtle::ConstantTimeEq;
	let Some(value) = headers.get(AUTHORIZATION) else {
		return false;
	};
	let Ok(s) = value.to_str() else { return false };
	let Some(token) = s.strip_prefix("Bearer ") else { return false };
	let exp = expected.as_bytes();
	let got = token.as_bytes();
	if exp.len() != got.len() {
		// Touch the expected slice so the early-exit branch still does
		// the same work as a length-equal compare (defence in depth —
		// the length is recoverable from network framing anyway, but
		// keep the codepath uniform).
		let _ = exp.ct_eq(exp);
		return false;
	}
	bool::from(exp.ct_eq(got))
}

enum BodyReadError {
	TooLarge,
	Io(String),
}

async fn read_request_body(body: Incoming) -> Result<Bytes, BodyReadError> {
	let limited = Limited::new(body, MAX_REQUEST_BODY_BYTES);
	match limited.collect().await {
		Ok(c) => Ok(c.to_bytes()),
		Err(e) => {
			// `Limited` boxes the underlying error; we discriminate
			// "too large" from "io" by downcasting to `LengthLimitError`.
			if e.downcast_ref::<http_body_util::LengthLimitError>().is_some() {
				Err(BodyReadError::TooLarge)
			} else {
				Err(BodyReadError::Io(e.to_string()))
			}
		}
	}
}

fn oneshot_response(frame: &Response) -> hyper::Response<RespBody> {
	let body_bytes = match serde_json::to_vec(frame) {
		Ok(b) => Bytes::from(b),
		Err(e) => {
			tracing::error!(?e, "mgmt http oneshot encode failed");
			return text_status(StatusCode::INTERNAL_SERVER_ERROR, "encode failed");
		}
	};
	build_response(StatusCode::OK, "application/json", full_body(body_bytes))
}

fn streaming_response(
	id: u64,
	mut stream: Box<dyn crate::server::EventStream + Send>,
) -> hyper::Response<RespBody> {
	// Channel decouples the stream producer task from hyper's body
	// poll loop. When hyper drops the body (client disconnect or
	// connection error) the receiver drops, the next `tx.send` fails,
	// and the producer task terminates — which drops `stream`,
	// triggering the EventStream's own cleanup. That is the
	// streaming-verb cancellation contract.
	let (tx, rx) = mpsc::channel::<Bytes>(STREAM_CHANNEL_DEPTH);
	tokio::spawn(async move {
		loop {
			let Some(event) = stream.next_event().await else {
				let end = Response { id, outcome: ResponseOutcome::End { end: EndMarker::default() } };
				if let Ok(bytes) = encode_line(&end) {
					let _ = tx.send(Bytes::from(bytes)).await;
				}
				return;
			};
			let frame = Response { id, outcome: ResponseOutcome::Event { event } };
			let bytes = match encode_line(&frame) {
				Ok(b) => Bytes::from(b),
				Err(e) => {
					tracing::error!(?e, id, "mgmt http stream encode failed");
					return;
				}
			};
			if tx.send(bytes).await.is_err() {
				// Client disconnected; drop the stream and exit.
				return;
			}
		}
	});
	let body = ChannelBody { rx }.boxed();
	build_response(StatusCode::OK, "application/x-ndjson", body)
}

struct ChannelBody {
	rx: mpsc::Receiver<Bytes>,
}

impl Body for ChannelBody {
	type Data = Bytes;
	type Error = std::io::Error;

	fn poll_frame(
		mut self: Pin<&mut Self>,
		cx: &mut Context<'_>,
	) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> {
		match self.rx.poll_recv(cx) {
			Poll::Ready(Some(b)) => Poll::Ready(Some(Ok(Frame::data(b)))),
			Poll::Ready(None) => Poll::Ready(None),
			Poll::Pending => Poll::Pending,
		}
	}
}

fn build_response(
	status: StatusCode,
	content_type: &'static str,
	body: RespBody,
) -> hyper::Response<RespBody> {
	let mut resp = hyper::Response::new(body);
	*resp.status_mut() = status;
	resp.headers_mut().insert(CONTENT_TYPE, content_type.parse().expect("static content type"));
	resp
}

fn full_body(bytes: Bytes) -> RespBody {
	Full::new(bytes).map_err(|never: std::convert::Infallible| match never {}).boxed()
}

fn simple_status(status: StatusCode) -> hyper::Response<RespBody> {
	let mut resp = hyper::Response::new(full_body(Bytes::new()));
	*resp.status_mut() = status;
	resp
}

fn text_status(status: StatusCode, body: &str) -> hyper::Response<RespBody> {
	let mut resp = hyper::Response::new(full_body(Bytes::copy_from_slice(body.as_bytes())));
	*resp.status_mut() = status;
	resp
		.headers_mut()
		.insert(CONTENT_TYPE, "text/plain; charset=utf-8".parse().expect("static content type"));
	resp
}

fn unauthorized() -> hyper::Response<RespBody> {
	let mut resp = simple_status(StatusCode::UNAUTHORIZED);
	resp.headers_mut().insert(WWW_AUTHENTICATE, "Bearer".parse().expect("static auth scheme"));
	resp
}

#[cfg(test)]
mod tests {
	use super::*;

	fn header_map(values: &[(hyper::header::HeaderName, &str)]) -> HeaderMap {
		let mut h = HeaderMap::new();
		for (name, val) in values {
			h.insert(name.clone(), val.parse().expect("valid header"));
		}
		h
	}

	#[test]
	fn verify_bearer_accepts_correct_token() {
		let token: Arc<str> = "s3cret".into();
		let headers = header_map(&[(AUTHORIZATION, "Bearer s3cret")]);
		assert!(verify_bearer(&headers, &token));
	}

	#[test]
	fn verify_bearer_rejects_wrong_token() {
		let token: Arc<str> = "s3cret".into();
		let headers = header_map(&[(AUTHORIZATION, "Bearer wrongx")]);
		assert!(!verify_bearer(&headers, &token));
	}

	#[test]
	fn verify_bearer_rejects_missing_header() {
		let token: Arc<str> = "s3cret".into();
		let headers = HeaderMap::new();
		assert!(!verify_bearer(&headers, &token));
	}

	#[test]
	fn verify_bearer_rejects_non_bearer_scheme() {
		let token: Arc<str> = "s3cret".into();
		let headers = header_map(&[(AUTHORIZATION, "Basic dXNlcjpwYXNz")]);
		assert!(!verify_bearer(&headers, &token));
	}

	#[test]
	fn verify_bearer_rejects_length_mismatch_without_panic() {
		// The length-mismatch branch must reject without panicking and
		// without leaking the prefix-match boundary via early return.
		let token: Arc<str> = "s3cret".into();
		let headers = header_map(&[(AUTHORIZATION, "Bearer s3")]);
		assert!(!verify_bearer(&headers, &token));
		let headers = header_map(&[(AUTHORIZATION, "Bearer s3cretextra")]);
		assert!(!verify_bearer(&headers, &token));
	}

	#[test]
	fn verify_bearer_rejects_empty_token_value() {
		let token: Arc<str> = "s3cret".into();
		let headers = header_map(&[(AUTHORIZATION, "Bearer ")]);
		assert!(!verify_bearer(&headers, &token));
	}
}