diff options
author | Tyler Murphy <tylerm@tylerm.dev> | 2023-07-02 22:40:04 -0400 |
---|---|---|
committer | Tyler Murphy <tylerm@tylerm.dev> | 2023-07-02 22:40:04 -0400 |
commit | 77eee644c9c9bf6e3c19a5c6efb9d31f5d91318e (patch) | |
tree | 9e8ef5086cb7e30057ead2a78b0347bf3341ce46 /src/script.rs | |
parent | add methods to routes, allow custom headers and response codes (diff) | |
download | bashttp-77eee644c9c9bf6e3c19a5c6efb9d31f5d91318e.tar.gz bashttp-77eee644c9c9bf6e3c19a5c6efb9d31f5d91318e.tar.bz2 bashttp-77eee644c9c9bf6e3c19a5c6efb9d31f5d91318e.zip |
move body to stdin
Diffstat (limited to '')
-rw-r--r-- | src/script.rs | 27 |
1 files changed, 21 insertions, 6 deletions
diff --git a/src/script.rs b/src/script.rs index 369fbf8..4a94ba4 100644 --- a/src/script.rs +++ b/src/script.rs @@ -1,6 +1,8 @@ -use std::{env, collections::HashMap, fs::read_to_string, process::Command, result::Result, error::Error}; +use std::{env, collections::HashMap, process::Stdio, fs::read_to_string, result::Result, error::Error}; +use tokio::process::Command; +use tokio::io::AsyncWriteExt; -use crate::http::{response::Response, method::Method}; +use crate::http::{response::Response, method::Method, header::Header}; pub struct Script { path: String @@ -11,17 +13,28 @@ impl Script { Self { path } } - pub fn run(&self, body: Option<&String>) -> Result<Response, std::io::Error> { + pub async fn run(&self, body: Option<&String>) -> Result<Response, String> { let mut res = Response::new(); let mut command = Command::new(&self.path); + command.stdin(Stdio::piped()); + command.stdout(Stdio::piped()); + + let mut child = match command.spawn() { + Ok(child) => child, + Err(err) => return Err(format!("{err}")) + }; + if let Some(body) = body { - command.args([body]); + let Some(mut stdin) = child.stdin.take() else { + return Err("failed to read stdin".into()) + }; + let _ = stdin.write(body.as_bytes()).await; } - let output = match command.output() { + let output = match child.wait_with_output().await { Ok(output) => output, - Err(err) => return Err(err) + Err(err) => return Err(format!("{err}")) }; let status = output.status.code().unwrap_or(500) as u16; @@ -32,6 +45,8 @@ impl Script { res.headers.serialize(&mut lines); let body: String = lines.collect::<Vec<&str>>().join("\n"); + res.headers.put(Header::new("Content-Length", &format!("{}", body.len()))); + if body.len() > 0 { res.body = Some(body); } |