status header
This commit is contained in:
parent
77eee644c9
commit
bdd87a4d54
7 changed files with 16 additions and 19 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -54,7 +54,7 @@ dependencies = [
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bashhttp"
|
name = "bashttp"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"chrono",
|
"chrono",
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
[package]
|
[package]
|
||||||
name = "bashhttp"
|
name = "bashttp"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
|
|
10
README.md
10
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
|
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.
|
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
|
```sh
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
# print headers that will be returned
|
# print headers that will be returned
|
||||||
printf "Content-Type: text/plain\n"
|
printf "Content-Type: text/plain\n"
|
||||||
printf "aaaaaaaa: AAAAAAAAAA\n"
|
printf "aaaaaaaa: AAAAAAAAAA\n"
|
||||||
|
printf "Status: 418\n" # return 418 im a teapot
|
||||||
printf "\n"
|
printf "\n"
|
||||||
|
|
||||||
printf "joe\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:
|
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
|
# the body of any http request made will be stored in stdin
|
||||||
cat
|
cat
|
||||||
|
|
||||||
exit 200
|
|
||||||
```
|
```
|
||||||
## License
|
## License
|
||||||
|
|
||||||
|
|
|
@ -6,5 +6,3 @@ printf "\n"
|
||||||
|
|
||||||
# the body of any http request made will be stored in stdin
|
# the body of any http request made will be stored in stdin
|
||||||
cat
|
cat
|
||||||
|
|
||||||
exit 200
|
|
||||||
|
|
|
@ -5,6 +5,3 @@
|
||||||
printf "\n"
|
printf "\n"
|
||||||
|
|
||||||
neofetch
|
neofetch
|
||||||
|
|
||||||
exit 200
|
|
||||||
|
|
||||||
|
|
|
@ -3,9 +3,7 @@
|
||||||
# print headers that will be returned
|
# print headers that will be returned
|
||||||
printf "Content-Type: text/plain\n"
|
printf "Content-Type: text/plain\n"
|
||||||
printf "aaaaaaaa: AAAAAAAAAA\n"
|
printf "aaaaaaaa: AAAAAAAAAA\n"
|
||||||
|
printf "Status: 418\n" # return 418 im a teapot
|
||||||
printf "\n"
|
printf "\n"
|
||||||
|
|
||||||
printf "joe\n"
|
printf "joe\n"
|
||||||
|
|
||||||
# return http code 200
|
|
||||||
exit 200
|
|
||||||
|
|
|
@ -37,13 +37,21 @@ impl Script {
|
||||||
Err(err) => return Err(format!("{err}"))
|
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 response = String::from_utf8_lossy(&output.stdout).into_owned();
|
||||||
let mut lines = response.split('\n').into_iter();
|
let mut lines = response.split('\n').into_iter();
|
||||||
|
|
||||||
res.headers.serialize(&mut lines);
|
res.headers.serialize(&mut lines);
|
||||||
|
|
||||||
|
let status = match res.headers.get("Status") {
|
||||||
|
Some(header) => {
|
||||||
|
match header.value.parse::<u16>() {
|
||||||
|
Ok(status) => status,
|
||||||
|
Err(err) => return Err(format!("{err}"))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
None => 200
|
||||||
|
};
|
||||||
|
|
||||||
let body: String = lines.collect::<Vec<&str>>().join("\n");
|
let body: String = lines.collect::<Vec<&str>>().join("\n");
|
||||||
res.headers.put(Header::new("Content-Length", &format!("{}", body.len())));
|
res.headers.put(Header::new("Content-Length", &format!("{}", body.len())));
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue