Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YShymaPrXmc3KYRUg2HMSs
38 lines
1.2 KiB
Rust
38 lines
1.2 KiB
Rust
//! Raw HTTP/1.1 helpers shared by the integration and TLS test binaries.
|
|
|
|
/// One request with `Connection: close`. `host` is used unless `headers`
|
|
/// already carries a `Host`.
|
|
pub fn request(
|
|
method: &str,
|
|
path: &str,
|
|
host: &str,
|
|
headers: &[(&str, &str)],
|
|
body: Option<&str>,
|
|
) -> String {
|
|
let mut req = format!("{method} {path} HTTP/1.1\r\nConnection: close\r\n");
|
|
if !headers.iter().any(|(k, _)| k.eq_ignore_ascii_case("host")) {
|
|
req.push_str(&format!("Host: {host}\r\n"));
|
|
}
|
|
for (k, v) in headers {
|
|
req.push_str(&format!("{k}: {v}\r\n"));
|
|
}
|
|
if let Some(b) = body {
|
|
req.push_str(&format!("Content-Length: {}\r\n\r\n{b}", b.len()));
|
|
} else {
|
|
req.push_str("\r\n");
|
|
}
|
|
req
|
|
}
|
|
|
|
/// Split a raw response into (status, lowercase head, body).
|
|
pub fn parse_response(buf: &[u8]) -> (u16, String, String) {
|
|
let text = String::from_utf8_lossy(buf);
|
|
let (head, body) = text.split_once("\r\n\r\n").unwrap_or((&text, ""));
|
|
let status = head
|
|
.split_whitespace()
|
|
.nth(1)
|
|
.and_then(|s| s.parse().ok())
|
|
.unwrap_or(0);
|
|
(status, head.to_lowercase(), body.to_owned())
|
|
}
|