diff options
Diffstat (limited to 'examples/csrf')
-rw-r--r-- | examples/csrf/src/main.rs | 18 |
1 files changed, 8 insertions, 10 deletions
diff --git a/examples/csrf/src/main.rs b/examples/csrf/src/main.rs index e7e1bfa..94fd09c 100644 --- a/examples/csrf/src/main.rs +++ b/examples/csrf/src/main.rs @@ -26,29 +26,27 @@ fn render_error(err: Error) -> (StatusCode, String) { async fn route(req: &mut Parts, body: Body) -> Result<Response, Error> { match (&req.method, req.uri.path()) { - (&Method::GET, "/form") => get_form(req).await, + (&Method::GET, "/form") => Ok(get_form(req)), (&Method::POST, "/form") => post_form(req, body).await, _ => return Err(Error::NotFound("page not found".to_owned())) } } -async fn get_form(req: &mut Parts) -> Result<Response, Error> { +fn get_form(req: &mut Parts) -> Response { let mut response = Builder::new(); - let csrf_token = req.csrf_token(&mut response); - Ok(response.content_type(mime::TEXT_HTML).body( + let csrf_input = req.csrf_html_input(&mut response); + response.content_type(mime::TEXT_HTML).body( format!("<form method=post> - <input name=text>{}<button>Submit</button></form>", csrf_token.html_input()).into() - ).unwrap()) + <input name=text>{}<button>Submit</button></form>", csrf_input).into() + ).unwrap() } #[derive(Deserialize)] struct FormData {text: String} async fn post_form(req: &mut Parts, body: Body) -> Result<Response, Error> { - let mut response = Builder::new(); - let csrf_token = req.csrf_token(&mut response); - let msg: FormData = body.into_form_csrf(&csrf_token).await?; - Ok(response.body( + let msg: FormData = body.into_form_csrf(req).await?; + Ok(Builder::new().body( format!("hello {}", msg.text).into() ).unwrap()) } |