1
/*
2
 * Hurl (https://hurl.dev)
3
 * Copyright (C) 2026 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
51
pub fn body(arg_matches: &ArgMatches) -> Option<String> {
23
51
    if let Some(v) = get_string(arg_matches, "data_raw") {
24
6
        return Some(format!("```\n{v}\n```"));
25
    }
26
45
    let v = get_string(arg_matches, "data")?;
27
6
    if let Some(filename) = v.strip_prefix('@') {
28
3
        Some(format!("file, {filename};"))
29
    } else {
30
3
        Some(format!("```\n{v}\n```"))
31
    }
32
}
33

            
34
51
pub fn method(arg_matches: &ArgMatches) -> String {
35
51
    match get_string(arg_matches, "method") {
36
        None => {
37
51
            if has_arg(arg_matches, "data_raw") || has_arg(arg_matches, "data") {
38
12
                "POST".to_string()
39
            } else {
40
39
                "GET".to_string()
41
            }
42
        }
43
        Some(v) => v,
44
    }
45
}
46

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

            
60
51
pub fn cookies(arg_matches: &ArgMatches) -> Vec<String> {
61
51
    get_strings(arg_matches, "cookies").unwrap_or_default()
62
}
63

            
64
51
pub fn headers(arg_matches: &ArgMatches) -> Vec<String> {
65
51
    let mut headers = get_strings(arg_matches, "headers").unwrap_or_default();
66
51
    if !has_content_type(&headers) {
67
42
        if has_arg(arg_matches, "data_raw") {
68
3
            headers.push("Content-Type: application/x-www-form-urlencoded".to_string());
69
39
        } else if let Some(data) = get_string(arg_matches, "data")
70
            && !data.starts_with('@')
71
        {
72
            headers.push("Content-Type: application/x-www-form-urlencoded".to_string());
73
        }
74
    }
75

            
76
51
    headers
77
}
78

            
79
51
pub fn options(arg_matches: &ArgMatches) -> Vec<HurlOption> {
80
51
    let mut options = vec![];
81
51
    if has_flag(arg_matches, "compressed") {
82
        options.push(HurlOption::new("compressed", "true"));
83
    }
84
51
    if has_flag(arg_matches, "digest") {
85
        options.push(HurlOption::new("digest", "true"));
86
    }
87
51
    if has_flag(arg_matches, "location") {
88
3
        options.push(HurlOption::new("location", "true"));
89
    }
90
51
    if has_flag(arg_matches, "insecure") {
91
3
        options.push(HurlOption::new("insecure", "true"));
92
    }
93
51
    if let Some(value) = get::<i32>(arg_matches, "max_redirects") {
94
        options.push(HurlOption::new("max-redirs", value.to_string().as_str()));
95
    }
96
51
    if has_flag(arg_matches, "negotiate") {
97
3
        options.push(HurlOption::new("negotiate", "true"));
98
    }
99
51
    if has_flag(arg_matches, "ntlm") {
100
3
        options.push(HurlOption::new("ntlm", "true"));
101
    }
102
51
    if let Some(value) = get::<i32>(arg_matches, "retry") {
103
3
        options.push(HurlOption::new("retry", value.to_string().as_str()));
104
    }
105
51
    if let Some(value) = get::<String>(arg_matches, "user") {
106
6
        options.push(HurlOption::new("user", value.to_string().as_str()));
107
    }
108
51
    if has_flag(arg_matches, "verbose") {
109
6
        options.push(HurlOption::new("verbose", "true"));
110
    }
111
51
    options
112
}
113

            
114
51
fn has_content_type(headers: &Vec<String>) -> bool {
115
51
    for header in headers {
116
27
        if header.starts_with("Content-Type") {
117
9
            return true;
118
        }
119
    }
120
42
    false
121
}
122

            
123
138
fn has_arg(matches: &ArgMatches, name: &str) -> bool {
124
138
    matches.get_one::<String>(name).is_some()
125
}
126

            
127
357
fn has_flag(matches: &ArgMatches, name: &str) -> bool {
128
357
    matches.get_one::<bool>(name) == Some(&true)
129
}
130

            
131
/// Returns an optional value of type `T` from the command line `matches` given the option `name`.
132
153
fn get<T: Clone + Send + Sync + 'static>(matches: &ArgMatches, name: &str) -> Option<T> {
133
153
    matches.get_one::<T>(name).cloned()
134
}
135

            
136
285
fn get_string(matches: &ArgMatches, name: &str) -> Option<String> {
137
306
    matches.get_one::<String>(name).map(|x| x.to_string())
138
}
139

            
140
/// Returns an optional list of `String` from the command line `matches` given the option `name`.
141
102
fn get_strings(matches: &ArgMatches, name: &str) -> Option<Vec<String>> {
142
102
    matches
143
102
        .get_many::<String>(name)
144
111
        .map(|v| v.map(|x| x.to_string()).collect())
145
}