aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Fischer <martin@push-f.com>2021-01-26 15:37:01 +0100
committerMartin Fischer <martin@push-f.com>2021-01-26 15:48:17 +0100
commit4502efc51474cdb7ffd0d77402f62aed8badf7bf (patch)
tree4ef9b5f6c80f5976b06b637b7141ccf3257d5ebc
parentfc15b41a37e123434ec39a277f107b78c1507bd8 (diff)
response: improve cookie & content-type methods
* add SputnikBuilder::set_cookie * make delete_cookie an independent method * add SputnikHeaders::content_type
-rw-r--r--README.md2
-rw-r--r--src/response.rs55
2 files changed, 31 insertions, 26 deletions
diff --git a/README.md b/README.md
index 56078ba..2932d70 100644
--- a/README.md
+++ b/README.md
@@ -126,7 +126,7 @@ let mut cookie = Cookie::new("userid",
cookie.set_secure(Some(true));
cookie.set_expires(expiry_date);
cookie.set_same_site(SameSite::Lax);
-resp.set_cookie(cookie);
+headers.set_cookie(cookie);
```
This session id cookie can then be retrieved and verified as follows:
diff --git a/src/response.rs b/src/response.rs
index ceb0f61..c8ff23a 100644
--- a/src/response.rs
+++ b/src/response.rs
@@ -10,6 +10,9 @@ use hyper::http::response::Builder;
pub trait SputnikBuilder {
/// Sets the Content-Type.
fn content_type(self, mime: mime::Mime) -> Builder;
+
+ /// Appends the Set-Cookie header.
+ fn set_cookie(self, cookie: Cookie) -> Builder;
}
pub fn redirect(location: &str, code: StatusCode) -> Builder {
@@ -18,31 +21,39 @@ pub fn redirect(location: &str, code: StatusCode) -> Builder {
impl SputnikBuilder for Builder {
fn content_type(mut self, mime: mime::Mime) -> Self {
- if let Some(headers) = self.headers_mut() {
- headers.insert(header::CONTENT_TYPE, mime.to_string().try_into().unwrap());
- }
+ self.headers_mut().map(|h| h.content_type(mime));
+ self
+ }
+
+ fn set_cookie(mut self, cookie: Cookie) -> Builder {
+ self.headers_mut().map(|h| h.set_cookie(cookie));
self
}
}
+/// Constructs an expired cookie to delete a cookie.
+pub fn delete_cookie(name: &str) -> Cookie {
+ let mut cookie = Cookie::new(name, "");
+ cookie.set_max_age(Duration::seconds(0));
+ cookie.set_expires(OffsetDateTime::now_utc() - Duration::days(365));
+ cookie
+}
+
pub trait SputnikHeaders {
+ /// Sets the Content-Type.
+ fn content_type(&mut self, mime: mime::Mime);
+
/// Appends a Set-Cookie header.
fn set_cookie(&mut self, cookie: Cookie);
-
- /// Appends a Set-Cookie header to delete a cookie.
- fn delete_cookie(&mut self, name: &str);
}
impl SputnikHeaders for HeaderMap {
- fn set_cookie(&mut self, cookie: Cookie) {
- self.append(header::SET_COOKIE, cookie.encoded().to_string().try_into().unwrap());
+ fn content_type(&mut self, mime: mime::Mime) {
+ self.insert(header::CONTENT_TYPE, mime.to_string().try_into().unwrap());
}
- fn delete_cookie(&mut self, name: &str) {
- let mut cookie = Cookie::new(name, "");
- cookie.set_max_age(Duration::seconds(0));
- cookie.set_expires(OffsetDateTime::now_utc() - Duration::days(365));
- self.set_cookie(cookie);
+ fn set_cookie(&mut self, cookie: Cookie) {
+ self.append(header::SET_COOKIE, cookie.encoded().to_string().try_into().unwrap());
}
}
@@ -71,16 +82,10 @@ mod tests {
#[test]
fn test_content_type() {
- let resp = Builder::new()
- .content_type(mime::TEXT_PLAIN)
- .content_type(mime::TEXT_HTML)
- .body(hyper::Body::empty()).unwrap();
- assert_eq!(resp.headers().len(), 1);
- assert_eq!(resp.headers().get(header::CONTENT_TYPE).unwrap(), "text/html");
-
- // doesn't panic after invalid header
- Builder::new()
- .header("foo", "invalid\r\n")
- .content_type(mime::TEXT_HTML);
+ let mut map = HeaderMap::new();
+ map.content_type(mime::TEXT_PLAIN);
+ map.content_type(mime::TEXT_HTML);
+ assert_eq!(map.len(), 1);
+ assert_eq!(map.get(header::CONTENT_TYPE).unwrap(), "text/html");
}
-} \ No newline at end of file
+}