Initial impl

This commit is contained in:
Torbjörn Molin committed 2026-10-19 09:31:57 +02:00
commit 4b729c5d32
133 files changed
+20475

No files matched your search

+96
View File
@@ -0,0 +1,96 @@
//! A runtime-agnostic cancellation token.
//!
//! It plays the role of `tokio_util::sync::CancellationToken` without pulling
//! Tokio into `core`. It can be polled synchronously (for curl's progress
//! callback) or awaited (for `select!`).
use std::future::Future;
use std::pin::Pin;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::task::{Context, Poll, Waker};
#[derive(Clone, Default)]
pub struct CancelToken {
inner: Arc<Inner>,
}
#[derive(Default)]
struct Inner {
cancelled: AtomicBool,
wakers: Mutex<Vec<Waker>>,
}
impl std::fmt::Debug for CancelToken {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CancelToken")
.field("cancelled", &self.is_cancelled())
.finish()
}
}
impl PartialEq for CancelToken {
fn eq(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.inner, &other.inner)
}
}
impl CancelToken {
pub fn new() -> Self {
Self::default()
}
pub fn cancel(&self) {
self.inner.cancelled.store(true, Ordering::SeqCst);
let wakers = std::mem::take(&mut *self.inner.wakers.lock().unwrap());
for w in wakers {
w.wake();
}
}
pub fn is_cancelled(&self) -> bool {
self.inner.cancelled.load(Ordering::SeqCst)
}
/// A future that completes once [`cancel`](Self::cancel) has been called.
pub fn cancelled(&self) -> Cancelled {
Cancelled { token: self.clone() }
}
}
pub struct Cancelled {
token: CancelToken,
}
impl Future for Cancelled {
type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
if self.token.is_cancelled() {
return Poll::Ready(());
}
let mut wakers = self.token.inner.wakers.lock().unwrap();
// Re-check under the lock so a concurrent cancel() cannot be missed.
if self.token.is_cancelled() {
return Poll::Ready(());
}
if !wakers.iter().any(|w| w.will_wake(cx.waker())) {
wakers.push(cx.waker().clone());
}
Poll::Pending
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn cancel_is_shared_between_clones() {
let a = CancelToken::new();
let b = a.clone();
assert!(!b.is_cancelled());
a.cancel();
assert!(b.is_cancelled());
}
}
+139
View File
@@ -0,0 +1,139 @@
//! A collection is a folder of `.http` files. Scanning builds the sidebar
//! tree (folder → file → requests) and parses every file.
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use crate::httpfile::HttpFile;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Node {
Folder {
name: String,
path: PathBuf,
children: Vec<Node>,
},
File {
name: String,
path: PathBuf,
},
}
impl Node {
pub fn name(&self) -> &str {
match self {
Node::Folder { name, .. } | Node::File { name, .. } => name,
}
}
pub fn path(&self) -> &Path {
match self {
Node::Folder { path, .. } | Node::File { path, .. } => path,
}
}
}
#[derive(Debug, Clone)]
pub struct Collection {
pub root: PathBuf,
pub tree: Node,
/// Parsed files by absolute path.
pub files: BTreeMap<PathBuf, HttpFile>,
}
pub fn is_http_file(path: &Path) -> bool {
matches!(path.extension().and_then(|e| e.to_str()), Some("http" | "rest"))
}
fn skip_dir(name: &str) -> bool {
name.starts_with('.') || matches!(name, "node_modules" | "target")
}
/// Scans `root` recursively. Folders without `.http` files are left out.
pub fn scan(root: &Path) -> std::io::Result<Collection> {
let mut files = BTreeMap::new();
let tree = scan_dir(root, &mut files)?.unwrap_or_else(|| Node::Folder {
name: dir_name(root),
path: root.to_path_buf(),
children: Vec::new(),
});
Ok(Collection {
root: root.to_path_buf(),
tree,
files,
})
}
fn dir_name(p: &Path) -> String {
p.file_name()
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_else(|| p.display().to_string())
}
fn scan_dir(dir: &Path, files: &mut BTreeMap<PathBuf, HttpFile>) -> std::io::Result<Option<Node>> {
let mut entries: Vec<_> = std::fs::read_dir(dir)?.filter_map(Result::ok).collect();
entries.sort_by_key(|e| e.file_name().to_string_lossy().to_lowercase());
let mut folders = Vec::new();
let mut leaves = Vec::new();
for e in entries {
let path = e.path();
let name = e.file_name().to_string_lossy().into_owned();
let Ok(ft) = e.file_type() else { continue };
if ft.is_dir() {
if skip_dir(&name) {
continue;
}
if let Some(n) = scan_dir(&path, files)? {
folders.push(n);
}
} else if is_http_file(&path) {
let text = std::fs::read_to_string(&path)?;
files.insert(path.clone(), HttpFile::parse(text));
leaves.push(Node::File { name, path });
}
}
if folders.is_empty() && leaves.is_empty() {
return Ok(None);
}
folders.extend(leaves);
Ok(Some(Node::Folder {
name: dir_name(dir),
path: dir.to_path_buf(),
children: folders,
}))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn scans_folders_files_and_requests() {
let dir = tempfile::tempdir().unwrap();
let root = dir.path();
std::fs::write(
root.join("users.http"),
"### List\nGET /users\n\n### Get\nGET /users/1\n",
)
.unwrap();
std::fs::create_dir_all(root.join("orders/payloads")).unwrap();
std::fs::write(root.join("orders/create.http"), "POST /orders\n").unwrap();
std::fs::write(root.join("orders/payloads/order.json"), "{}").unwrap();
std::fs::create_dir(root.join(".git")).unwrap();
std::fs::write(root.join(".git/x.http"), "GET /").unwrap();
std::fs::create_dir(root.join("empty")).unwrap();
let c = scan(root).unwrap();
let Node::Folder { children, .. } = &c.tree else {
panic!()
};
let names: Vec<&str> = children.iter().map(Node::name).collect();
assert_eq!(names, vec!["orders", "users.http"]);
let Node::Folder { children: orders, .. } = &children[0] else {
panic!()
};
assert_eq!(orders.len(), 1);
assert_eq!(c.files.len(), 2);
assert_eq!(c.files[&root.join("users.http")].request_count(), 2);
}
}
+281
View File
@@ -0,0 +1,281 @@
//! A small in-memory cookie jar (one per collection in the app).
use std::time::{Duration, SystemTime};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Cookie {
pub name: String,
pub value: String,
pub domain: String,
/// Set without a `Domain` attribute: only sent to exactly this host.
pub host_only: bool,
pub path: String,
pub secure: bool,
pub http_only: bool,
pub expires: Option<SystemTime>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct CookieJar {
cookies: Vec<Cookie>,
}
/// The parts of a URL the jar needs.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UrlParts {
pub scheme: String,
pub host: String,
pub path: String,
}
pub fn url_parts(url: &str) -> Option<UrlParts> {
let (scheme, rest) = url.split_once("://")?;
let end = rest.find(['/', '?', '#']).unwrap_or(rest.len());
let authority = &rest[..end];
let host_port = authority.rsplit_once('@').map(|(_, h)| h).unwrap_or(authority);
let host = if host_port.starts_with('[') {
host_port.split(']').next().unwrap_or("").trim_start_matches('[')
} else {
host_port.split(':').next().unwrap_or("")
};
let path_q = &rest[end..];
let path = path_q.split(['?', '#']).next().unwrap_or("");
Some(UrlParts {
scheme: scheme.to_ascii_lowercase(),
host: host.to_ascii_lowercase(),
path: if path.is_empty() { "/".into() } else { path.into() },
})
}
fn default_path(path: &str) -> String {
match path.rfind('/') {
Some(0) | None => "/".into(),
Some(i) => path[..i].into(),
}
}
fn domain_matches(host: &str, domain: &str) -> bool {
host == domain || (host.ends_with(domain) && host[..host.len() - domain.len()].ends_with('.'))
}
fn path_matches(req: &str, cookie: &str) -> bool {
req == cookie || (req.starts_with(cookie) && (cookie.ends_with('/') || req[cookie.len()..].starts_with('/')))
}
impl CookieJar {
pub fn new() -> Self {
Self::default()
}
pub fn cookies(&self) -> &[Cookie] {
&self.cookies
}
pub fn clear(&mut self) {
self.cookies.clear();
}
pub fn remove(&mut self, index: usize) {
if index < self.cookies.len() {
self.cookies.remove(index);
}
}
/// Stores cookies from the `Set-Cookie` headers of a response to `url`.
pub fn store<'a>(&mut self, url: &str, set_cookies: impl IntoIterator<Item = &'a str>, now: SystemTime) {
let Some(u) = url_parts(url) else { return };
for sc in set_cookies {
if let Some(c) = parse_set_cookie(sc, &u, now) {
self.cookies
.retain(|o| !(o.name == c.name && o.domain == c.domain && o.path == c.path));
if c.expires.is_none_or(|e| e > now) {
self.cookies.push(c);
}
}
}
}
/// The `Cookie` header value for a request to `url`, if any cookie applies.
pub fn header_for(&self, url: &str, now: SystemTime) -> Option<String> {
let u = url_parts(url)?;
let mut matching: Vec<&Cookie> = self
.cookies
.iter()
.filter(|c| {
c.expires.is_none_or(|e| e > now)
&& (!c.secure || u.scheme == "https")
&& if c.host_only {
u.host == c.domain
} else {
domain_matches(&u.host, &c.domain)
}
&& path_matches(&u.path, &c.path)
})
.collect();
if matching.is_empty() {
return None;
}
// Longer paths first (RFC 6265 5.4).
matching.sort_by_key(|c| std::cmp::Reverse(c.path.len()));
Some(
matching
.iter()
.map(|c| format!("{}={}", c.name, c.value))
.collect::<Vec<_>>()
.join("; "),
)
}
}
fn parse_set_cookie(s: &str, u: &UrlParts, now: SystemTime) -> Option<Cookie> {
let mut parts = s.split(';');
let (name, value) = parts.next()?.split_once('=')?;
let name = name.trim();
if name.is_empty() {
return None;
}
let mut c = Cookie {
name: name.into(),
value: value.trim().trim_matches('"').into(),
domain: u.host.clone(),
host_only: true,
path: default_path(&u.path),
secure: false,
http_only: false,
expires: None,
};
let mut max_age_set = false;
for attr in parts {
let (k, v) = attr.split_once('=').unwrap_or((attr, ""));
let (k, v) = (k.trim().to_ascii_lowercase(), v.trim());
match k.as_str() {
"domain" if !v.is_empty() => {
let d = v.trim_start_matches('.').to_ascii_lowercase();
if !domain_matches(&u.host, &d) {
return None; // a host can't set cookies for another domain
}
c.domain = d;
c.host_only = false;
}
"path" if v.starts_with('/') => c.path = v.into(),
"secure" => c.secure = true,
"httponly" => c.http_only = true,
"max-age" => {
if let Ok(n) = v.parse::<i64>() {
max_age_set = true;
c.expires = Some(if n <= 0 {
SystemTime::UNIX_EPOCH
} else {
now + Duration::from_secs(n as u64)
});
}
}
"expires" if !max_age_set => {
if let Some(t) = parse_http_date(v) {
c.expires = Some(t);
}
}
_ => {}
}
}
Some(c)
}
/// Parses `Sun, 06 Nov 1994 08:49:37 GMT` (and the `-` separated variant).
pub fn parse_http_date(s: &str) -> Option<SystemTime> {
let s = s.replace('-', " ");
let mut it = s.split_whitespace();
let first = it.next()?;
let day: i64 = if first.ends_with(',') { it.next()? } else { first }.parse().ok()?;
let month = match &it.next()?.to_ascii_lowercase()[..3] {
"jan" => 1,
"feb" => 2,
"mar" => 3,
"apr" => 4,
"may" => 5,
"jun" => 6,
"jul" => 7,
"aug" => 8,
"sep" => 9,
"oct" => 10,
"nov" => 11,
"dec" => 12,
_ => return None,
};
let mut year: i64 = it.next()?.parse().ok()?;
if year < 100 {
year += if year < 70 { 2000 } else { 1900 };
}
let hms: Vec<i64> = it.next()?.split(':').filter_map(|p| p.parse().ok()).collect();
let [h, m, sec] = hms[..] else { return None };
// Days from civil (Howard Hinnant).
let y = if month <= 2 { year - 1 } else { year };
let era = y.div_euclid(400);
let yoe = y - era * 400;
let mp = (month + 9) % 12;
let doy = (153 * mp + 2) / 5 + day - 1;
let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
let days = era * 146_097 + doe - 719_468;
let secs = days * 86_400 + h * 3600 + m * 60 + sec;
(secs >= 0).then(|| SystemTime::UNIX_EPOCH + Duration::from_secs(secs as u64))
}
#[cfg(test)]
mod tests {
use super::*;
fn now() -> SystemTime {
SystemTime::UNIX_EPOCH + Duration::from_secs(1_700_000_000)
}
#[test]
fn stores_and_sends_matching_cookies() {
let mut jar = CookieJar::new();
jar.store(
"https://api.example.com/v1/login",
[
"session=abc; Path=/; HttpOnly",
"pref=dark; Domain=example.com; Path=/v1",
"sec=1; Secure",
"evil=1; Domain=other.com",
],
now(),
);
assert_eq!(jar.cookies().len(), 3);
assert_eq!(
jar.header_for("https://api.example.com/v1/users", now()).as_deref(),
Some("pref=dark; sec=1; session=abc")
);
assert_eq!(
jar.header_for("http://www.example.com/v1/x", now()).as_deref(),
Some("pref=dark")
);
assert_eq!(jar.header_for("http://other.com/", now()), None);
}
#[test]
fn expiry_and_deletion() {
let mut jar = CookieJar::new();
jar.store(
"http://h/",
["a=1; Max-Age=10", "b=2; Expires=Wed, 21 Oct 2015 07:28:00 GMT"],
now(),
);
assert_eq!(jar.header_for("http://h/", now()).as_deref(), Some("a=1"));
assert_eq!(jar.header_for("http://h/", now() + Duration::from_secs(11)), None);
jar.store("http://h/", ["a=1; Max-Age=0"], now());
assert!(jar.cookies().is_empty());
}
#[test]
fn http_dates() {
assert_eq!(
parse_http_date("Sun, 06 Nov 1994 08:49:37 GMT"),
Some(SystemTime::UNIX_EPOCH + Duration::from_secs(784_111_777))
);
assert_eq!(
parse_http_date("Sunday, 06-Nov-94 08:49:37 GMT"),
Some(SystemTime::UNIX_EPOCH + Duration::from_secs(784_111_777))
);
}
}
+372
View File
@@ -0,0 +1,372 @@
//! `http-client.env.json` and `http-client.private.env.json`.
//!
//! Both files have the shape `{ "env": { "var": "value" } }`. The special
//! `$shared` environment applies to every environment (JetBrains convention).
//! Non-string values (numbers, booleans, objects such as JetBrains'
//! `SSLConfiguration`) are kept and written back unchanged.
use std::path::{Path, PathBuf};
use serde_json::{Map, Value};
use crate::template::{Resolver, VarSource};
pub const PUBLIC_FILE: &str = "http-client.env.json";
pub const PRIVATE_FILE: &str = "http-client.private.env.json";
pub const SHARED_ENV: &str = "$shared";
#[derive(Debug, thiserror::Error)]
pub enum EnvError {
#[error("{path}: {source}")]
Io { path: PathBuf, source: std::io::Error },
#[error("{path}: {source}")]
Json { path: PathBuf, source: serde_json::Error },
#[error("{path}: expected an object of environments")]
Shape { path: PathBuf },
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EnvVar {
pub name: String,
pub value: String,
/// The original JSON value when it was not a plain string.
original: Option<Value>,
}
impl EnvVar {
pub fn new(name: impl Into<String>, value: impl Into<String>) -> Self {
Self {
name: name.into(),
value: value.into(),
original: None,
}
}
/// Objects and arrays aren't variables; they're shown read-only.
pub fn is_structured(&self) -> bool {
matches!(self.original, Some(Value::Object(_) | Value::Array(_)))
}
fn to_json(&self) -> Value {
match &self.original {
Some(v) if scalar_string(v) == self.value || self.is_structured() => v.clone(),
_ => Value::String(self.value.clone()),
}
}
}
fn scalar_string(v: &Value) -> String {
match v {
Value::String(s) => s.clone(),
Value::Null => String::new(),
other => other.to_string(),
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Environment {
pub name: String,
pub vars: Vec<EnvVar>,
}
impl Environment {
pub fn get(&self, name: &str) -> Option<&str> {
self.vars
.iter()
.find(|v| v.name == name && !v.is_structured())
.map(|v| v.value.as_str())
}
pub fn set(&mut self, name: &str, value: impl Into<String>) {
let value = value.into();
match self.vars.iter_mut().find(|v| v.name == name) {
Some(v) => v.value = value,
None => self.vars.push(EnvVar::new(name, value)),
}
}
}
/// The contents of one env file.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct EnvFile {
pub envs: Vec<Environment>,
}
impl EnvFile {
pub fn parse(text: &str, path: &Path) -> Result<Self, EnvError> {
let value: Value = serde_json::from_str(text).map_err(|source| EnvError::Json {
path: path.to_path_buf(),
source,
})?;
let Value::Object(map) = value else {
return Err(EnvError::Shape {
path: path.to_path_buf(),
});
};
let mut envs = Vec::new();
for (name, vars) in map {
let Value::Object(vars) = vars else {
return Err(EnvError::Shape {
path: path.to_path_buf(),
});
};
envs.push(Environment {
name,
vars: vars
.into_iter()
.map(|(k, v)| EnvVar {
name: k,
value: scalar_string(&v),
original: (!v.is_string()).then_some(v),
})
.collect(),
});
}
Ok(Self { envs })
}
pub fn to_json(&self) -> String {
let mut root = Map::new();
for env in &self.envs {
let mut vars = Map::new();
for v in &env.vars {
vars.insert(v.name.clone(), v.to_json());
}
root.insert(env.name.clone(), Value::Object(vars));
}
let mut s = serde_json::to_string_pretty(&Value::Object(root)).unwrap();
s.push('\n');
s
}
pub fn env(&self, name: &str) -> Option<&Environment> {
self.envs.iter().find(|e| e.name == name)
}
pub fn env_mut(&mut self, name: &str) -> &mut Environment {
if let Some(i) = self.envs.iter().position(|e| e.name == name) {
return &mut self.envs[i];
}
self.envs.push(Environment {
name: name.to_string(),
vars: Vec::new(),
});
self.envs.last_mut().unwrap()
}
/// Variables of `env` with `$shared` underneath.
fn vars_for(&self, env: &str) -> Vec<(String, String)> {
let mut out: Vec<(String, String)> = Vec::new();
for e in [SHARED_ENV, env] {
if let Some(e) = self.env(e) {
for v in e.vars.iter().filter(|v| !v.is_structured()) {
out.retain(|(n, _)| n != &v.name);
out.push((v.name.clone(), v.value.clone()));
}
}
}
out
}
}
/// Both env files of a collection.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Environments {
pub public: EnvFile,
pub private: EnvFile,
}
impl Environments {
/// Loads the env files in `dir`. Missing files are treated as empty.
pub fn load(dir: &Path) -> Result<Self, EnvError> {
let read = |name: &str| -> Result<EnvFile, EnvError> {
let path = dir.join(name);
match std::fs::read_to_string(&path) {
Ok(text) if text.trim().is_empty() => Ok(EnvFile::default()),
Ok(text) => EnvFile::parse(&text, &path),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(EnvFile::default()),
Err(source) => Err(EnvError::Io { path, source }),
}
};
Ok(Self {
public: read(PUBLIC_FILE)?,
private: read(PRIVATE_FILE)?,
})
}
/// Environment names from both files, in file order, without `$shared`.
pub fn names(&self) -> Vec<String> {
let mut names: Vec<String> = Vec::new();
for e in self.public.envs.iter().chain(&self.private.envs) {
if e.name != SHARED_ENV && !names.contains(&e.name) {
names.push(e.name.clone());
}
}
names
}
pub fn is_private(&self, env: &str, var: &str) -> bool {
[env, SHARED_ENV]
.iter()
.any(|e| self.private.env(e).and_then(|e| e.get(var)).is_some())
}
/// Builds a resolver for `env` (or no environment) plus file variables.
pub fn resolver(&self, env: Option<&str>, file_vars: &[(String, String)]) -> Resolver {
let env = env.unwrap_or(SHARED_ENV);
Resolver::new()
.with_layer(VarSource::PrivateEnv, self.private.vars_for(env))
.with_layer(VarSource::PublicEnv, self.public.vars_for(env))
.with_layer(VarSource::File, file_vars.iter().cloned())
}
}
/// The directory whose env files apply to a `.http` file in `start`: the
/// nearest of `start` and its ancestors that has an env file.
pub fn find_env_dir(start: &Path) -> Option<PathBuf> {
start
.ancestors()
.find(|d| d.join(PUBLIC_FILE).is_file() || d.join(PRIVATE_FILE).is_file())
.map(Path::to_path_buf)
}
/// Whether a `.gitignore` in `dir` or one of its parents (up to the
/// repository root) ignores the private env file.
pub fn private_file_ignored(dir: &Path) -> bool {
let mut cur = Some(dir);
while let Some(d) = cur {
if let Ok(text) = std::fs::read_to_string(d.join(".gitignore")) {
if text.lines().any(|l| gitignore_matches(l.trim(), PRIVATE_FILE)) {
return true;
}
}
if d.join(".git").exists() {
break;
}
cur = d.parent();
}
false
}
fn gitignore_matches(pattern: &str, file: &str) -> bool {
if pattern.is_empty() || pattern.starts_with('#') || pattern.starts_with('!') {
return false;
}
let p = pattern.trim_start_matches("**/").trim_start_matches('/');
glob(p, file)
}
/// Minimal glob with `*` and `?`.
fn glob(p: &str, s: &str) -> bool {
let (p, s) = (p.as_bytes(), s.as_bytes());
let (mut pi, mut si, mut star, mut mark) = (0, 0, None, 0);
while si < s.len() {
if pi < p.len() && (p[pi] == b'?' || p[pi] == s[si]) {
pi += 1;
si += 1;
} else if pi < p.len() && p[pi] == b'*' {
star = Some(pi);
mark = si;
pi += 1;
} else if let Some(st) = star {
pi = st + 1;
mark += 1;
si = mark;
} else {
return false;
}
}
p[pi..].iter().all(|&c| c == b'*')
}
/// Text to append to a `.gitignore` so the private env file is ignored.
pub fn gitignore_addition(existing: &str) -> String {
let mut s = String::new();
if !existing.is_empty() && !existing.ends_with('\n') {
s.push('\n');
}
s.push_str(PRIVATE_FILE);
s.push('\n');
s
}
#[cfg(test)]
mod tests {
use super::*;
const PUBLIC: &str = r#"{
"$shared": { "version": "v1", "base_url": "http://shared" },
"dev": { "base_url": "http://localhost:8080", "port": 8080, "SSLConfiguration": { "verifyHostCertificate": false } },
"prod": { "base_url": "https://api.example.com" }
}"#;
const PRIVATE: &str = r#"{ "dev": { "token": "dev-secret" }, "prod": { "token": "prod-secret" } }"#;
fn envs() -> Environments {
Environments {
public: EnvFile::parse(PUBLIC, Path::new("pub")).unwrap(),
private: EnvFile::parse(PRIVATE, Path::new("priv")).unwrap(),
}
}
#[test]
fn names_skip_shared() {
assert_eq!(envs().names(), vec!["dev", "prod"]);
}
#[test]
fn resolves_with_shared_and_private() {
let r = envs().resolver(Some("dev"), &[("file_var".into(), "{{version}}".into())]);
assert_eq!(
r.resolve("{{base_url}}/{{version}}/{{port}}?t={{token}}&f={{file_var}}")
.unwrap(),
"http://localhost:8080/v1/8080?t=dev-secret&f=v1"
);
assert!(r.resolve("{{SSLConfiguration}}").is_err());
assert!(envs().is_private("dev", "token"));
assert!(!envs().is_private("dev", "base_url"));
}
#[test]
fn write_keeps_non_string_values() {
let mut f = EnvFile::parse(PUBLIC, Path::new("pub")).unwrap();
let again = EnvFile::parse(&f.to_json(), Path::new("pub")).unwrap();
assert_eq!(f, again);
assert!(f.to_json().contains("\"port\": 8080"));
f.env_mut("dev").set("port", "9090");
assert!(f.to_json().contains("\"port\": \"9090\""));
f.env_mut("staging").set("base_url", "http://staging");
assert!(f.to_json().contains("\"staging\": {"));
}
#[test]
fn gitignore_patterns() {
for p in [
"http-client.private.env.json",
"/http-client.private.env.json",
"**/http-client.private.env.json",
"*.private.env.json",
"*private*",
] {
assert!(gitignore_matches(p, PRIVATE_FILE), "{p}");
}
for p in [
"# http-client.private.env.json",
"*.http",
"!http-client.private.env.json",
"",
] {
assert!(!gitignore_matches(p, PRIVATE_FILE), "{p}");
}
}
#[test]
fn gitignore_lookup_walks_up_to_repo_root() {
let dir = tempfile::tempdir().unwrap();
std::fs::create_dir(dir.path().join(".git")).unwrap();
let sub = dir.path().join("api");
std::fs::create_dir(&sub).unwrap();
assert!(!private_file_ignored(&sub));
std::fs::write(dir.path().join(".gitignore"), "target\n*.private.env.json\n").unwrap();
assert!(private_file_ignored(&sub));
}
}
+135
View File
@@ -0,0 +1,135 @@
//! "Copy as curl": a POSIX shell command from a [`ResolvedRequest`].
use crate::http::{Proxy, ResolvedBody, ResolvedPartBody, ResolvedRequest};
/// Quotes a word for POSIX shells.
pub fn shell_quote(s: &str) -> String {
if !s.is_empty() && s.chars().all(|c| c.is_ascii_alphanumeric() || "-_./:=@%+,".contains(c)) {
return s.to_string();
}
format!("'{}'", s.replace('\'', "'\\''"))
}
pub fn to_curl(req: &ResolvedRequest) -> String {
let mut args: Vec<String> = vec!["curl".into()];
let has_body = !matches!(req.body, ResolvedBody::None);
let implied = matches!(
(&req.body, req.method.as_str()),
(ResolvedBody::None, "GET")
| (
ResolvedBody::Bytes(_) | ResolvedBody::File(_) | ResolvedBody::Multipart(_),
"POST"
)
);
if req.method == "HEAD" && !has_body {
args.push("--head".into());
} else if !implied {
args.push("-X".into());
args.push(req.method.clone());
}
args.push(shell_quote(&req.url));
for (n, v) in &req.headers {
args.push("-H".into());
args.push(shell_quote(&format!("{n}: {v}")));
}
match &req.body {
ResolvedBody::None => {}
ResolvedBody::Bytes(b) => {
args.push("--data-raw".into());
args.push(shell_quote(&String::from_utf8_lossy(b)));
}
ResolvedBody::File(p) => {
args.push("--data-binary".into());
args.push(shell_quote(&format!("@{}", p.display())));
}
ResolvedBody::Multipart(parts) => {
for p in parts {
let mut v = match &p.body {
ResolvedPartBody::Bytes(b) => format!("{}={}", p.name, String::from_utf8_lossy(b)),
ResolvedPartBody::File(f) => format!("{}=@{}", p.name, f.display()),
};
if let Some(f) = &p.filename {
v.push_str(&format!(";filename={f}"));
}
if let Some(ct) = &p.content_type {
v.push_str(&format!(";type={ct}"));
}
args.push("-F".into());
args.push(shell_quote(&v));
}
}
}
let o = &req.options;
if o.follow_redirects {
args.push("-L".into());
}
if !o.verify_tls {
args.push("-k".into());
}
if let Some(t) = o.timeout {
args.push("--max-time".into());
args.push(format!("{}", t.as_secs_f64()));
}
if let Some(t) = o.connect_timeout {
args.push("--connect-timeout".into());
args.push(format!("{}", t.as_secs_f64()));
}
match o.http_version.as_deref() {
Some("HTTP/1.1") => args.push("--http1.1".into()),
Some("HTTP/2" | "HTTP/2.0") => args.push("--http2".into()),
_ => {}
}
match &o.proxy {
Proxy::System => {}
Proxy::None => args.push("--noproxy '*'".into()),
Proxy::Manual(p) => {
args.push("-x".into());
args.push(shell_quote(p));
}
}
if let Some(ca) = &o.ca_file {
args.push("--cacert".into());
args.push(shell_quote(&ca.display().to_string()));
}
args.join(" ")
}
#[cfg(test)]
mod tests {
use super::*;
use crate::http::RequestOptions;
#[test]
fn exports() {
let req = ResolvedRequest {
method: "PUT".into(),
url: "https://x/a?b=1&c=2".into(),
headers: vec![("Content-Type".into(), "application/json".into())],
body: ResolvedBody::Bytes(b"{\"it's\": 1}".to_vec()),
options: RequestOptions {
follow_redirects: false,
..Default::default()
},
};
assert_eq!(
to_curl(&req),
r#"curl -X PUT 'https://x/a?b=1&c=2' -H 'Content-Type: application/json' --data-raw '{"it'\''s": 1}'"#
);
}
#[test]
fn round_trips_through_import() {
let req = ResolvedRequest {
method: "POST".into(),
url: "http://localhost/x".into(),
headers: vec![("A".into(), "b c".into())],
body: ResolvedBody::Bytes(b"x=1".to_vec()),
options: RequestOptions::default(),
};
let r = crate::import::curl::from_curl(&to_curl(&req)).unwrap();
assert_eq!(r.method, crate::model::Method::Post);
assert_eq!(r.url, "http://localhost/x");
assert_eq!(r.header("A"), Some("b c"));
assert_eq!(r.body, crate::model::Body::Text("x=1".into()));
}
}
+3
View File
@@ -0,0 +1,3 @@
//! Exporting requests to other formats.
pub mod curl;
+276
View File
@@ -0,0 +1,276 @@
//! The transport-facing types and the async [`HttpClient`] trait.
//!
//! [`ResolvedRequest`] is a request after variable substitution. It is the
//! only thing a transport sees, so "copy as curl" can be exact.
use std::future::Future;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;
use crate::cancel::CancelToken;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ResolvedRequest {
pub method: String,
pub url: String,
pub headers: Vec<(String, String)>,
pub body: ResolvedBody,
pub options: RequestOptions,
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub enum ResolvedBody {
#[default]
None,
Bytes(Vec<u8>),
/// Streamed from disk.
File(PathBuf),
Multipart(Vec<ResolvedPart>),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ResolvedPart {
pub name: String,
pub filename: Option<String>,
pub content_type: Option<String>,
pub body: ResolvedPartBody,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ResolvedPartBody {
Bytes(Vec<u8>),
File(PathBuf),
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub enum Proxy {
/// Use the system / environment proxy settings.
#[default]
System,
None,
Manual(String),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RequestOptions {
pub follow_redirects: bool,
pub max_redirects: u32,
pub timeout: Option<Duration>,
pub connect_timeout: Option<Duration>,
pub verify_tls: bool,
/// Extra CA bundle (PEM) in addition to the system store.
pub ca_file: Option<PathBuf>,
pub proxy: Proxy,
/// `HTTP/1.1` or `HTTP/2` from the request line; `None` lets curl negotiate.
pub http_version: Option<String>,
/// Bodies larger than this are written to a temp file.
pub spill_threshold: u64,
}
impl Default for RequestOptions {
fn default() -> Self {
Self {
follow_redirects: true,
max_redirects: 20,
timeout: None,
connect_timeout: None,
verify_tls: true,
ca_file: None,
proxy: Proxy::System,
http_version: None,
spill_threshold: 5 * 1024 * 1024,
}
}
}
/// A response body: in memory under the spill threshold, else a temp file
/// that is deleted when the last clone is dropped.
#[derive(Debug, Clone)]
pub enum BodyStore {
InMemory(Arc<Vec<u8>>),
TempFile(Arc<tempfile::TempPath>),
}
impl BodyStore {
pub fn empty() -> Self {
BodyStore::InMemory(Arc::new(Vec::new()))
}
/// Reads the whole body (from disk if spilled).
pub fn read_all(&self) -> std::io::Result<Vec<u8>> {
match self {
BodyStore::InMemory(b) => Ok(b.as_ref().clone()),
BodyStore::TempFile(p) => std::fs::read(&**p),
}
}
pub fn in_memory(&self) -> Option<&[u8]> {
match self {
BodyStore::InMemory(b) => Some(b),
BodyStore::TempFile(_) => None,
}
}
pub fn is_spilled(&self) -> bool {
matches!(self, BodyStore::TempFile(_))
}
}
/// Cumulative times from the start of the request, as curl reports them.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Timings {
pub dns: Duration,
pub connect: Duration,
pub tls: Duration,
pub ttfb: Duration,
pub total: Duration,
}
impl Timings {
/// `(label, phase duration)` pairs, each phase on its own.
pub fn phases(&self) -> [(&'static str, Duration); 5] {
let tls_end = self.tls.max(self.connect);
[
("DNS lookup", self.dns),
("TCP connect", self.connect.saturating_sub(self.dns)),
("TLS handshake", self.tls.saturating_sub(self.connect)),
("Waiting (TTFB)", self.ttfb.saturating_sub(tls_end)),
("Download", self.total.saturating_sub(self.ttfb)),
]
}
}
#[derive(Debug, Clone)]
pub struct Response {
pub status: u16,
pub reason: String,
pub http_version: String,
pub headers: Vec<(String, String)>,
pub body: BodyStore,
pub timings: Timings,
/// Body size in bytes (as received, after decompression).
pub size: u64,
/// The URL after redirects.
pub effective_url: String,
}
impl Response {
pub fn header(&self, name: &str) -> Option<&str> {
self.headers
.iter()
.find(|(n, _)| n.eq_ignore_ascii_case(name))
.map(|(_, v)| v.as_str())
}
pub fn headers_named<'a>(&'a self, name: &'a str) -> impl Iterator<Item = &'a str> {
self.headers
.iter()
.filter(move |(n, _)| n.eq_ignore_ascii_case(name))
.map(|(_, v)| v.as_str())
}
pub fn content_type(&self) -> &str {
self.header("content-type").unwrap_or("")
}
}
/// The standard reason phrase for a status code (HTTP/2 has none on the wire).
pub fn canonical_reason(status: u16) -> &'static str {
match status {
100 => "Continue",
101 => "Switching Protocols",
200 => "OK",
201 => "Created",
202 => "Accepted",
203 => "Non-Authoritative Information",
204 => "No Content",
205 => "Reset Content",
206 => "Partial Content",
207 => "Multi-Status",
300 => "Multiple Choices",
301 => "Moved Permanently",
302 => "Found",
303 => "See Other",
304 => "Not Modified",
307 => "Temporary Redirect",
308 => "Permanent Redirect",
400 => "Bad Request",
401 => "Unauthorized",
402 => "Payment Required",
403 => "Forbidden",
404 => "Not Found",
405 => "Method Not Allowed",
406 => "Not Acceptable",
407 => "Proxy Authentication Required",
408 => "Request Timeout",
409 => "Conflict",
410 => "Gone",
411 => "Length Required",
412 => "Precondition Failed",
413 => "Content Too Large",
414 => "URI Too Long",
415 => "Unsupported Media Type",
416 => "Range Not Satisfiable",
417 => "Expectation Failed",
418 => "I'm a teapot",
422 => "Unprocessable Content",
425 => "Too Early",
426 => "Upgrade Required",
428 => "Precondition Required",
429 => "Too Many Requests",
431 => "Request Header Fields Too Large",
451 => "Unavailable For Legal Reasons",
500 => "Internal Server Error",
501 => "Not Implemented",
502 => "Bad Gateway",
503 => "Service Unavailable",
504 => "Gateway Timeout",
505 => "HTTP Version Not Supported",
_ => "",
}
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum HttpError {
#[error("cancelled")]
Cancelled,
#[error("timed out")]
Timeout,
#[error("invalid URL: {0}")]
InvalidUrl(String),
#[error("could not resolve host: {0}")]
Dns(String),
#[error("could not connect: {0}")]
Connect(String),
#[error("TLS error: {0}")]
Tls(String),
#[error("I/O error: {0}")]
Io(String),
#[error("{0}")]
Other(String),
}
/// An HTTP transport. Implementations must be cancel-safe: when `cancel`
/// fires, `send` should return [`HttpError::Cancelled`] promptly.
pub trait HttpClient: Send + Sync {
fn send(
&self,
req: ResolvedRequest,
cancel: CancelToken,
) -> impl Future<Output = Result<Response, HttpError>> + Send;
}
/// Object-safe version of [`HttpClient`], for `Arc<dyn DynHttpClient>`.
/// Every `HttpClient` implements it.
#[async_trait::async_trait]
pub trait DynHttpClient: Send + Sync {
async fn send_dyn(&self, req: ResolvedRequest, cancel: CancelToken) -> Result<Response, HttpError>;
}
#[async_trait::async_trait]
impl<T: HttpClient> DynHttpClient for T {
async fn send_dyn(&self, req: ResolvedRequest, cancel: CancelToken) -> Result<Response, HttpError> {
self.send(req, cancel).await
}
}
+118
View File
@@ -0,0 +1,118 @@
//! Line-level helpers shared by the parser and the writer.
/// A line without its terminator.
pub(crate) fn content(line: &str) -> &str {
line.strip_suffix('\n')
.map(|l| l.strip_suffix('\r').unwrap_or(l))
.unwrap_or(line)
}
pub(crate) fn is_separator(line: &str) -> bool {
line.starts_with("###")
}
pub(crate) fn separator_title(line: &str) -> Option<String> {
let t = content(line).trim_start_matches('#').trim();
(!t.is_empty()).then(|| t.to_string())
}
pub(crate) fn is_blank(line: &str) -> bool {
content(line).trim().is_empty()
}
/// The text of a `#` or `//` comment, without the marker.
pub(crate) fn comment_text(line: &str) -> Option<&str> {
let c = content(line).trim_start();
if let Some(rest) = c.strip_prefix("//") {
Some(rest)
} else {
c.strip_prefix('#')
}
}
/// `# @name value` → `("name", "value")`. Also accepts `@name=value`.
pub(crate) fn parse_directive(line: &str) -> Option<(String, String)> {
let text = comment_text(line)?.trim_start();
let rest = text.strip_prefix('@')?;
let end = rest.find(|c: char| c.is_whitespace() || c == '=').unwrap_or(rest.len());
let name = &rest[..end];
if name.is_empty() {
return None;
}
let value = rest[end..].trim_start();
let value = value.strip_prefix('=').unwrap_or(value).trim();
Some((name.to_string(), value.to_string()))
}
/// `@name = value` → `("name", "value")`.
pub(crate) fn parse_variable(line: &str) -> Option<(String, String)> {
let rest = content(line).trim_start().strip_prefix('@')?;
let eq = rest.find('=')?;
let name = rest[..eq].trim();
if name.is_empty() || !name.chars().all(|c| c.is_alphanumeric() || "_-.".contains(c)) {
return None;
}
Some((name.to_string(), rest[eq + 1..].trim().to_string()))
}
fn is_token_char(c: char) -> bool {
c.is_ascii_alphanumeric() || "!#$%&'*+-.^_`|~".contains(c)
}
/// `Name: value` → `("Name", "value")`.
pub(crate) fn parse_header(text: &str) -> Option<(String, String)> {
let colon = text.find(':')?;
let name = text[..colon].trim();
if name.is_empty() || !name.chars().all(is_token_char) {
return None;
}
Some((name.to_string(), text[colon + 1..].trim().to_string()))
}
/// `# Name: value` → a disabled header.
pub(crate) fn parse_disabled_header(line: &str) -> Option<(String, String)> {
let text = comment_text(line)?.trim_start();
if text.starts_with('@') {
return None;
}
parse_header(text)
}
pub(crate) fn is_url_continuation(line: &str) -> bool {
let c = content(line);
let t = c.trim_start();
if t.starts_with('?') || t.starts_with('&') {
return true;
}
// Indented path segments: JetBrains allows splitting the URL like this.
c.len() != t.len() && t.starts_with('/') && !t.starts_with("//")
}
pub(crate) fn is_handler_start(line: &str) -> bool {
content(line).starts_with("> ")
}
pub(crate) fn is_response_ref(line: &str) -> bool {
content(line).starts_with("<> ")
}
/// Parses a duration directive value: `30`, `30s`, `500 ms`, `2 m`.
pub(crate) fn parse_duration(value: &str) -> Option<std::time::Duration> {
let v = value.trim();
let split = v.find(|c: char| !c.is_ascii_digit()).unwrap_or(v.len());
let n: u64 = v[..split].parse().ok()?;
match v[split..].trim() {
"" | "s" | "sec" => Some(std::time::Duration::from_secs(n)),
"ms" => Some(std::time::Duration::from_millis(n)),
"m" | "min" => Some(std::time::Duration::from_secs(n * 60)),
_ => None,
}
}
pub(crate) fn format_duration(d: std::time::Duration) -> String {
if d.subsec_millis() != 0 {
format!("{} ms", d.as_millis())
} else {
d.as_secs().to_string()
}
}
+289
View File
@@ -0,0 +1,289 @@
//! Lossless `.http` file support (JetBrains HTTP Client / VS Code REST Client).
//!
//! The file is split into blocks at `###` separator lines, and every line of a
//! block is classified ([`LineKind`]) with its byte span. That line list is the
//! concrete syntax tree: concatenating the spans of all blocks gives back the
//! exact input, and editing a request only rewrites its own block.
mod lexer;
mod parser;
mod writer;
use std::ops::Range;
pub use writer::render_request;
use crate::model::Request;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LineKind {
/// `### Title`
Separator,
Blank,
Comment,
/// `# @name value` / `// @no-redirect`
Directive,
/// `@name = value`
Variable,
RequestLine,
/// Indented `?a=1`, `&b=2` or `/path` continuation of the URL.
UrlContinuation,
Header,
/// A commented-out header: `# Name: value`
DisabledHeader,
Body,
/// `> {% … %}` (all its lines) or `> ./handler.js`. Kept, never run.
ResponseHandler,
/// `<> ./previous-response.json`
ResponseRef,
/// Something we don't understand; kept verbatim.
Unknown,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Line {
/// Byte range in the file, including the line terminator.
pub span: Range<usize>,
pub kind: LineKind,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Block {
/// Byte range in the file. Blocks are contiguous and cover the whole file.
pub span: Range<usize>,
pub lines: Vec<Line>,
pub request: Option<Request>,
}
/// Identifies a request inside a file: by its name when it has one, and by
/// position otherwise.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct RequestKey {
pub name: Option<String>,
pub index: usize,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HttpFile {
text: String,
blocks: Vec<Block>,
}
impl HttpFile {
pub fn parse(text: impl Into<String>) -> Self {
let text = text.into();
let blocks = parser::parse_blocks(&text);
Self { text, blocks }
}
pub fn text(&self) -> &str {
&self.text
}
/// Writes the file back from its syntax tree. Byte-identical to the input.
pub fn write(&self) -> String {
self.blocks.iter().map(|b| &self.text[b.span.clone()]).collect()
}
pub fn blocks(&self) -> &[Block] {
&self.blocks
}
/// The line terminator used by the file (`\r\n` if its first line uses it).
pub fn eol(&self) -> &'static str {
match self.text.find('\n') {
Some(i) if i > 0 && self.text.as_bytes()[i - 1] == b'\r' => "\r\n",
_ => "\n",
}
}
pub fn requests(&self) -> impl Iterator<Item = &Request> {
self.blocks.iter().filter_map(|b| b.request.as_ref())
}
pub fn request(&self, index: usize) -> Option<&Request> {
self.requests().nth(index)
}
pub fn request_count(&self) -> usize {
self.requests().count()
}
pub fn key_of(&self, index: usize) -> Option<RequestKey> {
self.request(index).map(|r| RequestKey {
name: r.display_name().map(str::to_string),
index,
})
}
/// Finds a request by name (if it has one and it's unique), else by index.
pub fn find(&self, key: &RequestKey) -> Option<usize> {
if let Some(name) = &key.name {
let matches: Vec<usize> = self
.requests()
.enumerate()
.filter(|(_, r)| r.display_name() == Some(name.as_str()))
.map(|(i, _)| i)
.collect();
match matches.len() {
1 => return Some(matches[0]),
0 => return None,
_ if matches.contains(&key.index) => return Some(key.index),
_ => return Some(matches[0]),
}
}
(key.index < self.request_count()).then_some(key.index)
}
pub fn find_by_name(&self, name: &str) -> Option<usize> {
self.requests().position(|r| r.display_name() == Some(name))
}
/// File-level variables (`@name = value`), in file order.
pub fn variables(&self) -> Vec<(String, String)> {
let mut vars = Vec::new();
for block in &self.blocks {
for line in &block.lines {
if line.kind == LineKind::Variable {
if let Some(v) = lexer::parse_variable(lexer::content(&self.text[line.span.clone()])) {
vars.push(v);
}
}
}
}
vars
}
fn request_block_index(&self, index: usize) -> Option<usize> {
self.blocks
.iter()
.enumerate()
.filter(|(_, b)| b.request.is_some())
.nth(index)
.map(|(i, _)| i)
}
/// The raw text of a request's block (what "copy as .http" copies).
pub fn request_text(&self, index: usize) -> Option<&str> {
let b = &self.blocks[self.request_block_index(index)?];
Some(&self.text[b.span.clone()])
}
/// Returns the file text with request `index` replaced by `new`. Only that
/// request's byte span changes; if `new` has the same content as the
/// current request the text is returned unchanged.
pub fn replace_request(&self, index: usize, new: &Request) -> Option<String> {
let bi = self.request_block_index(index)?;
let block = &self.blocks[bi];
let old_text = &self.text[block.span.clone()];
let new_text = writer::rewrite_block(old_text, block, new, self.eol());
let mut out = String::with_capacity(self.text.len() + new_text.len());
out.push_str(&self.text[..block.span.start]);
out.push_str(&new_text);
out.push_str(&self.text[block.span.end..]);
Some(out)
}
/// Returns the file text without request `index`.
pub fn remove_request(&self, index: usize) -> Option<String> {
let bi = self.request_block_index(index)?;
let span = self.blocks[bi].span.clone();
let mut out = String::with_capacity(self.text.len());
out.push_str(&self.text[..span.start]);
out.push_str(&self.text[span.end..]);
// Removing the first block can leave a file that starts mid-way
// through nothing in particular; that's fine, the next block starts
// with its own `###` line.
Some(out)
}
/// Returns the file text with `new` inserted after request `after`
/// (or appended at the end when `after` is `None`).
pub fn insert_request(&self, after: Option<usize>, new: &Request) -> String {
let eol = self.eol();
let mut rendered = render_request(new, eol);
if new.title.is_none() {
// A block needs a separator unless it is the very first one.
rendered = format!("###{eol}{rendered}");
}
self.insert_request_text(after, &rendered)
}
/// Inserts raw request text (a whole block, starting with `###`) after
/// request `after`, or at the end. Keeps one blank line between blocks.
pub fn insert_request_text(&self, after: Option<usize>, block: &str) -> String {
let eol = self.eol();
let at = match after.and_then(|i| self.request_block_index(i)) {
Some(bi) => self.blocks[bi].span.end,
None => self.text.len(),
};
let mut out = String::with_capacity(self.text.len() + block.len() + 4);
out.push_str(&self.text[..at]);
if !out.is_empty() {
if !out.ends_with('\n') {
out.push_str(eol);
}
if !out.ends_with(&format!("{eol}{eol}")) && !out.ends_with("\n\n") {
out.push_str(eol);
}
}
let block = block.trim_end_matches(['\r', '\n']);
out.push_str(block);
out.push_str(eol);
let rest = &self.text[at..];
if !rest.is_empty() {
out.push_str(eol);
}
out.push_str(rest);
out
}
/// Trailing blank lines of a block (the spacing before the next block).
fn trailing_blank(&self, bi: usize) -> &str {
let b = &self.blocks[bi];
let mut start = b.span.end;
for l in b.lines.iter().rev() {
if l.kind != LineKind::Blank {
break;
}
start = l.span.start;
}
&self.text[start..b.span.end]
}
/// The text of a request's block without its trailing blank lines (what
/// the Source tab shows).
pub fn request_source(&self, index: usize) -> Option<&str> {
let bi = self.request_block_index(index)?;
let b = &self.blocks[bi];
let trailing = self.trailing_blank(bi).len();
let text = &self.text[b.span.start..b.span.end - trailing];
Some(text)
}
/// Replaces a request's block with `text` (as edited in the Source tab),
/// keeping the original spacing before the next block.
pub fn replace_request_text(&self, index: usize, text: &str) -> Option<String> {
let bi = self.request_block_index(index)?;
let span = self.blocks[bi].span.clone();
let trailing = self.trailing_blank(bi);
let eol = self.eol();
let mut out = String::with_capacity(self.text.len() + text.len());
out.push_str(&self.text[..span.start]);
out.push_str(text);
// A line ending is needed if anything follows (or the old block had one).
let old_core = &self.text[span.start..span.end - trailing.len()];
if !text.is_empty()
&& !text.ends_with('\n')
&& (old_core.ends_with('\n') || span.end < self.text.len() || !trailing.is_empty())
{
out.push_str(eol);
}
out.push_str(trailing);
out.push_str(&self.text[span.end..]);
Some(out)
}
}
#[cfg(test)]
mod tests;
+363
View File
@@ -0,0 +1,363 @@
//! Splits a file into blocks, classifies lines, and extracts requests.
use std::path::PathBuf;
use super::lexer::*;
use super::{Block, Line, LineKind};
use crate::model::{Body, Directives, Header, Method, Part, PartBody, Request, SourceSpan};
pub(crate) fn parse_blocks(text: &str) -> Vec<Block> {
// Lines with their byte offsets.
let mut lines: Vec<(usize, &str)> = Vec::new();
let mut off = 0;
for l in text.split_inclusive('\n') {
lines.push((off, l));
off += l.len();
}
// Group into blocks at separator lines.
let mut groups: Vec<Vec<(usize, &str)>> = vec![Vec::new()];
for (o, l) in lines {
if is_separator(l) && !groups.last().unwrap().is_empty() {
groups.push(Vec::new());
}
groups.last_mut().unwrap().push((o, l));
}
groups
.into_iter()
.filter(|g| !g.is_empty())
.map(|g| parse_block(text, &g))
.collect()
}
#[derive(Clone, Copy, PartialEq)]
enum State {
Pre,
AfterRequestLine,
Headers,
Body,
Handler,
Tail,
}
fn parse_block(text: &str, lines: &[(usize, &str)]) -> Block {
let start = lines[0].0;
let end = lines.last().map(|(o, l)| o + l.len()).unwrap();
let mut out: Vec<Line> = Vec::with_capacity(lines.len());
let mut state = State::Pre;
for (i, &(o, l)) in lines.iter().enumerate() {
let kind = if i == 0 && is_separator(l) {
LineKind::Separator
} else {
match state {
State::Pre => {
if is_blank(l) {
LineKind::Blank
} else if parse_directive(l).is_some() {
LineKind::Directive
} else if comment_text(l).is_some() {
LineKind::Comment
} else if parse_variable(l).is_some() {
LineKind::Variable
} else {
state = State::AfterRequestLine;
LineKind::RequestLine
}
}
State::AfterRequestLine | State::Headers => {
if state == State::AfterRequestLine && is_url_continuation(l) {
LineKind::UrlContinuation
} else {
state = State::Headers;
if is_blank(l) {
state = State::Body;
LineKind::Blank
} else if parse_disabled_header(l).is_some() {
LineKind::DisabledHeader
} else if comment_text(l).is_some() {
LineKind::Comment
} else if parse_header(content(l)).is_some() {
LineKind::Header
} else if is_handler_start(l) {
state = handler_state(l);
LineKind::ResponseHandler
} else if is_response_ref(l) {
state = State::Tail;
LineKind::ResponseRef
} else {
LineKind::Unknown
}
}
}
State::Body => {
if is_handler_start(l) {
state = handler_state(l);
LineKind::ResponseHandler
} else if is_response_ref(l) {
state = State::Tail;
LineKind::ResponseRef
} else {
LineKind::Body
}
}
State::Handler => {
if content(l).contains("%}") {
state = State::Tail;
}
LineKind::ResponseHandler
}
State::Tail => {
if is_blank(l) {
LineKind::Blank
} else if is_handler_start(l) {
state = handler_state(l);
LineKind::ResponseHandler
} else if is_response_ref(l) {
LineKind::ResponseRef
} else if comment_text(l).is_some() {
LineKind::Comment
} else {
LineKind::Unknown
}
}
}
};
out.push(Line {
span: o..o + l.len(),
kind,
});
}
// Trailing blank lines after the body are not part of it.
for line in out.iter_mut().rev() {
match line.kind {
LineKind::Body if is_blank(&text[line.span.clone()]) => line.kind = LineKind::Blank,
LineKind::Body => break,
LineKind::Blank | LineKind::ResponseHandler | LineKind::ResponseRef => {}
LineKind::Comment | LineKind::Unknown => {}
_ => break,
}
}
let request = extract_request(text, &out, start..end);
Block {
span: start..end,
lines: out,
request,
}
}
fn handler_state(line: &str) -> State {
let c = content(line);
if c.starts_with("> {%") && !c.contains("%}") {
State::Handler
} else {
State::Tail
}
}
fn extract_request(text: &str, lines: &[Line], span: std::ops::Range<usize>) -> Option<Request> {
let line_text = |l: &Line| &text[l.span.clone()];
let rl = lines.iter().find(|l| l.kind == LineKind::RequestLine)?;
let mut req = Request {
source: SourceSpan {
start: span.start,
end: span.end,
},
..Request::default()
};
if let Some(first) = lines.first() {
if first.kind == LineKind::Separator {
req.title = separator_title(line_text(first));
}
}
for l in lines.iter().filter(|l| l.kind == LineKind::Directive) {
if let Some((name, value)) = parse_directive(line_text(l)) {
apply_directive(&mut req.directives, &mut req.name, &name, &value);
}
}
// Request line: `METHOD URL [HTTP/x]` or just `URL`.
let tokens: Vec<&str> = content(line_text(rl)).split_whitespace().collect();
let mut tokens = tokens.as_slice();
if tokens.len() >= 2 && tokens[tokens.len() - 1].starts_with("HTTP/") {
req.http_version = Some(tokens[tokens.len() - 1].to_string());
tokens = &tokens[..tokens.len() - 1];
}
if tokens.len() >= 2 {
if let Some(m) = Method::parse(tokens[0]) {
req.method = m;
tokens = &tokens[1..];
}
}
let mut url = tokens.join(" ");
for l in lines.iter().filter(|l| l.kind == LineKind::UrlContinuation) {
url.push_str(content(line_text(l)).trim());
}
req.url = url;
for l in lines {
match l.kind {
LineKind::Header => {
if let Some((n, v)) = parse_header(content(line_text(l))) {
req.headers.push(Header::new(n, v));
}
}
LineKind::DisabledHeader => {
if let Some((n, v)) = parse_disabled_header(line_text(l)) {
req.headers.push(Header {
name: n,
value: v,
disabled: true,
});
}
}
_ => {}
}
}
let body_lines: Vec<&Line> = lines.iter().filter(|l| l.kind == LineKind::Body).collect();
if let (Some(first), Some(last)) = (body_lines.first(), body_lines.last()) {
let raw = &text[first.span.start..last.span.end];
let raw = content(raw); // drop the final terminator only
req.body = parse_body(raw, &req.headers);
}
Some(req)
}
pub(crate) fn apply_directive(d: &mut Directives, name_out: &mut Option<String>, name: &str, value: &str) {
match name {
"name" if !value.is_empty() => *name_out = Some(value.to_string()),
"no-redirect" => d.no_redirect = true,
"no-cookie-jar" => d.no_cookie_jar = true,
"no-log" => d.no_log = true,
"timeout" if parse_duration(value).is_some() => d.timeout = parse_duration(value),
"connection-timeout" if parse_duration(value).is_some() => d.connection_timeout = parse_duration(value),
_ => d.other.push((name.to_string(), value.to_string())),
}
}
fn file_ref(line: &str) -> Option<(PathBuf, bool)> {
let t = line.trim();
if let Some(p) = t.strip_prefix("<@") {
let p = p.trim();
return (!p.is_empty()).then(|| (PathBuf::from(p), true));
}
let p = t.strip_prefix("< ")?.trim();
(!p.is_empty()).then(|| (PathBuf::from(p), false))
}
fn parse_body(raw: &str, headers: &[Header]) -> Body {
let content_type = headers
.iter()
.find(|h| !h.disabled && h.name.eq_ignore_ascii_case("content-type"))
.map(|h| h.value.as_str())
.unwrap_or("");
if content_type.to_ascii_lowercase().starts_with("multipart/") {
if let Some(boundary) = boundary_param(content_type) {
if let Some(parts) = parse_multipart(raw, &boundary) {
return Body::Multipart { boundary, parts };
}
}
}
if !raw.contains('\n') {
if let Some((path, substitute)) = file_ref(raw) {
return Body::FileRef { path, substitute };
}
}
Body::Text(raw.to_string())
}
pub(crate) fn boundary_param(content_type: &str) -> Option<String> {
content_type.split(';').skip(1).find_map(|p| {
let (k, v) = p.split_once('=')?;
k.trim()
.eq_ignore_ascii_case("boundary")
.then(|| v.trim().trim_matches('"').to_string())
})
}
/// Parses the body of a multipart request. Returns `None` if it doesn't look
/// like one (then it's kept as plain text).
fn parse_multipart(raw: &str, boundary: &str) -> Option<Vec<Part>> {
let delim = format!("--{boundary}");
let close = format!("--{boundary}--");
let mut parts = Vec::new();
let mut lines = raw.split_inclusive('\n').peekable();
// Skip anything before the first delimiter (should be nothing).
loop {
let l = lines.next()?;
if content(l).trim_end() == delim {
break;
}
if !is_blank(l) {
return None;
}
}
loop {
// Part headers.
let mut headers = Vec::new();
let mut name = None;
let mut filename = None;
loop {
let l = lines.next()?;
if is_blank(l) {
break;
}
let (n, v) = parse_header(content(l))?;
if n.eq_ignore_ascii_case("content-disposition") {
for p in v.split(';').skip(1) {
if let Some((k, val)) = p.split_once('=') {
let val = val.trim().trim_matches('"').to_string();
match k.trim() {
"name" => name = Some(val),
"filename" => filename = Some(val),
_ => {}
}
}
}
} else {
headers.push(Header::new(n, v));
}
}
// Part content up to the next delimiter.
let mut body = String::new();
let mut closed = false;
let mut found = false;
for l in lines.by_ref() {
let c = content(l).trim_end();
if c == delim || c == close {
closed = c == close;
found = true;
break;
}
body.push_str(l);
}
if !found {
return None;
}
let body = content(&body).to_string();
let body = match file_ref(&body) {
Some((p, _)) if !body.contains('\n') => PartBody::File(p),
_ => PartBody::Text(body),
};
parts.push(Part {
name: name?,
filename,
headers,
body,
});
if closed {
break;
}
}
Some(parts)
}
@@ -0,0 +1,29 @@
---
source: crates/core/src/httpfile/tests.rs
expression: dump(&f)
---
block 0 [0..28]
RequestLine "GET https://httpbin.org/get\n"
=> Request {
name: None,
title: None,
method: Get,
url: "https://httpbin.org/get",
http_version: None,
headers: [],
body: None,
directives: Directives {
no_redirect: false,
no_cookie_jar: false,
no_log: false,
timeout: None,
connection_timeout: None,
other: [],
},
source: SourceSpan {
start: 0,
end: 28,
},
}
variables: []
@@ -0,0 +1,130 @@
---
source: crates/core/src/httpfile/tests.rs
expression: dump(&f)
---
block 0 [0..73]
Separator "### List users\n"
RequestLine "GET {{base_url}}/users?limit=20\n"
Header "Accept: application/json\n"
Blank "\n"
=> Request {
name: None,
title: Some(
"List users",
),
method: Get,
url: "{{base_url}}/users?limit=20",
http_version: None,
headers: [
Header {
name: "Accept",
value: "application/json",
disabled: false,
},
],
body: None,
directives: Directives {
no_redirect: false,
no_cookie_jar: false,
no_log: false,
timeout: None,
connection_timeout: None,
other: [],
},
source: SourceSpan {
start: 0,
end: 73,
},
}
block 1 [73..230]
Separator "### Create user\n"
Directive "# @name create-user\n"
Directive "# @timeout 30\n"
RequestLine "POST {{base_url}}/users\n"
Header "Content-Type: application/json\n"
Header "Authorization: Bearer {{token}}\n"
Blank "\n"
Body "{ \"name\": \"Ada\" }\n"
Blank "\n"
=> Request {
name: Some(
"create-user",
),
title: Some(
"Create user",
),
method: Post,
url: "{{base_url}}/users",
http_version: None,
headers: [
Header {
name: "Content-Type",
value: "application/json",
disabled: false,
},
Header {
name: "Authorization",
value: "Bearer {{token}}",
disabled: false,
},
],
body: Text(
"{ \"name\": \"Ada\" }",
),
directives: Directives {
no_redirect: false,
no_cookie_jar: false,
no_log: false,
timeout: Some(
30s,
),
connection_timeout: None,
other: [],
},
source: SourceSpan {
start: 73,
end: 230,
},
}
block 2 [230..345]
Separator "### Upload avatar\n"
RequestLine "POST {{base_url}}/users/1/avatar\n"
Header "Content-Type: application/octet-stream\n"
Blank "\n"
Body "< ./payloads/avatar.png\n"
=> Request {
name: None,
title: Some(
"Upload avatar",
),
method: Post,
url: "{{base_url}}/users/1/avatar",
http_version: None,
headers: [
Header {
name: "Content-Type",
value: "application/octet-stream",
disabled: false,
},
],
body: FileRef {
path: "./payloads/avatar.png",
substitute: false,
},
directives: Directives {
no_redirect: false,
no_cookie_jar: false,
no_log: false,
timeout: None,
connection_timeout: None,
other: [],
},
source: SourceSpan {
start: 230,
end: 345,
},
}
variables: []
@@ -0,0 +1,35 @@
---
source: crates/core/src/httpfile/tests.rs
expression: dump(&f)
---
block 0 [0..131]
Variable "@host = localhost:8080\n"
Variable "@base = http://{{host}}/api\n"
Blank "\n"
Comment "# Comments before the first request\n"
Comment "// and in both styles\n"
Blank "\n"
RequestLine "GET {{base}}/health\n"
=> Request {
name: None,
title: None,
method: Get,
url: "{{base}}/health",
http_version: None,
headers: [],
body: None,
directives: Directives {
no_redirect: false,
no_cookie_jar: false,
no_log: false,
timeout: None,
connection_timeout: None,
other: [],
},
source: SourceSpan {
start: 0,
end: 131,
},
}
variables: [("host", "localhost:8080"), ("base", "http://{{host}}/api")]
@@ -0,0 +1,42 @@
---
source: crates/core/src/httpfile/tests.rs
expression: dump(&f)
---
block 0 [0..142]
Separator "### Search with params on separate lines\n"
RequestLine "GET https://api.example.com/search\n"
UrlContinuation " ?q=rust\n"
UrlContinuation " &page=2\n"
UrlContinuation " &per_page=50\n"
Header "Accept: application/json\n"
=> Request {
name: None,
title: Some(
"Search with params on separate lines",
),
method: Get,
url: "https://api.example.com/search?q=rust&page=2&per_page=50",
http_version: None,
headers: [
Header {
name: "Accept",
value: "application/json",
disabled: false,
},
],
body: None,
directives: Directives {
no_redirect: false,
no_cookie_jar: false,
no_log: false,
timeout: None,
connection_timeout: None,
other: [],
},
source: SourceSpan {
start: 0,
end: 142,
},
}
variables: []
@@ -0,0 +1,81 @@
---
source: crates/core/src/httpfile/tests.rs
expression: dump(&f)
---
block 0 [0..24]
Comment "# A file-level comment\n"
Blank "\n"
block 1 [24..205]
Separator "### Get thing\n"
Comment "// A comment before the request line\n"
Directive "# @name get-thing\n"
RequestLine "GET https://api.example.com/things/1\n"
DisabledHeader "# X-Debug: true\n"
Header "Accept: application/json\n"
Comment "// a stray comment among headers\n"
Blank "\n"
=> Request {
name: Some(
"get-thing",
),
title: Some(
"Get thing",
),
method: Get,
url: "https://api.example.com/things/1",
http_version: None,
headers: [
Header {
name: "X-Debug",
value: "true",
disabled: true,
},
Header {
name: "Accept",
value: "application/json",
disabled: false,
},
],
body: None,
directives: Directives {
no_redirect: false,
no_cookie_jar: false,
no_log: false,
timeout: None,
connection_timeout: None,
other: [],
},
source: SourceSpan {
start: 24,
end: 205,
},
}
block 2 [205..291]
Separator "###\n"
Blank "\n"
Comment "# Unnamed request after a bare separator\n"
RequestLine "DELETE https://api.example.com/things/1\n"
=> Request {
name: None,
title: None,
method: Delete,
url: "https://api.example.com/things/1",
http_version: None,
headers: [],
body: None,
directives: Directives {
no_redirect: false,
no_cookie_jar: false,
no_log: false,
timeout: None,
connection_timeout: None,
other: [],
},
source: SourceSpan {
start: 205,
end: 291,
},
}
variables: []
@@ -0,0 +1,78 @@
---
source: crates/core/src/httpfile/tests.rs
expression: dump(&f)
---
block 0 [0..349]
Separator "### Upload with form fields\n"
RequestLine "POST https://api.example.com/upload\n"
Header "Content-Type: multipart/form-data; boundary=WebAppBoundary\n"
Blank "\n"
Body "--WebAppBoundary\n"
Body "Content-Disposition: form-data; name=\"description\"\n"
Body "\n"
Body "A picture of a cat\n"
Body "--WebAppBoundary\n"
Body "Content-Disposition: form-data; name=\"file\"; filename=\"cat.png\"\n"
Body "Content-Type: image/png\n"
Body "\n"
Body "< ./cat.png\n"
Body "--WebAppBoundary--\n"
=> Request {
name: None,
title: Some(
"Upload with form fields",
),
method: Post,
url: "https://api.example.com/upload",
http_version: None,
headers: [
Header {
name: "Content-Type",
value: "multipart/form-data; boundary=WebAppBoundary",
disabled: false,
},
],
body: Multipart {
boundary: "WebAppBoundary",
parts: [
Part {
name: "description",
filename: None,
headers: [],
body: Text(
"A picture of a cat",
),
},
Part {
name: "file",
filename: Some(
"cat.png",
),
headers: [
Header {
name: "Content-Type",
value: "image/png",
disabled: false,
},
],
body: File(
"./cat.png",
),
},
],
},
directives: Directives {
no_redirect: false,
no_cookie_jar: false,
no_log: false,
timeout: None,
connection_timeout: None,
other: [],
},
source: SourceSpan {
start: 0,
end: 349,
},
}
variables: []
@@ -0,0 +1,92 @@
---
source: crates/core/src/httpfile/tests.rs
expression: dump(&f)
---
block 0 [0..270]
Separator "### Login\n"
RequestLine "POST {{base_url}}/login\n"
Header "Content-Type: application/json\n"
Blank "\n"
Body "{\n"
Body " \"user\": \"ada\",\n"
Body " \"password\": \"{{password}}\"\n"
Body "}\n"
Blank "\n"
ResponseHandler "> {%\n"
ResponseHandler " client.global.set(\"token\", response.body.token);\n"
ResponseHandler " client.test(\"ok\", function() {\n"
ResponseHandler " client.assert(response.status === 200);\n"
ResponseHandler " });\n"
ResponseHandler "%}\n"
Blank "\n"
=> Request {
name: None,
title: Some(
"Login",
),
method: Post,
url: "{{base_url}}/login",
http_version: None,
headers: [
Header {
name: "Content-Type",
value: "application/json",
disabled: false,
},
],
body: Text(
"{\n \"user\": \"ada\",\n \"password\": \"{{password}}\"\n}",
),
directives: Directives {
no_redirect: false,
no_cookie_jar: false,
no_log: false,
timeout: None,
connection_timeout: None,
other: [],
},
source: SourceSpan {
start: 0,
end: 270,
},
}
block 1 [270..389]
Separator "### Me\n"
RequestLine "GET {{base_url}}/me\n"
Header "Authorization: Bearer {{token}}\n"
Blank "\n"
ResponseHandler "> ./scripts/check-me.js\n"
Blank "\n"
ResponseRef "<> ./responses/me-2024-01-01.json\n"
=> Request {
name: None,
title: Some(
"Me",
),
method: Get,
url: "{{base_url}}/me",
http_version: None,
headers: [
Header {
name: "Authorization",
value: "Bearer {{token}}",
disabled: false,
},
],
body: None,
directives: Directives {
no_redirect: false,
no_cookie_jar: false,
no_log: false,
timeout: None,
connection_timeout: None,
other: [],
},
source: SourceSpan {
start: 270,
end: 389,
},
}
variables: []
@@ -0,0 +1,71 @@
---
source: crates/core/src/httpfile/tests.rs
expression: dump(&f)
---
block 0 [0..55]
Separator "### HTTP/2\n"
RequestLine "GET https://nghttp2.org/httpbin/get HTTP/2\n"
Blank "\n"
=> Request {
name: None,
title: Some(
"HTTP/2",
),
method: Get,
url: "https://nghttp2.org/httpbin/get",
http_version: Some(
"HTTP/2",
),
headers: [],
body: None,
directives: Directives {
no_redirect: false,
no_cookie_jar: false,
no_log: false,
timeout: None,
connection_timeout: None,
other: [],
},
source: SourceSpan {
start: 0,
end: 55,
},
}
block 1 [55..129]
Separator "### HTTP/1.1 explicit\n"
RequestLine "GET https://example.com/ HTTP/1.1\n"
Header "Host: example.com\n"
=> Request {
name: None,
title: Some(
"HTTP/1.1 explicit",
),
method: Get,
url: "https://example.com/",
http_version: Some(
"HTTP/1.1",
),
headers: [
Header {
name: "Host",
value: "example.com",
disabled: false,
},
],
body: None,
directives: Directives {
no_redirect: false,
no_cookie_jar: false,
no_log: false,
timeout: None,
connection_timeout: None,
other: [],
},
source: SourceSpan {
start: 55,
end: 129,
},
}
variables: []
@@ -0,0 +1,56 @@
---
source: crates/core/src/httpfile/tests.rs
expression: dump(&f)
---
block 0 [0..31]
RequestLine "https://example.com/no-method\n"
Blank "\n"
=> Request {
name: None,
title: None,
method: Get,
url: "https://example.com/no-method",
http_version: None,
headers: [],
body: None,
directives: Directives {
no_redirect: false,
no_cookie_jar: false,
no_log: false,
timeout: None,
connection_timeout: None,
other: [],
},
source: SourceSpan {
start: 0,
end: 31,
},
}
block 1 [31..58]
Separator "###\n"
Blank "\n"
RequestLine "example.com/no-scheme\n"
=> Request {
name: None,
title: None,
method: Get,
url: "example.com/no-scheme",
http_version: None,
headers: [],
body: None,
directives: Directives {
no_redirect: false,
no_cookie_jar: false,
no_log: false,
timeout: None,
connection_timeout: None,
other: [],
},
source: SourceSpan {
start: 31,
end: 58,
},
}
variables: []
@@ -0,0 +1,50 @@
---
source: crates/core/src/httpfile/tests.rs
expression: dump(&f)
---
block 0 [0..196]
Separator "### All the directives\n"
Directive "# @name everything\n"
Directive "# @no-redirect\n"
Directive "# @no-cookie-jar\n"
Directive "# @no-log\n"
Directive "# @timeout 600\n"
Directive "# @connection-timeout 2 s\n"
Directive "# @some-future-directive with a value\n"
RequestLine "GET https://example.com/redirect\n"
=> Request {
name: Some(
"everything",
),
title: Some(
"All the directives",
),
method: Get,
url: "https://example.com/redirect",
http_version: None,
headers: [],
body: None,
directives: Directives {
no_redirect: true,
no_cookie_jar: true,
no_log: true,
timeout: Some(
600s,
),
connection_timeout: Some(
2s,
),
other: [
(
"some-future-directive",
"with a value",
),
],
},
source: SourceSpan {
start: 0,
end: 196,
},
}
variables: []
@@ -0,0 +1,72 @@
---
source: crates/core/src/httpfile/tests.rs
expression: dump(&f)
---
block 0 [0..107]
Separator "### Windows line endings\r\n"
RequestLine "POST https://example.com/crlf\r\n"
Header "Content-Type: text/plain\r\n"
Blank "\r\n"
Body "line one\r\n"
Body "line two\r\n"
Blank "\r\n"
=> Request {
name: None,
title: Some(
"Windows line endings",
),
method: Post,
url: "https://example.com/crlf",
http_version: None,
headers: [
Header {
name: "Content-Type",
value: "text/plain",
disabled: false,
},
],
body: Text(
"line one\r\nline two",
),
directives: Directives {
no_redirect: false,
no_cookie_jar: false,
no_log: false,
timeout: None,
connection_timeout: None,
other: [],
},
source: SourceSpan {
start: 0,
end: 107,
},
}
block 1 [107..145]
Separator "### Second\r\n"
RequestLine "GET https://example.com/\r\n"
=> Request {
name: None,
title: Some(
"Second",
),
method: Get,
url: "https://example.com/",
http_version: None,
headers: [],
body: None,
directives: Directives {
no_redirect: false,
no_cookie_jar: false,
no_log: false,
timeout: None,
connection_timeout: None,
other: [],
},
source: SourceSpan {
start: 107,
end: 145,
},
}
variables: []
@@ -0,0 +1,43 @@
---
source: crates/core/src/httpfile/tests.rs
expression: dump(&f)
---
block 0 [0..91]
Separator "### No trailing newline\n"
RequestLine "POST https://example.com/x\n"
Header "Content-Type: application/json\n"
Blank "\n"
Body "{\"a\": 1}"
=> Request {
name: None,
title: Some(
"No trailing newline",
),
method: Post,
url: "https://example.com/x",
http_version: None,
headers: [
Header {
name: "Content-Type",
value: "application/json",
disabled: false,
},
],
body: Text(
"{\"a\": 1}",
),
directives: Directives {
no_redirect: false,
no_cookie_jar: false,
no_log: false,
timeout: None,
connection_timeout: None,
other: [],
},
source: SourceSpan {
start: 0,
end: 91,
},
}
variables: []
@@ -0,0 +1,51 @@
---
source: crates/core/src/httpfile/tests.rs
expression: dump(&f)
---
block 0 [0..169]
Separator "### GraphQL query\n"
RequestLine "POST https://api.github.com/graphql\n"
Header "Content-Type: application/json\n"
Header "Authorization: Bearer {{github_token}}\n"
Blank "\n"
Body "{\n"
Body " \"query\": \"query { viewer { login } }\"\n"
Body "}\n"
=> Request {
name: None,
title: Some(
"GraphQL query",
),
method: Post,
url: "https://api.github.com/graphql",
http_version: None,
headers: [
Header {
name: "Content-Type",
value: "application/json",
disabled: false,
},
Header {
name: "Authorization",
value: "Bearer {{github_token}}",
disabled: false,
},
],
body: Text(
"{\n \"query\": \"query { viewer { login } }\"\n}",
),
directives: Directives {
no_redirect: false,
no_cookie_jar: false,
no_log: false,
timeout: None,
connection_timeout: None,
other: [],
},
source: SourceSpan {
start: 0,
end: 169,
},
}
variables: []
@@ -0,0 +1,45 @@
---
source: crates/core/src/httpfile/tests.rs
expression: dump(&f)
---
block 0 [0..195]
Separator "### OAuth token\n"
RequestLine "POST https://auth.example.com/oauth/token\n"
Header "Content-Type: application/x-www-form-urlencoded\n"
Blank "\n"
Body "grant_type=client_credentials&\n"
Body "client_id={{client_id}}&\n"
Body "client_secret={{client_secret}}\n"
=> Request {
name: None,
title: Some(
"OAuth token",
),
method: Post,
url: "https://auth.example.com/oauth/token",
http_version: None,
headers: [
Header {
name: "Content-Type",
value: "application/x-www-form-urlencoded",
disabled: false,
},
],
body: Text(
"grant_type=client_credentials&\nclient_id={{client_id}}&\nclient_secret={{client_secret}}",
),
directives: Directives {
no_redirect: false,
no_cookie_jar: false,
no_log: false,
timeout: None,
connection_timeout: None,
other: [],
},
source: SourceSpan {
start: 0,
end: 195,
},
}
variables: []
@@ -0,0 +1,44 @@
---
source: crates/core/src/httpfile/tests.rs
expression: dump(&f)
---
block 0 [0..114]
Separator "### Body from a template file\n"
RequestLine "PUT {{base_url}}/orders/42\n"
Header "Content-Type: application/json\n"
Blank "\n"
Body "<@ ./payloads/order.json\n"
=> Request {
name: None,
title: Some(
"Body from a template file",
),
method: Put,
url: "{{base_url}}/orders/42",
http_version: None,
headers: [
Header {
name: "Content-Type",
value: "application/json",
disabled: false,
},
],
body: FileRef {
path: "./payloads/order.json",
substitute: true,
},
directives: Directives {
no_redirect: false,
no_cookie_jar: false,
no_log: false,
timeout: None,
connection_timeout: None,
other: [],
},
source: SourceSpan {
start: 0,
end: 114,
},
}
variables: []
@@ -0,0 +1,108 @@
---
source: crates/core/src/httpfile/tests.rs
expression: dump(&f)
---
block 0 [0..61]
Separator "### WebDAV\n"
RequestLine "PROPFIND https://dav.example.com/files/\n"
Header "Depth: 1\n"
Blank "\n"
=> Request {
name: None,
title: Some(
"WebDAV",
),
method: Custom(
"PROPFIND",
),
url: "https://dav.example.com/files/",
http_version: None,
headers: [
Header {
name: "Depth",
value: "1",
disabled: false,
},
],
body: None,
directives: Directives {
no_redirect: false,
no_cookie_jar: false,
no_log: false,
timeout: None,
connection_timeout: None,
other: [],
},
source: SourceSpan {
start: 0,
end: 61,
},
}
block 1 [61..171]
Separator "### Patch\n"
RequestLine "PATCH https://api.example.com/users/1\n"
Header "Content-Type: application/merge-patch+json\n"
Blank "\n"
Body "{\"name\":\"Grace\"}\n"
Blank "\n"
=> Request {
name: None,
title: Some(
"Patch",
),
method: Patch,
url: "https://api.example.com/users/1",
http_version: None,
headers: [
Header {
name: "Content-Type",
value: "application/merge-patch+json",
disabled: false,
},
],
body: Text(
"{\"name\":\"Grace\"}",
),
directives: Directives {
no_redirect: false,
no_cookie_jar: false,
no_log: false,
timeout: None,
connection_timeout: None,
other: [],
},
source: SourceSpan {
start: 61,
end: 171,
},
}
block 2 [171..216]
Separator "### Options\n"
RequestLine "OPTIONS https://api.example.com/\n"
=> Request {
name: None,
title: Some(
"Options",
),
method: Options,
url: "https://api.example.com/",
http_version: None,
headers: [],
body: None,
directives: Directives {
no_redirect: false,
no_cookie_jar: false,
no_log: false,
timeout: None,
connection_timeout: None,
other: [],
},
source: SourceSpan {
start: 171,
end: 216,
},
}
variables: []
@@ -0,0 +1,83 @@
---
source: crates/core/src/httpfile/tests.rs
expression: dump(&f)
---
block 0 [0..2]
Blank "\n"
Blank "\n"
block 1 [2..58]
Separator "### Lots of space around\n"
Blank "\n"
Blank "\n"
RequestLine "GET https://example.com/a\n"
Blank "\n"
Blank "\n"
Blank "\n"
=> Request {
name: None,
title: Some(
"Lots of space around",
),
method: Get,
url: "https://example.com/a",
http_version: None,
headers: [],
body: None,
directives: Directives {
no_redirect: false,
no_cookie_jar: false,
no_log: false,
timeout: None,
connection_timeout: None,
other: [],
},
source: SourceSpan {
start: 2,
end: 58,
},
}
block 2 [58..181]
Separator "### Body with blank lines inside\n"
RequestLine "POST https://example.com/b\n"
Header "Content-Type: text/plain\n"
Blank "\n"
Body "first paragraph\n"
Body "\n"
Body "second paragraph\n"
Blank "\n"
Blank "\n"
Blank "\n"
=> Request {
name: None,
title: Some(
"Body with blank lines inside",
),
method: Post,
url: "https://example.com/b",
http_version: None,
headers: [
Header {
name: "Content-Type",
value: "text/plain",
disabled: false,
},
],
body: Text(
"first paragraph\n\nsecond paragraph",
),
directives: Directives {
no_redirect: false,
no_cookie_jar: false,
no_log: false,
timeout: None,
connection_timeout: None,
other: [],
},
source: SourceSpan {
start: 58,
end: 181,
},
}
variables: []
@@ -0,0 +1,100 @@
---
source: crates/core/src/httpfile/tests.rs
expression: dump(&f)
---
block 0 [0..158]
Variable "@baseUrl = https://example.com/api\n"
Blank "\n"
Directive "# @name login\n"
RequestLine "POST {{baseUrl}}/api/login HTTP/1.1\n"
Header "Content-Type: application/x-www-form-urlencoded\n"
Blank "\n"
Body "name=foo&password=bar\n"
Blank "\n"
=> Request {
name: Some(
"login",
),
title: None,
method: Post,
url: "{{baseUrl}}/api/login",
http_version: Some(
"HTTP/1.1",
),
headers: [
Header {
name: "Content-Type",
value: "application/x-www-form-urlencoded",
disabled: false,
},
],
body: Text(
"name=foo&password=bar",
),
directives: Directives {
no_redirect: false,
no_cookie_jar: false,
no_log: false,
timeout: None,
connection_timeout: None,
other: [],
},
source: SourceSpan {
start: 0,
end: 158,
},
}
block 1 [158..368]
Separator "###\n"
Blank "\n"
Variable "@authToken = {{login.response.headers.X-AuthToken}}\n"
Blank "\n"
Directive "# @name createComment\n"
RequestLine "POST {{baseUrl}}/comments HTTP/1.1\n"
Header "Authorization: {{authToken}}\n"
Header "Content-Type: application/json\n"
Blank "\n"
Body "{\n"
Body " \"content\": \"fake content\"\n"
Body "}\n"
=> Request {
name: Some(
"createComment",
),
title: None,
method: Post,
url: "{{baseUrl}}/comments",
http_version: Some(
"HTTP/1.1",
),
headers: [
Header {
name: "Authorization",
value: "{{authToken}}",
disabled: false,
},
Header {
name: "Content-Type",
value: "application/json",
disabled: false,
},
],
body: Text(
"{\n \"content\": \"fake content\"\n}",
),
directives: Directives {
no_redirect: false,
no_cookie_jar: false,
no_log: false,
timeout: None,
connection_timeout: None,
other: [],
},
source: SourceSpan {
start: 158,
end: 368,
},
}
variables: [("baseUrl", "https://example.com/api"), ("authToken", "{{login.response.headers.X-AuthToken}}")]
@@ -0,0 +1,9 @@
---
source: crates/core/src/httpfile/tests.rs
expression: dump(&f)
---
block 0 [0..73]
Comment "# This file has no requests yet.\n"
Comment "// Just notes.\n"
Variable "@base = http://localhost\n"
variables: [("base", "http://localhost")]
@@ -0,0 +1,45 @@
---
source: crates/core/src/httpfile/tests.rs
expression: dump(&f)
---
block 0 [0..160]
Separator "### Body lines starting with # or // are body, not comments\n"
RequestLine "POST https://example.com/markdown\n"
Header "Content-Type: text/markdown\n"
Blank "\n"
Body "# Heading\n"
Body "// not a comment\n"
Body "Some text\n"
=> Request {
name: None,
title: Some(
"Body lines starting with # or // are body, not comments",
),
method: Post,
url: "https://example.com/markdown",
http_version: None,
headers: [
Header {
name: "Content-Type",
value: "text/markdown",
disabled: false,
},
],
body: Text(
"# Heading\n// not a comment\nSome text",
),
directives: Directives {
no_redirect: false,
no_cookie_jar: false,
no_log: false,
timeout: None,
connection_timeout: None,
other: [],
},
source: SourceSpan {
start: 0,
end: 160,
},
}
variables: []
+318
View File
@@ -0,0 +1,318 @@
use std::path::{Path, PathBuf};
use std::time::Duration;
use super::*;
use crate::model::{Body, Header, Method, PartBody};
fn fixtures_dir() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).join("../../tests/fixtures/http")
}
fn fixtures() -> Vec<(String, String)> {
let mut out: Vec<(String, String)> = std::fs::read_dir(fixtures_dir())
.unwrap()
.map(|e| e.unwrap().path())
.filter(|p| p.extension().is_some_and(|e| e == "http"))
.map(|p| {
(
p.file_name().unwrap().to_string_lossy().into_owned(),
std::fs::read_to_string(&p).unwrap(),
)
})
.collect();
out.sort();
assert!(out.len() >= 15, "expected the fixture corpus, found {}", out.len());
out
}
/// A readable dump of the syntax tree and the extracted requests.
fn dump(f: &HttpFile) -> String {
let mut s = String::new();
for (i, b) in f.blocks().iter().enumerate() {
s.push_str(&format!("block {i} [{}..{}]\n", b.span.start, b.span.end));
for l in &b.lines {
let text = &f.text()[l.span.clone()];
s.push_str(&format!(" {:<16} {:?}\n", format!("{:?}", l.kind), text));
}
if let Some(r) = &b.request {
s.push_str(&format!(" => {r:#?}\n").replace('\n', "\n "));
s.push('\n');
}
}
s.push_str(&format!("variables: {:?}\n", f.variables()));
s
}
#[test]
fn fixture_snapshots() {
for (name, text) in fixtures() {
let f = HttpFile::parse(text);
insta::assert_snapshot!(name, dump(&f));
}
}
#[test]
fn round_trip_is_byte_identical_for_every_fixture() {
for (name, text) in fixtures() {
let f = HttpFile::parse(text.clone());
assert_eq!(f.write(), text, "{name}");
// Blocks are contiguous and cover the whole file.
let mut pos = 0;
for b in f.blocks() {
assert_eq!(b.span.start, pos, "{name}");
pos = b.span.end;
let mut lpos = b.span.start;
for l in &b.lines {
assert_eq!(l.span.start, lpos, "{name}");
lpos = l.span.end;
}
assert_eq!(lpos, b.span.end, "{name}");
}
assert_eq!(pos, text.len(), "{name}");
}
}
#[test]
fn replacing_a_request_with_itself_changes_nothing() {
for (name, text) in fixtures() {
let f = HttpFile::parse(text.clone());
for i in 0..f.request_count() {
let r = f.request(i).unwrap().clone();
assert_eq!(f.replace_request(i, &r).unwrap(), text, "{name} #{i}");
}
}
}
#[test]
fn editing_one_request_leaves_the_rest_of_the_file_byte_identical() {
for (name, text) in fixtures() {
let f = HttpFile::parse(text.clone());
for i in 0..f.request_count() {
let mut r = f.request(i).unwrap().clone();
r.url.push_str("/edited");
r.headers.push(Header::new("X-Edited", "yes"));
let new_text = f.replace_request(i, &r).unwrap();
let span = f.request(i).unwrap().source;
assert_eq!(&new_text[..span.start], &text[..span.start], "{name} #{i}: prefix");
let suffix_len = text.len() - span.end;
assert_eq!(
&new_text[new_text.len() - suffix_len..],
&text[span.end..],
"{name} #{i}: suffix"
);
// The edit is visible and everything else about the request survives.
let g = HttpFile::parse(new_text);
assert_eq!(g.request_count(), f.request_count(), "{name} #{i}");
let mut got = g.request(i).unwrap().clone();
got.source = r.source;
assert_eq!(got, r, "{name} #{i}");
for j in (0..f.request_count()).filter(|&j| j != i) {
assert!(g.request(j).unwrap().same_content(f.request(j).unwrap()), "{name} #{j}");
}
}
}
}
#[test]
fn parses_the_plan_example() {
let f = HttpFile::parse(std::fs::read_to_string(fixtures_dir().join("02-titles-and-names.http")).unwrap());
let reqs: Vec<_> = f.requests().collect();
assert_eq!(reqs.len(), 3);
assert_eq!(reqs[0].title.as_deref(), Some("List users"));
assert_eq!(reqs[0].url, "{{base_url}}/users?limit=20");
assert_eq!(reqs[1].name.as_deref(), Some("create-user"));
assert_eq!(reqs[1].display_name(), Some("create-user"));
assert_eq!(reqs[1].method, Method::Post);
assert_eq!(reqs[1].directives.timeout, Some(Duration::from_secs(30)));
assert_eq!(reqs[1].body, Body::Text("{ \"name\": \"Ada\" }".into()));
assert_eq!(
reqs[2].body,
Body::FileRef {
path: "./payloads/avatar.png".into(),
substitute: false
}
);
}
#[test]
fn query_continuations_join_the_url() {
let f = HttpFile::parse("GET https://x/search\n ?q=rust\n &page=2\nAccept: */*\n");
let r = f.request(0).unwrap();
assert_eq!(r.url, "https://x/search?q=rust&page=2");
assert_eq!(r.headers, vec![Header::new("Accept", "*/*")]);
}
#[test]
fn multipart_parts() {
let f = HttpFile::parse(std::fs::read_to_string(fixtures_dir().join("06-multipart.http")).unwrap());
let Body::Multipart { boundary, parts } = &f.request(0).unwrap().body else {
panic!("not multipart")
};
assert_eq!(boundary, "WebAppBoundary");
assert_eq!(parts.len(), 2);
assert_eq!(parts[0].body, PartBody::Text("A picture of a cat".into()));
assert_eq!(parts[1].filename.as_deref(), Some("cat.png"));
assert_eq!(parts[1].content_type(), Some("image/png"));
assert_eq!(parts[1].body, PartBody::File("./cat.png".into()));
}
#[test]
fn handlers_are_not_part_of_the_body() {
let f = HttpFile::parse(std::fs::read_to_string(fixtures_dir().join("07-response-handler.http")).unwrap());
let Body::Text(body) = &f.request(0).unwrap().body else {
panic!()
};
assert!(body.starts_with('{') && body.ends_with('}'), "{body:?}");
assert!(f.request(1).unwrap().body.is_none());
}
#[test]
fn render_and_reparse() {
let mut r = Request {
title: Some("New".into()),
name: Some("new-one".into()),
method: Method::Post,
url: "{{base}}/x".into(),
headers: vec![
Header::new("Content-Type", "application/json"),
Header {
name: "X-Off".into(),
value: "1".into(),
disabled: true,
},
],
body: Body::Text("{\n \"a\": 1\n}".into()),
..Request::default()
};
r.directives.no_redirect = true;
r.directives.timeout = Some(Duration::from_millis(1500));
let text = render_request(&r, "\n");
insta::assert_snapshot!(text, @r#"
### New
# @name new-one
# @no-redirect
# @timeout 1500 ms
POST {{base}}/x
Content-Type: application/json
# X-Off: 1
{
"a": 1
}
"#);
let back = HttpFile::parse(text);
assert!(back.request(0).unwrap().same_content(&r));
}
#[test]
fn insert_and_remove() {
let text = "### A\nGET /a\n\n### B\nGET /b\n";
let f = HttpFile::parse(text);
let new = Request {
url: "/new".into(),
..Request::default()
};
let inserted = f.insert_request(Some(0), &new);
assert_eq!(inserted, "### A\nGET /a\n\n###\nGET /new\n\n### B\nGET /b\n");
let g = HttpFile::parse(inserted);
assert_eq!(
g.requests().map(|r| r.url.as_str()).collect::<Vec<_>>(),
["/a", "/new", "/b"]
);
assert_eq!(g.remove_request(1).unwrap(), text);
let appended = f.insert_request(
None,
&Request {
title: Some("C".into()),
..new.clone()
},
);
assert_eq!(appended, "### A\nGET /a\n\n### B\nGET /b\n\n### C\nGET /new\n");
let empty = HttpFile::parse("");
assert_eq!(empty.insert_request(None, &new), "###\nGET /new\n");
}
#[test]
fn keys_find_requests_by_name_then_index() {
let f = HttpFile::parse("### A\nGET /a\n\n###\nGET /x\n\n### B\nGET /b\n");
assert_eq!(f.key_of(2).unwrap().name.as_deref(), Some("B"));
assert_eq!(
f.find(&RequestKey {
name: Some("B".into()),
index: 0
}),
Some(2)
);
assert_eq!(f.find(&RequestKey { name: None, index: 1 }), Some(1));
assert_eq!(
f.find(&RequestKey {
name: Some("gone".into()),
index: 1
}),
None
);
}
#[test]
fn crlf_is_preserved_on_edit() {
let text = "### A\r\nGET /a\r\n\r\n### B\r\nGET /b\r\n";
let f = HttpFile::parse(text);
assert_eq!(f.eol(), "\r\n");
let mut r = f.request(0).unwrap().clone();
r.headers.push(Header::new("X", "1"));
assert_eq!(
f.replace_request(0, &r).unwrap(),
"### A\r\nGET /a\r\nX: 1\r\n\r\n### B\r\nGET /b\r\n"
);
}
#[test]
fn adding_a_body_to_a_request_at_eof_without_newline() {
let f = HttpFile::parse("GET /a");
let mut r = f.request(0).unwrap().clone();
r.method = Method::Post;
r.body = Body::Text("hi".into());
assert_eq!(f.replace_request(0, &r).unwrap(), "POST /a\n\nhi\n");
}
#[test]
fn removing_a_body_keeps_trailing_spacing_and_handlers() {
let text = "### A\nPOST /a\n\n{}\n\n> ./h.js\n\n### B\nGET /b\n";
let f = HttpFile::parse(text);
let mut r = f.request(0).unwrap().clone();
r.body = Body::None;
assert_eq!(
f.replace_request(0, &r).unwrap(),
"### A\nPOST /a\n\n> ./h.js\n\n### B\nGET /b\n"
);
}
#[test]
fn source_text_round_trip() {
let text = "### A\nGET /a\n# comment\n\n\n### B\nGET /b";
let f = HttpFile::parse(text);
assert_eq!(f.request_source(0), Some("### A\nGET /a\n# comment\n"));
assert_eq!(f.request_source(1), Some("### B\nGET /b"));
for i in 0..2 {
let src = f.request_source(i).unwrap().to_string();
assert_eq!(f.replace_request_text(i, &src).unwrap(), text);
}
assert_eq!(
f.replace_request_text(0, "### A2\nPOST /a\n").unwrap(),
"### A2\nPOST /a\n\n\n### B\nGET /b"
);
}
#[test]
fn moving_a_block_between_files() {
let from = HttpFile::parse("### A\nGET /a\n# keep me\n\n### B\nGET /b\n");
let to = HttpFile::parse("### C\nGET /c\n");
let moved = from.request_source(0).unwrap();
assert_eq!(from.remove_request(0).unwrap(), "### B\nGET /b\n");
assert_eq!(
to.insert_request_text(None, moved),
"### C\nGET /c\n\n### A\nGET /a\n# keep me\n"
);
}
+220
View File
@@ -0,0 +1,220 @@
//! Rendering requests to `.http` text, and rewriting one block in place while
//! keeping everything we don't model (comments, handlers, spacing).
use super::lexer::format_duration;
use super::{Block, LineKind};
use crate::model::{Body, Header, PartBody, Request};
/// Renders a request as a fresh block (separator line only if it has a title).
pub fn render_request(req: &Request, eol: &str) -> String {
let mut out = String::new();
if let Some(t) = &req.title {
out.push_str(&format!("### {t}{eol}"));
}
out.push_str(&render_directives(req, eol));
out.push_str(&render_request_line(req, eol));
out.push_str(&render_headers(&req.headers, eol));
if !req.body.is_none() {
out.push_str(eol);
out.push_str(&render_body(&req.body, eol));
}
out
}
fn render_directives(req: &Request, eol: &str) -> String {
let d = &req.directives;
let mut out = String::new();
if let Some(n) = &req.name {
out.push_str(&format!("# @name {n}{eol}"));
}
if d.no_redirect {
out.push_str(&format!("# @no-redirect{eol}"));
}
if d.no_cookie_jar {
out.push_str(&format!("# @no-cookie-jar{eol}"));
}
if d.no_log {
out.push_str(&format!("# @no-log{eol}"));
}
if let Some(t) = d.timeout {
out.push_str(&format!("# @timeout {}{eol}", format_duration(t)));
}
if let Some(t) = d.connection_timeout {
out.push_str(&format!("# @connection-timeout {}{eol}", format_duration(t)));
}
for (n, v) in &d.other {
if v.is_empty() {
out.push_str(&format!("# @{n}{eol}"));
} else {
out.push_str(&format!("# @{n} {v}{eol}"));
}
}
out
}
fn render_request_line(req: &Request, eol: &str) -> String {
match &req.http_version {
Some(v) => format!("{} {} {v}{eol}", req.method, req.url),
None => format!("{} {}{eol}", req.method, req.url),
}
}
fn render_headers(headers: &[Header], eol: &str) -> String {
let mut out = String::new();
for h in headers {
if h.disabled {
out.push_str(&format!("# {}: {}{eol}", h.name, h.value));
} else {
out.push_str(&format!("{}: {}{eol}", h.name, h.value));
}
}
out
}
/// Renders a body, ending with a line terminator.
pub(crate) fn render_body(body: &Body, eol: &str) -> String {
match body {
Body::None => String::new(),
Body::Text(t) => format!("{t}{eol}"),
Body::FileRef { path, substitute } => {
let marker = if *substitute { "<@" } else { "<" };
format!("{marker} {}{eol}", path.display())
}
Body::Multipart { boundary, parts } => {
let mut out = String::new();
for p in parts {
out.push_str(&format!("--{boundary}{eol}"));
out.push_str(&format!("Content-Disposition: form-data; name=\"{}\"", p.name));
if let Some(f) = &p.filename {
out.push_str(&format!("; filename=\"{f}\""));
}
out.push_str(eol);
out.push_str(&render_headers(&p.headers, eol));
out.push_str(eol);
match &p.body {
PartBody::Text(t) => out.push_str(&format!("{t}{eol}")),
PartBody::File(f) => out.push_str(&format!("< {}{eol}", f.display())),
}
}
out.push_str(&format!("--{boundary}--{eol}"));
out
}
}
}
/// Rewrites a request block so it describes `new`, keeping comments, response
/// handlers and trailing spacing from the original. Parts of the request that
/// didn't change keep their original text.
// Index loops read best here: each range walks `kinds` and `lt` together.
#[allow(clippy::needless_range_loop)]
pub(crate) fn rewrite_block(old_text: &str, block: &Block, new: &Request, eol: &str) -> String {
let Some(old) = &block.request else {
return render_request(new, eol);
};
if old.same_content(new) {
return old_text.to_string();
}
let base = block.span.start;
let lt = |i: usize| {
let s = &block.lines[i].span;
&old_text[s.start - base..s.end - base]
};
let kinds: Vec<&LineKind> = block.lines.iter().map(|l| &l.kind).collect();
let mut out = String::with_capacity(old_text.len() + 64);
let push = |out: &mut String, s: &str| {
if !out.is_empty() && !out.ends_with('\n') {
out.push_str(eol);
}
out.push_str(s);
};
let rl = kinds
.iter()
.position(|k| **k == LineKind::RequestLine)
.expect("request block has a request line");
// Separator.
let has_sep = kinds.first() == Some(&&LineKind::Separator);
if new.title == old.title {
if has_sep {
push(&mut out, lt(0));
}
} else if let Some(t) = &new.title {
push(&mut out, &format!("### {t}{eol}"));
} else if has_sep {
push(&mut out, &format!("###{eol}"));
}
// Lines before the request line: comments, variables, directives.
let directives_changed = old.directives != new.directives || old.name != new.name;
for i in usize::from(has_sep)..rl {
if *kinds[i] == LineKind::Directive && directives_changed {
continue;
}
push(&mut out, lt(i));
}
if directives_changed {
push(&mut out, &render_directives(new, eol));
}
// Request line and URL continuations.
let mut i = rl + 1;
while i < kinds.len() && *kinds[i] == LineKind::UrlContinuation {
i += 1;
}
if old.method == new.method && old.url == new.url && old.http_version == new.http_version {
for j in rl..i {
push(&mut out, lt(j));
}
} else {
push(&mut out, &render_request_line(new, eol));
}
// Header section: up to the first blank line (or body/handler).
let header_start = i;
while i < kinds.len()
&& matches!(
kinds[i],
LineKind::Header | LineKind::DisabledHeader | LineKind::Comment | LineKind::Unknown
)
{
i += 1;
}
if old.headers == new.headers {
for j in header_start..i {
push(&mut out, lt(j));
}
} else {
push(&mut out, &render_headers(&new.headers, eol));
for j in header_start..i {
if matches!(kinds[j], LineKind::Comment | LineKind::Unknown) {
push(&mut out, lt(j));
}
}
}
// Body: the blank separator line plus all body lines.
let last_body = kinds.iter().rposition(|k| **k == LineKind::Body);
let tail_start = match last_body {
Some(lb) if lb >= i => lb + 1,
_ => i,
};
if old.body == new.body {
for j in i..tail_start {
push(&mut out, lt(j));
}
} else if !new.body.is_none() {
push(&mut out, eol);
out.push_str(&render_body(&new.body, eol));
}
// Everything after: trailing blanks, response handlers, comments.
for j in tail_start..kinds.len() {
push(&mut out, lt(j));
}
if old_text.ends_with('\n') && !out.ends_with('\n') {
out.push_str(eol);
}
out
}
+437
View File
@@ -0,0 +1,437 @@
//! Importing a curl command line (as copied from browser devtools, Unix or
//! Windows flavour) into a [`Request`].
use std::path::PathBuf;
use std::time::Duration;
use crate::model::{Body, Header, Method, Part, PartBody, Request};
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum CurlImportError {
#[error("not a curl command")]
NotCurl,
#[error("could not split the command into words: {0}")]
Tokenize(String),
#[error("option {0} needs a value")]
MissingValue(String),
#[error("no URL in the command")]
NoUrl,
}
/// Whether `text` looks like a curl command (used to detect pastes into the
/// URL bar).
pub fn looks_like_curl(text: &str) -> bool {
let t = text.trim_start();
t.starts_with("curl ") || t.starts_with("curl.exe ") || t.starts_with("curl\n")
}
const MULTIPART_BOUNDARY: &str = "WebAppBoundary";
pub fn from_curl(cmd: &str) -> Result<Request, CurlImportError> {
let words = tokenize(cmd)?;
let mut words = words.into_iter();
match words.next().as_deref() {
Some("curl" | "curl.exe") => {}
_ => return Err(CurlImportError::NotCurl),
}
let mut method: Option<Method> = None;
let mut url: Option<String> = None;
let mut headers: Vec<Header> = Vec::new();
let mut data: Vec<String> = Vec::new();
let mut data_file: Option<PathBuf> = None;
let mut parts: Vec<Part> = Vec::new();
let mut get = false;
let mut head = false;
let mut req = Request::default();
// Expand `-XPOST` and `-sSL` style short options.
let mut args: Vec<String> = Vec::new();
for w in words {
if w.starts_with('-') && !w.starts_with("--") && w.len() > 2 {
let flag = &w[..2];
if takes_value(flag) {
args.push(flag.to_string());
args.push(w[2..].to_string());
} else if w[1..].chars().all(|c| c.is_ascii_alphabetic()) {
for (i, c) in w[1..].char_indices() {
let f = format!("-{c}");
if takes_value(&f) {
args.push(f);
args.push(w[1 + i + 1..].to_string());
break;
}
args.push(f);
}
} else {
args.push(w);
}
} else {
args.push(w);
}
}
let mut it = args.into_iter();
while let Some(a) = it.next() {
let mut value = |name: &str| it.next().ok_or_else(|| CurlImportError::MissingValue(name.to_string()));
match a.as_str() {
"-X" | "--request" => method = Method::parse(&value(&a)?.to_ascii_uppercase()),
"-H" | "--header" => {
let h = value(&a)?;
if let Some((n, v)) = h.split_once(':') {
let v = v.trim();
// `-H 'X;'` sends an empty header; `-H 'X:'` removes one.
if !v.is_empty() {
headers.push(Header::new(n.trim(), v));
}
} else if let Some(n) = h.strip_suffix(';') {
headers.push(Header::new(n.trim(), ""));
}
}
"-d" | "--data" | "--data-ascii" | "--data-binary" => {
let v = value(&a)?;
match v.strip_prefix('@') {
Some(f) if f != "-" => data_file = Some(PathBuf::from(f)),
_ => data.push(v),
}
}
"--data-raw" => data.push(value(&a)?),
"--data-urlencode" => {
let v = value(&a)?;
data.push(match v.split_once('=') {
Some((k, val)) if !k.is_empty() => format!("{k}={}", urlencode(val)),
Some((_, val)) => urlencode(val),
None => urlencode(&v),
});
}
"--json" => {
data.push(value(&a)?);
if !headers.iter().any(|h| h.name.eq_ignore_ascii_case("content-type")) {
headers.push(Header::new("Content-Type", "application/json"));
}
if !headers.iter().any(|h| h.name.eq_ignore_ascii_case("accept")) {
headers.push(Header::new("Accept", "application/json"));
}
}
"-F" | "--form" | "--form-string" => {
let v = value(&a)?;
if let Some(p) = parse_form(&v, a == "--form-string") {
parts.push(p);
}
}
"-u" | "--user" => {
let v = value(&a)?;
let (user, pass) = v.split_once(':').unwrap_or((&v, ""));
headers.push(Header::new("Authorization", format!("Basic {user} {pass}")));
}
"-A" | "--user-agent" => headers.push(Header::new("User-Agent", value(&a)?)),
"-e" | "--referer" => headers.push(Header::new("Referer", value(&a)?)),
"-b" | "--cookie" => {
let v = value(&a)?;
if v.contains('=') {
headers.push(Header::new("Cookie", v));
}
}
"--url" => url = Some(value(&a)?),
"-G" | "--get" => get = true,
"-I" | "--head" => head = true,
"-k" | "--insecure" => req.directives.other.push(("insecure".into(), String::new())),
"-m" | "--max-time" => req.directives.timeout = parse_secs(&value(&a)?),
"--connect-timeout" => req.directives.connection_timeout = parse_secs(&value(&a)?),
"--http1.1" => req.http_version = Some("HTTP/1.1".into()),
"--http2" | "--http2-prior-knowledge" => req.http_version = Some("HTTP/2".into()),
"-L" | "--location" | "--compressed" => {}
s if takes_value(s) => {
value(s)?;
}
s if s.starts_with('-') => {} // flags without a value we don't need
_ => {
if url.is_none() {
url = Some(a);
}
}
}
}
let mut url = url.ok_or(CurlImportError::NoUrl)?;
// Let curl negotiate compression (and decode it), as `--compressed` does;
// a copied `br, zstd` list could ask for encodings we can't decode.
headers.retain(|h| !h.name.eq_ignore_ascii_case("accept-encoding"));
let has_ct = headers.iter().any(|h| h.name.eq_ignore_ascii_case("content-type"));
if get && !data.is_empty() {
url.push(if url.contains('?') { '&' } else { '?' });
url.push_str(&data.join("&"));
data.clear();
}
if !parts.is_empty() {
headers.retain(|h| !h.name.eq_ignore_ascii_case("content-type"));
headers.push(Header::new(
"Content-Type",
format!("multipart/form-data; boundary={MULTIPART_BOUNDARY}"),
));
req.body = Body::Multipart {
boundary: MULTIPART_BOUNDARY.into(),
parts,
};
} else if let Some(f) = data_file {
req.body = Body::FileRef {
path: f,
substitute: false,
};
} else if !data.is_empty() {
req.body = Body::Text(data.join("&"));
if !has_ct {
headers.push(Header::new("Content-Type", "application/x-www-form-urlencoded"));
}
}
req.method = match (method, head, req.body.is_none()) {
(Some(m), _, _) => m,
(None, true, _) => Method::Head,
(None, false, false) => Method::Post,
(None, false, true) => Method::Get,
};
req.url = url;
req.headers = headers;
Ok(req)
}
fn takes_value(flag: &str) -> bool {
matches!(
flag,
"-X" | "--request"
| "-H"
| "--header"
| "-d"
| "--data"
| "--data-ascii"
| "--data-binary"
| "--data-raw"
| "--data-urlencode"
| "--json"
| "-F"
| "--form"
| "--form-string"
| "-u"
| "--user"
| "-A"
| "--user-agent"
| "-e"
| "--referer"
| "-b"
| "--cookie"
| "--url"
| "-m"
| "--max-time"
| "--connect-timeout"
| "-o"
| "--output"
| "-w"
| "--write-out"
| "-x"
| "--proxy"
| "-U"
| "--proxy-user"
| "--cacert"
| "--capath"
| "-E"
| "--cert"
| "--key"
| "-c"
| "--cookie-jar"
| "--retry"
| "-K"
| "--config"
| "-T"
| "--upload-file"
| "--resolve"
| "--connect-to"
| "--max-redirs"
| "-r"
| "--range"
| "-z"
| "--time-cond"
)
}
fn parse_secs(v: &str) -> Option<Duration> {
v.parse::<f64>().ok().map(Duration::from_secs_f64)
}
fn urlencode(s: &str) -> String {
let mut out = String::new();
for b in s.bytes() {
if b.is_ascii_alphanumeric() || b"-_.~".contains(&b) {
out.push(b as char);
} else {
out.push_str(&format!("%{b:02X}"));
}
}
out
}
/// `name=value`, `name=@file;type=x/y;filename=z`, `name=<file`.
fn parse_form(v: &str, literal: bool) -> Option<Part> {
let (name, rest) = v.split_once('=')?;
let mut segs = rest.split(';');
let first = segs.next().unwrap_or("");
let mut part = Part {
name: name.to_string(),
filename: None,
headers: Vec::new(),
body: PartBody::Text(first.to_string()),
};
if literal {
part.body = PartBody::Text(rest.to_string());
return Some(part);
}
if let Some(f) = first.strip_prefix('@') {
part.filename = std::path::Path::new(f)
.file_name()
.map(|n| n.to_string_lossy().into_owned());
part.body = PartBody::File(PathBuf::from(f));
} else if let Some(f) = first.strip_prefix('<') {
part.body = PartBody::File(PathBuf::from(f));
}
for s in segs {
match s.split_once('=') {
Some(("type", t)) => part.headers.push(Header::new("Content-Type", t)),
Some(("filename", f)) => part.filename = Some(f.trim_matches('"').to_string()),
_ => {}
}
}
Some(part)
}
/// Splits the command into words, handling `\`/`^`/`` ` `` line continuations,
/// Windows cmd `^` escapes and bash `$'…'` strings.
fn tokenize(cmd: &str) -> Result<Vec<String>, CurlImportError> {
let mut s = cmd.trim().replace("\r\n", "\n");
let cmd_style = s.contains("^\"") || s.contains("^\n");
if cmd_style {
// cmd.exe: `^` escapes the next character, `^` + newline continues.
let mut out = String::with_capacity(s.len());
let mut chars = s.chars();
while let Some(c) = chars.next() {
if c == '^' {
match chars.next() {
Some('\n') => out.push(' '),
Some(n) => out.push(n),
None => {}
}
} else {
out.push(c);
}
}
s = out;
} else if s.starts_with("curl.exe") {
s = powershell_to_posix(&s.replace("`\n", " "));
} else {
s = s.replace("\\\n", " ");
}
let s = expand_ansi_c_quotes(&s);
shell_words::split(&s).map_err(|e| CurlImportError::Tokenize(e.to_string()))
}
/// PowerShell quoting: `""` and `` `" `` inside double quotes are a literal
/// quote; backslashes are literal.
fn powershell_to_posix(s: &str) -> String {
let mut out = String::with_capacity(s.len());
let mut chars = s.chars().peekable();
let mut in_double = false;
let mut in_single = false;
while let Some(c) = chars.next() {
match c {
'"' if !in_single => {
if in_double && chars.peek() == Some(&'"') {
chars.next();
out.push_str("\\\"");
} else {
in_double = !in_double;
out.push('"');
}
}
'\'' if !in_double => {
in_single = !in_single;
out.push(c);
}
'`' if in_double => match chars.next() {
Some('"') => out.push_str("\\\""),
Some('n') => out.push('\n'),
Some('t') => out.push('\t'),
Some(o) => out.push(o),
None => {}
},
'\\' if in_double => out.push_str("\\\\"),
_ => out.push(c),
}
}
out
}
/// Rewrites bash `$'…'` strings as plain single-quoted strings.
fn expand_ansi_c_quotes(s: &str) -> String {
let mut out = String::with_capacity(s.len());
let mut chars = s.chars().peekable();
let mut in_single = false;
let mut in_double = false;
while let Some(c) = chars.next() {
match c {
'\'' if !in_double => {
in_single = !in_single;
out.push(c);
}
'"' if !in_single => {
in_double = !in_double;
out.push(c);
}
'\\' if !in_single => {
out.push(c);
if let Some(n) = chars.next() {
out.push(n);
}
}
'$' if !in_single && !in_double && chars.peek() == Some(&'\'') => {
chars.next();
let mut val = String::new();
while let Some(c) = chars.next() {
match c {
'\'' => break,
'\\' => match chars.next() {
Some('n') => val.push('\n'),
Some('t') => val.push('\t'),
Some('r') => val.push('\r'),
Some('0') => val.push('\0'),
Some('x') => {
let h: String = std::iter::from_fn(|| chars.next_if(|c| c.is_ascii_hexdigit()))
.take(2)
.collect();
if let Ok(b) = u8::from_str_radix(&h, 16) {
val.push(b as char);
}
}
Some('u') => {
let h: String = std::iter::from_fn(|| chars.next_if(|c| c.is_ascii_hexdigit()))
.take(4)
.collect();
if let Some(ch) = u32::from_str_radix(&h, 16).ok().and_then(char::from_u32) {
val.push(ch);
}
}
Some(o) => val.push(o),
None => {}
},
o => val.push(o),
}
}
out.push('\'');
out.push_str(&val.replace('\'', "'\\''"));
out.push('\'');
}
_ => out.push(c),
}
}
out
}
+4
View File
@@ -0,0 +1,4 @@
//! Importing from other tools into `.http` requests.
pub mod curl;
pub mod postman;
+387
View File
@@ -0,0 +1,387 @@
//! Postman Collection v2.1 import.
//!
//! Folders become directories, and the requests directly inside a folder
//! become `###` blocks in one `.http` file named after the folder. Collection
//! variables go to `$shared` in `http-client.env.json`; Postman environment
//! exports become environments (secret values go to the private env file).
//! Auth is turned into headers, inherited auth is resolved per request, and
//! scripts are skipped with a warning.
use std::path::PathBuf;
use serde_json::Value;
use crate::env::{EnvFile, SHARED_ENV};
use crate::httpfile::render_request;
use crate::model::{Body, Header, Method, Part, PartBody, Request};
#[derive(Debug, thiserror::Error)]
pub enum PostmanError {
#[error("invalid JSON: {0}")]
Json(#[from] serde_json::Error),
#[error("not a Postman v2 collection (missing `info` or `item`)")]
NotCollection,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct PostmanImport {
/// Relative paths and contents of the `.http` files.
pub files: Vec<(PathBuf, String)>,
/// `http-client.env.json`, if there are any variables.
pub public_env: Option<String>,
/// `http-client.private.env.json`, if any variable is secret.
pub private_env: Option<String>,
pub warnings: Vec<String>,
}
const BOUNDARY: &str = "WebAppBoundary";
/// Imports a collection plus any number of Postman environment exports.
pub fn import(collection_json: &str, environment_jsons: &[&str]) -> Result<PostmanImport, PostmanError> {
let root: Value = serde_json::from_str(collection_json)?;
let (Some(info), Some(items)) = (root.get("info"), root.get("item").and_then(Value::as_array)) else {
return Err(PostmanError::NotCollection);
};
let name = info.get("name").and_then(Value::as_str).unwrap_or("collection");
let mut out = PostmanImport::default();
let mut ctx = Ctx {
out: &mut out,
used_paths: Vec::new(),
};
let auth = root.get("auth").cloned();
if has_script(&root) {
ctx.out
.warnings
.push(format!("Skipped collection-level scripts in \"{name}\""));
}
ctx.folder(items, PathBuf::new(), &slug(name), auth.as_ref(), name);
// Environments.
let mut public = EnvFile::default();
let mut private = EnvFile::default();
if let Some(vars) = root.get("variable").and_then(Value::as_array) {
for v in vars {
if let (Some(k), Some(val)) = (v.get("key").and_then(Value::as_str), v.get("value")) {
public.env_mut(SHARED_ENV).set(k, value_string(val));
}
}
}
for env_json in environment_jsons {
let env: Value = serde_json::from_str(env_json)?;
let env_name = env
.get("name")
.and_then(Value::as_str)
.unwrap_or("imported")
.to_string();
public.env_mut(&env_name);
for v in env.get("values").and_then(Value::as_array).into_iter().flatten() {
if v.get("enabled").and_then(Value::as_bool) == Some(false) {
continue;
}
let Some(k) = v.get("key").and_then(Value::as_str) else {
continue;
};
let val = v.get("value").map(value_string).unwrap_or_default();
if v.get("type").and_then(Value::as_str) == Some("secret") {
private.env_mut(&env_name).set(k, val);
} else {
public.env_mut(&env_name).set(k, val);
}
}
}
if !public.envs.is_empty() {
out.public_env = Some(public.to_json());
}
if !private.envs.is_empty() {
out.private_env = Some(private.to_json());
}
Ok(out)
}
struct Ctx<'a> {
out: &'a mut PostmanImport,
used_paths: Vec<PathBuf>,
}
impl Ctx<'_> {
fn folder(&mut self, items: &[Value], dir: PathBuf, file_stem: &str, auth: Option<&Value>, label: &str) {
let mut text = String::new();
for item in items {
let item_name = item.get("name").and_then(Value::as_str).unwrap_or("Untitled");
if let Some(children) = item.get("item").and_then(Value::as_array) {
if has_script(item) {
self.out
.warnings
.push(format!("Skipped scripts on folder \"{label}/{item_name}\""));
}
let sub_auth = item.get("auth").or(auth);
let sub = slug(item_name);
self.folder(
children,
dir.join(&sub),
&sub,
sub_auth,
&format!("{label}/{item_name}"),
);
} else if let Some(r) = item.get("request") {
if has_script(item) {
self.out
.warnings
.push(format!("Skipped scripts on request \"{label}/{item_name}\""));
}
let req = self.request(r, item_name, auth, &format!("{label}/{item_name}"));
if !text.is_empty() {
text.push('\n');
}
text.push_str(&render_request(&req, "\n"));
}
}
if !text.is_empty() {
let mut path = dir.join(format!("{file_stem}.http"));
let mut n = 2;
while self.used_paths.contains(&path) {
path = dir.join(format!("{file_stem}-{n}.http"));
n += 1;
}
self.used_paths.push(path.clone());
self.out.files.push((path, text));
}
}
fn request(&mut self, r: &Value, name: &str, inherited_auth: Option<&Value>, label: &str) -> Request {
let mut req = Request {
title: Some(name.to_string()),
..Request::default()
};
// A request can be just a URL string.
if let Some(url) = r.as_str() {
req.url = url.to_string();
return req;
}
req.method = r
.get("method")
.and_then(Value::as_str)
.and_then(|m| Method::parse(&m.to_ascii_uppercase()))
.unwrap_or(Method::Get);
req.url = match r.get("url") {
Some(Value::String(s)) => s.clone(),
Some(u) => u.get("raw").and_then(Value::as_str).unwrap_or("").to_string(),
None => String::new(),
};
for h in r.get("header").and_then(Value::as_array).into_iter().flatten() {
let (Some(k), Some(v)) = (h.get("key").and_then(Value::as_str), h.get("value")) else {
continue;
};
req.headers.push(Header {
name: k.to_string(),
value: value_string(v),
disabled: h.get("disabled").and_then(Value::as_bool).unwrap_or(false),
});
}
let auth = match r.get("auth") {
Some(a) if a.get("type").and_then(Value::as_str) == Some("inherit") => inherited_auth,
Some(a) => Some(a),
None => inherited_auth,
};
if let Some(a) = auth {
self.apply_auth(&mut req, a, label);
}
if let Some(body) = r.get("body") {
if body.get("disabled").and_then(Value::as_bool) != Some(true) {
self.body(&mut req, body, label);
}
}
req
}
fn apply_auth(&mut self, req: &mut Request, auth: &Value, label: &str) {
let ty = auth.get("type").and_then(Value::as_str).unwrap_or("noauth");
let param = |key: &str| -> Option<String> {
auth.get(ty)?
.as_array()?
.iter()
.find(|p| p.get("key").and_then(Value::as_str) == Some(key))
.and_then(|p| p.get("value"))
.map(value_string)
};
match ty {
"noauth" => {}
"bearer" => {
let token = param("token").unwrap_or_default();
req.set_header("Authorization", format!("Bearer {token}"));
}
"basic" => {
let u = param("username").unwrap_or_default();
let p = param("password").unwrap_or_default();
req.set_header("Authorization", format!("Basic {u} {p}"));
}
"apikey" => {
let key = param("key").unwrap_or_else(|| "X-API-Key".into());
let value = param("value").unwrap_or_default();
if param("in").as_deref() == Some("query") {
let sep = if req.url.contains('?') { '&' } else { '?' };
req.url = format!("{}{sep}{key}={value}", req.url);
} else {
req.set_header(&key, value);
}
}
other => self.out.warnings.push(format!(
"Unsupported auth type \"{other}\" on \"{label}\"; add the header by hand"
)),
}
}
fn body(&mut self, req: &mut Request, body: &Value, label: &str) {
let mode = body.get("mode").and_then(Value::as_str).unwrap_or("");
let set_ct = |req: &mut Request, ct: &str| {
if req.header("content-type").is_none() {
req.headers.push(Header::new("Content-Type", ct));
}
};
match mode {
"raw" => {
let raw = body.get("raw").and_then(Value::as_str).unwrap_or("");
if raw.is_empty() {
return;
}
let lang = body
.pointer("/options/raw/language")
.and_then(Value::as_str)
.unwrap_or("text");
let ct = match lang {
"json" => "application/json",
"xml" => "application/xml",
"html" => "text/html",
"javascript" => "application/javascript",
_ => "text/plain",
};
set_ct(req, ct);
req.body = Body::Text(raw.replace("\r\n", "\n"));
}
"urlencoded" => {
let pairs: Vec<String> = body
.get("urlencoded")
.and_then(Value::as_array)
.into_iter()
.flatten()
.filter(|p| p.get("disabled").and_then(Value::as_bool) != Some(true))
.filter_map(|p| {
let k = p.get("key")?.as_str()?;
Some(format!("{k}={}", p.get("value").map(value_string).unwrap_or_default()))
})
.collect();
set_ct(req, "application/x-www-form-urlencoded");
req.body = Body::Text(pairs.join("&"));
}
"formdata" => {
let mut parts = Vec::new();
for p in body.get("formdata").and_then(Value::as_array).into_iter().flatten() {
if p.get("disabled").and_then(Value::as_bool) == Some(true) {
continue;
}
let Some(k) = p.get("key").and_then(Value::as_str) else {
continue;
};
let mut part = Part {
name: k.to_string(),
filename: None,
headers: Vec::new(),
body: PartBody::Text(p.get("value").map(value_string).unwrap_or_default()),
};
if let Some(ct) = p.get("contentType").and_then(Value::as_str) {
part.headers.push(Header::new("Content-Type", ct));
}
if p.get("type").and_then(Value::as_str) == Some("file") {
let src = match p.get("src") {
Some(Value::String(s)) => s.clone(),
Some(Value::Array(a)) => a.first().and_then(Value::as_str).unwrap_or("").to_string(),
_ => String::new(),
};
if src.is_empty() {
self.out
.warnings
.push(format!("Form file \"{k}\" on \"{label}\" has no file; set it by hand"));
}
part.filename = std::path::Path::new(&src)
.file_name()
.map(|n| n.to_string_lossy().into_owned());
part.body = PartBody::File(PathBuf::from(src));
}
parts.push(part);
}
req.remove_header("content-type");
req.headers.push(Header::new(
"Content-Type",
format!("multipart/form-data; boundary={BOUNDARY}"),
));
req.body = Body::Multipart {
boundary: BOUNDARY.into(),
parts,
};
}
"file" => {
if let Some(src) = body.pointer("/file/src").and_then(Value::as_str) {
req.body = Body::FileRef {
path: PathBuf::from(src),
substitute: false,
};
}
}
"graphql" => {
let q = body.pointer("/graphql/query").and_then(Value::as_str).unwrap_or("");
let vars = body.pointer("/graphql/variables").and_then(Value::as_str).unwrap_or("");
let vars: Value = serde_json::from_str(vars).unwrap_or(Value::Object(Default::default()));
let json = serde_json::json!({ "query": q, "variables": vars });
set_ct(req, "application/json");
req.body = Body::Text(serde_json::to_string_pretty(&json).unwrap());
}
"" => {}
other => self
.out
.warnings
.push(format!("Unsupported body mode \"{other}\" on \"{label}\"")),
}
}
}
fn has_script(item: &Value) -> bool {
item.get("event").and_then(Value::as_array).is_some_and(|events| {
events.iter().any(|e| {
let exec = e.pointer("/script/exec");
match exec {
Some(Value::Array(lines)) => lines.iter().any(|l| l.as_str().is_some_and(|s| !s.trim().is_empty())),
Some(Value::String(s)) => !s.trim().is_empty(),
_ => false,
}
})
})
}
fn value_string(v: &Value) -> String {
match v {
Value::String(s) => s.clone(),
Value::Null => String::new(),
other => other.to_string(),
}
}
/// A file-system friendly name: lowercase, `-` separated.
pub fn slug(name: &str) -> String {
let mut out = String::new();
for c in name.chars() {
if c.is_alphanumeric() {
out.extend(c.to_lowercase());
} else if !out.ends_with('-') && !out.is_empty() {
out.push('-');
}
}
let out = out.trim_end_matches('-').to_string();
if out.is_empty() {
"untitled".into()
} else {
out
}
}
+21
View File
@@ -0,0 +1,21 @@
//! Core of reqlite: the request model, the lossless `.http` parser and writer,
//! environments, templating, collections and import/export.
//!
//! This crate has no UI, no network code and no async runtime. It only defines
//! the async [`http::HttpClient`] trait that the transport crates implement.
pub mod cancel;
pub mod collection;
pub mod cookies;
pub mod env;
pub mod export;
pub mod http;
pub mod httpfile;
pub mod import;
pub mod model;
pub mod params;
pub mod resolve;
pub mod template;
pub use cancel::CancelToken;
pub use model::{Body, Directives, Header, Method, Part, PartBody, Request, SourceSpan};
+223
View File
@@ -0,0 +1,223 @@
//! The request model shared by the parser, the UI and the CLI.
use std::path::PathBuf;
use std::time::Duration;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Method {
Get,
Post,
Put,
Patch,
Delete,
Head,
Options,
Trace,
Connect,
Custom(String),
}
impl Method {
pub const COMMON: [Method; 7] = [
Method::Get,
Method::Post,
Method::Put,
Method::Patch,
Method::Delete,
Method::Head,
Method::Options,
];
pub fn as_str(&self) -> &str {
match self {
Method::Get => "GET",
Method::Post => "POST",
Method::Put => "PUT",
Method::Patch => "PATCH",
Method::Delete => "DELETE",
Method::Head => "HEAD",
Method::Options => "OPTIONS",
Method::Trace => "TRACE",
Method::Connect => "CONNECT",
Method::Custom(s) => s,
}
}
/// Parses a method token. Unknown all-uppercase tokens become `Custom`.
pub fn parse(s: &str) -> Option<Method> {
Some(match s {
"GET" => Method::Get,
"POST" => Method::Post,
"PUT" => Method::Put,
"PATCH" => Method::Patch,
"DELETE" => Method::Delete,
"HEAD" => Method::Head,
"OPTIONS" => Method::Options,
"TRACE" => Method::Trace,
"CONNECT" => Method::Connect,
_ if !s.is_empty() && s.bytes().all(|b| b.is_ascii_uppercase() || b == b'-') => {
Method::Custom(s.to_string())
}
_ => return None,
})
}
}
impl std::fmt::Display for Method {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Header {
pub name: String,
pub value: String,
/// A commented-out header (`# Name: value`).
pub disabled: bool,
}
impl Header {
pub fn new(name: impl Into<String>, value: impl Into<String>) -> Self {
Self {
name: name.into(),
value: value.into(),
disabled: false,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub enum Body {
#[default]
None,
Text(String),
/// `< ./file` (sent as-is) or `<@ ./file` (variables substituted).
FileRef {
path: PathBuf,
substitute: bool,
},
Multipart {
boundary: String,
parts: Vec<Part>,
},
}
impl Body {
pub fn is_none(&self) -> bool {
matches!(self, Body::None)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Part {
pub name: String,
pub filename: Option<String>,
/// Headers of the part other than `Content-Disposition`.
pub headers: Vec<Header>,
pub body: PartBody,
}
impl Part {
pub fn content_type(&self) -> Option<&str> {
self.headers
.iter()
.find(|h| h.name.eq_ignore_ascii_case("content-type"))
.map(|h| h.value.as_str())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PartBody {
Text(String),
File(PathBuf),
}
/// Per-request directives written as `# @directive value` comments.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Directives {
pub no_redirect: bool,
pub no_cookie_jar: bool,
pub no_log: bool,
pub timeout: Option<Duration>,
pub connection_timeout: Option<Duration>,
/// Directives we don't interpret, kept verbatim as `(name, value)`.
pub other: Vec<(String, String)>,
}
/// Byte range of a request block in its source file.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct SourceSpan {
pub start: usize,
pub end: usize,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Request {
/// From `# @name`.
pub name: Option<String>,
/// From the `### Title` separator.
pub title: Option<String>,
pub method: Method,
/// May contain `{{vars}}`; the query string lives in the URL.
pub url: String,
/// `HTTP/1.1`, `HTTP/2` … when given on the request line.
pub http_version: Option<String>,
pub headers: Vec<Header>,
pub body: Body,
pub directives: Directives,
pub source: SourceSpan,
}
impl Default for Request {
fn default() -> Self {
Self {
name: None,
title: None,
method: Method::Get,
url: String::new(),
http_version: None,
headers: Vec::new(),
body: Body::None,
directives: Directives::default(),
source: SourceSpan::default(),
}
}
}
impl Request {
/// The name shown in the UI and used to identify the request in its file:
/// `@name` if present, else the title.
pub fn display_name(&self) -> Option<&str> {
self.name.as_deref().or(self.title.as_deref())
}
pub fn header(&self, name: &str) -> Option<&str> {
self.headers
.iter()
.find(|h| !h.disabled && h.name.eq_ignore_ascii_case(name))
.map(|h| h.value.as_str())
}
/// Sets (or adds) an enabled header, replacing the first one with this name.
pub fn set_header(&mut self, name: &str, value: impl Into<String>) {
let value = value.into();
if let Some(h) = self.headers.iter_mut().find(|h| h.name.eq_ignore_ascii_case(name)) {
h.value = value;
h.disabled = false;
} else {
self.headers.push(Header::new(name, value));
}
}
pub fn remove_header(&mut self, name: &str) {
self.headers.retain(|h| !h.name.eq_ignore_ascii_case(name));
}
/// Compares two requests ignoring their source position.
pub fn same_content(&self, other: &Request) -> bool {
let mut a = self.clone();
a.source = other.source;
a == *other
}
}
+133
View File
@@ -0,0 +1,133 @@
//! Keeping a URL and its query-parameter table in sync.
//!
//! Parameters are kept as raw text (no percent-decoding) so that editing the
//! table and going back to the URL never changes anything the user didn't
//! touch, and `{{vars}}` survive untouched.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Param {
pub key: String,
/// `None` for a bare `?flag` without `=`.
pub value: Option<String>,
}
impl Param {
pub fn new(key: impl Into<String>, value: impl Into<String>) -> Self {
Self {
key: key.into(),
value: Some(value.into()),
}
}
}
/// Splits a URL into `(base, query, fragment)` without the `?` and `#`.
pub fn split_url(url: &str) -> (&str, Option<&str>, Option<&str>) {
let (rest, fragment) = match find_outside_vars(url, '#') {
Some(i) => (&url[..i], Some(&url[i + 1..])),
None => (url, None),
};
match find_outside_vars(rest, '?') {
Some(i) => (&rest[..i], Some(&rest[i + 1..]), fragment),
None => (rest, None, fragment),
}
}
/// Finds `c` outside of `{{…}}` placeholders.
fn find_outside_vars(s: &str, c: char) -> Option<usize> {
let mut depth = 0usize;
let b = s.as_bytes();
let mut i = 0;
while i < b.len() {
if b[i] == b'{' && b.get(i + 1) == Some(&b'{') {
depth += 1;
i += 2;
continue;
}
if b[i] == b'}' && b.get(i + 1) == Some(&b'}') && depth > 0 {
depth -= 1;
i += 2;
continue;
}
if depth == 0 && b[i] == c as u8 {
return Some(i);
}
i += 1;
}
None
}
pub fn parse_params(url: &str) -> Vec<Param> {
let (_, query, _) = split_url(url);
let Some(q) = query else { return Vec::new() };
if q.is_empty() {
return Vec::new();
}
q.split('&')
.map(|kv| match kv.split_once('=') {
Some((k, v)) => Param::new(k, v),
None => Param {
key: kv.to_string(),
value: None,
},
})
.collect()
}
/// Rebuilds `url` with the given parameters. Rows with an empty key and value
/// are dropped (they are the "new row" placeholders in the table).
pub fn with_params(url: &str, params: &[Param]) -> String {
let (base, _, fragment) = split_url(url);
let query: Vec<String> = params
.iter()
.filter(|p| !(p.key.is_empty() && p.value.as_deref().unwrap_or("").is_empty()))
.map(|p| match &p.value {
Some(v) => format!("{}={v}", p.key),
None => p.key.clone(),
})
.collect();
let mut out = base.to_string();
if !query.is_empty() {
out.push('?');
out.push_str(&query.join("&"));
}
if let Some(f) = fragment {
out.push('#');
out.push_str(f);
}
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn round_trips() {
for url in [
"http://x/a?b=1&c=2",
"{{base}}/users?limit={{n}}&flag",
"http://x/a?b=1#frag",
"http://x/a",
"http://x/a?q=a%20b&e=",
] {
assert_eq!(with_params(url, &parse_params(url)), url, "{url}");
}
}
#[test]
fn edits_apply_to_the_url() {
let url = "{{base}}/users?limit=20#top";
let mut p = parse_params(url);
assert_eq!(p, vec![Param::new("limit", "20")]);
p[0].value = Some("50".into());
p.push(Param::new("sort", "name"));
p.push(Param::default());
assert_eq!(with_params(url, &p), "{{base}}/users?limit=50&sort=name#top");
assert_eq!(with_params(url, &[]), "{{base}}/users#top");
}
#[test]
fn question_marks_inside_vars_are_ignored() {
assert_eq!(split_url("{{a?b}}/x?y=1"), ("{{a?b}}/x", Some("y=1"), None));
}
}
+240
View File
@@ -0,0 +1,240 @@
//! Turning a [`Request`] into a [`ResolvedRequest`]: variable substitution,
//! file references, Basic auth encoding and directives.
use std::path::Path;
use base64::Engine;
use crate::http::{RequestOptions, ResolvedBody, ResolvedPart, ResolvedPartBody, ResolvedRequest};
use crate::model::{Body, PartBody, Request};
use crate::template::{Resolver, TemplateError};
#[derive(Debug, thiserror::Error)]
pub enum ResolveError {
#[error("{}", .0.iter().map(|e| e.to_string()).collect::<Vec<_>>().join(", "))]
Template(Vec<TemplateError>),
#[error("cannot read {path}: {source}")]
File {
path: std::path::PathBuf,
source: std::io::Error,
},
#[error("the URL is empty")]
EmptyUrl,
}
/// Resolves `req`. Relative file references are taken relative to `base_dir`
/// (the directory of the `.http` file). `defaults` provides the options that
/// directives don't override (proxy, TLS, default timeout …).
pub fn resolve_request(
req: &Request,
resolver: &Resolver,
base_dir: &Path,
defaults: &RequestOptions,
) -> Result<ResolvedRequest, ResolveError> {
let mut errors = Vec::new();
let mut sub = |s: &str| match resolver.resolve(s) {
Ok(v) => v,
Err(e) => {
errors.extend(e);
s.to_string()
}
};
let mut url = sub(req.url.trim());
if url.is_empty() {
return Err(ResolveError::EmptyUrl);
}
if !url.contains("://") {
url = format!("http://{url}");
}
let headers: Vec<(String, String)> = req
.headers
.iter()
.filter(|h| !h.disabled)
.map(|h| {
let value = sub(&h.value);
let value = if h.name.eq_ignore_ascii_case("authorization") {
encode_basic(&value)
} else {
value
};
(sub(&h.name), value)
})
.collect();
let read = |p: &Path| {
let path = base_dir.join(p);
std::fs::read(&path).map_err(|source| ResolveError::File { path, source })
};
let body = match &req.body {
Body::None => ResolvedBody::None,
Body::Text(t) => ResolvedBody::Bytes(sub(t).into_bytes()),
Body::FileRef {
path,
substitute: false,
} => ResolvedBody::File(base_dir.join(path)),
Body::FileRef { path, substitute: true } => {
let text = String::from_utf8_lossy(&read(path)?).into_owned();
ResolvedBody::Bytes(sub(&text).into_bytes())
}
Body::Multipart { parts, .. } => ResolvedBody::Multipart(
parts
.iter()
.map(|p| ResolvedPart {
name: sub(&p.name),
filename: p.filename.as_deref().map(&mut sub),
content_type: p.content_type().map(&mut sub),
body: match &p.body {
PartBody::Text(t) => ResolvedPartBody::Bytes(sub(t).into_bytes()),
PartBody::File(f) => ResolvedPartBody::File(base_dir.join(f)),
},
})
.collect(),
),
};
if !errors.is_empty() {
errors.dedup();
return Err(ResolveError::Template(errors));
}
let mut options = defaults.clone();
let d = &req.directives;
if d.no_redirect {
options.follow_redirects = false;
}
// reqlite extension, written by curl import for `-k`.
if d.other.iter().any(|(n, _)| n == "insecure") {
options.verify_tls = false;
}
if d.timeout.is_some() {
options.timeout = d.timeout;
}
if d.connection_timeout.is_some() {
options.connect_timeout = d.connection_timeout;
}
options.http_version = req.http_version.clone();
Ok(ResolvedRequest {
method: req.method.as_str().to_string(),
url,
headers,
body,
options,
})
}
/// `Basic user password` (JetBrains style) → `Basic base64(user:password)`.
/// An already-encoded `Basic xyz` value is left alone.
fn encode_basic(value: &str) -> String {
let Some(rest) = value.strip_prefix("Basic ") else {
return value.to_string();
};
let rest = rest.trim();
match rest.split_once(' ') {
Some((user, pass)) if !user.is_empty() => {
let token = base64::engine::general_purpose::STANDARD.encode(format!("{user}:{}", pass.trim()));
format!("Basic {token}")
}
_ => {
// `Basic user:password` in clear text is also accepted.
if rest.contains(':') && base64::engine::general_purpose::STANDARD.decode(rest).is_err() {
format!("Basic {}", base64::engine::general_purpose::STANDARD.encode(rest))
} else {
value.to_string()
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::httpfile::HttpFile;
use crate::template::VarSource;
fn resolver() -> Resolver {
Resolver::new().with_layer(
VarSource::PublicEnv,
[("base", "http://localhost:1234"), ("user", "ada"), ("pass", "s3cret")],
)
}
#[test]
fn resolves_url_headers_body_and_directives() {
let f = HttpFile::parse(
"# @no-redirect\n# @timeout 5\nPOST {{base}}/users HTTP/1.1\nAuthorization: Basic {{user}} {{pass}}\n# X-Off: 1\n\n{\"u\": \"{{user}}\"}\n",
);
let r = resolve_request(
f.request(0).unwrap(),
&resolver(),
Path::new("."),
&RequestOptions::default(),
)
.unwrap();
assert_eq!(r.method, "POST");
assert_eq!(r.url, "http://localhost:1234/users");
assert_eq!(
r.headers,
vec![("Authorization".into(), "Basic YWRhOnMzY3JldA==".into())]
);
assert_eq!(r.body, ResolvedBody::Bytes(b"{\"u\": \"ada\"}".to_vec()));
assert!(!r.options.follow_redirects);
assert_eq!(r.options.timeout, Some(std::time::Duration::from_secs(5)));
assert_eq!(r.options.http_version.as_deref(), Some("HTTP/1.1"));
}
#[test]
fn reports_all_unknown_variables() {
let f = HttpFile::parse("GET {{nope}}/x\nX-A: {{also_nope}}\n");
let err = resolve_request(
f.request(0).unwrap(),
&resolver(),
Path::new("."),
&RequestOptions::default(),
)
.unwrap_err();
assert_eq!(err.to_string(), "unknown variable `nope`, unknown variable `also_nope`");
}
#[test]
fn adds_scheme_and_keeps_encoded_basic() {
let f = HttpFile::parse("GET example.com\nAuthorization: Basic YWRhOnMzY3JldA==\n");
let r = resolve_request(
f.request(0).unwrap(),
&resolver(),
Path::new("."),
&RequestOptions::default(),
)
.unwrap();
assert_eq!(r.url, "http://example.com");
assert_eq!(r.headers[0].1, "Basic YWRhOnMzY3JldA==");
assert_eq!(encode_basic("Basic ada:pw"), "Basic YWRhOnB3");
}
#[test]
fn file_refs_resolve_relative_to_the_http_file() {
let dir = tempfile::tempdir().unwrap();
std::fs::write(dir.path().join("body.json"), "{\"u\":\"{{user}}\"}").unwrap();
let f = HttpFile::parse("POST /x\n\n<@ ./body.json\n");
let r = resolve_request(
f.request(0).unwrap(),
&resolver(),
dir.path(),
&RequestOptions::default(),
)
.unwrap();
assert_eq!(r.body, ResolvedBody::Bytes(b"{\"u\":\"ada\"}".to_vec()));
let f = HttpFile::parse("POST /x\n\n< ./body.json\n");
let r = resolve_request(
f.request(0).unwrap(),
&resolver(),
dir.path(),
&RequestOptions::default(),
)
.unwrap();
assert_eq!(r.body, ResolvedBody::File(dir.path().join("./body.json")));
}
}
+393
View File
@@ -0,0 +1,393 @@
//! `{{name}}` placeholders: parsing, lookup and resolution.
//!
//! Lookup order (first match wins): private environment, public environment,
//! file variables, then `$`-prefixed built-ins (`{{$uuid}}`, `{{$timestamp}}`,
//! `{{$isoTimestamp}}`, `{{$randomInt}}`, `{{$randomInt 1 10}}`).
//!
//! `\{{` is an escaped, literal `{{`.
use std::collections::HashMap;
use std::ops::Range;
use std::time::{SystemTime, UNIX_EPOCH};
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum TemplateError {
#[error("unknown variable `{name}`")]
Unknown { name: String, span: Range<usize> },
#[error("unclosed `{{{{` at byte {pos}")]
Unclosed { pos: usize },
#[error("empty variable name at byte {pos}")]
Empty { pos: usize },
#[error("variable `{name}` refers to itself (directly or indirectly)")]
Recursive { name: String },
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Segment<'a> {
Lit(&'a str),
/// `span` covers the braces; `name` is trimmed.
Var {
name: &'a str,
span: Range<usize>,
},
}
/// Splits a template into literal and variable segments.
pub fn parse(s: &str) -> Result<Vec<Segment<'_>>, TemplateError> {
let mut out = Vec::new();
let bytes = s.as_bytes();
let mut lit_start = 0;
let mut i = 0;
while i + 1 < bytes.len() {
if bytes[i] == b'\\' && bytes[i + 1] == b'{' && bytes.get(i + 2) == Some(&b'{') {
if lit_start < i {
out.push(Segment::Lit(&s[lit_start..i]));
}
out.push(Segment::Lit("{{"));
i += 3;
lit_start = i;
continue;
}
if bytes[i] == b'{' && bytes[i + 1] == b'{' {
let Some(close) = s[i + 2..].find("}}") else {
return Err(TemplateError::Unclosed { pos: i });
};
let name = s[i + 2..i + 2 + close].trim();
if name.is_empty() {
return Err(TemplateError::Empty { pos: i });
}
if lit_start < i {
out.push(Segment::Lit(&s[lit_start..i]));
}
let end = i + 2 + close + 2;
out.push(Segment::Var { name, span: i..end });
i = end;
lit_start = i;
continue;
}
i += 1;
}
if lit_start < s.len() {
out.push(Segment::Lit(&s[lit_start..]));
}
Ok(out)
}
/// Lenient scan for highlighting: every well-formed `{{…}}` with its span.
pub fn find_vars(s: &str) -> Vec<(Range<usize>, String)> {
let mut out = Vec::new();
let mut i = 0;
while let Some(open) = s[i..].find("{{") {
let start = i + open;
if start > 0 && s.as_bytes()[start - 1] == b'\\' {
i = start + 2;
continue;
}
let Some(close) = s[start + 2..].find("}}") else {
break;
};
let end = start + 2 + close + 2;
out.push((start..end, s[start + 2..start + 2 + close].trim().to_string()));
i = end;
}
out
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum VarSource {
PrivateEnv,
PublicEnv,
File,
Builtin,
}
/// Source of time and randomness for built-in variables (fixed in tests).
pub trait Dynamic: Send + Sync {
fn uuid(&self) -> String;
fn now(&self) -> SystemTime;
fn random_u32(&self) -> u32;
}
pub struct SystemDynamic;
impl Dynamic for SystemDynamic {
fn uuid(&self) -> String {
uuid::Uuid::new_v4().to_string()
}
fn now(&self) -> SystemTime {
SystemTime::now()
}
fn random_u32(&self) -> u32 {
let b = uuid::Uuid::new_v4().into_bytes();
u32::from_le_bytes([b[0], b[1], b[2], b[3]])
}
}
pub struct Resolver {
layers: Vec<(VarSource, HashMap<String, String>)>,
dynamic: Box<dyn Dynamic>,
}
impl Default for Resolver {
fn default() -> Self {
Self {
layers: Vec::new(),
dynamic: Box::new(SystemDynamic),
}
}
}
impl Resolver {
pub fn new() -> Self {
Self::default()
}
pub fn with_dynamic(mut self, d: impl Dynamic + 'static) -> Self {
self.dynamic = Box::new(d);
self
}
/// Adds a layer. Layers are consulted in [`VarSource`] order regardless of
/// the order they were added in.
pub fn with_layer<K: Into<String>, V: Into<String>>(
mut self,
source: VarSource,
vars: impl IntoIterator<Item = (K, V)>,
) -> Self {
let map = vars.into_iter().map(|(k, v)| (k.into(), v.into())).collect();
self.layers.push((source, map));
self.layers.sort_by_key(|(s, _)| *s as u8);
self
}
/// The raw (unresolved) value of a variable and where it comes from.
pub fn lookup(&self, name: &str) -> Option<(String, VarSource)> {
for (src, map) in &self.layers {
if let Some(v) = map.get(name) {
return Some((v.clone(), *src));
}
}
self.builtin(name).map(|v| (v, VarSource::Builtin))
}
pub fn is_defined(&self, name: &str) -> bool {
self.layers.iter().any(|(_, m)| m.contains_key(name)) || is_builtin(name)
}
/// Resolves all placeholders. Variable values may contain placeholders
/// themselves. All unknown variables are reported, not just the first.
pub fn resolve(&self, s: &str) -> Result<String, Vec<TemplateError>> {
let mut errors = Vec::new();
let mut stack = Vec::new();
let out = self.resolve_inner(s, &mut stack, &mut errors);
if errors.is_empty() {
Ok(out)
} else {
Err(errors)
}
}
fn resolve_inner(&self, s: &str, stack: &mut Vec<String>, errors: &mut Vec<TemplateError>) -> String {
let segments = match parse(s) {
Ok(s) => s,
Err(e) => {
errors.push(e);
return s.to_string();
}
};
let mut out = String::with_capacity(s.len());
for seg in segments {
match seg {
Segment::Lit(l) => out.push_str(l),
Segment::Var { name, span } => {
if stack.iter().any(|n| n == name) {
errors.push(TemplateError::Recursive { name: name.to_string() });
continue;
}
match self.lookup(name) {
Some((v, VarSource::Builtin)) => out.push_str(&v),
Some((v, _)) => {
stack.push(name.to_string());
out.push_str(&self.resolve_inner(&v, stack, errors));
stack.pop();
}
None => errors.push(TemplateError::Unknown {
name: name.to_string(),
span,
}),
}
}
}
}
out
}
fn builtin(&self, name: &str) -> Option<String> {
let mut parts = name.split_whitespace();
let head = parts.next()?;
let args: Vec<&str> = parts.collect();
Some(match head {
"$uuid" | "$random.uuid" | "$guid" => self.dynamic.uuid(),
"$timestamp" => unix_secs(self.dynamic.now()).to_string(),
"$isoTimestamp" => iso8601(self.dynamic.now()),
"$randomInt" | "$random.integer" => {
let (lo, hi) = match args.as_slice() {
[lo, hi] => (lo.parse::<i64>().ok()?, hi.parse::<i64>().ok()?),
[] => (0, 1000),
_ => return None,
};
if hi <= lo {
return None;
}
let span = (hi - lo) as u64;
(lo + (self.dynamic.random_u32() as u64 % span) as i64).to_string()
}
_ => return None,
})
}
}
fn is_builtin(name: &str) -> bool {
matches!(
name.split_whitespace().next(),
Some("$uuid" | "$random.uuid" | "$guid" | "$timestamp" | "$isoTimestamp" | "$randomInt" | "$random.integer")
)
}
fn unix_secs(t: SystemTime) -> u64 {
t.duration_since(UNIX_EPOCH).map(|d| d.as_secs()).unwrap_or(0)
}
/// `YYYY-MM-DDTHH:MM:SS.mmmZ`, without a date crate.
pub fn iso8601(t: SystemTime) -> String {
let d = t.duration_since(UNIX_EPOCH).unwrap_or_default();
let secs = d.as_secs() as i64;
let (days, rem) = (secs.div_euclid(86_400), secs.rem_euclid(86_400));
// Civil-from-days (Howard Hinnant).
let z = days + 719_468;
let era = z.div_euclid(146_097);
let doe = z.rem_euclid(146_097);
let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365;
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
let mp = (5 * doy + 2) / 153;
let day = doy - (153 * mp + 2) / 5 + 1;
let month = if mp < 10 { mp + 3 } else { mp - 9 };
let year = yoe + era * 400 + i64::from(month <= 2);
format!(
"{year:04}-{month:02}-{day:02}T{:02}:{:02}:{:02}.{:03}Z",
rem / 3600,
rem % 3600 / 60,
rem % 60,
d.subsec_millis()
)
}
#[cfg(test)]
mod tests {
use super::*;
use std::time::Duration;
struct Fixed;
impl Dynamic for Fixed {
fn uuid(&self) -> String {
"00000000-0000-4000-8000-000000000000".into()
}
fn now(&self) -> SystemTime {
UNIX_EPOCH + Duration::from_millis(1_700_000_000_123)
}
fn random_u32(&self) -> u32 {
42
}
}
fn resolver() -> Resolver {
Resolver::new()
.with_dynamic(Fixed)
.with_layer(
VarSource::File,
[
("host", "file.example"),
("only_file", "f"),
("base", "https://{{host}}"),
],
)
.with_layer(
VarSource::PublicEnv,
[("host", "public.example"), ("token", "public-token")],
)
.with_layer(VarSource::PrivateEnv, [("token", "secret")])
}
#[test]
fn table() {
let r = resolver();
let cases: &[(&str, Result<&str, &str>)] = &[
// Precedence: private > public > file.
("{{token}}", Ok("secret")),
("{{host}}", Ok("public.example")),
("{{only_file}}", Ok("f")),
// Nested values are resolved with the same precedence.
("{{base}}/users", Ok("https://public.example/users")),
// Whitespace inside braces.
("{{ token }}", Ok("secret")),
// Literals and escaping.
("no vars", Ok("no vars")),
("", Ok("")),
("a \\{{token}} b", Ok("a {{token}} b")),
("{ single }", Ok("{ single }")),
// Built-ins.
("{{$uuid}}", Ok("00000000-0000-4000-8000-000000000000")),
("{{$timestamp}}", Ok("1700000000")),
("{{$isoTimestamp}}", Ok("2023-11-14T22:13:20.123Z")),
("{{$randomInt}}", Ok("42")),
("{{$randomInt 10 20}}", Ok("12")),
// Errors.
("{{missing}}", Err("unknown variable `missing`")),
("{{token", Err("unclosed `{{` at byte 0")),
("x{{ }}", Err("empty variable name at byte 1")),
("{{$nope}}", Err("unknown variable `$nope`")),
];
for (input, expected) in cases {
let got = r.resolve(input).map_err(|e| e[0].to_string());
let expected = expected.map(str::to_string).map_err(str::to_string);
assert_eq!(got, expected, "input: {input:?}");
}
}
#[test]
fn reports_every_unknown_variable() {
let errs = resolver().resolve("{{a}}/{{b}}").unwrap_err();
assert_eq!(errs.len(), 2);
assert_eq!(
errs[1],
TemplateError::Unknown {
name: "b".into(),
span: 6..11
}
);
}
#[test]
fn detects_recursion() {
let r = Resolver::new().with_layer(VarSource::File, [("a", "{{b}}"), ("b", "{{a}}")]);
let errs = r.resolve("{{a}}").unwrap_err();
assert!(matches!(errs[0], TemplateError::Recursive { .. }));
}
#[test]
fn lookup_reports_source() {
let r = resolver();
assert_eq!(r.lookup("token"), Some(("secret".into(), VarSource::PrivateEnv)));
assert_eq!(r.lookup("host").unwrap().1, VarSource::PublicEnv);
assert_eq!(r.lookup("only_file").unwrap().1, VarSource::File);
assert_eq!(r.lookup("nope"), None);
}
#[test]
fn find_vars_for_highlighting() {
assert_eq!(
find_vars("{{a}}/x/{{ b }}/\\{{c}}/{{open"),
vec![(0..5, "a".to_string()), (8..15, "b".to_string())]
);
}
}