ETag for files, based on last modified
This commit is contained in:
@@ -1,59 +1,62 @@
|
|||||||
package com.gempukku.lotro.async.handler;
|
package com.gempukku.lotro.async.handler;
|
||||||
|
|
||||||
import com.gempukku.lotro.async.HttpProcessingException;
|
import com.gempukku.lotro.async.HttpProcessingException;
|
||||||
import com.gempukku.lotro.async.ResponseWriter;
|
import com.gempukku.lotro.async.ResponseWriter;
|
||||||
import org.jboss.netty.channel.MessageEvent;
|
import org.jboss.netty.channel.MessageEvent;
|
||||||
import org.jboss.netty.handler.codec.http.HttpHeaders;
|
import org.jboss.netty.handler.codec.http.HttpHeaders;
|
||||||
|
|
||||||
import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.IF_NONE_MATCH;
|
import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.IF_NONE_MATCH;
|
||||||
|
|
||||||
import org.jboss.netty.handler.codec.http.HttpRequest;
|
import org.jboss.netty.handler.codec.http.HttpRequest;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class WebRequestHandler implements UriRequestHandler {
|
public class WebRequestHandler implements UriRequestHandler {
|
||||||
private String _root;
|
private String _root;
|
||||||
private String _uniqueEtag;
|
|
||||||
|
public WebRequestHandler(String root) {
|
||||||
public WebRequestHandler(String root) {
|
_root = root;
|
||||||
_root = root;
|
}
|
||||||
_uniqueEtag = "\"" + String.valueOf(System.currentTimeMillis()) + "\"";
|
|
||||||
}
|
@Override
|
||||||
|
public void handleRequest(String uri, HttpRequest request, Map<Type, Object> context, ResponseWriter responseWriter, MessageEvent e) throws Exception {
|
||||||
@Override
|
if (uri.equals(""))
|
||||||
public void handleRequest(String uri, HttpRequest request, Map<Type, Object> context, ResponseWriter responseWriter, MessageEvent e) throws Exception {
|
uri = "index.html";
|
||||||
if (clientHasCurrentVersion(request))
|
|
||||||
throw new HttpProcessingException(304);
|
uri = uri.replace('/', File.separatorChar);
|
||||||
|
|
||||||
if (uri.equals(""))
|
if ((uri.contains(".."))
|
||||||
uri = "index.html";
|
|| uri.contains(File.separator + ".")
|
||||||
|
|| uri.startsWith(".") || uri.endsWith("."))
|
||||||
uri = uri.replace('/', File.separatorChar);
|
throw new HttpProcessingException(403);
|
||||||
|
|
||||||
if ((uri.contains(".."))
|
File file = new File(_root + uri);
|
||||||
|| uri.contains(File.separator + ".")
|
if (!file.getCanonicalPath().startsWith(_root))
|
||||||
|| uri.startsWith(".") || uri.endsWith("."))
|
throw new HttpProcessingException(403);
|
||||||
throw new HttpProcessingException(403);
|
|
||||||
|
if (!file.exists())
|
||||||
File file = new File(_root + uri);
|
throw new HttpProcessingException(404);
|
||||||
if (!file.getCanonicalPath().startsWith(_root))
|
|
||||||
throw new HttpProcessingException(403);
|
final String etag = "\""+file.lastModified()+"\"";
|
||||||
|
|
||||||
responseWriter.writeFile(file, Collections.singletonMap(HttpHeaders.Names.ETAG, _uniqueEtag));
|
if (clientHasCurrentVersion(request, etag))
|
||||||
}
|
throw new HttpProcessingException(304);
|
||||||
|
|
||||||
private boolean clientHasCurrentVersion(HttpRequest request) {
|
responseWriter.writeFile(file, Collections.singletonMap(HttpHeaders.Names.ETAG, etag));
|
||||||
String ifNoneMatch = request.getHeader(IF_NONE_MATCH);
|
}
|
||||||
if (ifNoneMatch != null) {
|
|
||||||
String[] clientKnownVersions = ifNoneMatch.split(",");
|
private boolean clientHasCurrentVersion(HttpRequest request, String etag) {
|
||||||
for (String clientKnownVersion : clientKnownVersions) {
|
String ifNoneMatch = request.getHeader(IF_NONE_MATCH);
|
||||||
if (clientKnownVersion.trim().equals(_uniqueEtag))
|
if (ifNoneMatch != null) {
|
||||||
return true;
|
String[] clientKnownVersions = ifNoneMatch.split(",");
|
||||||
}
|
for (String clientKnownVersion : clientKnownVersions) {
|
||||||
}
|
if (clientKnownVersion.trim().equals(etag))
|
||||||
return false;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user