Skip to content

Commit 616e777

Browse files
makingbclozel
authored andcommitted
Add RFC-8246 support to CacheControl
This commit adds the "immutable" Cache-Control directives in `CacheControl`. Closes gh-29955
1 parent 84c7f07 commit 616e777

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

spring-web/src/main/java/org/springframework/http/CacheControl.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ public class CacheControl {
7777
@Nullable
7878
private Duration sMaxAge;
7979

80+
private boolean immutable = false;
81+
8082

8183
/**
8284
* Create an empty CacheControl instance.
@@ -320,6 +322,18 @@ public CacheControl staleIfError(Duration staleIfError) {
320322
return this;
321323
}
322324

325+
/**
326+
* Add an "immutable" directive.
327+
* <p>This directive indicates that the origin server will not update the
328+
* representation of that resource during the freshness lifetime of the response.
329+
* @return {@code this}, to facilitate method chaining
330+
* @see <a href="https://tools.ietf.org/html/rfc8246">rfc8246</a>
331+
*/
332+
public CacheControl immutable() {
333+
this.immutable = true;
334+
return this;
335+
}
336+
323337
/**
324338
* Return the "Cache-Control" header value, if any.
325339
* @return the header value, or {@code null} if no directive was added
@@ -369,6 +383,9 @@ private String toHeaderValue() {
369383
if (this.staleWhileRevalidate != null) {
370384
appendDirective(headerValue, "stale-while-revalidate=" + this.staleWhileRevalidate.getSeconds());
371385
}
386+
if (this.immutable) {
387+
appendDirective(headerValue, "immutable");
388+
}
372389
return headerValue.toString();
373390
}
374391

spring-web/src/test/java/org/springframework/http/CacheControlTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,10 @@ public void staleWhileRevalidate_duration() throws Exception {
101101
assertThat(cc.getHeaderValue()).isEqualTo("max-age=3600, stale-while-revalidate=7200");
102102
}
103103

104+
@Test
105+
public void immutable() throws Exception {
106+
CacheControl cc = CacheControl.maxAge(Duration.ofHours(1)).immutable();
107+
assertThat(cc.getHeaderValue()).isEqualTo("max-age=3600, immutable");
108+
}
109+
104110
}

0 commit comments

Comments
 (0)