10
10
import io .vertx .httpproxy .ProxyRequest ;
11
11
import io .vertx .httpproxy .ProxyResponse ;
12
12
import io .vertx .httpproxy .spi .cache .Cache ;
13
+ import io .vertx .httpproxy .spi .cache .Resource ;
13
14
14
15
import java .time .Instant ;
15
16
import java .util .function .BiFunction ;
17
+ import java .util .function .Predicate ;
16
18
17
19
class CachingFilter implements ProxyInterceptor {
18
20
19
- private static final BiFunction <String , Resource , Resource > CACHE_GET_AND_VALIDATE = (key , resource ) -> {
20
- long now = System .currentTimeMillis ();
21
- long val = resource .timestamp + resource .maxAge ;
22
- return val < now ? null : resource ;
23
- };
21
+ private final Cache cache ;
24
22
25
- private final Cache <String , Resource > cache ;
26
-
27
- public CachingFilter (Cache <String , Resource > cache ) {
23
+ public CachingFilter (Cache cache ) {
28
24
this .cache = cache ;
29
25
}
30
26
31
27
@ Override
32
28
public Future <ProxyResponse > handleProxyRequest (ProxyContext context ) {
33
- Future <ProxyResponse > future = tryHandleProxyRequestFromCache (context );
34
- if (future != null ) {
35
- return future ;
36
- }
37
- return context .sendRequest ();
29
+ return tryHandleProxyRequestFromCache (context );
38
30
}
39
31
40
32
@ Override
@@ -66,32 +58,33 @@ private Future<Void> sendAndTryCacheProxyResponse(ProxyContext context) {
66
58
System .currentTimeMillis (),
67
59
response .maxAge ());
68
60
Body body = response .getBody ();
69
- response .setBody (Body .body (new BufferingReadStream (body .stream (), res .content ), body .length ()));
61
+ response .setBody (Body .body (new BufferingReadStream (body .stream (), res .getContent () ), body .length ()));
70
62
Future <Void > fut = context .sendResponse ();
71
63
fut .onSuccess (v -> {
72
64
cache .put (absoluteUri , res );
73
65
});
74
66
return fut ;
67
+ } else if (request .getMethod () != HttpMethod .HEAD ) {
68
+ return context .sendResponse ();
75
69
} else {
76
- if (request .getMethod () == HttpMethod .HEAD ) {
77
- Resource resource = cache .get (request .absoluteURI ());
70
+ return cache .get (request .absoluteURI ()).compose (resource -> {
78
71
if (resource != null ) {
79
72
if (!revalidateResource (response , resource )) {
80
73
// Invalidate cache
81
74
cache .remove (request .absoluteURI ());
82
75
}
83
76
}
84
- }
85
- return context . sendResponse ( );
77
+ return context . sendResponse ();
78
+ } );
86
79
}
87
80
} else {
88
81
return context .sendResponse ();
89
82
}
90
83
}
91
84
92
85
private static boolean revalidateResource (ProxyResponse response , Resource resource ) {
93
- if (resource .etag != null && response .etag () != null ) {
94
- return resource .etag .equals (response .etag ());
86
+ if (resource .getEtag () != null && response .etag () != null ) {
87
+ return resource .getEtag () .equals (response .etag ());
95
88
}
96
89
return true ;
97
90
}
@@ -102,49 +95,54 @@ private Future<ProxyResponse> tryHandleProxyRequestFromCache(ProxyContext contex
102
95
103
96
HttpServerRequest response = proxyRequest .proxiedRequest ();
104
97
105
- Resource resource ;
106
98
HttpMethod method = response .method ();
107
- if (method == HttpMethod .GET || method == HttpMethod .HEAD ) {
108
- String cacheKey = proxyRequest .absoluteURI ();
109
- resource = cache .computeIfPresent (cacheKey , CACHE_GET_AND_VALIDATE );
99
+ if (method != HttpMethod .GET && method != HttpMethod .HEAD ) {
100
+ return context .sendRequest ();
101
+ }
102
+
103
+ String cacheKey = proxyRequest .absoluteURI ();
104
+ return cache .get (cacheKey ).compose (resource -> {
110
105
if (resource == null ) {
111
- return null ;
106
+ return context .sendRequest ();
107
+ }
108
+
109
+ long now = System .currentTimeMillis ();
110
+ long val = resource .getTimestamp () + resource .getMaxAge ();
111
+ if (val < now ) {
112
+ return cache .remove (cacheKey ).compose (v -> context .sendRequest ());
112
113
}
113
- } else {
114
- return null ;
115
- }
116
114
117
- String cacheControlHeader = response .getHeader (HttpHeaders .CACHE_CONTROL );
118
- if (cacheControlHeader != null ) {
119
- CacheControl cacheControl = new CacheControl ().parse (cacheControlHeader );
120
- if (cacheControl .maxAge () >= 0 ) {
121
- long now = System . currentTimeMillis ();
122
- long currentAge = now - resource . timestamp ;
123
- if ( currentAge > cacheControl . maxAge () * 1000 ) {
124
- String etag = resource . headers . get ( HttpHeaders . ETAG );
125
- if ( etag != null ) {
126
- proxyRequest . headers (). set (HttpHeaders . IF_NONE_MATCH , resource . etag );
127
- context .set ( "cached_resource" , resource );
128
- return context . sendRequest ();
129
- } else {
130
- return null ;
115
+ String cacheControlHeader = response .getHeader (HttpHeaders .CACHE_CONTROL );
116
+ if (cacheControlHeader != null ) {
117
+ CacheControl cacheControl = new CacheControl ().parse (cacheControlHeader );
118
+ if (cacheControl .maxAge () >= 0 ) {
119
+ long currentAge = now - resource . getTimestamp ();
120
+ if ( currentAge > cacheControl . maxAge () * 1000 ) {
121
+ String etag = resource . getHeaders (). get ( HttpHeaders . ETAG );
122
+ if ( etag != null ) {
123
+ proxyRequest . headers (). set ( HttpHeaders . IF_NONE_MATCH , resource . getEtag ());
124
+ context . set ("cached_resource" , resource );
125
+ return context .sendRequest ( );
126
+ } else {
127
+ return context . sendRequest ();
128
+ }
131
129
}
132
130
}
133
131
}
134
- }
135
132
136
- //
137
- String ifModifiedSinceHeader = response .getHeader (HttpHeaders .IF_MODIFIED_SINCE );
138
- if ((response .method () == HttpMethod .GET || response .method () == HttpMethod .HEAD ) && ifModifiedSinceHeader != null && resource .lastModified != null ) {
139
- Instant ifModifiedSince = ParseUtils .parseHeaderDate (ifModifiedSinceHeader );
140
- if (!ifModifiedSince .isAfter (resource .lastModified )) {
141
- response . response ().setStatusCode (304 ). end ( );
142
- return Future . succeededFuture ();
133
+ //
134
+ String ifModifiedSinceHeader = response .getHeader (HttpHeaders .IF_MODIFIED_SINCE );
135
+ if ((response .method () == HttpMethod .GET || response .method () == HttpMethod .HEAD ) && ifModifiedSinceHeader != null && resource .getLastModified () != null ) {
136
+ Instant ifModifiedSince = ParseUtils .parseHeaderDate (ifModifiedSinceHeader );
137
+ if (!ifModifiedSince .isAfter (resource .getLastModified () )) {
138
+ return Future . succeededFuture ( proxyRequest . release (). response ().setStatusCode (304 ));
139
+ }
143
140
}
144
- }
145
- proxyRequest .release ();
146
- ProxyResponse proxyResponse = proxyRequest .response ();
147
- resource .init (proxyResponse );
148
- return Future .succeededFuture (proxyResponse );
141
+ proxyRequest .release ();
142
+ ProxyResponse proxyResponse = proxyRequest .response ();
143
+ resource .init (proxyResponse );
144
+ return Future .succeededFuture (proxyResponse );
145
+ });
146
+
149
147
}
150
148
}
0 commit comments