You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# GitHub Issue Summary: Add Transparent HTTP Compression Support to Java HTTP Client
1
+
# GitHub Issue Summary: Add HTTP Caching Support to Java HTTP Client
2
2
3
3
## Title
4
-
Add transparent HTTP compression support to java.net.http.HttpClient
4
+
Add HTTP caching support to java.net.http.HttpClient
5
5
6
6
## Component
7
7
core-libs/java.net
@@ -11,107 +11,251 @@ Enhancement
11
11
12
12
## Summary
13
13
14
-
The Java HTTP Client (java.net.http.HttpClient) does not support transparent HTTP compression, requiring applications to manually implement both request header management and response decompression. This enhancement request proposes adding automatic compression support similar to other modern HTTP clients.
14
+
The Java HTTP Client (java.net.http.HttpClient) does not support HTTP caching, requiring applications to manually implement cache storage, conditional requests, and response validation. This enhancement request proposes adding opt-in caching support similar to the historical java.net.ResponseCache but with a modern design aligned with current HTTP standards.
15
15
16
16
## Problem
17
17
18
-
HTTP compression is a fundamental HTTP feature providing significant benefits:
19
-
- Reduced bandwidth usage (critical for mobile connections)
20
-
- Faster request completion (allowing servers to handle more concurrent connections)
21
-
- Better cache utilization (proxies often cache compressed content)
22
-
- Improved security (fewer bytes to encrypt/decrypt)
18
+
HTTP caching is a fundamental HTTP feature providing significant benefits:
19
+
- Reduced server load (prevents redundant requests)
20
+
- Faster response times (eliminates network round-trips)
21
+
- Reduced bandwidth usage (conditional requests only transfer data when content changes)
22
+
- Offline capabilities (can serve stale content when network unavailable)
23
+
- Better user experience (instant responses from cache)
23
24
24
25
**Current limitations demonstrated by test suite:**
25
26
26
-
1. The HttpClient does NOT automatically add `Accept-Encoding` header to requests
27
-
2. The HttpClient does NOT automatically decompress compressed responses
28
-
3. Applications must manually implement both sides of compression support
27
+
1. The HttpClient does NOT automatically handle `Cache-Control` headers
28
+
2. The HttpClient does NOT automatically use ETags for conditional requests
29
+
3. The HttpClient does NOT handle 304 Not Modified responses transparently
30
+
4. Applications must manually implement complete caching lifecycle
29
31
30
-
This creates drawbacks:
31
-
- Each application must implement compression independently
32
-
- Implementation is non-trivial and error-prone
33
-
- Compression is often forgotten or incompletely implemented
34
-
- Transport-level compression is treated as an application concern
32
+
This creates significant drawbacks:
33
+
- Each application must implement caching independently
34
+
- Implementation is non-trivial and error-prone (requires handling cache storage, ETags, Last-Modified, 304 responses, expiration)
35
+
- Caching is often forgotten or incompletely implemented
36
+
- HTTP-level caching is treated as an application concern
37
+
38
+
## Historical Context
39
+
40
+
Java has supported HTTP caching for the older java.net.URL connection mechanism since Java SE 6 via `java.net.ResponseCache`. However, this caching mechanism is **not available** for the modern `java.net.http.HttpClient` introduced in Java 11.
35
41
36
42
## Proposed Solution
37
43
38
-
Add transparent compression support with the following behavior:
44
+
Add opt-in HTTP caching support with configurable behavior modes:
45
+
46
+
### Option 1: Fully Transparent Mode
47
+
```java
48
+
HttpCache cache =HttpCache.automatic();
49
+
HttpClient client =HttpClient.newBuilder()
50
+
.cache(cache)
51
+
.build();
52
+
53
+
// Client automatically:
54
+
// - Checks cache and returns cached content if valid
-[Java SE 6+ ResponseCache](https://docs.oracle.com/javase/8/docs/technotes/guides/net/http-cache.html)
105
204
106
205
## Testing
107
206
108
207
The enhancement should include:
109
-
- Unit tests for gzip and deflate compression/decompression
110
-
- Integration tests for transparent mode behavior
111
-
- Tests for opt-out mechanism (custom Accept-Encoding headers)
208
+
- Unit tests for cache storage and retrieval
209
+
- Tests for ETag and Last-Modified validation
210
+
- Tests for Cache-Control directive handling
211
+
- Integration tests for transparent and semi-automatic modes
212
+
- Tests for Vary header support
213
+
- Tests for stale content handling
112
214
- Tests for both HTTP/1.1 and HTTP/2
113
215
- Tests for both HTTP and HTTPS protocols
114
216
217
+
## Implementation Considerations
218
+
219
+
### Compatibility with java.net.ResponseCache
220
+
221
+
Two potential approaches:
222
+
223
+
**Option A: Separate Implementation (Recommended)**
224
+
- Create new `java.net.http.HttpCache` API independent of `java.net.ResponseCache`
225
+
- Modern design with builder pattern and fluent API
226
+
- Better aligned with HttpClient design philosophy
227
+
- Allows for HTTP/2-specific optimizations
228
+
229
+
**Option B: Compatibility Bridge**
230
+
- Make HttpClient use existing `java.net.ResponseCache` if configured
231
+
- Allows migration of existing ResponseCache implementations
232
+
- May have design limitations due to older API
233
+
234
+
**Recommendation**: Option A with optional bridge for migration scenarios.
235
+
236
+
### Security Considerations
237
+
238
+
- Respect `private` vs `public` cache directives
239
+
- Do not cache responses with authentication credentials by default
240
+
- Validate cache keys to prevent cache poisoning
241
+
- Consider size limits to prevent memory exhaustion
242
+
- Support secure deletion of sensitive cached content
243
+
244
+
### Performance Considerations
245
+
246
+
- Use efficient cache key generation
247
+
- Support concurrent access to cache
248
+
- Minimize synchronization overhead
249
+
- Consider using soft references for memory management
250
+
- Support background revalidation to avoid blocking
251
+
115
252
## Priority Rationale
116
253
117
-
While HTTP compression is an important feature, applications can currently implement it manually (albeit with significant effort). This enhancement would primarily improve developer experience and reduce boilerplate code. Suggested priority: P4 (Nice to have).
254
+
HTTP caching is a fundamental web feature supported by all major browsers and HTTP clients. While applications can implement caching manually, it's complex and error-prone. This enhancement would:
255
+
- Reduce boilerplate code significantly
256
+
- Improve application performance by default
257
+
- Align Java HTTP Client with modern HTTP client implementations
258
+
- Reduce server load across the ecosystem
259
+
260
+
Suggested priority: **P3** (Desirable feature that improves developer experience and application performance)
0 commit comments