curl-parser 0.6.0

Convert curl command to a ParsedRequest (could be further converted to reqwest::RequestBuilder)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
use crate::{ParsedRequest, error::*};
use base64::{Engine, engine::general_purpose::STANDARD};
use http::{
    HeaderValue, Method,
    header::{ACCEPT, AUTHORIZATION, CONTENT_TYPE, HeaderName},
};
use minijinja::Environment;
use pest::Parser as _;
use pest_derive::Parser;
use serde::Serialize;
use snafu::ResultExt;
use std::str::FromStr;
use std::sync::OnceLock;

#[derive(Debug, Parser)]
#[grammar = "src/curl.pest"]
pub struct CurlParser;

fn parse_input(input: &str) -> Result<ParsedRequest> {
    let pairs = CurlParser::parse(Rule::input, input).context(ParseRuleSnafu)?;
    let mut parsed = ParsedRequest::default();
    for pair in pairs {
        match pair.as_rule() {
            Rule::method => {
                let method = pair.as_str().parse().context(ParseMethodSnafu)?;
                parsed.method = method;
            }
            Rule::url => {
                let url = pair.into_inner().as_str();

                // if empty scheme set curl defaults to HTTP
                #[cfg(feature = "uri")]
                let url = if url.contains("://") {
                    url.parse().context(ParseUrlSnafu)?
                } else {
                    // Pre-allocate with known prefix length
                    let mut full_url = String::with_capacity(7 + url.len()); // "http://" + url
                    full_url.push_str("http://");
                    full_url.push_str(url);
                    full_url.parse().context(ParseUrlSnafu)?
                };
                #[cfg(not(feature = "uri"))]
                let url = if url.contains("://") {
                    url.to_string()
                } else {
                    // Pre-allocate with known prefix length
                    let mut full_url = String::with_capacity(8 + url.len()); // "http://" + url + "/"
                    full_url.push_str("http://");
                    full_url.push_str(url);
                    full_url.push('/');
                    full_url
                };

                parsed.url = url;
            }
            Rule::location => {
                let s = pair
                    .into_inner()
                    .next()
                    .expect("location string must be present")
                    .as_str();
                #[cfg(feature = "uri")]
                let location = s.parse().context(ParseUrlSnafu)?;
                #[cfg(not(feature = "uri"))]
                let location = s.to_string();
                parsed.url = location;
            }
            Rule::header => {
                let s = pair
                    .into_inner()
                    .next()
                    .expect("header string must be present")
                    .as_str();

                // Use split_once for better performance
                if let Some((name, value)) = s.split_once(':') {
                    let header_value = unescape_string(value.trim());
                    parsed.headers.insert(
                        HeaderName::from_str(name.trim()).context(ParseHeaderNameSnafu)?,
                        HeaderValue::from_str(&header_value).context(ParseHeaderValueSnafu)?,
                    );
                } else {
                    // Fallback for malformed headers (should be rare)
                    let mut kv = s.splitn(2, ':');
                    let name = kv.next().expect("key must present").trim();
                    let value = kv.next().expect("value must present").trim();
                    let header_value = unescape_string(value);
                    parsed.headers.insert(
                        HeaderName::from_str(name).context(ParseHeaderNameSnafu)?,
                        HeaderValue::from_str(&header_value).context(ParseHeaderValueSnafu)?,
                    );
                }
            }
            Rule::auth => {
                let s = pair
                    .into_inner()
                    .next()
                    .expect("header string must be present")
                    .as_str();
                let encoded = STANDARD.encode(s.as_bytes());
                // Pre-allocate with known prefix length
                let mut basic_auth = String::with_capacity(6 + encoded.len()); // "Basic " + encoded
                basic_auth.push_str("Basic ");
                basic_auth.push_str(&encoded);
                parsed.headers.insert(
                    AUTHORIZATION,
                    basic_auth.parse().context(ParseHeaderValueSnafu)?,
                );
            }
            Rule::body => {
                let s = pair.as_str().trim();
                let s = remove_quote(s);
                parsed.body.push(s.into());
            }
            Rule::ssl_verify_option => {
                parsed.insecure = true;
            }
            Rule::EOI => break,
            _ => unreachable!("Unexpected rule: {:?}", pair.as_rule()),
        }
    }

    if parsed.headers.get(CONTENT_TYPE).is_none() && !parsed.body.is_empty() {
        parsed.headers.insert(
            CONTENT_TYPE,
            HeaderValue::from_static("application/x-www-form-urlencoded"),
        );
    }
    if parsed.headers.get(ACCEPT).is_none() {
        parsed
            .headers
            .insert(ACCEPT, HeaderValue::from_static("*/*"));
    }
    if !parsed.body.is_empty() && parsed.method == Method::GET {
        parsed.method = Method::POST
    }
    Ok(parsed)
}

