2019-08-31 06:08:59 +02:00
|
|
|
use crate::commands::UnevaluatedCallInfo;
|
2019-08-30 20:27:15 +02:00
|
|
|
use crate::prelude::*;
|
|
|
|
use base64::encode;
|
|
|
|
use mime::Mime;
|
2019-11-30 01:21:05 +01:00
|
|
|
use nu_errors::ShellError;
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
use nu_protocol::{
|
|
|
|
CallInfo, Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value,
|
|
|
|
};
|
2019-11-21 15:33:14 +01:00
|
|
|
use nu_source::AnchorLocation;
|
2019-08-31 06:08:59 +02:00
|
|
|
use std::path::PathBuf;
|
2019-08-30 20:27:15 +02:00
|
|
|
use std::str::FromStr;
|
|
|
|
use surf::mime;
|
2019-09-14 18:30:24 +02:00
|
|
|
|
2019-09-29 04:03:10 +02:00
|
|
|
pub enum HeaderKind {
|
|
|
|
ContentType(String),
|
|
|
|
ContentLength(String),
|
|
|
|
}
|
|
|
|
|
2019-08-30 20:27:15 +02:00
|
|
|
pub struct Post;
|
|
|
|
|
|
|
|
impl PerItemCommand for Post {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"post"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build(self.name())
|
2019-10-28 06:15:35 +01:00
|
|
|
.required("path", SyntaxShape::Any, "the URL to post to")
|
|
|
|
.required("body", SyntaxShape::Any, "the contents of the post body")
|
|
|
|
.named("user", SyntaxShape::Any, "the username when authenticating")
|
|
|
|
.named(
|
|
|
|
"password",
|
|
|
|
SyntaxShape::Any,
|
|
|
|
"the password when authenticating",
|
|
|
|
)
|
|
|
|
.named(
|
|
|
|
"content-type",
|
|
|
|
SyntaxShape::Any,
|
|
|
|
"the MIME type of content to post",
|
|
|
|
)
|
|
|
|
.named(
|
|
|
|
"content-length",
|
|
|
|
SyntaxShape::Any,
|
|
|
|
"the length of the content being posted",
|
|
|
|
)
|
|
|
|
.switch("raw", "return values as a string instead of a table")
|
2019-08-30 20:27:15 +02:00
|
|
|
}
|
|
|
|
|
2019-08-30 00:52:32 +02:00
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Post content to a url and retrieve data as a table if possible."
|
|
|
|
}
|
|
|
|
|
2019-08-30 20:27:15 +02:00
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
call_info: &CallInfo,
|
2019-08-31 06:08:59 +02:00
|
|
|
registry: &CommandRegistry,
|
|
|
|
raw_args: &RawCommandArgs,
|
2019-11-21 15:33:14 +01:00
|
|
|
_input: Value,
|
2019-08-30 20:27:15 +02:00
|
|
|
) -> Result<OutputStream, ShellError> {
|
2019-08-31 06:08:59 +02:00
|
|
|
run(call_info, registry, raw_args)
|
2019-08-30 20:27:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-31 06:08:59 +02:00
|
|
|
fn run(
|
|
|
|
call_info: &CallInfo,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
raw_args: &RawCommandArgs,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
2019-10-13 06:12:43 +02:00
|
|
|
let name_tag = call_info.name_tag.clone();
|
2019-08-31 06:08:59 +02:00
|
|
|
let call_info = call_info.clone();
|
2019-10-13 06:12:43 +02:00
|
|
|
let path =
|
|
|
|
match call_info.args.nth(0).ok_or_else(|| {
|
|
|
|
ShellError::labeled_error("No url specified", "for command", &name_tag)
|
|
|
|
})? {
|
|
|
|
file => file.clone(),
|
|
|
|
};
|
2019-11-21 15:33:14 +01:00
|
|
|
let path_tag = path.tag.clone();
|
2019-10-13 06:12:43 +02:00
|
|
|
let body =
|
|
|
|
match call_info.args.nth(1).ok_or_else(|| {
|
|
|
|
ShellError::labeled_error("No body specified", "for command", &name_tag)
|
|
|
|
})? {
|
|
|
|
file => file.clone(),
|
|
|
|
};
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
let path_str = path.as_string()?.to_string();
|
2019-08-30 20:27:15 +02:00
|
|
|
let has_raw = call_info.args.has("raw");
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
let user = call_info
|
|
|
|
.args
|
|
|
|
.get("user")
|
|
|
|
.map(|x| x.as_string().unwrap().to_string());
|
2019-08-30 20:27:15 +02:00
|
|
|
let password = call_info
|
|
|
|
.args
|
|
|
|
.get("password")
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
.map(|x| x.as_string().unwrap().to_string());
|
2019-08-31 06:08:59 +02:00
|
|
|
let registry = registry.clone();
|
|
|
|
let raw_args = raw_args.clone();
|
2019-08-30 20:27:15 +02:00
|
|
|
|
2019-09-29 10:29:43 +02:00
|
|
|
let headers = get_headers(&call_info)?;
|
2019-09-29 04:03:10 +02:00
|
|
|
|
2019-09-26 02:22:17 +02:00
|
|
|
let stream = async_stream! {
|
2019-10-13 06:12:43 +02:00
|
|
|
let (file_extension, contents, contents_tag) =
|
2019-11-21 15:33:14 +01:00
|
|
|
post(&path_str, &body, user, password, &headers, path_tag.clone(), ®istry, &raw_args).await.unwrap();
|
2019-08-31 06:08:59 +02:00
|
|
|
|
|
|
|
let file_extension = if has_raw {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
// If the extension could not be determined via mimetype, try to use the path
|
|
|
|
// extension. Some file types do not declare their mimetypes (such as bson files).
|
|
|
|
file_extension.or(path_str.split('.').last().map(String::from))
|
|
|
|
};
|
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
let tagged_contents = contents.into_value(&contents_tag);
|
2019-08-30 20:27:15 +02:00
|
|
|
|
2019-08-31 06:08:59 +02:00
|
|
|
if let Some(extension) = file_extension {
|
|
|
|
let command_name = format!("from-{}", extension);
|
|
|
|
if let Some(converter) = registry.get_command(&command_name) {
|
|
|
|
let new_args = RawCommandArgs {
|
|
|
|
host: raw_args.host,
|
2019-10-13 06:12:43 +02:00
|
|
|
ctrl_c: raw_args.ctrl_c,
|
2019-08-31 06:08:59 +02:00
|
|
|
shell_manager: raw_args.shell_manager,
|
|
|
|
call_info: UnevaluatedCallInfo {
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
args: nu_parser::hir::Call {
|
2019-08-31 06:08:59 +02:00
|
|
|
head: raw_args.call_info.args.head,
|
|
|
|
positional: None,
|
2019-11-21 15:33:14 +01:00
|
|
|
named: None,
|
|
|
|
span: Span::unknown()
|
2019-08-31 06:08:59 +02:00
|
|
|
},
|
|
|
|
source: raw_args.call_info.source,
|
2019-09-14 18:30:24 +02:00
|
|
|
name_tag: raw_args.call_info.name_tag,
|
2019-08-31 06:08:59 +02:00
|
|
|
}
|
|
|
|
};
|
2019-11-04 02:04:01 +01:00
|
|
|
let mut result = converter.run(new_args.with_input(vec![tagged_contents]), ®istry);
|
2019-08-31 06:08:59 +02:00
|
|
|
let result_vec: Vec<Result<ReturnSuccess, ShellError>> = result.drain_vec().await;
|
|
|
|
for res in result_vec {
|
|
|
|
match res {
|
2019-11-21 15:33:14 +01:00
|
|
|
Ok(ReturnSuccess::Value(Value { value: UntaggedValue::Table(list), ..})) => {
|
2019-08-31 06:08:59 +02:00
|
|
|
for l in list {
|
|
|
|
yield Ok(ReturnSuccess::Value(l));
|
|
|
|
}
|
|
|
|
}
|
2019-11-21 15:33:14 +01:00
|
|
|
Ok(ReturnSuccess::Value(Value { value, .. })) => {
|
|
|
|
yield Ok(ReturnSuccess::Value(Value { value, tag: contents_tag.clone() }));
|
2019-08-31 06:08:59 +02:00
|
|
|
}
|
|
|
|
x => yield x,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
yield ReturnSuccess::value(tagged_contents);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
yield ReturnSuccess::value(tagged_contents);
|
|
|
|
}
|
2019-08-30 20:27:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(stream.to_output_stream())
|
|
|
|
}
|
|
|
|
|
2019-09-29 10:29:43 +02:00
|
|
|
fn get_headers(call_info: &CallInfo) -> Result<Vec<HeaderKind>, ShellError> {
|
|
|
|
let mut headers = vec![];
|
|
|
|
|
|
|
|
match extract_header_value(&call_info, "content-type") {
|
|
|
|
Ok(h) => match h {
|
|
|
|
Some(ct) => headers.push(HeaderKind::ContentType(ct)),
|
|
|
|
None => {}
|
|
|
|
},
|
|
|
|
Err(e) => {
|
|
|
|
return Err(e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
match extract_header_value(&call_info, "content-length") {
|
|
|
|
Ok(h) => match h {
|
|
|
|
Some(cl) => headers.push(HeaderKind::ContentLength(cl)),
|
|
|
|
None => {}
|
|
|
|
},
|
|
|
|
Err(e) => {
|
|
|
|
return Err(e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(headers)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn extract_header_value(call_info: &CallInfo, key: &str) -> Result<Option<String>, ShellError> {
|
|
|
|
if call_info.args.has(key) {
|
2019-09-29 23:43:39 +02:00
|
|
|
let tagged = call_info.args.get(key);
|
|
|
|
let val = match tagged {
|
2019-11-21 15:33:14 +01:00
|
|
|
Some(Value {
|
|
|
|
value: UntaggedValue::Primitive(Primitive::String(s)),
|
2019-09-29 10:29:43 +02:00
|
|
|
..
|
|
|
|
}) => s.clone(),
|
2019-11-21 15:33:14 +01:00
|
|
|
Some(Value { tag, .. }) => {
|
2019-09-29 23:43:39 +02:00
|
|
|
return Err(ShellError::labeled_error(
|
|
|
|
format!("{} not in expected format. Expected string.", key),
|
|
|
|
"post error",
|
|
|
|
tag,
|
|
|
|
));
|
|
|
|
}
|
2019-09-29 10:29:43 +02:00
|
|
|
_ => {
|
2019-09-29 23:43:39 +02:00
|
|
|
return Err(ShellError::labeled_error(
|
|
|
|
format!("{} not in expected format. Expected string.", key),
|
|
|
|
"post error",
|
|
|
|
Tag::unknown(),
|
|
|
|
));
|
2019-09-29 10:29:43 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
return Ok(Some(val));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
|
2019-08-30 20:27:15 +02:00
|
|
|
pub async fn post(
|
|
|
|
location: &str,
|
2019-11-21 15:33:14 +01:00
|
|
|
body: &Value,
|
2019-08-30 20:27:15 +02:00
|
|
|
user: Option<String>,
|
|
|
|
password: Option<String>,
|
2019-09-29 04:03:10 +02:00
|
|
|
headers: &Vec<HeaderKind>,
|
2019-09-14 18:30:24 +02:00
|
|
|
tag: Tag,
|
2019-08-31 06:08:59 +02:00
|
|
|
registry: &CommandRegistry,
|
|
|
|
raw_args: &RawCommandArgs,
|
2019-11-21 15:33:14 +01:00
|
|
|
) -> Result<(Option<String>, UntaggedValue, Tag), ShellError> {
|
2019-08-31 06:08:59 +02:00
|
|
|
let registry = registry.clone();
|
|
|
|
let raw_args = raw_args.clone();
|
2019-08-30 20:27:15 +02:00
|
|
|
if location.starts_with("http:") || location.starts_with("https:") {
|
2019-09-01 08:44:56 +02:00
|
|
|
let login = match (user, password) {
|
|
|
|
(Some(user), Some(password)) => Some(encode(&format!("{}:{}", user, password))),
|
|
|
|
(Some(user), _) => Some(encode(&format!("{}:", user))),
|
|
|
|
_ => None,
|
|
|
|
};
|
2019-08-31 06:08:59 +02:00
|
|
|
let response = match body {
|
2019-11-21 15:33:14 +01:00
|
|
|
Value {
|
|
|
|
value: UntaggedValue::Primitive(Primitive::String(body_str)),
|
2019-08-31 06:08:59 +02:00
|
|
|
..
|
|
|
|
} => {
|
2019-09-01 08:44:56 +02:00
|
|
|
let mut s = surf::post(location).body_string(body_str.to_string());
|
|
|
|
if let Some(login) = login {
|
|
|
|
s = s.set_header("Authorization", format!("Basic {}", login));
|
|
|
|
}
|
2019-09-29 04:03:10 +02:00
|
|
|
|
|
|
|
for h in headers {
|
|
|
|
s = match h {
|
|
|
|
HeaderKind::ContentType(ct) => s.set_header("Content-Type", ct),
|
|
|
|
HeaderKind::ContentLength(cl) => s.set_header("Content-Length", cl),
|
|
|
|
};
|
|
|
|
}
|
2019-09-01 08:44:56 +02:00
|
|
|
s.await
|
2019-08-31 06:08:59 +02:00
|
|
|
}
|
2019-11-21 15:33:14 +01:00
|
|
|
Value {
|
|
|
|
value: UntaggedValue::Primitive(Primitive::Binary(b)),
|
2019-08-31 06:08:59 +02:00
|
|
|
..
|
|
|
|
} => {
|
2019-09-01 08:44:56 +02:00
|
|
|
let mut s = surf::post(location).body_bytes(b);
|
|
|
|
if let Some(login) = login {
|
|
|
|
s = s.set_header("Authorization", format!("Basic {}", login));
|
|
|
|
}
|
|
|
|
s.await
|
2019-08-31 06:08:59 +02:00
|
|
|
}
|
2019-11-21 15:33:14 +01:00
|
|
|
Value { value, tag } => {
|
2019-08-31 06:08:59 +02:00
|
|
|
if let Some(converter) = registry.get_command("to-json") {
|
|
|
|
let new_args = RawCommandArgs {
|
|
|
|
host: raw_args.host,
|
2019-10-13 06:12:43 +02:00
|
|
|
ctrl_c: raw_args.ctrl_c,
|
2019-08-31 06:08:59 +02:00
|
|
|
shell_manager: raw_args.shell_manager,
|
|
|
|
call_info: UnevaluatedCallInfo {
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
args: nu_parser::hir::Call {
|
2019-08-31 06:08:59 +02:00
|
|
|
head: raw_args.call_info.args.head,
|
|
|
|
positional: None,
|
|
|
|
named: None,
|
2019-11-21 15:33:14 +01:00
|
|
|
span: Span::unknown(),
|
2019-08-31 06:08:59 +02:00
|
|
|
},
|
|
|
|
source: raw_args.call_info.source,
|
2019-09-14 18:30:24 +02:00
|
|
|
name_tag: raw_args.call_info.name_tag,
|
2019-08-31 06:08:59 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
let mut result = converter.run(
|
2019-11-21 15:33:14 +01:00
|
|
|
new_args.with_input(vec![value.clone().into_value(tag.clone())]),
|
2019-08-31 06:08:59 +02:00
|
|
|
®istry,
|
|
|
|
);
|
|
|
|
let result_vec: Vec<Result<ReturnSuccess, ShellError>> =
|
|
|
|
result.drain_vec().await;
|
|
|
|
let mut result_string = String::new();
|
|
|
|
for res in result_vec {
|
|
|
|
match res {
|
2019-11-21 15:33:14 +01:00
|
|
|
Ok(ReturnSuccess::Value(Value {
|
|
|
|
value: UntaggedValue::Primitive(Primitive::String(s)),
|
2019-08-31 06:08:59 +02:00
|
|
|
..
|
|
|
|
})) => {
|
|
|
|
result_string.push_str(&s);
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
return Err(ShellError::labeled_error(
|
|
|
|
"Save could not successfully save",
|
|
|
|
"unexpected data during save",
|
2019-10-13 06:12:43 +02:00
|
|
|
tag,
|
2019-08-31 06:08:59 +02:00
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-01 08:44:56 +02:00
|
|
|
|
|
|
|
let mut s = surf::post(location).body_string(result_string);
|
|
|
|
|
|
|
|
if let Some(login) = login {
|
|
|
|
s = s.set_header("Authorization", format!("Basic {}", login));
|
|
|
|
}
|
|
|
|
s.await
|
2019-08-31 06:08:59 +02:00
|
|
|
} else {
|
|
|
|
return Err(ShellError::labeled_error(
|
|
|
|
"Could not automatically convert table",
|
|
|
|
"needs manual conversion",
|
2019-10-13 06:12:43 +02:00
|
|
|
tag,
|
2019-08-31 06:08:59 +02:00
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2019-08-30 20:27:15 +02:00
|
|
|
match response {
|
|
|
|
Ok(mut r) => match r.headers().get("content-type") {
|
|
|
|
Some(content_type) => {
|
|
|
|
let content_type = Mime::from_str(content_type).unwrap();
|
|
|
|
match (content_type.type_(), content_type.subtype()) {
|
|
|
|
(mime::APPLICATION, mime::XML) => Ok((
|
|
|
|
Some("xml".to_string()),
|
2019-12-04 20:52:31 +01:00
|
|
|
UntaggedValue::string(r.body_string().await.map_err(|_| {
|
2019-08-30 20:27:15 +02:00
|
|
|
ShellError::labeled_error(
|
|
|
|
"Could not load text from remote url",
|
|
|
|
"could not load",
|
2019-10-13 06:12:43 +02:00
|
|
|
&tag,
|
2019-08-30 20:27:15 +02:00
|
|
|
)
|
|
|
|
})?),
|
2019-10-13 06:12:43 +02:00
|
|
|
Tag {
|
|
|
|
anchor: Some(AnchorLocation::Url(location.to_string())),
|
|
|
|
span: tag.span,
|
|
|
|
},
|
2019-08-30 20:27:15 +02:00
|
|
|
)),
|
|
|
|
(mime::APPLICATION, mime::JSON) => Ok((
|
|
|
|
Some("json".to_string()),
|
2019-12-04 20:52:31 +01:00
|
|
|
UntaggedValue::string(r.body_string().await.map_err(|_| {
|
2019-08-30 20:27:15 +02:00
|
|
|
ShellError::labeled_error(
|
|
|
|
"Could not load text from remote url",
|
|
|
|
"could not load",
|
2019-10-13 06:12:43 +02:00
|
|
|
&tag,
|
2019-08-30 20:27:15 +02:00
|
|
|
)
|
|
|
|
})?),
|
2019-10-13 06:12:43 +02:00
|
|
|
Tag {
|
|
|
|
anchor: Some(AnchorLocation::Url(location.to_string())),
|
|
|
|
span: tag.span,
|
|
|
|
},
|
2019-08-30 20:27:15 +02:00
|
|
|
)),
|
|
|
|
(mime::APPLICATION, mime::OCTET_STREAM) => {
|
|
|
|
let buf: Vec<u8> = r.body_bytes().await.map_err(|_| {
|
|
|
|
ShellError::labeled_error(
|
|
|
|
"Could not load binary file",
|
|
|
|
"could not load",
|
2019-10-13 06:12:43 +02:00
|
|
|
&tag,
|
2019-08-30 20:27:15 +02:00
|
|
|
)
|
|
|
|
})?;
|
|
|
|
Ok((
|
|
|
|
None,
|
2019-12-04 20:52:31 +01:00
|
|
|
UntaggedValue::binary(buf),
|
2019-10-13 06:12:43 +02:00
|
|
|
Tag {
|
|
|
|
anchor: Some(AnchorLocation::Url(location.to_string())),
|
|
|
|
span: tag.span,
|
|
|
|
},
|
2019-08-30 20:27:15 +02:00
|
|
|
))
|
|
|
|
}
|
|
|
|
(mime::IMAGE, image_ty) => {
|
|
|
|
let buf: Vec<u8> = r.body_bytes().await.map_err(|_| {
|
|
|
|
ShellError::labeled_error(
|
|
|
|
"Could not load image file",
|
|
|
|
"could not load",
|
2019-10-13 06:12:43 +02:00
|
|
|
&tag,
|
2019-08-30 20:27:15 +02:00
|
|
|
)
|
|
|
|
})?;
|
|
|
|
Ok((
|
|
|
|
Some(image_ty.to_string()),
|
2019-12-04 20:52:31 +01:00
|
|
|
UntaggedValue::binary(buf),
|
2019-10-13 06:12:43 +02:00
|
|
|
Tag {
|
|
|
|
anchor: Some(AnchorLocation::Url(location.to_string())),
|
|
|
|
span: tag.span,
|
|
|
|
},
|
2019-08-30 20:27:15 +02:00
|
|
|
))
|
|
|
|
}
|
|
|
|
(mime::TEXT, mime::HTML) => Ok((
|
|
|
|
Some("html".to_string()),
|
2019-12-04 20:52:31 +01:00
|
|
|
UntaggedValue::string(r.body_string().await.map_err(|_| {
|
2019-08-30 20:27:15 +02:00
|
|
|
ShellError::labeled_error(
|
|
|
|
"Could not load text from remote url",
|
|
|
|
"could not load",
|
2019-10-13 06:12:43 +02:00
|
|
|
&tag,
|
2019-08-30 20:27:15 +02:00
|
|
|
)
|
|
|
|
})?),
|
2019-10-13 06:12:43 +02:00
|
|
|
Tag {
|
|
|
|
anchor: Some(AnchorLocation::Url(location.to_string())),
|
|
|
|
span: tag.span,
|
|
|
|
},
|
2019-08-30 20:27:15 +02:00
|
|
|
)),
|
|
|
|
(mime::TEXT, mime::PLAIN) => {
|
|
|
|
let path_extension = url::Url::parse(location)
|
|
|
|
.unwrap()
|
|
|
|
.path_segments()
|
|
|
|
.and_then(|segments| segments.last())
|
|
|
|
.and_then(|name| if name.is_empty() { None } else { Some(name) })
|
|
|
|
.and_then(|name| {
|
|
|
|
PathBuf::from(name)
|
|
|
|
.extension()
|
|
|
|
.map(|name| name.to_string_lossy().to_string())
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok((
|
|
|
|
path_extension,
|
2019-12-04 20:52:31 +01:00
|
|
|
UntaggedValue::string(r.body_string().await.map_err(|_| {
|
2019-08-30 20:27:15 +02:00
|
|
|
ShellError::labeled_error(
|
|
|
|
"Could not load text from remote url",
|
|
|
|
"could not load",
|
2019-10-13 06:12:43 +02:00
|
|
|
&tag,
|
2019-08-30 20:27:15 +02:00
|
|
|
)
|
|
|
|
})?),
|
2019-10-13 06:12:43 +02:00
|
|
|
Tag {
|
|
|
|
anchor: Some(AnchorLocation::Url(location.to_string())),
|
|
|
|
span: tag.span,
|
|
|
|
},
|
2019-08-30 20:27:15 +02:00
|
|
|
))
|
|
|
|
}
|
|
|
|
(ty, sub_ty) => Ok((
|
|
|
|
None,
|
2019-12-04 20:52:31 +01:00
|
|
|
UntaggedValue::string(format!(
|
2019-08-30 20:27:15 +02:00
|
|
|
"Not yet supported MIME type: {} {}",
|
|
|
|
ty, sub_ty
|
|
|
|
)),
|
2019-10-13 06:12:43 +02:00
|
|
|
Tag {
|
|
|
|
anchor: Some(AnchorLocation::Url(location.to_string())),
|
|
|
|
span: tag.span,
|
|
|
|
},
|
2019-08-30 20:27:15 +02:00
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => Ok((
|
|
|
|
None,
|
2019-12-04 20:52:31 +01:00
|
|
|
UntaggedValue::string(format!("No content type found")),
|
2019-10-13 06:12:43 +02:00
|
|
|
Tag {
|
|
|
|
anchor: Some(AnchorLocation::Url(location.to_string())),
|
|
|
|
span: tag.span,
|
|
|
|
},
|
2019-08-30 20:27:15 +02:00
|
|
|
)),
|
|
|
|
},
|
|
|
|
Err(_) => {
|
|
|
|
return Err(ShellError::labeled_error(
|
|
|
|
"URL could not be opened",
|
|
|
|
"url not found",
|
2019-09-14 18:30:24 +02:00
|
|
|
tag,
|
2019-08-30 20:27:15 +02:00
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Err(ShellError::labeled_error(
|
|
|
|
"Expected a url",
|
|
|
|
"needs a url",
|
2019-09-14 18:30:24 +02:00
|
|
|
tag,
|
2019-08-30 20:27:15 +02:00
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|