1
/*
2
 * Hurl (https://hurl.dev)
3
 * Copyright (C) 2024 Orange
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *          http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 */
18
use clap::ArgMatches;
19

            
20
use super::HurlOption;
21

            
22
24
pub fn body(arg_matches: &ArgMatches) -> Option<String> {
23
24
    match get_string(arg_matches, "data") {
24
18
        None => None,
25
6
        Some(v) => {
26
6
            if let Some(filename) = v.strip_prefix('@') {
27
3
                Some(format!("file, {filename};"))
28
            } else {
29
3
                Some(format!("```\n{v}\n```"))
30
            }
31
        }
32
    }
33
}
34

            
35
24
pub fn method(arg_matches: &ArgMatches) -> String {
36
24
    match get_string(arg_matches, "method") {
37
        None => {
38
24
            if arg_matches.contains_id("data") {
39
6
                "POST".to_string()
40
            } else {
41
18
                "GET".to_string()
42
            }
43
        }
44
        Some(v) => v,
45
    }
46
}
47

            
48
24
pub fn url(arg_matches: &ArgMatches) -> String {
49
24
    let s = if let Some(value) = get_string(arg_matches, "url") {
50
3
        value
51
    } else {
52
21
        get_string(arg_matches, "url_param").unwrap()
53
    };
54
24
    if !s.starts_with("http") {
55
        format!("https://{s}")
56
    } else {
57
24
        s
58
    }
59
}
60

            
61
24
pub fn headers(arg_matches: &ArgMatches) -> Vec<String> {
62
24
    let mut headers = get_strings(arg_matches, "headers").unwrap_or_default();
63
24
    if !has_content_type(&headers) {
64
18
        if let Some(data) = get_string(arg_matches, "data") {
65
            if !data.starts_with('@') {
66
                headers.push("Content-Type: application/x-www-form-urlencoded".to_string());
67
            }
68
        }
69
    }
70

            
71
24
    headers
72
}
73

            
74
24
pub fn options(arg_matches: &ArgMatches) -> Vec<HurlOption> {
75
24
    let mut options = vec![];
76
24
    if has_flag(arg_matches, "compressed") {
77
        options.push(HurlOption::new("compressed", "true"));
78
    }
79
24
    if has_flag(arg_matches, "location") {
80
3
        options.push(HurlOption::new("location", "true"));
81
    }
82
24
    if has_flag(arg_matches, "insecure") {
83
3
        options.push(HurlOption::new("insecure", "true"));
84
    }
85
24
    if let Some(value) = get::<i32>(arg_matches, "max_redirects") {
86
        options.push(HurlOption::new("max-redirs", value.to_string().as_str()));
87
    }
88
24
    if let Some(value) = get::<i32>(arg_matches, "retry") {
89
3
        options.push(HurlOption::new("retry", value.to_string().as_str()));
90
    }
91
24
    options
92
}
93

            
94
24
fn has_content_type(headers: &Vec<String>) -> bool {
95
39
    for header in headers {
96
21
        if header.starts_with("Content-Type") {
97
6
            return true;
98
        }
99
    }
100
18
    false
101
}
102

            
103
72
fn has_flag(matches: &ArgMatches, name: &str) -> bool {
104
72
    matches.get_one::<bool>(name) == Some(&true)
105
}
106

            
107
/// Returns an optional value of type `T` from the command line `matches` given the option `name`.
108
48
fn get<T: Clone + Send + Sync + 'static>(matches: &ArgMatches, name: &str) -> Option<T> {
109
48
    matches.get_one::<T>(name).cloned()
110
}
111

            
112
111
fn get_string(matches: &ArgMatches, name: &str) -> Option<String> {
113
121
    matches.get_one::<String>(name).map(|x| x.to_string())
114
}
115

            
116
/// Returns an optional list of `String` from the command line `matches` given the option `name`.
117
24
fn get_strings(matches: &ArgMatches, name: &str) -> Option<Vec<String>> {
118
24
    matches
119
24
        .get_many::<String>(name)
120
31
        .map(|v| v.map(|x| x.to_string()).collect())
121
}