1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-12-23 16:40:37 +00:00

Remove special casing for empty cookie list

The cookie crate already does that, so no need for actix-web to
do it.
This commit is contained in:
Konrad Borowski 2018-06-18 14:04:50 +02:00
parent 68cd5bdf68
commit dc045f0a13
2 changed files with 29 additions and 55 deletions

View file

@ -131,7 +131,7 @@ impl ClientRequest {
ClientRequestBuilder {
request: Some(ClientRequest::default()),
err: None,
cookies: None,
cookies: CookieJar::new(),
default_headers: true,
}
}
@ -274,7 +274,7 @@ impl fmt::Debug for ClientRequest {
pub struct ClientRequestBuilder {
request: Option<ClientRequest>,
err: Option<HttpError>,
cookies: Option<CookieJar>,
cookies: CookieJar,
default_headers: bool,
}
@ -496,13 +496,7 @@ impl ClientRequestBuilder {
/// }
/// ```
pub fn cookie<'c>(&mut self, cookie: Cookie<'c>) -> &mut Self {
if self.cookies.is_none() {
let mut jar = CookieJar::new();
jar.add(cookie.into_owned());
self.cookies = Some(jar)
} else {
self.cookies.as_mut().unwrap().add(cookie.into_owned());
}
self.cookies.add(cookie.into_owned());
self
}
@ -626,13 +620,13 @@ impl ClientRequestBuilder {
let mut request = self.request.take().expect("cannot reuse request builder");
// set cookies
if let Some(ref mut jar) = self.cookies {
let mut cookie = String::new();
for c in jar.delta() {
for c in self.cookies.delta() {
let name = percent_encode(c.name().as_bytes(), USERINFO_ENCODE_SET);
let value = percent_encode(c.value().as_bytes(), USERINFO_ENCODE_SET);
let _ = write!(&mut cookie, "; {}={}", name, value);
}
if !cookie.is_empty() {
request.headers.insert(
header::COOKIE,
HeaderValue::from_str(&cookie.as_str()[2..]).unwrap(),
@ -703,7 +697,7 @@ impl ClientRequestBuilder {
ClientRequestBuilder {
request: self.request.take(),
err: self.err.take(),
cookies: self.cookies.take(),
cookies: mem::replace(&mut self.cookies, CookieJar::new()),
default_headers: self.default_headers,
}
}

View file

@ -98,15 +98,9 @@ impl HttpResponse {
#[inline]
pub fn into_builder(mut self) -> HttpResponseBuilder {
// If this response has cookies, load them into a jar
let mut jar: Option<CookieJar> = None;
let mut jar = CookieJar::new();
for c in self.cookies() {
if let Some(ref mut j) = jar {
j.add_original(c.into_owned());
} else {
let mut j = CookieJar::new();
j.add_original(c.into_owned());
jar = Some(j);
}
jar.add_original(c.into_owned());
}
let response = self.0.take();
@ -352,7 +346,7 @@ pub struct HttpResponseBuilder {
response: Option<Box<InnerHttpResponse>>,
pool: Option<Rc<UnsafeCell<HttpResponsePool>>>,
err: Option<HttpError>,
cookies: Option<CookieJar>,
cookies: CookieJar,
}
impl HttpResponseBuilder {
@ -546,13 +540,7 @@ impl HttpResponseBuilder {
/// }
/// ```
pub fn cookie<'c>(&mut self, cookie: Cookie<'c>) -> &mut Self {
if self.cookies.is_none() {
let mut jar = CookieJar::new();
jar.add(cookie.into_owned());
self.cookies = Some(jar)
} else {
self.cookies.as_mut().unwrap().add(cookie.into_owned());
}
self.cookies.add(cookie.into_owned());
self
}
@ -573,15 +561,9 @@ impl HttpResponseBuilder {
/// }
/// ```
pub fn del_cookie<'a>(&mut self, cookie: &Cookie<'a>) -> &mut Self {
{
if self.cookies.is_none() {
self.cookies = Some(CookieJar::new())
}
let jar = self.cookies.as_mut().unwrap();
let cookie = cookie.clone().into_owned();
jar.add_original(cookie.clone());
jar.remove(cookie);
}
self.cookies.add_original(cookie.clone());
self.cookies.remove(cookie);
self
}
@ -631,14 +613,12 @@ impl HttpResponseBuilder {
return Error::from(e).into();
}
let mut response = self.response.take().expect("cannot reuse response builder");
if let Some(ref jar) = self.cookies {
for cookie in jar.delta() {
for cookie in self.cookies.delta() {
match HeaderValue::from_str(&cookie.to_string()) {
Ok(val) => response.headers.append(header::SET_COOKIE, val),
Err(e) => return Error::from(e).into(),
};
}
}
response.body = body.into();
HttpResponse(Some(response), self.pool.take().unwrap())
}
@ -691,7 +671,7 @@ impl HttpResponseBuilder {
response: self.response.take(),
pool: self.pool.take(),
err: self.err.take(),
cookies: self.cookies.take(),
cookies: mem::replace(&mut self.cookies, CookieJar::new()),
}
}
}
@ -939,7 +919,7 @@ impl HttpResponsePool {
response: Some(msg),
pool: Some(Rc::clone(pool)),
err: None,
cookies: None,
cookies: CookieJar::new(),
}
} else {
let msg = Box::new(InnerHttpResponse::new(status, Body::Empty));
@ -947,7 +927,7 @@ impl HttpResponsePool {
response: Some(msg),
pool: Some(Rc::clone(pool)),
err: None,
cookies: None,
cookies: CookieJar::new(),
}
}
}