summaryrefslogtreecommitdiffstats
path: root/src/http
diff options
context:
space:
mode:
Diffstat (limited to 'src/http')
-rw-r--r--src/http/mod.rs14
1 files changed, 11 insertions, 3 deletions
diff --git a/src/http/mod.rs b/src/http/mod.rs
index 24da43f..81c526c 100644
--- a/src/http/mod.rs
+++ b/src/http/mod.rs
@@ -36,17 +36,22 @@ pub struct Req {
pub body: String,
pub cookies: Vec<(String, String)>,
pub query_pairs: Vec<(String, String)>,
+ /// The body's form pairs, parsed once when the request is decoded (n-3).
+ /// `form_get` used to re-parse the whole body on every call, and a single
+ /// handler — approve, with its two hashes, its note and its CSRF token —
+ /// asks five times.
+ pub form_pairs: Vec<(String, String)>,
/// Whether the client said it can take gzip. Used to decide whether to hand
/// out the precompressed static asset.
pub accept_gzip: bool,
}
impl Req {
- pub fn form(&self) -> Vec<(String, String)> {
- forms::parse(&self.body)
+ pub fn form(&self) -> &[(String, String)] {
+ &self.form_pairs
}
pub fn form_get(&self, key: &str) -> Option<String> {
- self.form().into_iter().find(|(k, _)| k == key).map(|(_, v)| v)
+ self.form_pairs.iter().find(|(k, _)| k == key).map(|(_, v)| v.clone())
}
pub fn query_get(&self, key: &str) -> Option<String> {
self.query_pairs.iter().find(|(k, _)| k == key).map(|(_, v)| v.clone())
@@ -162,6 +167,8 @@ fn serve(state: Arc<State>, mut request: tiny_http::Request) -> std::io::Result<
.find(|h| h.field.equiv("Accept-Encoding"))
.map(|h| h.value.as_str().contains("gzip"))
.unwrap_or(false);
+ // Parsed once, here, rather than per `form_get` call (n-3).
+ let form_pairs = forms::parse(&body);
let req = Req {
method: request.method().as_str().to_string(),
path: percent_decode(&path),
@@ -169,6 +176,7 @@ fn serve(state: Arc<State>, mut request: tiny_http::Request) -> std::io::Result<
body,
cookies,
query_pairs: forms::parse(&query),
+ form_pairs,
accept_gzip,
};