mirror of https://github.com/adamdruppe/arsd.git
relative expires more conveniently
This commit is contained in:
parent
16b55ca4c9
commit
f71c360e83
15
cgi.d
15
cgi.d
|
@ -1999,9 +1999,20 @@ class Cgi {
|
||||||
setCache(true); // need to enable caching so the date has meaning
|
setCache(true); // need to enable caching so the date has meaning
|
||||||
|
|
||||||
responseIsPublic = isPublic;
|
responseIsPublic = isPublic;
|
||||||
|
responseExpiresRelative = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets a cache-control max-age header for whenFromNow, in seconds.
|
||||||
|
void setResponseExpiresRelative(int whenFromNow, bool isPublic = false) {
|
||||||
|
responseExpires = whenFromNow;
|
||||||
|
setCache(true); // need to enable caching so the date has meaning
|
||||||
|
|
||||||
|
responseIsPublic = isPublic;
|
||||||
|
responseExpiresRelative = true;
|
||||||
}
|
}
|
||||||
private long responseExpires = long.min;
|
private long responseExpires = long.min;
|
||||||
private bool responseIsPublic = false;
|
private bool responseIsPublic = false;
|
||||||
|
private bool responseExpiresRelative = false;
|
||||||
|
|
||||||
/// This is like setResponseExpires, but it can be called multiple times. The setting most in the past is the one kept.
|
/// This is like setResponseExpires, but it can be called multiple times. The setting most in the past is the one kept.
|
||||||
/// If you have multiple functions, they all might call updateResponseExpires about their own return value. The program
|
/// If you have multiple functions, they all might call updateResponseExpires about their own return value. The program
|
||||||
|
@ -2112,12 +2123,16 @@ class Cgi {
|
||||||
hd ~= "Location: " ~ responseLocation;
|
hd ~= "Location: " ~ responseLocation;
|
||||||
}
|
}
|
||||||
if(!noCache && responseExpires != long.min) { // an explicit expiration date is set
|
if(!noCache && responseExpires != long.min) { // an explicit expiration date is set
|
||||||
|
if(responseExpiresRelative) {
|
||||||
|
hd ~= "Cache-Control: "~(responseIsPublic ? "public" : "private")~", max-age="~to!string(responseExpires)~", no-cache=\"set-cookie, set-cookie2\"";
|
||||||
|
} else {
|
||||||
auto expires = SysTime(unixTimeToStdTime(cast(int)(responseExpires / 1000)), UTC());
|
auto expires = SysTime(unixTimeToStdTime(cast(int)(responseExpires / 1000)), UTC());
|
||||||
hd ~= "Expires: " ~ printDate(
|
hd ~= "Expires: " ~ printDate(
|
||||||
cast(DateTime) expires);
|
cast(DateTime) expires);
|
||||||
// FIXME: assuming everything is private unless you use nocache - generally right for dynamic pages, but not necessarily
|
// FIXME: assuming everything is private unless you use nocache - generally right for dynamic pages, but not necessarily
|
||||||
hd ~= "Cache-Control: "~(responseIsPublic ? "public" : "private")~", no-cache=\"set-cookie, set-cookie2\"";
|
hd ~= "Cache-Control: "~(responseIsPublic ? "public" : "private")~", no-cache=\"set-cookie, set-cookie2\"";
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if(responseCookies !is null && responseCookies.length > 0) {
|
if(responseCookies !is null && responseCookies.length > 0) {
|
||||||
foreach(c; responseCookies)
|
foreach(c; responseCookies)
|
||||||
hd ~= "Set-Cookie: " ~ c;
|
hd ~= "Set-Cookie: " ~ c;
|
||||||
|
|
Loading…
Reference in New Issue