Closes #8585. Prior to this change, the `http` commands could get stuck for 30s while attempting to make a connection to a remote server. After this change, `ctrl+c` works as expected:  To make this work, we perform blocking `ureq` calls in a background thread and poll the channel while checking `ctrl+c`.
205 lines
5.7 KiB
Rust
205 lines
5.7 KiB
Rust
use nu_engine::CallExt;
|
|
use nu_protocol::ast::Call;
|
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
|
use nu_protocol::{
|
|
Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
|
|
};
|
|
|
|
use crate::network::http::client::{
|
|
http_client, http_parse_url, request_add_authorization_header, request_add_custom_headers,
|
|
request_handle_response, request_set_timeout, send_request,
|
|
};
|
|
|
|
use super::client::RequestFlags;
|
|
|
|
#[derive(Clone)]
|
|
pub struct SubCommand;
|
|
|
|
impl Command for SubCommand {
|
|
fn name(&self) -> &str {
|
|
"http get"
|
|
}
|
|
|
|
fn signature(&self) -> Signature {
|
|
Signature::build("http get")
|
|
.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(
|
|
"raw",
|
|
"fetch contents as text rather than a table",
|
|
Some('r'),
|
|
)
|
|
.switch(
|
|
"insecure",
|
|
"allow insecure server connections when using SSL",
|
|
Some('k'),
|
|
)
|
|
.switch(
|
|
"full",
|
|
"returns the full response instead of only the body",
|
|
Some('f'),
|
|
)
|
|
.switch(
|
|
"allow-errors",
|
|
"do not fail if the server returns an error code",
|
|
Some('e'),
|
|
)
|
|
.filter()
|
|
.category(Category::Network)
|
|
}
|
|
|
|
fn usage(&self) -> &str {
|
|
"Fetch the contents from a URL."
|
|
}
|
|
|
|
fn extra_usage(&self) -> &str {
|
|
"Performs HTTP GET operation."
|
|
}
|
|
|
|
fn search_terms(&self) -> Vec<&str> {
|
|
vec![
|
|
"network", "fetch", "pull", "request", "download", "curl", "wget",
|
|
]
|
|
}
|
|
|
|
fn run(
|
|
&self,
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
call: &Call,
|
|
input: PipelineData,
|
|
) -> Result<PipelineData, ShellError> {
|
|
run_get(engine_state, stack, call, input)
|
|
}
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
vec![
|
|
Example {
|
|
description: "Get content from example.com",
|
|
example: "http get https://www.example.com",
|
|
result: None,
|
|
},
|
|
Example {
|
|
description: "Get content from example.com, with username and password",
|
|
example: "http get -u myuser -p mypass https://www.example.com",
|
|
result: None,
|
|
},
|
|
Example {
|
|
description: "Get content from example.com, with custom header",
|
|
example: "http get -H [my-header-key my-header-value] https://www.example.com",
|
|
result: None,
|
|
},
|
|
]
|
|
}
|
|
}
|
|
|
|
struct Arguments {
|
|
url: Value,
|
|
headers: Option<Value>,
|
|
raw: bool,
|
|
insecure: bool,
|
|
user: Option<String>,
|
|
password: Option<String>,
|
|
timeout: Option<Value>,
|
|
full: bool,
|
|
allow_errors: bool,
|
|
}
|
|
|
|
fn run_get(
|
|
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")?,
|
|
raw: call.has_flag("raw"),
|
|
insecure: call.has_flag("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")?,
|
|
full: call.has_flag("full"),
|
|
allow_errors: call.has_flag("allow-errors"),
|
|
};
|
|
helper(engine_state, stack, call, args)
|
|
}
|
|
|
|
// 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,
|
|
) -> Result<PipelineData, ShellError> {
|
|
let span = args.url.span()?;
|
|
let ctrl_c = engine_state.ctrlc.clone();
|
|
let (requested_url, _) = http_parse_url(call, span, args.url)?;
|
|
|
|
let client = http_client(args.insecure);
|
|
let mut request = client.get(&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, ctrl_c);
|
|
|
|
let request_flags = RequestFlags {
|
|
raw: args.raw,
|
|
full: args.full,
|
|
allow_errors: args.allow_errors,
|
|
};
|
|
|
|
request_handle_response(
|
|
engine_state,
|
|
stack,
|
|
span,
|
|
&requested_url,
|
|
request_flags,
|
|
response,
|
|
)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn test_examples() {
|
|
use crate::test_examples;
|
|
|
|
test_examples(SubCommand {})
|
|
}
|
|
}
|