Disable caching for revision URLs using "current"
The current revision is resolved dynamically and may change. Do not allow browser level caching on these URLs as the contents can (and usually does) change in the future. Clients that want caching must request the revision-id using a stable identifier, ideally the commit SHA-1, or at least the unique patch set number. This allows contents to cache as data behind the URL will not change. Change-Id: I00c5c607a78297dadaf5d5338f025aaf4832af63
This commit is contained in:
@@ -36,6 +36,10 @@ public class FileResource implements RestResource {
|
||||
return key;
|
||||
}
|
||||
|
||||
public boolean isCacheable() {
|
||||
return rev.isCacheable();
|
||||
}
|
||||
|
||||
Account.Id getAccountId() {
|
||||
return rev.getAccountId();
|
||||
}
|
||||
|
@@ -24,6 +24,7 @@ import com.google.gerrit.extensions.restapi.Response;
|
||||
import com.google.gerrit.extensions.restapi.RestReadView;
|
||||
import com.google.gerrit.extensions.restapi.RestView;
|
||||
import com.google.gerrit.reviewdb.client.PatchSet;
|
||||
import com.google.gerrit.server.change.FileInfoJson.FileInfo;
|
||||
import com.google.gerrit.server.patch.PatchListNotAvailableException;
|
||||
import com.google.gwtorm.server.OrmException;
|
||||
import com.google.inject.Inject;
|
||||
@@ -31,6 +32,7 @@ import com.google.inject.Provider;
|
||||
|
||||
import org.kohsuke.args4j.Option;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
class Files implements ChildCollection<RevisionResource, FileResource> {
|
||||
@@ -77,11 +79,14 @@ class Files implements ChildCollection<RevisionResource, FileResource> {
|
||||
resource.getChangeResource(), IdString.fromDecoded(base));
|
||||
basePatchSet = baseResource.getPatchSet();
|
||||
}
|
||||
return Response.ok(fileInfoJson.toFileInfoMap(
|
||||
Response<Map<String, FileInfo>> r = Response.ok(fileInfoJson.toFileInfoMap(
|
||||
resource.getChange(),
|
||||
resource.getPatchSet(),
|
||||
basePatchSet))
|
||||
.caching(CacheControl.PRIVATE(7, TimeUnit.DAYS));
|
||||
basePatchSet));
|
||||
if (resource.isCacheable()) {
|
||||
r.caching(CacheControl.PRIVATE(7, TimeUnit.DAYS));
|
||||
}
|
||||
return r;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -35,7 +35,10 @@ public class GetCommit implements RestReadView<RevisionResource> {
|
||||
@Override
|
||||
public Response<CommitInfo> apply(RevisionResource resource)
|
||||
throws OrmException, PatchSetInfoNotAvailableException {
|
||||
return Response.ok(json.toCommit(resource.getPatchSet()))
|
||||
.caching(CacheControl.PRIVATE(7, TimeUnit.DAYS));
|
||||
Response<CommitInfo> r = Response.ok(json.toCommit(resource.getPatchSet()));
|
||||
if (resource.isCacheable()) {
|
||||
r.caching(CacheControl.PRIVATE(7, TimeUnit.DAYS));
|
||||
}
|
||||
return r;
|
||||
}
|
||||
}
|
||||
|
@@ -152,7 +152,11 @@ public class GetDiff implements RestReadView<FileResource> {
|
||||
result.diffHeader = ps.getPatchHeader();
|
||||
}
|
||||
result.content = content.lines;
|
||||
return Response.ok(result).caching(CacheControl.PRIVATE(7, TimeUnit.DAYS));
|
||||
Response<Result> r = Response.ok(result);
|
||||
if (resource.isCacheable()) {
|
||||
r.caching(CacheControl.PRIVATE(7, TimeUnit.DAYS));
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
static class Result {
|
||||
|
@@ -29,12 +29,17 @@ public class RevisionResource implements RestResource {
|
||||
|
||||
private final ChangeResource change;
|
||||
private final PatchSet ps;
|
||||
private boolean cacheable = true;
|
||||
|
||||
public RevisionResource(ChangeResource change, PatchSet ps) {
|
||||
this.change = change;
|
||||
this.ps = ps;
|
||||
}
|
||||
|
||||
public boolean isCacheable() {
|
||||
return cacheable;
|
||||
}
|
||||
|
||||
public ChangeResource getChangeResource() {
|
||||
return change;
|
||||
}
|
||||
@@ -58,4 +63,9 @@ public class RevisionResource implements RestResource {
|
||||
IdentifiedUser getUser() {
|
||||
return (IdentifiedUser) getControl().getCurrentUser();
|
||||
}
|
||||
|
||||
RevisionResource doNotCache() {
|
||||
cacheable = false;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
@@ -59,7 +59,7 @@ public class Revisions implements ChildCollection<ChangeResource, RevisionResour
|
||||
PatchSet.Id p = change.getChange().currentPatchSetId();
|
||||
PatchSet ps = p != null ? dbProvider.get().patchSets().get(p) : null;
|
||||
if (ps != null && visible(change, ps)) {
|
||||
return new RevisionResource(change, ps);
|
||||
return new RevisionResource(change, ps).doNotCache();
|
||||
}
|
||||
throw new ResourceNotFoundException(id);
|
||||
}
|
||||
|
Reference in New Issue
Block a user