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 /license-generator/src/main.rs | |
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
Diffstat (limited to 'license-generator/src/main.rs')
-rw-r--r-- | license-generator/src/main.rs | 75 |
1 files changed, 64 insertions, 11 deletions
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(()) |