cache: Added more advanced VaryComparator functionality for accept-encoding
This commit is contained in:
cache/main/java/org/omegazero/proxyaccelerator/cache
@ -49,7 +49,7 @@ public class CacheEntry implements java.io.Serializable {
|
||||
* @return <code>true</code> if the Vary request headers match in both requests or the Vary header in the response was empty or nonexistent
|
||||
*/
|
||||
public boolean isVaryMatching(HTTPRequest request) {
|
||||
return this.properties.isVaryMatching(request);
|
||||
return this.properties.isVaryMatching(request, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -61,11 +61,9 @@ public class CacheEntry implements java.io.Serializable {
|
||||
* @param request The request
|
||||
* @param error If {@code true}, allows this {@code CacheEntry} to be used even if it is stale but within the limit set by the <i>stale-if-error</i> response directive
|
||||
* @return <code>true</code> if this entry is suitable to be used as a response to the given <b>request</b>
|
||||
* @see #isVaryMatching(HTTPMessage)
|
||||
* @see CacheConfig#isUsable(HTTPMessage, CacheEntry)
|
||||
*/
|
||||
public boolean isUsableFor(HTTPRequest request, boolean error) {
|
||||
if(!this.isVaryMatching(request))
|
||||
if(!this.properties.isVaryMatching(request, this.response))
|
||||
return false;
|
||||
|
||||
if(this.getProperties().ignoreClientRefresh)
|
||||
@ -188,9 +186,15 @@ public class CacheEntry implements java.io.Serializable {
|
||||
}
|
||||
|
||||
|
||||
public boolean isVaryMatching(HTTPMessage request) {
|
||||
public boolean isVaryMatching(HTTPRequest request, HTTPResponse cachedres) {
|
||||
for(String k : this.varyValues.keySet()){
|
||||
if(!CachePlugin.getVaryComparator(k).semanticallyEquivalent(request.getHeader(k), this.varyValues.get(k)))
|
||||
VaryComparator vc = CachePlugin.getVaryComparator(k);
|
||||
if(cachedres != null){
|
||||
Boolean ua = vc.isUsableAnyway(request, cachedres);
|
||||
if(ua != null && ua)
|
||||
return true;
|
||||
}
|
||||
if(!vc.semanticallyEquivalent(request.getHeader(k), this.varyValues.get(k)))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
@ -30,6 +30,7 @@ import org.omegazero.common.eventbus.SubscribeEvent.Priority;
|
||||
import org.omegazero.common.logging.Logger;
|
||||
import org.omegazero.common.plugins.ExtendedPluginConfiguration;
|
||||
import org.omegazero.http.common.HTTPMessage;
|
||||
import org.omegazero.http.common.HTTPRequest;
|
||||
import org.omegazero.http.common.HTTPResponse;
|
||||
import org.omegazero.http.common.HTTPResponseData;
|
||||
import org.omegazero.http.util.HTTPStatus;
|
||||
@ -528,7 +529,7 @@ public class CachePlugin {
|
||||
}
|
||||
|
||||
|
||||
static{
|
||||
static {
|
||||
CachePlugin.registerCacheImplementation("lru", (config) -> {
|
||||
org.omegazero.proxyaccelerator.cache.impl.LRUCache cache = new org.omegazero.proxyaccelerator.cache.impl.LRUCache();
|
||||
cache.setMaxCacheSize(config.optLong("sizeLimit", (long) (Runtime.getRuntime().maxMemory() * 0.5f)));
|
||||
@ -538,5 +539,27 @@ public class CachePlugin {
|
||||
return new org.omegazero.proxyaccelerator.cache.impl.SoftReferenceCache();
|
||||
});
|
||||
CachePlugin.registerCacheImplementationByClassName("disk", "org.omegazero.proxyaccelerator.cache.impl.DiskCache");
|
||||
CachePlugin.registerVaryComparator("accept-encoding", new VaryComparator(){
|
||||
|
||||
@Override
|
||||
public boolean semanticallyEquivalent(String value1, String value2) {
|
||||
return VaryComparator.DIRECTIVE_LIST_COMPARATOR.semanticallyEquivalent(value1, value2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean isUsableAnyway(HTTPRequest req, HTTPResponse cachedres){
|
||||
String ae = req.getHeader("accept-encoding");
|
||||
String ce = cachedres.getHeader("content-encoding");
|
||||
if(ae == null || ce == null)
|
||||
return null;
|
||||
String[] ael = ae.split(",");
|
||||
for(String aec : ael){
|
||||
if(aec.trim().equals(ce))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
});
|
||||
CachePlugin.registerVaryComparator("accept-language", VaryComparator.DIRECTIVE_LIST_COMPARATOR);
|
||||
}
|
||||
}
|
||||
|
@ -11,11 +11,18 @@
|
||||
*/
|
||||
package org.omegazero.proxyaccelerator.cache;
|
||||
|
||||
import org.omegazero.http.common.HTTPRequest;
|
||||
import org.omegazero.http.common.HTTPResponse;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface VaryComparator {
|
||||
|
||||
public boolean semanticallyEquivalent(String value1, String value2);
|
||||
|
||||
public default Boolean isUsableAnyway(HTTPRequest req, HTTPResponse cachedres){
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public static final VaryComparator EQUALS_COMPARATOR = new VaryComparator(){
|
||||
|
||||
|
Reference in New Issue
Block a user