1
/*
2
 * Hurl (https://hurl.dev)
3
 * Copyright (C) 2025 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
33
pub fn body(arg_matches: &ArgMatches) -> Option<String> {
23
33
    match get_string(arg_matches, "data") {
24
27
        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
33
pub fn method(arg_matches: &ArgMatches) -> String {
36
33
    match get_string(arg_matches, "method") {
37
        None => {
38
33
            if arg_matches.contains_id("data") {
39
6
                "POST".to_string()
40
            } else {
41
27
                "GET".to_string()
42
            }
43
        }
44
        Some(v) => v,
45
    }
46
}
47

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

            
61
33
pub fn headers(arg_matches: &ArgMatches) -> Vec<String> {
62
33
    let mut headers = get_strings(arg_matches, "headers").unwrap_or_default();
63
33
    if !has_content_type(&headers) {
64
27
        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
33
    headers
72
}
73

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

            
97
33
fn has_content_type(headers: &Vec<String>) -> bool {
98
51
    for header in headers {
99
24
        if header.starts_with("Content-Type") {
100
6
            return true;
101
        }
102
    }
103
27
    false
104
}
105

            
106
132
fn has_flag(matches: &ArgMatches, name: &str) -> bool {
107
132
    matches.get_one::<bool>(name) == Some(&true)
108
}
109

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

            
115
156
fn get_string(matches: &ArgMatches, name: &str) -> Option<String> {
116
169
    matches.get_one::<String>(name).map(|x| x.to_string())
117
}
118

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