use std::collections::BTreeMap;
use std::path::Path;
use std::time::Duration;
pub const RESPONSE_CAP_BYTES: usize = 8 * 1024 * 1024;
pub const REQUEST_TIMEOUT: Duration = Duration::from_secs(30);
pub const ENV_FILE: &str = ".http.env.json";
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct HttpRequest {
pub method: String,
pub url: String,
pub headers: Vec<(String, String)>,
pub body: Option<String>,
pub start: usize,
pub line: usize,
pub end: usize,
}
pub fn is_http_file(path: &Path) -> bool {
matches!(
path.extension().and_then(|e| e.to_str()),
Some("http") | Some("rest")
)
}
pub fn parse_requests(text: &str) -> Vec<HttpRequest> {
let text = text.strip_prefix('\u{feff}').unwrap_or(text);
let lines: Vec<&str> = text.lines().collect();
let mut blocks: Vec<(usize, usize)> = Vec::new();
let mut start = 0usize;
for (i, line) in lines.iter().enumerate() {
if line.trim_start().starts_with("###") {
if i > start {
blocks.push((start, i));
}
start = i;
}
}
if lines.len() > start {
blocks.push((start, lines.len()));
}
let mut out = Vec::new();
for (s, e) in blocks {
if let Some(req) = parse_block(&lines, s, e) {
out.push(req);
}
}
out
}
fn parse_block(lines: &[&str], start: usize, end: usize) -> Option<HttpRequest> {
let mut i = start;
while i < end {
let t = lines[i].trim();
if t.is_empty() || t.starts_with('#') || t.starts_with("//") {
i += 1;
continue;
}
break;
}
if i >= end {
return None;
}
let line_row = i;
let mut parts = lines[i].split_whitespace();
let first = parts.next()?;
let looks_like_method =
first.len() <= 10 && !first.is_empty() && first.chars().all(|c| c.is_ascii_uppercase());
let (method, url) = match parts.next() {
Some(second) if looks_like_method => (first.to_string(), second.to_string()),
_ => (String::from("GET"), first.to_string()),
};
i += 1;
let mut headers = Vec::new();
while i < end {
let t = lines[i].trim();
if t.is_empty() {
break;
}
if t.starts_with('#') || t.starts_with("//") {
i += 1;
continue;
}
if let Some((name, value)) = t.split_once(':') {
headers.push((name.trim().to_string(), value.trim().to_string()));
}
i += 1;
}
let mut body_lines: Vec<&str> = lines[i.min(end)..end].to_vec();
while body_lines.first().is_some_and(|l| l.trim().is_empty()) {
body_lines.remove(0);
}
while body_lines.last().is_some_and(|l| l.trim().is_empty()) {
body_lines.pop();
}
let body = if body_lines.is_empty() {
None
} else {
Some(body_lines.join("\n"))
};
Some(HttpRequest {
method,
url,
headers,
body,
start,
line: line_row,
end,
})
}
pub fn request_at(requests: &[HttpRequest], row: usize) -> Option<&HttpRequest> {
requests.iter().find(|r| r.start <= row && row < r.end)
}
pub fn load_env(dir: &Path) -> BTreeMap<String, String> {
let mut out = BTreeMap::new();
let Ok(bytes) = std::fs::read(dir.join(ENV_FILE)) else {
return out;
};
let Ok(serde_json::Value::Object(map)) = serde_json::from_slice(&bytes) else {
return out;
};
for (k, v) in map {
let val = match v {
serde_json::Value::String(s) => s,
serde_json::Value::Number(n) => n.to_string(),
serde_json::Value::Bool(b) => b.to_string(),
_ => continue,
};
out.insert(k, val);
}
out
}
pub fn substitute(text: &str, vars: &BTreeMap<String, String>) -> (String, Vec<String>) {
let mut out = String::with_capacity(text.len());
let mut missing = Vec::new();
let mut rest = text;
while let Some(open) = rest.find("{{") {
out.push_str(&rest[..open]);
let after = &rest[open + 2..];
let Some(close) = after.find("}}") else {
out.push_str(&rest[open..]);
rest = "";
break;
};
let name = after[..close].trim();
let value = if let Some(env_name) = name.strip_prefix("$env.") {
std::env::var(env_name).ok()
} else {
vars.get(name).cloned()
};
match value {
Some(v) => out.push_str(&v),
None => {
out.push_str(&rest[open..open + 2 + close + 2]);
if !missing.contains(&name.to_string()) {
missing.push(name.to_string());
}
}
}
rest = &after[close + 2..];
}
out.push_str(rest);
(out, missing)
}
#[derive(Clone, Debug)]
pub struct ResolvedRequest {
pub method: String,
pub url: String,
pub headers: Vec<(String, String)>,
pub body: Option<String>,
}
pub fn resolve(
req: &HttpRequest,
vars: &BTreeMap<String, String>,
) -> (ResolvedRequest, Vec<String>) {
let mut missing: Vec<String> = Vec::new();
let mut sub = |s: &str| {
let (v, m) = substitute(s, vars);
for name in m {
if !missing.contains(&name) {
missing.push(name);
}
}
v
};
let resolved = ResolvedRequest {
method: req.method.clone(),
url: sub(&req.url),
headers: req
.headers
.iter()
.map(|(k, v)| (k.clone(), sub(v)))
.collect(),
body: req.body.as_deref().map(&mut sub),
};
(resolved, missing)
}
pub fn to_curl(req: &ResolvedRequest) -> String {
fn sq(s: &str) -> String {
format!("'{}'", s.replace('\'', "'\\''"))
}
let mut out = format!("curl -X {} {}", req.method, sq(&req.url));
for (k, v) in &req.headers {
out.push_str(&format!(" -H {}", sq(&format!("{k}: {v}"))));
}
if let Some(body) = &req.body {
out.push_str(&format!(" --data {}", sq(body)));
}
out
}
#[derive(Clone, Debug)]
pub struct HttpResponse {
pub status: u16,
pub status_text: String,
pub http_version: String,
pub headers: Vec<(String, String)>,
pub body: Vec<u8>,
pub truncated: bool,
pub elapsed_ms: u64,
}
impl HttpResponse {
pub fn content_type(&self) -> String {
self.headers
.iter()
.find(|(k, _)| k.eq_ignore_ascii_case("content-type"))
.map(|(_, v)| {
v.split(';')
.next()
.unwrap_or("")
.trim()
.to_ascii_lowercase()
})
.unwrap_or_default()
}
}
pub fn send(req: &ResolvedRequest, timeout: Duration) -> Result<HttpResponse, String> {
use std::io::Read;
let agent = ureq::AgentBuilder::new()
.timeout(timeout)
.redirects(4)
.build();
let mut r = agent.request(&req.method, &req.url);
for (k, v) in &req.headers {
r = r.set(k, v);
}
let started = std::time::Instant::now();
let result = match &req.body {
Some(body) => r.send_string(body),
None => r.call(),
};
let resp = match result {
Ok(resp) => resp,
Err(ureq::Error::Status(_, resp)) => resp,
Err(ureq::Error::Transport(t)) => return Err(t.kind().to_string()),
};
let elapsed_ms = started.elapsed().as_millis() as u64;
let status = resp.status();
let status_text = resp.status_text().to_string();
let http_version = resp.http_version().to_string();
let mut headers: Vec<(String, String)> = Vec::new();
let mut seen = std::collections::HashSet::new();
for n in resp.headers_names() {
if seen.insert(n.clone()) {
for v in resp.all(&n) {
headers.push((n.clone(), v.to_string()));
}
}
}
let mut body = Vec::new();
let mut reader = resp.into_reader().take(RESPONSE_CAP_BYTES as u64 + 1);
reader
.read_to_end(&mut body)
.map_err(|e| format!("reading the response body: {e}"))?;
let truncated = body.len() > RESPONSE_CAP_BYTES;
body.truncate(RESPONSE_CAP_BYTES);
Ok(HttpResponse {
status,
status_text,
http_version,
headers,
body,
truncated,
elapsed_ms,
})
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ResponseKind {
Text(&'static str),
Image(&'static str),
}
pub fn response_kind(content_type: &str) -> ResponseKind {
match content_type {
"image/png" => ResponseKind::Image("png"),
"image/jpeg" | "image/jpg" => ResponseKind::Image("jpg"),
"image/gif" => ResponseKind::Image("gif"),
"image/webp" => ResponseKind::Image("webp"),
"image/bmp" => ResponseKind::Image("bmp"),
t if t == "application/json" || t.ends_with("+json") => ResponseKind::Text("jsonc"),
t if t == "application/xml" || t == "text/xml" || t.ends_with("+xml") => {
ResponseKind::Text("xml")
}
_ => ResponseKind::Text("txt"),
}
}
pub fn response_doc(raw_request_line: &str, resp: &HttpResponse) -> (&'static str, String) {
let kind = response_kind(&resp.content_type());
let ext = match kind {
ResponseKind::Text(e) => e,
ResponseKind::Image(_) => "txt",
};
let (open, close): (&str, &str) = match ext {
"xml" => ("<!-- ", " -->"),
_ => ("// ", ""),
};
let esc = |s: &str| -> String {
if ext == "xml" {
s.replace("-->", "-->")
} else {
s.to_string()
}
};
let mut doc = String::new();
let size = resp.body.len();
doc.push_str(&format!(
"{open}{} \u{2192} HTTP/{} {} {} \u{00b7} {} ms \u{00b7} {size} B{close}\n",
esc(raw_request_line),
resp.http_version.trim_start_matches("HTTP/"),
resp.status,
esc(&resp.status_text),
resp.elapsed_ms,
));
for (k, v) in &resp.headers {
doc.push_str(&format!("{open}{}: {}{close}\n", esc(k), esc(v)));
}
if resp.truncated {
doc.push_str(&format!(
"{open}body cut at {RESPONSE_CAP_BYTES} bytes{close}\n"
));
}
doc.push('\n');
let body_text = String::from_utf8_lossy(&resp.body);
if ext == "jsonc" {
match serde_json::from_str::<serde_json::Value>(&body_text) {
Ok(v) => {
doc.push_str(
&serde_json::to_string_pretty(&v).unwrap_or_else(|_| body_text.to_string()),
);
}
Err(_) => doc.push_str(&body_text),
}
} else {
doc.push_str(&body_text);
}
if !doc.ends_with('\n') {
doc.push('\n');
}
(ext, doc)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_repeated_response_header_keeps_every_value() {
use std::io::{Read, Write};
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let port = listener.local_addr().unwrap().port();
std::thread::spawn(move || {
let (mut s, _) = listener.accept().unwrap();
let mut buf = [0u8; 4096];
let _ = s.read(&mut buf);
let _ = s.write_all(
b"HTTP/1.1 200 OK\r\nset-cookie: a=1\r\nset-cookie: b=2\r\ncontent-length: 0\r\nconnection: close\r\n\r\n",
);
});
let resp = send(
&ResolvedRequest {
method: String::from("GET"),
url: format!("http://127.0.0.1:{port}/"),
headers: Vec::new(),
body: None,
},
Duration::from_secs(10),
)
.unwrap();
let cookies: Vec<_> = resp
.headers
.iter()
.filter(|(n, _)| n == "set-cookie")
.map(|(_, v)| v.as_str())
.collect();
assert_eq!(cookies, ["a=1", "b=2"]);
}
const THREE: &str = "\
# users, with a token from the env file
GET {{host}}/users\n\
Authorization: Bearer {{token}}\n\
\n\
###\n\
POST {{host}}/users HTTP/1.1\n\
Content-Type: application/json\n\
\n\
{\"name\": \"ada\"}\n\
\n\
### delete one\n\
DELETE {{host}}/users/1\n";
#[test]
fn parses_three_requests_with_separators_headers_and_bodies() {
let reqs = parse_requests(THREE);
assert_eq!(reqs.len(), 3);
assert_eq!(reqs[0].method, "GET");
assert_eq!(reqs[0].url, "{{host}}/users");
assert_eq!(
reqs[0].headers,
vec![(
String::from("Authorization"),
String::from("Bearer {{token}}")
)]
);
assert_eq!(reqs[0].body, None);
assert_eq!(reqs[1].method, "POST");
assert_eq!(reqs[1].body.as_deref(), Some("{\"name\": \"ada\"}"));
assert_eq!(reqs[2].method, "DELETE");
assert_eq!(reqs[2].line, 11, "the request line, not the ### above it");
let bare = parse_requests("example.com/health\n");
assert_eq!(bare[0].method, "GET");
assert_eq!(bare[0].url, "example.com/health");
}
#[test]
fn request_at_covers_each_whole_block() {
let reqs = parse_requests(THREE);
assert_eq!(request_at(&reqs, 0).unwrap().method, "GET");
assert_eq!(request_at(&reqs, 3).unwrap().method, "GET");
assert_eq!(
request_at(&reqs, 4).unwrap().method,
"POST",
"the ### row belongs to the next block"
);
assert_eq!(request_at(&reqs, 8).unwrap().method, "POST");
assert_eq!(request_at(&reqs, 11).unwrap().method, "DELETE");
assert!(request_at(&reqs, 99).is_none());
}
#[test]
fn substitution_fills_names_env_and_reports_missing() {
let mut vars = BTreeMap::new();
vars.insert(String::from("host"), String::from("http://x.test"));
let (s, missing) = substitute("{{host}}/u/{{id}}", &vars);
assert_eq!(s, "http://x.test/u/{{id}}");
assert_eq!(missing, vec![String::from("id")]);
unsafe { std::env::set_var("CROFT_HTTP_TEST_VAR", "7") };
let (s, missing) = substitute("v={{$env.CROFT_HTTP_TEST_VAR}}", &vars);
assert_eq!(s, "v=7");
assert!(missing.is_empty());
let (reqs, vars2) = (parse_requests(THREE), {
let mut v = vars.clone();
v.insert(String::from("token"), String::from("s3cret"));
v
});
let (resolved, missing) = resolve(&reqs[0], &vars2);
assert!(missing.is_empty());
assert_eq!(resolved.url, "http://x.test/users");
assert_eq!(resolved.headers[0].1, "Bearer s3cret");
}
#[test]
fn malformed_lines_and_boms_degrade_honestly() {
let reqs = parse_requests("GET\nAccept: x\n\n###\nGET http://ok/\n");
assert_eq!(reqs.len(), 2, "the malformed block is not dropped");
assert_eq!(reqs[0].url, "GET");
assert_eq!(request_at(&reqs, 1).unwrap().url, "GET");
let reqs = parse_requests("\u{feff}GET http://x/a\n");
assert_eq!(reqs[0].method, "GET");
assert_eq!(reqs[0].url, "http://x/a");
}
#[test]
fn a_missing_name_is_reported_once_across_the_request() {
let req = HttpRequest {
method: String::from("GET"),
url: String::from("{{tok}}/{{host}}"),
headers: vec![(String::from("X-A"), String::from("{{tok}}"))],
body: None,
start: 0,
line: 0,
end: 1,
};
let (_, missing) = resolve(&req, &BTreeMap::new());
assert_eq!(missing, vec![String::from("tok"), String::from("host")]);
}
#[test]
fn xml_prologue_survives_a_hostile_header_value() {
let resp = HttpResponse {
status: 200,
status_text: String::from("OK"),
http_version: String::from("HTTP/1.1"),
headers: vec![
(String::from("content-type"), String::from("text/xml")),
(String::from("x-evil"), String::from("a --> <boom/> <!--")),
],
body: b"<a/>".to_vec(),
truncated: false,
elapsed_ms: 1,
};
let (ext, doc) = response_doc("GET x", &resp);
assert_eq!(ext, "xml");
assert!(
doc.contains("a --> <boom/> <!-- -->"),
"the header stays inside its comment: {doc}"
);
assert!(!doc.contains("a --> <boom/>"), "no raw escape survives");
}
#[test]
fn env_file_loads_flat_values_only() {
let dir = tempfile::TempDir::new().unwrap();
std::fs::write(
dir.path().join(ENV_FILE),
"{\"host\": \"http://a\", \"port\": 8080, \"on\": true, \"nested\": {\"x\": 1}}",
)
.unwrap();
let vars = load_env(dir.path());
assert_eq!(vars.get("host").map(String::as_str), Some("http://a"));
assert_eq!(vars.get("port").map(String::as_str), Some("8080"));
assert_eq!(vars.get("on").map(String::as_str), Some("true"));
assert!(!vars.contains_key("nested"));
assert!(load_env(&dir.path().join("missing")).is_empty());
}
#[test]
fn curl_line_quotes_for_a_posix_shell() {
let req = ResolvedRequest {
method: String::from("POST"),
url: String::from("http://x.test/it's"),
headers: vec![(String::from("X-A"), String::from("b c"))],
body: Some(String::from("{\"q\": \"a'b\"}")),
};
let curl = to_curl(&req);
assert_eq!(
curl,
"curl -X POST 'http://x.test/it'\\''s' -H 'X-A: b c' --data '{\"q\": \"a'\\''b\"}'"
);
}
#[test]
fn response_documents_render_by_content_type() {
let mut resp = HttpResponse {
status: 200,
status_text: String::from("OK"),
http_version: String::from("HTTP/1.1"),
headers: vec![(
String::from("content-type"),
String::from("application/json; charset=utf-8"),
)],
body: b"{\"a\":[1,2]}".to_vec(),
truncated: false,
elapsed_ms: 42,
};
let (ext, doc) = response_doc("GET {{host}}/users", &resp);
assert_eq!(ext, "jsonc");
assert!(doc.starts_with(
"// GET {{host}}/users \u{2192} HTTP/1.1 200 OK \u{00b7} 42 ms \u{00b7} 11 B\n"
));
assert!(doc.contains("// content-type: application/json"));
assert!(
doc.contains("{\n \"a\": [\n 1,\n 2\n ]\n}"),
"pretty-printed: {doc}"
);
resp.headers[0].1 = String::from("text/xml");
resp.body = b"<a/>".to_vec();
let (ext, doc) = response_doc("GET x", &resp);
assert_eq!(ext, "xml");
assert!(doc.contains("<!-- GET x"));
assert!(doc.ends_with("<a/>\n"));
resp.headers.clear();
resp.truncated = true;
let (ext, doc) = response_doc("GET x", &resp);
assert_eq!(ext, "txt");
assert!(doc.contains("// body cut at"));
assert_eq!(response_kind("image/png"), ResponseKind::Image("png"));
assert_eq!(
response_kind("application/hal+json"),
ResponseKind::Text("jsonc")
);
}
}