// Cached minijinja environment for better performance
static TEMPLATE_ENV: OnceLock<Environment<'static>> = OnceLock::new();

fn get_template_env() -> &'static Environment<'static> {
    TEMPLATE_ENV.get_or_init(Environment::new)
}

impl ParsedRequest {
    pub fn load(input: &str, context: impl Serialize) -> Result<Self> {
        let env = get_template_env();
        let input = env.render_str(input, context).context(RenderSnafu)?;
        parse_input(&input)
    }

    pub fn body(&self) -> Option<String> {
        if self.body.is_empty() {
            return None;
        }

        match self.headers.get(CONTENT_TYPE) {
            Some(content_type) if content_type == "application/x-www-form-urlencoded" => {
                Some(self.form_urlencoded())
            }
            Some(content_type) if content_type == "application/json" => self.body.last().cloned(),
            v => unimplemented!("Unsupported content type: {:?}", v),
        }
    }

    fn form_urlencoded(&self) -> String {
        let mut encoded = form_urlencoded::Serializer::new(String::new());
        for item in &self.body {
            // Use split_once for better performance
            if let Some((key, value)) = item.split_once('=') {
                encoded.append_pair(remove_quote(key), remove_quote(value));
            } else {
                // Fallback for malformed form data (should be rare)
                let mut kv = item.splitn(2, '=');
                let key = kv.next().expect("key must present");
                let value = kv.next().expect("value must present");
                encoded.append_pair(remove_quote(key), remove_quote(value));
            }
        }
        encoded.finish()
    }
}

impl FromStr for ParsedRequest {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self> {
        parse_input(s)
    }
}

#[cfg(feature = "reqwest")]
impl TryFrom<&ParsedRequest> for reqwest::RequestBuilder {
    type Error = reqwest::Error;

    fn try_from(req: &ParsedRequest) -> Result<Self, Self::Error> {
        let body = req.body();
        let client = reqwest::Client::builder()
            .danger_accept_invalid_certs(req.insecure)
            .build()?;

        let req_builder = client
            .request(req.method.clone(), req.url.to_string())
            .headers(req.headers.clone());

        let req = if let Some(body) = body {
            req_builder.body(body)
        } else {
            req_builder
        };

        Ok(req)
    }
}

fn remove_quote(s: &str) -> &str {
    let bytes = s.as_bytes();

    // Check if string is long enough and has matching quotes
    if bytes.len() >= 2 {
        match (bytes[0], bytes[bytes.len() - 1]) {
            (b'\'', b'\'') => &s[1..s.len() - 1],
            (b'"', b'"') => &s[1..s.len() - 1],
            _ => s,
        }
    } else {
        s
    }
}

fn unescape_string(s: &str) -> String {
    let mut result = String::with_capacity(s.len());
    let mut chars = s.chars();

    while let Some(ch) = chars.next() {
        if ch == '\\' {
            if let Some(next_ch) = chars.next() {
                match next_ch {
                    '"' | '\\' | '/' => result.push(next_ch),
                    'n' => result.push('\n'),
                    'r' => result.push('\r'),
                    't' => result.push('\t'),
                    _ => {
                        // If it's not a recognized escape sequence, keep both characters
                        result.push(ch);
                        result.push(next_ch);
                    }
                }
            } else {
                result.push(ch);
            }
        } else {
            result.push(ch);
        }
    }

    result
}

#[cfg(test)]
mod tests {
    use super::*;
    use anyhow::Result;
    use http::{Method, header::ACCEPT};
    use serde_json::json;

    #[test]
    fn parse_curl_1_should_work() -> Result<()> {
        let input = r#"curl \
          -X PATCH \
          -d '{"visibility":"private"}' \
          -H "Accept: application/vnd.github+json" \
          -H "Authorization: Bearer {{ token }}"\
          -H "X-GitHub-Api-Version: 2022-11-28" \
          https://api.github.com/user/email/visibility "#;
        let parsed = ParsedRequest::load(input, json!({ "token": "abcd1234" }))?;
        assert_eq!(parsed.method, Method::PATCH);
        assert_eq!(
            parsed.url.to_string(),
            "https://api.github.com/user/email/visibility"
        );
        assert_eq!(
            parsed.headers.get(ACCEPT),
            Some(&HeaderValue::from_static("application/vnd.github+json"))
        );
        assert_eq!(parsed.body, vec!["{\"visibility\":\"private\"}"]);

        Ok(())
    }

