1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
use crate::formats::value_to_json_value;
use crate::BufferedReader;
use base64::encode;
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::RawStream;
use reqwest::{blocking::Response, StatusCode};
use std::path::PathBuf;
use std::str::FromStr;
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Value,
};
use std::collections::HashMap;
use std::io::BufReader;
#[derive(Clone)]
pub struct SubCommand;
impl Command for SubCommand {
fn name(&self) -> &str {
"post"
}
fn signature(&self) -> Signature {
Signature::build("post")
.required("path", SyntaxShape::String, "the URL to post to")
.required("body", SyntaxShape::Any, "the contents of the post body")
.named(
"user",
SyntaxShape::Any,
"the username when authenticating",
Some('u'),
)
.named(
"password",
SyntaxShape::Any,
"the password when authenticating",
Some('p'),
)
.named(
"content-type",
SyntaxShape::Any,
"the MIME type of content to post",
Some('t'),
)
.named(
"content-length",
SyntaxShape::Any,
"the length of the content being posted",
Some('l'),
)
.named(
"headers",
SyntaxShape::Any,
"custom headers you want to add ",
Some('H'),
)
.switch(
"raw",
"return values as a string instead of a table",
Some('r'),
)
.switch(
"insecure",
"allow insecure server connections when using SSL",
Some('k'),
)
.filter()
.category(Category::Network)
}
fn usage(&self) -> &str {
"Post a body to a URL."
}
fn extra_usage(&self) -> &str {
"Performs HTTP POST operation."
}
fn search_terms(&self) -> Vec<&str> {
vec!["network", "post", "send", "push", "http"]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
run_post(engine_state, stack, call, input)
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Post content to url.com",
example: "post url.com 'body'",
result: None,
},
Example {
description: "Post content to url.com, with username and password",
example: "post -u myuser -p mypass url.com 'body'",
result: None,
},
Example {
description: "Post content to url.com, with custom header",
example: "post -H [my-header-key my-header-value] url.com",
result: None,
},
Example {
description: "Post content to url.com with a json body",
example: "post -t application/json url.com { field: value }",
result: None,
},
]
}
}
struct Arguments {
path: Option<Value>,
body: Option<Value>,
headers: Option<Value>,
raw: bool,
insecure: Option<bool>,
user: Option<String>,
password: Option<String>,
content_type: Option<String>,
content_length: Option<String>,
}
#[derive(PartialEq, Eq)]
enum BodyType {
Json,
Form,
Unknown,
}
fn run_post(
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let args = Arguments {
path: Some(call.req(engine_state, stack, 0)?),
body: Some(call.req(engine_state, stack, 1)?),
headers: call.get_flag(engine_state, stack, "headers")?,
raw: call.has_flag("raw"),
user: call.get_flag(engine_state, stack, "user")?,
password: call.get_flag(engine_state, stack, "password")?,
insecure: call.get_flag(engine_state, stack, "insecure")?,
content_type: call.get_flag(engine_state, stack, "content-type")?,
content_length: call.get_flag(engine_state, stack, "content-length")?,
};
helper(engine_state, stack, call, args)
}
fn helper(
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
args: Arguments,
) -> std::result::Result<PipelineData, ShellError> {
let url_value = if let Some(val) = args.path {
val
} else {
return Err(ShellError::UnsupportedInput(
"Expecting a URL as a string but got nothing".to_string(),
call.head,
));
};
let body = if let Some(body) = args.body {
body
} else {
return Err(ShellError::UnsupportedInput(
"Expecting a body parameter but got nothing".to_string(),
call.head,
));
};
let span = url_value.span()?;
let requested_url = url_value.as_string()?;
let url = match url::Url::parse(&requested_url) {
Ok(u) => u,
Err(_e) => {
return Err(ShellError::UnsupportedInput(
"Incomplete or incorrect URL. Expected a full URL, e.g., https://www.example.com"
.to_string(),
span,
));
}
};
let user = args.user.clone();
let password = args.password;
let headers = args.headers;
let location = url;
let raw = args.raw;
let login = match (user, password) {
(Some(user), Some(password)) => Some(encode(&format!("{}:{}", user, password))),
(Some(user), _) => Some(encode(&format!("{}:", user))),
_ => None,
};
let body_type = match &args.content_type {
Some(it) if it == "application/json" => BodyType::Json,
Some(it) if it == "application/x-www-form-urlencoded" => BodyType::Form,
_ => BodyType::Unknown,
};
let mut request = http_client(args.insecure.is_some()).post(location);
if let Some(val) = args.content_type {
request = request.header("Content-Type", val);
}
match body {
Value::Binary { val, .. } => {
request = request.body(val);
}
Value::String { val, .. } => {
request = request.body(val);
}
Value::Record { .. } if body_type == BodyType::Json => {
let data = value_to_json_value(&body)?;
request = request.json(&data);
}
Value::Record { .. } if body_type == BodyType::Form => {
let data = value_to_json_value(&body)?;
request = request.form(&data);
}
Value::List { vals, .. } if body_type == BodyType::Form => {
if vals.len() % 2 != 0 {
return Err(ShellError::IOError("unsupported body input".into()));
}
let data = vals
.chunks(2)
.map(|it| Ok((it[0].as_string()?, it[1].as_string()?)))
.collect::<Result<Vec<(String, String)>, ShellError>>()?;
request = request.form(&data)
}
_ => {
return Err(ShellError::IOError("unsupported body input".into()));
}
};
if let Some(val) = args.content_length {
request = request.header("Content-Length", val);
}
if let Some(login) = login {
request = request.header("Authorization", format!("Basic {}", login));
}
if let Some(headers) = headers {
let mut custom_headers: HashMap<String, Value> = HashMap::new();
match &headers {
Value::List { vals: table, .. } => {
if table.len() == 1 {
match &table[0] {
Value::Record { cols, vals, .. } => {
for (k, v) in cols.iter().zip(vals.iter()) {
custom_headers.insert(k.to_string(), v.clone());
}
}
x => {
return Err(ShellError::CantConvert(
"string list or single row".into(),
x.get_type().to_string(),
headers.span().unwrap_or_else(|_| Span::new(0, 0)),
None,
));
}
}
} else {
for row in table.chunks(2) {
if row.len() == 2 {
custom_headers.insert(row[0].as_string()?, (&row[1]).clone());
}
}
}
}
x => {
return Err(ShellError::CantConvert(
"string list or single row".into(),
x.get_type().to_string(),
headers.span().unwrap_or_else(|_| Span::new(0, 0)),
None,
));
}
};
for (k, v) in &custom_headers {
if let Ok(s) = v.as_string() {
request = request.header(k, s);
}
}
}
match request.send() {
Ok(resp) => match resp.headers().get("content-type") {
Some(content_type) => {
let content_type = content_type.to_str().map_err(|e| {
ShellError::GenericError(
e.to_string(),
"".to_string(),
None,
Some("MIME type were invalid".to_string()),
Vec::new(),
)
})?;
let content_type = mime::Mime::from_str(content_type).map_err(|_| {
ShellError::GenericError(
format!("MIME type unknown: {}", content_type),
"".to_string(),
None,
Some("given unknown MIME type".to_string()),
Vec::new(),
)
})?;
let ext = match (content_type.type_(), content_type.subtype()) {
(mime::TEXT, mime::PLAIN) => {
let path_extension = url::Url::parse(&requested_url)
.map_err(|_| {
ShellError::GenericError(
format!("Cannot parse URL: {}", requested_url),
"".to_string(),
None,
Some("cannot parse".to_string()),
Vec::new(),
)
})?
.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())
});
path_extension
}
_ => Some(content_type.subtype().to_string()),
};
let output = response_to_buffer(resp, engine_state, span);
if raw {
return Ok(output);
}
if let Some(ext) = ext {
match engine_state.find_decl(format!("from {}", ext).as_bytes(), &[]) {
Some(converter_id) => engine_state.get_decl(converter_id).run(
engine_state,
stack,
&Call::new(span),
output,
),
None => Ok(output),
}
} else {
Ok(output)
}
}
None => Ok(response_to_buffer(resp, engine_state, span)),
},
Err(e) if e.is_status() => match e.status() {
Some(err_code) if err_code == StatusCode::NOT_FOUND => Err(ShellError::NetworkFailure(
format!("Requested file not found (404): {:?}", requested_url),
span,
)),
Some(err_code) if err_code == StatusCode::MOVED_PERMANENTLY => {
Err(ShellError::NetworkFailure(
format!("Resource moved permanently (301): {:?}", requested_url),
span,
))
}
Some(err_code) if err_code == StatusCode::BAD_REQUEST => {
Err(ShellError::NetworkFailure(
format!("Bad request (400) to {:?}", requested_url),
span,
))
}
Some(err_code) if err_code == StatusCode::FORBIDDEN => Err(ShellError::NetworkFailure(
format!("Access forbidden (403) to {:?}", requested_url),
span,
)),
_ => Err(ShellError::NetworkFailure(
format!(
"Cannot make request to {:?}. Error is {:?}",
requested_url,
e.to_string()
),
span,
)),
},
Err(e) => Err(ShellError::NetworkFailure(
format!(
"Cannot make request to {:?}. Error is {:?}",
requested_url,
e.to_string()
),
span,
)),
}
}
fn response_to_buffer(
response: Response,
engine_state: &EngineState,
span: Span,
) -> nu_protocol::PipelineData {
let buffered_input = BufReader::new(response);
PipelineData::ExternalStream {
stdout: Some(RawStream::new(
Box::new(BufferedReader {
input: buffered_input,
}),
engine_state.ctrlc.clone(),
span,
)),
stderr: None,
exit_code: None,
span,
metadata: None,
}
}
#[allow(clippy::unwrap_used)]
fn http_client(allow_insecure: bool) -> reqwest::blocking::Client {
reqwest::blocking::Client::builder()
.user_agent("nushell")
.danger_accept_invalid_certs(allow_insecure)
.build()
.expect("Failed to build reqwest client")
}