use std::collections::HashMap;
use std::io::{BufRead, BufReader, Read, Write};
use std::net::{TcpListener, TcpStream};
use std::time::Duration;
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Request {
pub method: String,
pub path: String,
pub headers: HashMap<String, String>,
pub body: String,
}
impl Request {
pub fn header(&self, name: &str) -> Option<&str> {
self.headers
.get(&name.to_ascii_lowercase())
.map(|s| s.as_str())
}
pub fn wants_stream(&self) -> bool {
serde_json::from_str::<serde_json::Value>(&self.body)
.ok()
.and_then(|v| v.get("stream").and_then(|s| s.as_bool()))
.unwrap_or(false)
}
}
pub fn parse_request_line(line: &str) -> Option<(String, String)> {
let mut parts = line.split_whitespace();
let method = parts.next()?.to_ascii_uppercase();
let path = parts.next()?.to_string();
let version = parts.next()?;
if !version.starts_with("HTTP/1.") {
return None;
}
Some((method, path))
}
pub fn parse_header(line: &str) -> Option<(String, String)> {
let (name, value) = line.split_once(':')?;
let name = name.trim().to_ascii_lowercase();
if name.is_empty() {
return None;
}
Some((name, value.trim().to_string()))
}
pub fn render_response(status: u16, body: &str, retry_after: Option<u32>) -> String {
let reason = match status {
200 => "OK",
400 => "Bad Request",
404 => "Not Found",
409 => "Conflict",
500 => "Internal Server Error",
502 => "Bad Gateway",
503 => "Service Unavailable",
_ => "Status",
};
let mut head = format!(
"HTTP/1.1 {status} {reason}\r\n\
content-type: application/json\r\n\
content-length: {}\r\n\
connection: close\r\n",
body.len()
);
if let Some(secs) = retry_after {
head.push_str(&format!("retry-after: {secs}\r\n"));
}
head.push_str("\r\n");
head.push_str(body);
head
}
pub fn read_request(stream: &TcpStream) -> Result<Request, String> {
let mut reader = BufReader::new(stream);
let mut line = String::new();
reader
.read_line(&mut line)
.map_err(|e| format!("reading request line: {e}"))?;
let (method, path) =
parse_request_line(line.trim_end()).ok_or_else(|| format!("bad request line: {line:?}"))?;
let mut headers = HashMap::new();
loop {
let mut h = String::new();
let n = reader
.read_line(&mut h)
.map_err(|e| format!("reading headers: {e}"))?;
if n == 0 || h.trim().is_empty() {
break;
}
if let Some((k, v)) = parse_header(h.trim_end()) {
headers.insert(k, v);
}
if headers.len() > 100 {
return Err("too many headers".into());
}
}
let len: usize = headers
.get("content-length")
.and_then(|v| v.parse().ok())
.unwrap_or(0);
let mut body = vec![0u8; len];
if len > 0 {
reader
.read_exact(&mut body)
.map_err(|e| format!("reading body ({len} bytes): {e}"))?;
}
Ok(Request {
method,
path,
headers,
body: String::from_utf8_lossy(&body).to_string(),
})
}
pub fn proxy(
upstream: &str,
req: &Request,
client: &mut TcpStream,
connect_timeout: Duration,
) -> Result<(), String> {
let addr = upstream
.parse()
.map_err(|e| format!("bad upstream address {upstream}: {e}"))?;
let mut up = TcpStream::connect_timeout(&addr, connect_timeout)
.map_err(|e| format!("could not reach {upstream}: {e}"))?;
up.set_write_timeout(Some(connect_timeout)).ok();
let mut head = format!(
"{} {} HTTP/1.1\r\nhost: {}\r\ncontent-length: {}\r\nconnection: close\r\n",
req.method,
req.path,
upstream,
req.body.len()
);
for (k, v) in &req.headers {
if matches!(
k.as_str(),
"host" | "content-length" | "connection" | "keep-alive" | "transfer-encoding"
) {
continue;
}
head.push_str(&format!("{k}: {v}\r\n"));
}
head.push_str("\r\n");
up.write_all(head.as_bytes())
.and_then(|_| up.write_all(req.body.as_bytes()))
.and_then(|_| up.flush())
.map_err(|e| format!("writing to {upstream}: {e}"))?;
let mut buf = [0u8; 16 * 1024];
loop {
match up.read(&mut buf) {
Ok(0) => break,
Ok(n) => {
if client.write_all(&buf[..n]).is_err() {
break;
}
let _ = client.flush();
}
Err(e) if e.kind() == std::io::ErrorKind::Interrupted => continue,
Err(e) => return Err(format!("reading from {upstream}: {e}")),
}
}
Ok(())
}
pub struct Server {
listener: TcpListener,
}
impl Server {
pub fn bind(addr: &str) -> Result<Server, String> {
let listener =
TcpListener::bind(addr).map_err(|e| format!("could not bind {addr}: {e}"))?;
Ok(Server { listener })
}
pub fn local_addr(&self) -> String {
self.listener
.local_addr()
.map(|a| a.to_string())
.unwrap_or_default()
}
pub fn incoming(&self) -> impl Iterator<Item = std::io::Result<TcpStream>> + '_ {
self.listener.incoming()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_request_line_parses() {
assert_eq!(
parse_request_line("POST /v1/chat/completions HTTP/1.1"),
Some(("POST".into(), "/v1/chat/completions".into()))
);
assert_eq!(
parse_request_line("get /health HTTP/1.0"),
Some(("GET".into(), "/health".into())),
"the method is case-insensitive on the wire"
);
}
#[test]
fn garbage_is_not_a_request_line() {
assert_eq!(parse_request_line("SSH-2.0-OpenSSH_9.6"), None);
assert_eq!(parse_request_line(""), None);
assert_eq!(parse_request_line("GET /only-two-parts"), None);
}
#[test]
fn header_names_are_lowercased_because_clients_disagree_about_casing() {
assert_eq!(
parse_header("Content-Type: application/json"),
Some(("content-type".into(), "application/json".into()))
);
assert_eq!(
parse_header("AUTHORIZATION: Bearer sk-x "),
Some(("authorization".into(), "Bearer sk-x".into()))
);
assert_eq!(parse_header("no colon here"), None);
assert_eq!(parse_header(": empty name"), None);
}
#[test]
fn a_header_value_may_contain_colons() {
assert_eq!(
parse_header("host: 127.0.0.1:8090"),
Some(("host".into(), "127.0.0.1:8090".into()))
);
}
#[test]
fn a_rendered_response_has_a_content_length_that_matches() {
let body = r#"{"status":"ok"}"#;
let out = render_response(200, body, None);
assert!(out.starts_with("HTTP/1.1 200 OK\r\n"));
assert!(out.contains(&format!("content-length: {}", body.len())));
assert!(out.ends_with(body));
assert!(!out.contains("retry-after"));
}
#[test]
fn retry_after_appears_only_when_it_was_asked_for() {
assert!(render_response(503, "{}", Some(5)).contains("retry-after: 5"));
assert!(!render_response(409, "{}", None).contains("retry-after"));
}
#[test]
fn every_status_the_gateway_emits_has_a_reason_phrase() {
for s in [200u16, 400, 404, 409, 502, 503] {
let out = render_response(s, "{}", None);
let line = out.lines().next().unwrap();
assert!(
!line.ends_with("Status"),
"status {s} fell through to the generic phrase: {line}"
);
}
}
#[test]
fn a_streaming_request_is_recognised_from_its_body() {
let mut r = Request {
body: r#"{"model":"m","stream":true}"#.into(),
..Default::default()
};
assert!(r.wants_stream());
r.body = r#"{"model":"m","stream":false}"#.into();
assert!(!r.wants_stream());
r.body = r#"{"model":"m"}"#.into();
assert!(!r.wants_stream(), "absent means not streaming");
r.body = "not json".into();
assert!(!r.wants_stream(), "and unreadable must not panic");
}
#[test]
fn headers_are_looked_up_case_insensitively() {
let mut headers = HashMap::new();
headers.insert("authorization".to_string(), "Bearer x".to_string());
let r = Request {
headers,
..Default::default()
};
assert_eq!(r.header("Authorization"), Some("Bearer x"));
assert_eq!(r.header("AUTHORIZATION"), Some("Bearer x"));
assert_eq!(r.header("nope"), None);
}
}