Add RFC-8246 support to CacheControl
This commit adds the "immutable" Cache-Control directives in `CacheControl`. Closes gh-29955
This commit is contained in:
committed by
Brian Clozel
parent
84c7f07364
commit
616e7779e0
@@ -77,6 +77,8 @@ public class CacheControl {
|
||||
@Nullable
|
||||
private Duration sMaxAge;
|
||||
|
||||
private boolean immutable = false;
|
||||
|
||||
|
||||
/**
|
||||
* Create an empty CacheControl instance.
|
||||
@@ -320,6 +322,18 @@ public class CacheControl {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an "immutable" directive.
|
||||
* <p>This directive indicates that the origin server will not update the
|
||||
* representation of that resource during the freshness lifetime of the response.
|
||||
* @return {@code this}, to facilitate method chaining
|
||||
* @see <a href="https://tools.ietf.org/html/rfc8246">rfc8246</a>
|
||||
*/
|
||||
public CacheControl immutable() {
|
||||
this.immutable = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the "Cache-Control" header value, if any.
|
||||
* @return the header value, or {@code null} if no directive was added
|
||||
@@ -369,6 +383,9 @@ public class CacheControl {
|
||||
if (this.staleWhileRevalidate != null) {
|
||||
appendDirective(headerValue, "stale-while-revalidate=" + this.staleWhileRevalidate.getSeconds());
|
||||
}
|
||||
if (this.immutable) {
|
||||
appendDirective(headerValue, "immutable");
|
||||
}
|
||||
return headerValue.toString();
|
||||
}
|
||||
|
||||
|
||||
@@ -101,4 +101,10 @@ public class CacheControlTests {
|
||||
assertThat(cc.getHeaderValue()).isEqualTo("max-age=3600, stale-while-revalidate=7200");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void immutable() throws Exception {
|
||||
CacheControl cc = CacheControl.maxAge(Duration.ofHours(1)).immutable();
|
||||
assertThat(cc.getHeaderValue()).isEqualTo("max-age=3600, immutable");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user