    #[test]
    fn parse_curl_2_should_work() -> Result<()> {
        let input = r#"curl \
        -X POST \
        -H "Accept: application/vnd.github+json" \
        -H "Authorization: Bearer {{ token }}"\
        -H "X-GitHub-Api-Version: 2022-11-28" \
        -L "https://api.github.com/user/emails" \
        -d '{"emails":["octocat@github.com","mona@github.com","octocat@octocat.org"]}'"#;
        let parsed = ParsedRequest::load(input, json!({ "token": "abcd1234" }))?;
        assert_eq!(parsed.method, Method::POST);
        assert_eq!(parsed.url.to_string(), "https://api.github.com/user/emails");
        assert_eq!(
            parsed.headers.get(AUTHORIZATION),
            Some(&HeaderValue::from_static("Bearer abcd1234"))
        );
        assert_eq!(
            parsed.body,
            vec![r#"{"emails":["octocat@github.com","mona@github.com","octocat@octocat.org"]}"#]
        );
        Ok(())
    }

    #[tokio::test]
    async fn parse_curl_3_should_work() -> Result<()> {
        let input = r#"curl https://httpbin.org/basic-auth/testuser/testpass \
        -u {{ username }}:{{ password }} \
        -H "Accept: application/json""#;

        let parsed = ParsedRequest::load(
            input,
            json!({ "username": "testuser", "password": "testpass" }),
        )?;
        assert_eq!(parsed.method, Method::GET);
        assert_eq!(
            parsed.url.to_string(),
            "https://httpbin.org/basic-auth/testuser/testpass"
        );
        assert_eq!(
            parsed.headers.get(AUTHORIZATION),
            Some(&HeaderValue::from_str("Basic dGVzdHVzZXI6dGVzdHBhc3M=")?)
        );

        #[cfg(feature = "reqwest")]
        {
            let req = reqwest::RequestBuilder::try_from(&parsed)?;
            let res = req.send().await?;
            assert_eq!(res.status(), 200);
            let _body = res.text().await?;
        }
        Ok(())
    }

    #[tokio::test]
    async fn parse_curl_4_should_work() -> Result<()> {
        let input = r#"curl "https://ifconfig.me/""#;

        let parsed = ParsedRequest::from_str(input)?;
        assert_eq!(parsed.method, Method::GET);
        assert_eq!(parsed.url.to_string(), "https://ifconfig.me/");

        #[cfg(feature = "reqwest")]
        {
            let req = reqwest::RequestBuilder::try_from(&parsed)?;
            let res = req.send().await?;
            assert_eq!(res.status(), 200);
            let _body = res.text().await?;
        }
        Ok(())
    }

    #[tokio::test]
    async fn parse_curl_5_should_work() -> Result<()> {
        let input = r#"curl 'ifconfig.me'"#;

        let parsed = ParsedRequest::from_str(input)?;
        assert_eq!(parsed.method, Method::GET);
        assert_eq!(parsed.url.to_string(), "http://ifconfig.me/");

        #[cfg(feature = "reqwest")]
        {
            let req = reqwest::RequestBuilder::try_from(&parsed)?;
            let res = req.send().await?;
            assert_eq!(res.status(), 200);
            let _body = res.text().await?;
        }
        Ok(())
    }

    #[tokio::test]
    async fn parse_curl_with_insecure_should_work() -> Result<(), Box<dyn std::error::Error>> {
        let input = r#"#this is good
        curl -k 'https://example.com/'"#;

        let parsed: ParsedRequest = input.parse()?;
        assert_eq!(parsed.method, Method::GET);
        assert_eq!(parsed.url.to_string(), "https://example.com/");
        assert!(parsed.insecure);
        Ok(())
    }

    #[tokio::test]
    async fn parse_curl_with_body_should_work() -> Result<()> {
        let input = r#"curl --location https://example.com --header 'Content-Type: application/json' -d '{"-name":"--John"," --age":30}'"#;
        let parsed = ParsedRequest::from_str(input)?;
        assert_eq!(parsed.method, Method::POST);
        assert_eq!(parsed.body, vec!["{\"-name\":\"--John\",\" --age\":30}"]);
        Ok(())
    }

    #[test]
    fn parse_curl_with_escaped_json_in_header() -> Result<()> {
        let input = r#"curl https://api.github.com/repos/owner/repo \
            -H "X-GitHub-Api-Version: 2022-11-28" \
            -H "X-Custom-Metadata: {\"version\":\"1.0.0\",\"client\":\"rust\"}" \
            -H "Accept: application/json""#;
        let parsed = ParsedRequest::from_str(input)?;
        assert_eq!(parsed.method, Method::GET);
        assert_eq!(
            parsed.url.to_string(),
            "https://api.github.com/repos/owner/repo"
        );

        // Verify the header with escaped JSON is parsed correctly
        let header_value = parsed.headers.get("X-Custom-Metadata");
        assert!(header_value.is_some());
        let header_str = header_value.unwrap().to_str().unwrap();
        assert_eq!(header_str, r#"{"version":"1.0.0","client":"rust"}"#);

        Ok(())
    }
}