diff --git a/Cargo.lock b/Cargo.lock index ff7e4a7..77129e9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -54,7 +54,7 @@ dependencies = [ ] [[package]] -name = "bashhttp" +name = "bashttp" version = "0.1.0" dependencies = [ "chrono", diff --git a/Cargo.toml b/Cargo.toml index 0e5451e..4ecdc96 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "bashhttp" +name = "bashttp" version = "0.1.0" edition = "2021" diff --git a/README.md b/README.md index e6b7580..d66fa32 100644 --- a/README.md +++ b/README.md @@ -21,21 +21,19 @@ Anything written to a scripts stdout will be treated as part of the HTTP Respons Therefore the first things returned will be treated as headers. To stop reading headers, print a `\n` to stdout to tell the HTTP Response that the headers have ended. -All content after the headers will be treated as the response body, and the scripts exit code will be treated as the http response code. +All content after the headers will be treated as the response body, and if the Status header is given, it will be treated as the http response code. -An example script that gives custom headers is shown below: +An example script that gives custom headers (with res code) is shown below: ```sh #!/bin/bash # print headers that will be returned printf "Content-Type: text/plain\n" printf "aaaaaaaa: AAAAAAAAAA\n" +printf "Status: 418\n" # return 418 im a teapot printf "\n" printf "joe\n" - -# return http code 200 -exit 200 ``` Finally if you wish to read the request body given by the user, that is always stored in stdin. For example to create a echo endpoint: @@ -48,8 +46,6 @@ printf "\n" # the body of any http request made will be stored in stdin cat - -exit 200 ``` ## License diff --git a/examples/body.sh b/examples/body.sh index bf671df..afb98c2 100755 --- a/examples/body.sh +++ b/examples/body.sh @@ -6,5 +6,3 @@ printf "\n" # the body of any http request made will be stored in stdin cat - -exit 200 diff --git a/examples/command.sh b/examples/command.sh index 64e159b..5c31058 100755 --- a/examples/command.sh +++ b/examples/command.sh @@ -5,6 +5,3 @@ printf "\n" neofetch - -exit 200 - diff --git a/examples/headers.sh b/examples/headers.sh index 32c7a53..2907ab0 100755 --- a/examples/headers.sh +++ b/examples/headers.sh @@ -3,9 +3,7 @@ # print headers that will be returned printf "Content-Type: text/plain\n" printf "aaaaaaaa: AAAAAAAAAA\n" +printf "Status: 418\n" # return 418 im a teapot printf "\n" printf "joe\n" - -# return http code 200 -exit 200 diff --git a/src/script.rs b/src/script.rs index 4a94ba4..064bc67 100644 --- a/src/script.rs +++ b/src/script.rs @@ -37,13 +37,21 @@ impl Script { Err(err) => return Err(format!("{err}")) }; - let status = output.status.code().unwrap_or(500) as u16; - let response = String::from_utf8_lossy(&output.stdout).into_owned(); let mut lines = response.split('\n').into_iter(); res.headers.serialize(&mut lines); + let status = match res.headers.get("Status") { + Some(header) => { + match header.value.parse::() { + Ok(status) => status, + Err(err) => return Err(format!("{err}")) + } + }, + None => 200 + }; + let body: String = lines.collect::>().join("\n"); res.headers.put(Header::new("Content-Length", &format!("{}", body.len())));