Skip to content

Commit

Permalink
[playframework#1162] Session is lost when application.session.maxAge …
Browse files Browse the repository at this point in the history
…is set to 30d
  • Loading branch information
pepite committed May 10, 2012
1 parent c95fe69 commit 9c5bf9f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion framework/src/play/libs/Time.java
Expand Up @@ -37,7 +37,7 @@ public static int parseDuration(String duration) {
return 60 * 60 * 24 * 30;
}
int toAdd = -1;
if (days.matcher(duration).matches()) {
if (days.matcher(duration).matches()) {
Matcher matcher = days.matcher(duration);
matcher.matches();
toAdd = Integer.parseInt(matcher.group(1)) * (60 * 60) * 24;
Expand Down
2 changes: 1 addition & 1 deletion framework/src/play/mvc/Http.java
Expand Up @@ -150,7 +150,7 @@ public static class Cookie implements Serializable {
*/
public String value;
/**
* Cookie max-age
* Cookie max-age in second
*/
public Integer maxAge;
/**
Expand Down
13 changes: 8 additions & 5 deletions framework/src/play/mvc/Scope.java
Expand Up @@ -177,6 +177,9 @@ static Session restore() {
try {
Session session = new Session();
Http.Cookie cookie = Http.Request.current().cookies.get(COOKIE_PREFIX + "_SESSION");
final int duration = Time.parseDuration(COOKIE_EXPIRE) ;
final long expiration = (duration * 1000l);

if (cookie != null && Play.started && cookie.value != null && !cookie.value.trim().equals("")) {
String value = cookie.value;
String sign = value.substring(0, value.indexOf("-"));
Expand All @@ -190,23 +193,23 @@ static Session restore() {
}
if (COOKIE_EXPIRE != null) {
// Verify that the session contains a timestamp, and that it's not expired
if (!session.contains(TS_KEY)) {
if (!session.contains(TS_KEY)) {
session = new Session();
} else {
if (Long.parseLong(session.get(TS_KEY)) < System.currentTimeMillis()) {
if ((Long.parseLong(session.get(TS_KEY))) < System.currentTimeMillis()) {
// Session expired
session = new Session();
}
}
session.put(TS_KEY, System.currentTimeMillis() + (Time.parseDuration(COOKIE_EXPIRE) * 1000));
session.put(TS_KEY, System.currentTimeMillis() + expiration);
} else {
// Just restored. Nothing changed. No cookie-expire.
session.changed = false;
}
} else {
// no previous cookie to restore; but we may have to set the timestamp in the new cookie
if (COOKIE_EXPIRE != null) {
session.put(TS_KEY, System.currentTimeMillis() + (Time.parseDuration(COOKIE_EXPIRE) * 1000));
if (COOKIE_EXPIRE != null) {
session.put(TS_KEY, (System.currentTimeMillis() + expiration));
}
}

Expand Down

0 comments on commit 9c5bf9f

Please sign in to comment.