diff options
author | Teddy Wing | 2018-11-10 23:49:20 +0100 |
---|---|---|
committer | Teddy Wing | 2018-11-10 23:49:20 +0100 |
commit | 86e5b666e5fea3e037734f8aacb0537a365518ab (patch) | |
tree | cac20aadfaa38919c37f76af9aa7d6d6352b2cbd | |
parent | 20f7a268cce05cd63842adbc7a5d05d4d4cc5bc7 (diff) | |
download | dome-key-web-86e5b666e5fea3e037734f8aacb0537a365518ab.tar.bz2 |
main(): Set up HTTP responses
* If no `REQUEST_METHOD` is found, send a 500 error
* If the `REQUEST_METHOD` is not "POST", send a 405
* If POST params could not be read from stdin, send 500
* If an error occurred during request verification, send 500
* If the request didn't pass verification, send 403
* Otherwise send 200
-rw-r--r-- | license-generator/src/lib.rs | 1 | ||||
-rw-r--r-- | license-generator/src/main.rs | 75 | ||||
-rw-r--r-- | license-generator/src/response.rs | 21 |
3 files changed, 86 insertions, 11 deletions
diff --git a/license-generator/src/lib.rs b/license-generator/src/lib.rs index bf3ed57..407097a 100644 --- a/license-generator/src/lib.rs +++ b/license-generator/src/lib.rs @@ -14,3 +14,4 @@ pub mod database; pub mod errors; pub mod purchaser; pub mod request; +pub mod response; diff --git a/license-generator/src/main.rs b/license-generator/src/main.rs index ccf2a73..bd8b192 100644 --- a/license-generator/src/main.rs +++ b/license-generator/src/main.rs @@ -17,6 +17,7 @@ use license_generator::database; use license_generator::errors::*; use license_generator::purchaser::Purchaser; use license_generator::request; +use license_generator::response; fn main() -> Result<()> { let log_file_path = env::var("LOG_FILE") @@ -48,23 +49,75 @@ fn main() -> Result<()> { } fastcgi::run(|mut req| { - write!(&mut req.stdout(), "Content-Type: text/plain\n\nHello, world!") - .unwrap_or(()); + match req.param("REQUEST_METHOD") { + Some(method) => { + if method != "POST" { + response::set_405(&mut req.stdout(), "POST") + .unwrap_or(()); + write!(&mut req.stdout(), "Content-Type: text/plain + +405 Method Not Allowed") + .unwrap_or(()); + + return; + } + }, + None => { + response::set_500(&mut req.stdout()).unwrap_or(()); + write!(&mut req.stdout(), "Content-Type: text/plain + +500 Internal Server Error") + .unwrap_or(()); + + return; + }, + }; + + let mut stdin = String::new(); + match req.stdin().read_to_string(&mut stdin) { + Ok(_) => (), + Err(e) => { + error!("{}", e); + + response::set_500(&mut req.stdout()).unwrap_or(()); + write!(&mut req.stdout(), "Content-Type: text/plain - let mut params = String::new(); - for (key, val) in req.params() { - params.push_str(format!("{}: {}\n", key, val).as_str()); +500 Internal Server Error") + .unwrap_or(()); + + return; + }, } - info!("{}", params); + let is_verified = match request::verified(&stdin) { + Ok(v) => v, + Err(e) => { + error!("{}", e); - let mut stdin = String::new(); - req.stdin().read_to_string(&mut stdin).unwrap(); + response::set_500(&mut req.stdout()).unwrap_or(()); + write!(&mut req.stdout(), "Content-Type: text/plain + +500 Internal Server Error") + .unwrap_or(()); - info!("{}", stdin); + return; + }, + }; - let is_verified = request::verified(&stdin); - info!("{:?}", is_verified); + if !is_verified { + response::set_403(&mut req.stdout()).unwrap_or(()); + write!(&mut req.stdout(), "Content-Type: text/plain + +403 Forbidden: Invalid request signature") + .unwrap_or(()); + + return; + } + + write!(&mut req.stdout(), "Content-Type: text/plain + +200 OK") + .unwrap_or(()); }); Ok(()) diff --git a/license-generator/src/response.rs b/license-generator/src/response.rs new file mode 100644 index 0000000..701bf1a --- /dev/null +++ b/license-generator/src/response.rs @@ -0,0 +1,21 @@ +use std::io::Write; + +use errors::*; + +pub fn set_403<W: Write>(w: &mut W) -> Result<()> { + Ok(writeln!(w, "Status: 403")?) +} + +pub fn set_405<W: Write>(w: &mut W, allowed_methods: &str) -> Result<()> { + Ok( + writeln!( + w, + "Status: 405 +Allow: {}", + allowed_methods)? + ) +} + +pub fn set_500<W: Write>(w: &mut W) -> Result<()> { + Ok(writeln!(w, "Status: 500")?) +} |