Files
reqlite/crates/core/src/import/curl.rs
T
2026-10-19 09:31:57 +02:00

438 lines
15 KiB
Rust

//! 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
}