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
pub mod cli;

use ansi_term::Colour::*;
use colored_json::prelude::*;
use colored_json::{Color, Styler};
use reqwest::blocking::{Client, RequestBuilder};
use std::fs;

// options not supported by reqwest
const METHODS: [&str; 6] = ["GET", "HEAD", "POST", "PUT", "DELETE", "PATCH"];

#[derive(Clone)]
struct Line {
    text: String,
    number: usize,
}

impl Line {
    fn new(text: &str, number: usize) -> Line {
        Line {
            text: text.to_string(),
            number,
        }
    }
}

struct Request {
    lines: Vec<Line>,
    method: String,
}

impl Request {
    fn new(lines: Vec<Line>, method: &str) -> Request {
        Request {
            lines,
            method: method.to_string(),
        }
    }

    fn error(&self, line: usize, msg: &str) -> String {
        format!("Error (line {}): {}", line, msg)
    }

    fn get_uri(&self) -> Result<String, String> {
        let mut host = self.lines[0].text.to_string();
        let last_line = &self.lines[self.lines.len() - 1];
        let mut words = last_line.text.split(' ');
        words.next();
        let location = match words.next() {
            Some(location) if !location.is_empty() => location,
            _ => {
                return Err(self.error(last_line.number, "Expected location"));
            }
        };
        host.push_str(&location);
        Ok(host)
    }

    fn parse(&self, client: &Client, color: bool) -> Result<RequestBuilder, String> {
        let uri = match self.get_uri() {
            Ok(uri) => uri,
            Err(e) => return Err(e),
        };

        let mut in_body = false;
        let mut body = "".to_string();

        let mut headers: Vec<&Line> = vec![];

        for line in self.lines[1..self.lines.len() - 1].iter() {
            // Don't like this. Very hacky
            if in_body || line.text.starts_with('{') || line.text.contains('}') {
                body.push_str(&line.text);
                in_body = !line.text.ends_with('}');
            } else {
                // Assume headers
                headers.push(line);
            }
        }

        let mut req: RequestBuilder = match &self.method[..] {
            "GET" => client.get(&uri),
            "POST" => client.post(&uri),
            "PUT" => client.put(&uri),
            "DELETE" => client.delete(&uri),
            "HEAD" => client.head(&uri),
            "PATCH" => client.patch(&uri),
            //"OPTIONS" => client.options(uri), // Uh oh
            method => {
                return Err(
                    self.error(self.lines[0].number, &format!("Invalid method: {}", method))
                );
            }
        };

        for header in headers {
            let mut parts = header.text.split(':');
            let name = match parts.next() {
                Some(name) => name,
                _ => {
                    return Err(self.error(header.number, "Invalid header syntax"));
                }
            };
            let value = match parts.next() {
                Some(name) => name.trim(),
                _ => {
                    return Err(self.error(header.number, "Invalid header syntax"));
                }
            };

            req = req.header(name, value);
        }

        req = match &body[..] {
            "" => req,
            _ => req.body(body),
        };

        if color {
            println!("{} {}\n", self.method, Yellow.paint(uri));
        } else {
            println!("{} {}\n", self.method, uri);
        };

        Ok(req)
    }
}

pub fn run(config: cli::Cli) {
    #[cfg(windows)]
    let enabled = ansi_term::enable_ansi_support();

    let contents = fs::read_to_string(config.path).expect("Something went wrong reading the file");
    let client = Client::new();

    let mut n = 0;
    let mut lines: Vec<Line> = vec![];

    for line in contents.lines() {
        n += 1;
        if !line.is_empty() && !line.starts_with('#') {
            lines.push(Line::new(line, n));
        }
    }

    let mut start_line = 0;
    n = 0;

    for line in &lines {
        n += 1;
        for method in METHODS.iter() {
            if line.text.starts_with(method) {
                println!("\n---------------");
                let req = Request::new(lines[start_line..n].to_vec(), method)
                    .parse(&client, !config.no_color);
                match req {
                    Ok(req) => {
                        send_req(req, config.verbose, !config.no_color).unwrap_or_else(|e| {
                            println!("{}", e);
                        });
                    }
                    Err(e) => {
                        println!("{}", e);
                    }
                }

                start_line = n;
            }
        }
    }
}

