mirror of
https://github.com/nushell/nushell
synced 2025-01-04 01:09:05 +00:00
1867bb1a88
# Description
Possible fix of #11456
This PR fixes a bug where builtin commands did not respect the logic of
dynamically passed boolean flags. The reason is
[has_flag](6f59abaf43/crates/nu-protocol/src/ast/call.rs (L204C5-L212C6)
)
method did not evaluate and take into consideration expression used with
flag.
To address this issue a solution is proposed:
1. `has_flag` method is moved to `CallExt` and new logic to evaluate
expression and check if it is a boolean value is added
2. `has_flag_const` method is added to `CallExt` which is a constant
version of `has_flag`
3. `has_named` method is added to `Call` which is basically the old
logic of `has_flag`
4. All usages of `has_flag` in code are updated, mostly to pass
`engine_state` and `stack` to new `has_flag`. In `run_const` commands it
is replaced with `has_flag_const`. And in a few select places: parser,
`to nuon` and `into string` old logic via `has_named` is used.
# User-Facing Changes
Explicit values of boolean flags are now respected in builtin commands.
Before:
![image](https://github.com/nushell/nushell/assets/17511668/f9fbabb2-3cfd-43f9-ba9e-ece76d80043c)
After:
![image](https://github.com/nushell/nushell/assets/17511668/21867596-2075-437f-9c85-45563ac70083)
Another example:
Before:
![image](https://github.com/nushell/nushell/assets/17511668/efdbc5ca-5227-45a4-ac5b-532cdc2bbf5f)
After:
![image](https://github.com/nushell/nushell/assets/17511668/2907d5c5-aa93-404d-af1c-21cdc3d44646)
# Tests + Formatting
Added test reproducing some variants of original issue.
181 lines
5.5 KiB
Rust
181 lines
5.5 KiB
Rust
use std::sync::atomic::AtomicBool;
|
|
use std::sync::Arc;
|
|
|
|
use nu_engine::CallExt;
|
|
use nu_protocol::ast::Call;
|
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
|
use nu_protocol::{
|
|
Category, Example, PipelineData, ShellError, Signature, Spanned, SyntaxShape, Type, Value,
|
|
};
|
|
|
|
use crate::network::http::client::{
|
|
check_response_redirection, http_client, http_parse_redirect_mode, http_parse_url,
|
|
request_add_authorization_header, request_add_custom_headers, request_handle_response_headers,
|
|
request_set_timeout, send_request,
|
|
};
|
|
|
|
#[derive(Clone)]
|
|
pub struct SubCommand;
|
|
|
|
impl Command for SubCommand {
|
|
fn name(&self) -> &str {
|
|
"http head"
|
|
}
|
|
|
|
fn signature(&self) -> Signature {
|
|
Signature::build("http head")
|
|
.input_output_types(vec![(Type::Nothing, Type::Any)])
|
|
.allow_variants_without_examples(true)
|
|
.required(
|
|
"URL",
|
|
SyntaxShape::String,
|
|
"The URL to fetch the contents from.",
|
|
)
|
|
.named(
|
|
"user",
|
|
SyntaxShape::Any,
|
|
"the username when authenticating",
|
|
Some('u'),
|
|
)
|
|
.named(
|
|
"password",
|
|
SyntaxShape::Any,
|
|
"the password when authenticating",
|
|
Some('p'),
|
|
)
|
|
.named(
|
|
"max-time",
|
|
SyntaxShape::Int,
|
|
"timeout period in seconds",
|
|
Some('m'),
|
|
)
|
|
.named(
|
|
"headers",
|
|
SyntaxShape::Any,
|
|
"custom headers you want to add ",
|
|
Some('H'),
|
|
)
|
|
.switch(
|
|
"insecure",
|
|
"allow insecure server connections when using SSL",
|
|
Some('k'),
|
|
).named(
|
|
"redirect-mode",
|
|
SyntaxShape::String,
|
|
"What to do when encountering redirects. Default: 'follow'. Valid options: 'follow' ('f'), 'manual' ('m'), 'error' ('e').",
|
|
Some('R')
|
|
)
|
|
.filter()
|
|
.category(Category::Network)
|
|
}
|
|
|
|
fn usage(&self) -> &str {
|
|
"Get the headers from a URL."
|
|
}
|
|
|
|
fn extra_usage(&self) -> &str {
|
|
"Performs HTTP HEAD operation."
|
|
}
|
|
|
|
fn search_terms(&self) -> Vec<&str> {
|
|
vec!["network", "request", "curl", "wget", "headers", "header"]
|
|
}
|
|
|
|
fn run(
|
|
&self,
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
call: &Call,
|
|
input: PipelineData,
|
|
) -> Result<PipelineData, ShellError> {
|
|
run_head(engine_state, stack, call, input)
|
|
}
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
vec![
|
|
Example {
|
|
description: "Get headers from example.com",
|
|
example: "http head https://www.example.com",
|
|
result: None,
|
|
},
|
|
Example {
|
|
description: "Get headers from example.com, with username and password",
|
|
example: "http head --user myuser --password mypass https://www.example.com",
|
|
result: None,
|
|
},
|
|
Example {
|
|
description: "Get headers from example.com, with custom header",
|
|
example:
|
|
"http head --headers [my-header-key my-header-value] https://www.example.com",
|
|
result: None,
|
|
},
|
|
]
|
|
}
|
|
}
|
|
|
|
struct Arguments {
|
|
url: Value,
|
|
headers: Option<Value>,
|
|
insecure: bool,
|
|
user: Option<String>,
|
|
password: Option<String>,
|
|
timeout: Option<Value>,
|
|
redirect: Option<Spanned<String>>,
|
|
}
|
|
|
|
fn run_head(
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
call: &Call,
|
|
_input: PipelineData,
|
|
) -> Result<PipelineData, ShellError> {
|
|
let args = Arguments {
|
|
url: call.req(engine_state, stack, 0)?,
|
|
headers: call.get_flag(engine_state, stack, "headers")?,
|
|
insecure: call.has_flag(engine_state, stack, "insecure")?,
|
|
user: call.get_flag(engine_state, stack, "user")?,
|
|
password: call.get_flag(engine_state, stack, "password")?,
|
|
timeout: call.get_flag(engine_state, stack, "max-time")?,
|
|
redirect: call.get_flag(engine_state, stack, "redirect-mode")?,
|
|
};
|
|
let ctrl_c = engine_state.ctrlc.clone();
|
|
|
|
helper(engine_state, stack, call, args, ctrl_c)
|
|
}
|
|
|
|
// Helper function that actually goes to retrieve the resource from the url given
|
|
// The Option<String> return a possible file extension which can be used in AutoConvert commands
|
|
fn helper(
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
call: &Call,
|
|
args: Arguments,
|
|
ctrlc: Option<Arc<AtomicBool>>,
|
|
) -> Result<PipelineData, ShellError> {
|
|
let span = args.url.span();
|
|
let (requested_url, _) = http_parse_url(call, span, args.url)?;
|
|
let redirect_mode = http_parse_redirect_mode(args.redirect)?;
|
|
|
|
let client = http_client(args.insecure, redirect_mode, engine_state, stack)?;
|
|
let mut request = client.head(&requested_url);
|
|
|
|
request = request_set_timeout(args.timeout, request)?;
|
|
request = request_add_authorization_header(args.user, args.password, request);
|
|
request = request_add_custom_headers(args.headers, request)?;
|
|
|
|
let response = send_request(request, None, None, ctrlc);
|
|
check_response_redirection(redirect_mode, span, &response)?;
|
|
request_handle_response_headers(span, response)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn test_examples() {
|
|
use crate::test_examples;
|
|
|
|
test_examples(SubCommand {})
|
|
}
|
|
}
|