mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-28 01:32:57 +01:00
session code cleanup
This commit is contained in:
parent
7910c7b511
commit
5b9b8c6dd9
@ -27,11 +27,7 @@ pub struct RedisSession {
|
|||||||
impl SessionImpl for RedisSession {
|
impl SessionImpl for RedisSession {
|
||||||
|
|
||||||
fn get(&self, key: &str) -> Option<&str> {
|
fn get(&self, key: &str) -> Option<&str> {
|
||||||
if let Some(s) = self.state.get(key) {
|
self.state.get(key).map(|s| s.as_str())
|
||||||
Some(s)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set(&mut self, key: &str, value: String) {
|
fn set(&mut self, key: &str, value: String) {
|
||||||
@ -88,6 +84,7 @@ impl RedisSessionBackend {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set custom cookie name for session id
|
||||||
pub fn cookie_name(mut self, name: &str) -> Self {
|
pub fn cookie_name(mut self, name: &str) -> Self {
|
||||||
Rc::get_mut(&mut self.0).unwrap().name = name.to_owned();
|
Rc::get_mut(&mut self.0).unwrap().name = name.to_owned();
|
||||||
self
|
self
|
||||||
@ -108,15 +105,13 @@ impl<S> SessionBackend<S> for RedisSessionBackend {
|
|||||||
changed: false,
|
changed: false,
|
||||||
inner: inner,
|
inner: inner,
|
||||||
state: state,
|
state: state,
|
||||||
value: Some(value),
|
value: Some(value) }
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
RedisSession {
|
RedisSession {
|
||||||
changed: false,
|
changed: false,
|
||||||
inner: inner,
|
inner: inner,
|
||||||
state: HashMap::new(),
|
state: HashMap::new(),
|
||||||
value: None,
|
value: None }
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
@ -130,8 +125,10 @@ struct Inner {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Inner {
|
impl Inner {
|
||||||
|
#[cfg_attr(feature = "cargo-clippy", allow(type_complexity))]
|
||||||
fn load<S>(&self, req: &mut HttpRequest<S>)
|
fn load<S>(&self, req: &mut HttpRequest<S>)
|
||||||
-> Box<Future<Item=Option<(HashMap<String, String>, String)>, Error=Error>> {
|
-> Box<Future<Item=Option<(HashMap<String, String>, String)>, Error=Error>>
|
||||||
|
{
|
||||||
if let Ok(cookies) = req.cookies() {
|
if let Ok(cookies) = req.cookies() {
|
||||||
for cookie in cookies {
|
for cookie in cookies {
|
||||||
if cookie.name() == self.name {
|
if cookie.name() == self.name {
|
||||||
@ -142,28 +139,26 @@ impl Inner {
|
|||||||
return Box::new(
|
return Box::new(
|
||||||
self.addr.call_fut(Command(resp_array!["GET", cookie.value()]))
|
self.addr.call_fut(Command(resp_array!["GET", cookie.value()]))
|
||||||
.map_err(Error::from)
|
.map_err(Error::from)
|
||||||
.and_then(move |res| {
|
.and_then(move |res| match res {
|
||||||
match res {
|
Ok(val) => {
|
||||||
Ok(val) => {
|
match val {
|
||||||
match val {
|
RespValue::Error(err) =>
|
||||||
RespValue::Error(err) =>
|
return Err(
|
||||||
return Err(
|
error::ErrorInternalServerError(err).into()),
|
||||||
error::ErrorInternalServerError(err).into()),
|
RespValue::SimpleString(s) =>
|
||||||
RespValue::SimpleString(s) =>
|
if let Ok(val) = serde_json::from_str(&s) {
|
||||||
if let Ok(val) = serde_json::from_str(&s) {
|
return Ok(Some((val, value)))
|
||||||
return Ok(Some((val, value)))
|
|
||||||
},
|
|
||||||
RespValue::BulkString(s) => {
|
|
||||||
if let Ok(val) = serde_json::from_slice(&s) {
|
|
||||||
return Ok(Some((val, value)))
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
_ => (),
|
RespValue::BulkString(s) => {
|
||||||
}
|
if let Ok(val) = serde_json::from_slice(&s) {
|
||||||
Ok(None)
|
return Ok(Some((val, value)))
|
||||||
},
|
}
|
||||||
Err(err) => Err(error::ErrorInternalServerError(err).into())
|
},
|
||||||
}
|
_ => (),
|
||||||
|
}
|
||||||
|
Ok(None)
|
||||||
|
},
|
||||||
|
Err(err) => Err(error::ErrorInternalServerError(err).into())
|
||||||
}))
|
}))
|
||||||
} else {
|
} else {
|
||||||
return Box::new(FutOk(None))
|
return Box::new(FutOk(None))
|
||||||
@ -198,27 +193,23 @@ impl Inner {
|
|||||||
Box::new(
|
Box::new(
|
||||||
match serde_json::to_string(state) {
|
match serde_json::to_string(state) {
|
||||||
Err(e) => Either::A(FutErr(e.into())),
|
Err(e) => Either::A(FutErr(e.into())),
|
||||||
Ok(body) => {
|
Ok(body) => Either::B(
|
||||||
Either::B(
|
self.addr.call_fut(
|
||||||
self.addr.call_fut(
|
Command(resp_array!["SET", value, body,"EX", &self.ttl]))
|
||||||
Command(resp_array!["SET", value, body,"EX", &self.ttl]))
|
.map_err(Error::from)
|
||||||
.map_err(Error::from)
|
.and_then(move |res| match res {
|
||||||
.and_then(move |res| {
|
Ok(_) => {
|
||||||
match res {
|
if let Some(jar) = jar {
|
||||||
Ok(_) => {
|
for cookie in jar.delta() {
|
||||||
if let Some(jar) = jar {
|
let val = HeaderValue::from_str(
|
||||||
for cookie in jar.delta() {
|
&cookie.to_string())?;
|
||||||
let val = HeaderValue::from_str(
|
resp.headers_mut().append(header::SET_COOKIE, val);
|
||||||
&cookie.to_string())?;
|
}
|
||||||
resp.headers_mut().append(header::SET_COOKIE, val);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(resp)
|
|
||||||
},
|
|
||||||
Err(err) => Err(error::ErrorInternalServerError(err).into())
|
|
||||||
}
|
}
|
||||||
}))
|
Ok(resp)
|
||||||
}
|
},
|
||||||
|
Err(err) => Err(error::ErrorInternalServerError(err).into())
|
||||||
|
}))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user