fn send_req(req: RequestBuilder, verbose: bool, color: bool) -> Result<(), reqwest::Error> {
    let res = match req.send() {
        Ok(res) => res,
        Err(e) => {
            return Err(e);
        }
    };

    let status = res.status();
    let reason = match status.canonical_reason() {
        Some(reason) => reason,
        None => "",
    };
    let code = status.as_str();
    if color {
        let code = if status.is_success() {
            Green.paint(code)
        } else if status.is_redirection() {
            Blue.paint(code)
        } else if status.is_informational() {
            Yellow.paint(code)
        } else {
            Red.paint(code)
        };
        println!("{} {}", code, reason);
    } else {
        println!("{} {}", code, reason);
    }

    if verbose {
        for (key, value) in res.headers().iter() {
            if color {
                println!("{}: {:?}", Cyan.paint(key.as_str()), value);
            } else {
                println!("{}: {:?}", key.as_str(), value);
            }
        }
        println!();
    }

    let default = &reqwest::header::HeaderValue::from_str("").unwrap();
    let content_type = res.headers().get("content-type").unwrap_or(default);
    // Regex?
    if content_type == "application/json; charset=utf-8" {
        let color_mode = match color {
            true => ColorMode::On,
            false => ColorMode::Off,
        };
        let res_body = res
            .text()
            .unwrap()
            .to_colored_json_with_styler(
                color_mode,
                Styler {
                    key: Color::Green.normal(),
                    string_value: Color::Cyan.normal(),
                    integer_value: Color::Yellow.normal(),
                    float_value: Color::Yellow.normal(),
                    object_brackets: Default::default(),
                    array_brackets: Default::default(),
                    bool_value: Color::Red.normal(),
                    ..Default::default()
                },
            )
            .unwrap();
        println!("{}", res_body);
    } else {
        println!("{}", res.text().unwrap());
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    fn setup(text: &str, method: &str) -> Result<RequestBuilder, String> {
        let mut n = 0;
        let mut lines = vec![];
        for line in text.split("\n") {
            n += 1;
            lines.push(Line::new(line, n));
        }
        let client = Client::new();
        Request::new(lines, method).parse(&client)
    }

    #[test]
    fn get() {
        let req = setup("https://example.com\nGET /route", "GET")
            .unwrap()
            .build()
            .unwrap();

        assert_eq!(req.method().as_str(), "GET");
        assert_eq!(
            *req.url(),
            reqwest::Url::parse("https://example.com/route").unwrap()
        );
        assert!(req.headers().is_empty());
        match req.body() {
            Some(_) => panic!("Body should be empty"),
            None => {} // OK
        }
    }

    #[test]
    fn no_location_specified() {
        let req = setup("http://localhost:8080\nPUT ", "PUT");
        match req {
            Ok(_) => {
                panic!("Expected error");
            }
            Err(e) => {
                assert_eq!(e, "Error (line 2): Expected location");
            }
        }
    }

    #[test]
    fn post() {
        let text = r#"http://localhost
Content-Type: application/json
{
    "key": "value"
}
POST /"#;
        let req = setup(text, "POST").unwrap().build().unwrap();

        assert_eq!(req.method().as_str(), "POST");
        assert_eq!(
            *req.url(),
            reqwest::Url::parse("http://localhost/").unwrap()
        );
        let headers = req.headers();
        assert_eq!(headers.len(), 1);
        assert_eq!(headers.get("content-type").unwrap(), &"application/json");
        match req.body() {
            Some(body) => {
                let expected_body = r#"{    "key": "value"}"#;
                assert_eq!(
                    body.as_bytes().unwrap(),
                    reqwest::blocking::Body::from(expected_body)
                        .as_bytes()
                        .unwrap()
                );
            }
            None => panic!("Expected body"),
        }
    }

    #[test]
    fn invalid_header() {
        let text = "https://www.example.com
Content-Type: text/html
other header
DELETE /api/thing";
        let req = setup(text, "DELETE");
        match req {
            Ok(_) => {
                panic!("Expected error");
            }
            Err(e) => {
                assert_eq!(e, "Error (line 3): Invalid header syntax");
            }
        }
    }
}