nushell/crates/nu_plugin_post/src/main.rs

570 lines
21 KiB
Rust
Raw Normal View History

2019-08-30 20:27:15 +02:00
use base64::encode;
2019-12-07 04:46:05 +01:00
use futures::executor::block_on;
2019-08-30 20:27:15 +02:00
use mime::Mime;
2019-12-07 04:46:05 +01:00
use nu_errors::{CoerceInto, ShellError};
use nu_plugin::{serve_plugin, Plugin};
use nu_protocol::{
CallInfo, CommandAction, Primitive, ReturnSuccess, ReturnValue, Signature, SyntaxShape,
UnspannedPathMember, UntaggedValue, Value,
};
2019-12-07 04:46:05 +01:00
use nu_source::{AnchorLocation, Tag, TaggedItem};
use num_traits::cast::ToPrimitive;
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;
#[derive(Clone)]
2019-09-29 04:03:10 +02:00
pub enum HeaderKind {
ContentType(String),
ContentLength(String),
}
struct Post {
path: Option<Value>,
has_raw: bool,
body: Option<Value>,
user: Option<String>,
password: Option<String>,
headers: Vec<HeaderKind>,
2020-01-01 21:45:32 +01:00
tag: Tag,
}
2019-12-07 04:46:05 +01:00
impl Post {
fn new() -> Post {
Post {
path: None,
has_raw: false,
body: None,
user: None,
password: None,
headers: vec![],
2020-01-01 21:45:32 +01:00
tag: Tag::unknown(),
}
}
fn setup(&mut self, call_info: CallInfo) -> ReturnValue {
self.path = Some(
match call_info.args.nth(0).ok_or_else(|| {
ShellError::labeled_error(
"No file or directory specified",
"for command",
&call_info.name_tag,
)
})? {
file => file.clone(),
},
);
self.has_raw = call_info.args.has("raw");
self.body = match call_info.args.nth(1).ok_or_else(|| {
ShellError::labeled_error("No body specified", "for command", &call_info.name_tag)
})? {
file => Some(file.clone()),
};
2020-01-01 21:45:32 +01:00
self.user = match call_info.args.get("user") {
Some(user) => Some(user.as_string()?),
None => None,
};
2020-01-01 21:45:32 +01:00
self.password = match call_info.args.get("password") {
Some(password) => Some(password.as_string()?),
None => None,
};
self.headers = get_headers(&call_info)?;
2020-01-01 21:45:32 +01:00
self.tag = call_info.name_tag;
ReturnSuccess::value(UntaggedValue::nothing().into_untagged_value())
2019-08-30 20:27:15 +02:00
}
2019-12-07 04:46:05 +01:00
}
2019-08-30 20:27:15 +02:00
2019-12-07 04:46:05 +01:00
impl Plugin for Post {
fn config(&mut self) -> Result<Signature, ShellError> {
Ok(Signature::build("post")
.desc("Post content to a url and retrieve data as a table if possible.")
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-12-07 04:46:05 +01:00
.filter())
2019-08-30 20:27:15 +02:00
}
fn begin_filter(&mut self, call_info: CallInfo) -> Result<Vec<ReturnValue>, ShellError> {
self.setup(call_info)?;
Ok(vec![])
}
fn filter(&mut self, row: Value) -> Result<Vec<ReturnValue>, ShellError> {
Ok(vec![block_on(post_helper(
2020-01-01 21:45:32 +01:00
&self.path.clone().ok_or_else(|| {
ShellError::labeled_error("expected a 'path'", "expected a 'path'", &self.tag)
})?,
self.has_raw,
2020-01-01 21:45:32 +01:00
&self.body.clone().ok_or_else(|| {
ShellError::labeled_error("expected a 'body'", "expected a 'body'", &self.tag)
})?,
self.user.clone(),
self.password.clone(),
&self.headers.clone(),
row,
))])
2019-08-30 20:27:15 +02:00
}
}
2019-12-07 04:46:05 +01:00
fn main() {
serve_plugin(&mut Post::new());
}
async fn post_helper(
path: &Value,
has_raw: bool,
body: &Value,
user: Option<String>,
password: Option<String>,
headers: &[HeaderKind],
row: Value,
) -> ReturnValue {
let path_tag = path.tag.clone();
let path_str = path.as_string()?.to_string();
//FIXME: this is a workaround because plugins don't yet support per-item iteration
let path_str = if path_str == "$it" {
let path_buf = row.as_path()?;
path_buf.display().to_string()
} else {
path_str
};
//FIXME: this is a workaround because plugins don't yet support per-item iteration
let body = if let Ok(x) = body.as_string() {
if x == "$it" {
&row
} else {
body
}
} else {
body
};
2019-09-29 04:03:10 +02:00
2019-12-07 04:46:05 +01:00
let (file_extension, contents, contents_tag) =
2020-01-01 21:45:32 +01:00
post(&path_str, &body, user, password, &headers, path_tag.clone()).await?;
2019-08-30 20:27:15 +02:00
2019-12-07 04:46:05 +01: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_else(|| path_str.split('.').last().map(String::from))
};
2019-12-07 04:46:05 +01:00
let tagged_contents = contents.into_value(&contents_tag);
2019-12-07 04:46:05 +01:00
if let Some(extension) = file_extension {
Ok(ReturnSuccess::Action(CommandAction::AutoConvert(
2019-12-07 04:46:05 +01:00
tagged_contents,
extension,
)))
2019-12-07 04:46:05 +01:00
} else {
ReturnSuccess::value(tagged_contents)
}
}
2019-08-30 20:27:15 +02:00
pub async fn post(
location: &str,
body: &Value,
2019-08-30 20:27:15 +02:00
user: Option<String>,
password: Option<String>,
headers: &[HeaderKind],
tag: Tag,
) -> Result<(Option<String>, UntaggedValue, Tag), ShellError> {
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 {
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
}
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
}
Value { value, tag } => {
2019-12-07 04:46:05 +01:00
match value_to_json_value(&value.clone().into_untagged_value()) {
Ok(json_value) => match serde_json::to_string(&json_value) {
Ok(result_string) => {
let mut s = surf::post(location).body_string(result_string);
if let Some(login) = login {
s = s.set_header("Authorization", format!("Basic {}", login));
2019-08-31 06:08:59 +02:00
}
2019-12-07 04:46:05 +01:00
s.await
2019-08-31 06:08:59 +02:00
}
2019-12-07 04:46:05 +01:00
_ => {
return Err(ShellError::labeled_error(
"Could not automatically convert table",
"needs manual conversion",
tag,
));
}
},
_ => {
return Err(ShellError::labeled_error(
"Could not automatically convert table",
"needs manual conversion",
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) => {
2020-01-01 21:45:32 +01:00
let content_type = Mime::from_str(content_type).map_err(|_| {
ShellError::labeled_error(
format!("Unknown MIME type: {}", content_type),
"unknown MIME type",
&tag,
)
})?;
2019-08-30 20:27:15 +02:00
match (content_type.type_(), content_type.subtype()) {
(mime::APPLICATION, mime::XML) => Ok((
Some("xml".to_string()),
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",
&tag,
2019-08-30 20:27:15 +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()),
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",
&tag,
2019-08-30 20:27:15 +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",
&tag,
2019-08-30 20:27:15 +02:00
)
})?;
Ok((
None,
UntaggedValue::binary(buf),
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",
&tag,
2019-08-30 20:27:15 +02:00
)
})?;
Ok((
Some(image_ty.to_string()),
UntaggedValue::binary(buf),
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()),
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",
&tag,
2019-08-30 20:27:15 +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)
2020-01-01 21:45:32 +01:00
.map_err(|_| {
ShellError::labeled_error(
format!("could not parse URL: {}", location),
"could not parse URL",
&tag,
)
})?
2019-08-30 20:27:15 +02:00
.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,
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",
&tag,
2019-08-30 20:27:15 +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,
UntaggedValue::string(format!(
2019-08-30 20:27:15 +02:00
"Not yet supported MIME type: {} {}",
ty, sub_ty
)),
Tag {
anchor: Some(AnchorLocation::Url(location.to_string())),
span: tag.span,
},
2019-08-30 20:27:15 +02:00
)),
}
}
None => Ok((
None,
UntaggedValue::string("No content type found".to_owned()),
Tag {
anchor: Some(AnchorLocation::Url(location.to_string())),
span: tag.span,
},
2019-08-30 20:27:15 +02:00
)),
},
Err(_) => Err(ShellError::labeled_error(
"URL could not be opened",
"url not found",
tag,
)),
2019-08-30 20:27:15 +02:00
}
} else {
Err(ShellError::labeled_error(
"Expected a url",
"needs a url",
tag,
2019-08-30 20:27:15 +02:00
))
}
}
2019-12-07 04:46:05 +01:00
// FIXME FIXME FIXME
// Ultimately, we don't want to duplicate to-json here, but we need to because there isn't an easy way to call into it, yet
pub fn value_to_json_value(v: &Value) -> Result<serde_json::Value, ShellError> {
Ok(match &v.value {
UntaggedValue::Primitive(Primitive::Boolean(b)) => serde_json::Value::Bool(*b),
UntaggedValue::Primitive(Primitive::Bytes(b)) => serde_json::Value::Number(
serde_json::Number::from(b.to_u64().expect("What about really big numbers")),
),
UntaggedValue::Primitive(Primitive::Duration(secs)) => {
serde_json::Value::Number(serde_json::Number::from(*secs))
}
UntaggedValue::Primitive(Primitive::Date(d)) => serde_json::Value::String(d.to_string()),
UntaggedValue::Primitive(Primitive::EndOfStream) => serde_json::Value::Null,
UntaggedValue::Primitive(Primitive::BeginningOfStream) => serde_json::Value::Null,
UntaggedValue::Primitive(Primitive::Decimal(f)) => serde_json::Value::Number(
serde_json::Number::from_f64(
f.to_f64().expect("TODO: What about really big decimals?"),
)
2020-01-01 21:45:32 +01:00
.ok_or_else(|| {
ShellError::labeled_error(
"Can not convert big decimal to f64",
"cannot convert big decimal to f64",
&v.tag,
)
})?,
2019-12-07 04:46:05 +01:00
),
UntaggedValue::Primitive(Primitive::Int(i)) => {
serde_json::Value::Number(serde_json::Number::from(CoerceInto::<i64>::coerce_into(
i.tagged(&v.tag),
"converting to JSON number",
)?))
}
UntaggedValue::Primitive(Primitive::Nothing) => serde_json::Value::Null,
UntaggedValue::Primitive(Primitive::Pattern(s)) => serde_json::Value::String(s.clone()),
UntaggedValue::Primitive(Primitive::String(s)) => serde_json::Value::String(s.clone()),
UntaggedValue::Primitive(Primitive::Line(s)) => serde_json::Value::String(s.clone()),
UntaggedValue::Primitive(Primitive::ColumnPath(path)) => serde_json::Value::Array(
path.iter()
.map(|x| match &x.unspanned {
UnspannedPathMember::String(string) => {
Ok(serde_json::Value::String(string.clone()))
}
UnspannedPathMember::Int(int) => Ok(serde_json::Value::Number(
serde_json::Number::from(CoerceInto::<i64>::coerce_into(
int.tagged(&v.tag),
"converting to JSON number",
)?),
)),
})
.collect::<Result<Vec<serde_json::Value>, ShellError>>()?,
),
UntaggedValue::Primitive(Primitive::Path(s)) => {
serde_json::Value::String(s.display().to_string())
}
UntaggedValue::Table(l) => serde_json::Value::Array(json_list(l)?),
UntaggedValue::Error(e) => return Err(e.clone()),
Add Range and start Signature support This commit contains two improvements: - Support for a Range syntax (and a corresponding Range value) - Work towards a signature syntax Implementing the Range syntax resulted in cleaning up how operators in the core syntax works. There are now two kinds of infix operators - tight operators (`.` and `..`) - loose operators Tight operators may not be interspersed (`$it.left..$it.right` is a syntax error). Loose operators require whitespace on both sides of the operator, and can be arbitrarily interspersed. Precedence is left to right in the core syntax. Note that delimited syntax (like `( ... )` or `[ ... ]`) is a single token node in the core syntax. A single token node can be parsed from beginning to end in a context-free manner. The rule for `.` is `<token node>.<member>`. The rule for `..` is `<token node>..<token node>`. Loose operators all have the same syntactic rule: `<token node><space><loose op><space><token node>`. The second aspect of this pull request is the beginning of support for a signature syntax. Before implementing signatures, a necessary prerequisite is for the core syntax to support multi-line programs. That work establishes a few things: - `;` and newlines are handled in the core grammar, and both count as "separators" - line comments begin with `#` and continue until the end of the line In this commit, multi-token productions in the core grammar can use separators interchangably with spaces. However, I think we will ultimately want a different rule preventing separators from occurring before an infix operator, so that the end of a line is always unambiguous. This would avoid gratuitous differences between modules and repl usage. We already effectively have this rule, because otherwise `x<newline> | y` would be a single pipeline, but of course that wouldn't work.
2019-12-04 22:14:52 +01:00
UntaggedValue::Block(_) | UntaggedValue::Primitive(Primitive::Range(_)) => {
serde_json::Value::Null
}
2020-01-01 21:45:32 +01:00
UntaggedValue::Primitive(Primitive::Binary(b)) => {
let mut output = vec![];
for item in b.iter() {
output.push(serde_json::Value::Number(
serde_json::Number::from_f64(*item as f64).ok_or_else(|| {
ShellError::labeled_error(
"Cannot create number from from f64",
"cannot created number from f64",
&v.tag,
)
})?,
));
}
serde_json::Value::Array(output)
}
2019-12-07 04:46:05 +01:00
UntaggedValue::Row(o) => {
let mut m = serde_json::Map::new();
for (k, v) in o.entries.iter() {
m.insert(k.clone(), value_to_json_value(v)?);
}
serde_json::Value::Object(m)
}
})
}
fn json_list(input: &[Value]) -> Result<Vec<serde_json::Value>, ShellError> {
2019-12-07 04:46:05 +01:00
let mut out = vec![];
for value in input {
out.push(value_to_json_value(value)?);
}
Ok(out)
}
fn get_headers(call_info: &CallInfo) -> Result<Vec<HeaderKind>, ShellError> {
let mut headers = vec![];
match extract_header_value(&call_info, "content-type") {
Ok(h) => {
if let Some(ct) = h {
headers.push(HeaderKind::ContentType(ct))
}
}
2019-12-07 04:46:05 +01:00
Err(e) => {
return Err(e);
}
};
match extract_header_value(&call_info, "content-length") {
Ok(h) => {
if let Some(cl) = h {
headers.push(HeaderKind::ContentLength(cl))
}
}
2019-12-07 04:46:05 +01:00
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) {
let tagged = call_info.args.get(key);
let val = match tagged {
Some(Value {
value: UntaggedValue::Primitive(Primitive::String(s)),
..
}) => s.clone(),
Some(Value { tag, .. }) => {
return Err(ShellError::labeled_error(
format!("{} not in expected format. Expected string.", key),
"post error",
tag,
));
}
_ => {
return Err(ShellError::labeled_error(
format!("{} not in expected format. Expected string.", key),
"post error",
Tag::unknown(),
));
}
};
return Ok(Some(val));
}
Ok(None)
}