Implement proper caching of files
This commit is contained in:
parent
a92d375d41
commit
f3f50e5171
@ -18,12 +18,12 @@
|
||||
<body>
|
||||
<div class="contentBody">
|
||||
|
||||
<h2>Directory Listing for /</h2><div class="folderlisting"><a href="/.." class="icon">📁</a><a href="/..">.. </a><a href="/.." class="nounderline">(Previous Directory)</a></div><div class="folderlisting"><a href="/articles" class="icon">📁</a><a href="/articles">articles</a> 2022-05-06T16:14:07.054613Z gitpod 190</div>
|
||||
<div class="filelisting"><a href="/codeBackground" class="icon">🗎</a><a href="/codeBackground">codeBackground</a> 2022-05-06T16:14:07.010613Z gitpod 2635</div>
|
||||
<div class="filelisting"><a href="/otherpage.html" class="icon">🗎</a><a href="/otherpage.html">otherpage.html</a> 2022-05-06T16:14:07.038613Z gitpod 8892</div>
|
||||
<div class="filelisting"><a href="/reset.css" class="icon">🗎</a><a href="/reset.css">reset.css</a> 2022-05-06T16:14:07.014613Z gitpod 1093</div>
|
||||
<div class="filelisting"><a href="/sig.css" class="icon">🗎</a><a href="/sig.css">sig.css</a> 2022-05-06T16:14:07.022613Z gitpod 4702</div>
|
||||
<div class="filelisting"><a href="/testfile.html" class="icon">🗎</a><a href="/testfile.html">testfile.html</a> 2022-05-06T16:14:07.038613Z gitpod 829</div>
|
||||
<h2>Directory Listing for /</h2><div class="folderlisting"><a href="/.." class="icon">📁</a><a href="/..">.. </a><a href="/.." class="nounderline">(Previous Directory)</a></div><div class="folderlisting"><a href="/articles" class="icon">📁</a><a href="/articles">articles</a> 2022-05-06T18:16:24.073441Z gitpod 190</div>
|
||||
<div class="filelisting"><a href="/codeBackground" class="icon">🗎</a><a href="/codeBackground">codeBackground</a> 2022-05-06T18:16:24.001441Z gitpod 2635</div>
|
||||
<div class="filelisting"><a href="/otherpage.html" class="icon">🗎</a><a href="/otherpage.html">otherpage.html</a> 2022-05-06T18:16:24.045441Z gitpod 8892</div>
|
||||
<div class="filelisting"><a href="/reset.css" class="icon">🗎</a><a href="/reset.css">reset.css</a> 2022-05-06T18:16:24.009441Z gitpod 1093</div>
|
||||
<div class="filelisting"><a href="/sig.css" class="icon">🗎</a><a href="/sig.css">sig.css</a> 2022-05-06T18:16:24.021441Z gitpod 4702</div>
|
||||
<div class="filelisting"><a href="/testfile.html" class="icon">🗎</a><a href="/testfile.html">testfile.html</a> 2022-05-06T18:16:24.049441Z gitpod 829</div>
|
||||
|
||||
</div> <!--End Content Body-->
|
||||
<div class="codeForeground" id="codeForeground"></div>
|
||||
|
||||
@ -18,11 +18,11 @@
|
||||
<body>
|
||||
<div class="contentBody">
|
||||
|
||||
<h2>Directory Listing for /articles/</h2><div class="folderlisting"><a href="/articles/.." class="icon">📁</a><a href="/articles/..">.. </a><a href="/articles/.." class="nounderline">(Previous Directory)</a></div><div class="filelisting"><a href="/articles/curabitur vel tempus ex.article" class="icon">🗎</a><a href="/articles/curabitur vel tempus ex.article">curabitur vel tempus ex.article</a> 2022-05-06T16:14:06.994613Z gitpod 579</div>
|
||||
<div class="filelisting"><a href="/articles/sigplace1.article" class="icon">🗎</a><a href="/articles/sigplace1.article">sigplace1.article</a> 2022-05-06T16:14:06.998613Z gitpod 3807</div>
|
||||
<div class="folderlisting"><a href="/articles/test articles" class="icon">📁</a><a href="/articles/test articles">test articles</a> 2022-05-06T16:14:07.050613Z gitpod 147</div>
|
||||
<div class="filelisting"><a href="/articles/curabitur vel tempus ex.article.html" class="icon">🗎</a><a href="/articles/curabitur vel tempus ex.article.html">curabitur vel tempus ex.article.html</a> 2022-05-06T16:14:07.042613Z gitpod 1296</div>
|
||||
<div class="filelisting"><a href="/articles/sigplace1.article.html" class="icon">🗎</a><a href="/articles/sigplace1.article.html">sigplace1.article.html</a> 2022-05-06T16:14:07.046613Z gitpod 4524</div>
|
||||
<h2>Directory Listing for /articles/</h2><div class="folderlisting"><a href="/articles/.." class="icon">📁</a><a href="/articles/..">.. </a><a href="/articles/.." class="nounderline">(Previous Directory)</a></div><div class="filelisting"><a href="/articles/curabitur vel tempus ex.article" class="icon">🗎</a><a href="/articles/curabitur vel tempus ex.article">curabitur vel tempus ex.article</a> 2022-05-06T18:16:23.981441Z gitpod 579</div>
|
||||
<div class="filelisting"><a href="/articles/sigplace1.article" class="icon">🗎</a><a href="/articles/sigplace1.article">sigplace1.article</a> 2022-05-06T18:16:23.989441Z gitpod 3807</div>
|
||||
<div class="folderlisting"><a href="/articles/test articles" class="icon">📁</a><a href="/articles/test articles">test articles</a> 2022-05-06T18:16:24.061441Z gitpod 147</div>
|
||||
<div class="filelisting"><a href="/articles/curabitur vel tempus ex.article.html" class="icon">🗎</a><a href="/articles/curabitur vel tempus ex.article.html">curabitur vel tempus ex.article.html</a> 2022-05-06T18:16:24.053441Z gitpod 1296</div>
|
||||
<div class="filelisting"><a href="/articles/sigplace1.article.html" class="icon">🗎</a><a href="/articles/sigplace1.article.html">sigplace1.article.html</a> 2022-05-06T18:16:24.057441Z gitpod 4524</div>
|
||||
|
||||
</div> <!--End Content Body-->
|
||||
<div class="codeForeground" id="codeForeground"></div>
|
||||
|
||||
@ -18,12 +18,12 @@
|
||||
<body>
|
||||
<div class="contentBody">
|
||||
|
||||
<h2>Directory Listing for /articles/test articles/</h2><div class="folderlisting"><a href="/articles/test articles/.." class="icon">📁</a><a href="/articles/test articles/..">.. </a><a href="/articles/test articles/.." class="nounderline">(Previous Directory)</a></div><div class="filelisting"><a href="/articles/test articles/test1.article" class="icon">🗎</a><a href="/articles/test articles/test1.article">test1.article</a> 2022-05-06T16:14:07.002613Z gitpod 1068</div>
|
||||
<div class="filelisting"><a href="/articles/test articles/test2.article" class="icon">🗎</a><a href="/articles/test articles/test2.article">test2.article</a> 2022-05-06T16:14:07.002613Z gitpod 764</div>
|
||||
<div class="filelisting"><a href="/articles/test articles/test3.article" class="icon">🗎</a><a href="/articles/test articles/test3.article">test3.article</a> 2022-05-06T16:14:07.002613Z gitpod 977</div>
|
||||
<div class="filelisting"><a href="/articles/test articles/test1.article.html" class="icon">🗎</a><a href="/articles/test articles/test1.article.html">test1.article.html</a> 2022-05-06T16:14:07.046613Z gitpod 1785</div>
|
||||
<div class="filelisting"><a href="/articles/test articles/test2.article.html" class="icon">🗎</a><a href="/articles/test articles/test2.article.html">test2.article.html</a> 2022-05-06T16:14:07.046613Z gitpod 1481</div>
|
||||
<div class="filelisting"><a href="/articles/test articles/test3.article.html" class="icon">🗎</a><a href="/articles/test articles/test3.article.html">test3.article.html</a> 2022-05-06T16:14:07.050613Z gitpod 1694</div>
|
||||
<h2>Directory Listing for /articles/test articles/</h2><div class="folderlisting"><a href="/articles/test articles/.." class="icon">📁</a><a href="/articles/test articles/..">.. </a><a href="/articles/test articles/.." class="nounderline">(Previous Directory)</a></div><div class="filelisting"><a href="/articles/test articles/test1.article" class="icon">🗎</a><a href="/articles/test articles/test1.article">test1.article</a> 2022-05-06T18:16:23.989441Z gitpod 1068</div>
|
||||
<div class="filelisting"><a href="/articles/test articles/test2.article" class="icon">🗎</a><a href="/articles/test articles/test2.article">test2.article</a> 2022-05-06T18:16:23.993441Z gitpod 764</div>
|
||||
<div class="filelisting"><a href="/articles/test articles/test3.article" class="icon">🗎</a><a href="/articles/test articles/test3.article">test3.article</a> 2022-05-06T18:16:23.993441Z gitpod 977</div>
|
||||
<div class="filelisting"><a href="/articles/test articles/test1.article.html" class="icon">🗎</a><a href="/articles/test articles/test1.article.html">test1.article.html</a> 2022-05-06T18:16:24.057441Z gitpod 1785</div>
|
||||
<div class="filelisting"><a href="/articles/test articles/test2.article.html" class="icon">🗎</a><a href="/articles/test articles/test2.article.html">test2.article.html</a> 2022-05-06T18:16:24.061441Z gitpod 1481</div>
|
||||
<div class="filelisting"><a href="/articles/test articles/test3.article.html" class="icon">🗎</a><a href="/articles/test articles/test3.article.html">test3.article.html</a> 2022-05-06T18:16:24.061441Z gitpod 1694</div>
|
||||
|
||||
</div> <!--End Content Body-->
|
||||
<div class="codeForeground" id="codeForeground"></div>
|
||||
|
||||
@ -5,10 +5,19 @@
|
||||
try (Socket client = socket.accept()) {
|
||||
System.out.println("New client connection detected: "+client.toString());
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
|
||||
String line;
|
||||
line=in.readLine(); //Read the first line, this should be our request.
|
||||
if (line!=null) {
|
||||
String[] splitter = line.split(Pattern.quote(" "));
|
||||
String requestLine,line;
|
||||
ZonedDateTime modifiedDate = null;
|
||||
requestLine=in.readLine(); //Read the first line, this should be our request.
|
||||
if (requestLine!=null) {
|
||||
while (!(line=in.readLine()).isBlank()) {
|
||||
//System.out.println(requestLine);
|
||||
if (line.contains("If-Modified-Since: ")) {
|
||||
String modifiedSince=line.replace("If-Modified-Since: ","");
|
||||
modifiedDate = ZonedDateTime.parse(modifiedSince,DateTimeFormatter.RFC_1123_DATE_TIME);
|
||||
//System.out.println("Found a modified date of: "+modifiedDate);
|
||||
}
|
||||
}
|
||||
String[] splitter = requestLine.split(Pattern.quote(" "));
|
||||
if (splitter.length==3) {
|
||||
//This is valid.
|
||||
if (splitter[0].equals("GET")) { //This is a GET request.
|
||||
@ -27,14 +36,5 @@
|
||||
}
|
||||
if (requestloc.equals("/")) {
|
||||
//Send default directory.
|
||||
CreateRequest(client,"200","OK","testfile.html");
|
||||
} else {
|
||||
CreateRequest(client,"200","OK",URLDecoder.decode(requestloc.replaceFirst("/",""),StandardCharsets.UTF_8));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
CreateRequest(client,"501","Not Implemented","testfile.html");
|
||||
}
|
||||
}
|
||||
while (!(line=in.readLine()).isBlank()) {
|
||||
System.out.println(line);
|
||||
if (modifiedDate==null||modifiedDate.isBefore(GetLastModifiedDate(sigPlace.OUTDIR,"testfile.html"))) {
|
||||
System.out.println(GetLastModifiedDate(sigPlace.OUTDIR,"testfile.html")+"//"+modifiedDate);
|
||||
|
||||
@ -11,12 +11,13 @@ import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.attribute.FileTime;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.TextStyle;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class sigServer {
|
||||
@ -29,10 +30,19 @@ public class sigServer {
|
||||
try (Socket client = socket.accept()) {
|
||||
System.out.println("New client connection detected: "+client.toString());
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
|
||||
String line;
|
||||
line=in.readLine(); //Read the first line, this should be our request.
|
||||
if (line!=null) {
|
||||
String[] splitter = line.split(Pattern.quote(" "));
|
||||
String requestLine,line;
|
||||
ZonedDateTime modifiedDate = null;
|
||||
requestLine=in.readLine(); //Read the first line, this should be our request.
|
||||
if (requestLine!=null) {
|
||||
while (!(line=in.readLine()).isBlank()) {
|
||||
//System.out.println(requestLine);
|
||||
if (line.contains("If-Modified-Since: ")) {
|
||||
String modifiedSince=line.replace("If-Modified-Since: ","");
|
||||
modifiedDate = ZonedDateTime.parse(modifiedSince,DateTimeFormatter.RFC_1123_DATE_TIME);
|
||||
//System.out.println("Found a modified date of: "+modifiedDate);
|
||||
}
|
||||
}
|
||||
String[] splitter = requestLine.split(Pattern.quote(" "));
|
||||
if (splitter.length==3) {
|
||||
//This is valid.
|
||||
if (splitter[0].equals("GET")) { //This is a GET request.
|
||||
@ -51,18 +61,28 @@ public class sigServer {
|
||||
}
|
||||
if (requestloc.equals("/")) {
|
||||
//Send default directory.
|
||||
CreateRequest(client,"200","OK","testfile.html");
|
||||
if (modifiedDate==null||modifiedDate.isBefore(GetLastModifiedDate(sigPlace.OUTDIR,"testfile.html"))) {
|
||||
System.out.println(GetLastModifiedDate(sigPlace.OUTDIR,"testfile.html")+"//"+modifiedDate);
|
||||
CreateRequest(client,"200","OK","testfile.html");
|
||||
} else {
|
||||
//System.out.println(" testfile.html is cached! No sending required.");
|
||||
CreateRequest(client,"304","Not Modified","testfile.html");
|
||||
}
|
||||
} else {
|
||||
CreateRequest(client,"200","OK",URLDecoder.decode(requestloc.replaceFirst("/",""),StandardCharsets.UTF_8));
|
||||
String location = URLDecoder.decode(requestloc.replaceFirst("/",""),StandardCharsets.UTF_8);
|
||||
if (modifiedDate==null||modifiedDate.isBefore(GetLastModifiedDate(sigPlace.OUTDIR,location)))
|
||||
{
|
||||
CreateRequest(client,"200","OK",location);
|
||||
} else {
|
||||
//System.out.println(" "+location+" is cached! No sending required.");
|
||||
CreateRequest(client,"304","Not Modified",location);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
CreateRequest(client,"501","Not Implemented","testfile.html");
|
||||
}
|
||||
}
|
||||
while (!(line=in.readLine()).isBlank()) {
|
||||
System.out.println(line);
|
||||
}
|
||||
}
|
||||
} catch(SocketException|NullPointerException e) {
|
||||
e.printStackTrace();
|
||||
@ -73,6 +93,12 @@ public class sigServer {
|
||||
}
|
||||
}
|
||||
|
||||
private ZonedDateTime GetLastModifiedDate(String first,String...more) throws IOException {
|
||||
Instant newTime = Files.getLastModifiedTime(Paths.get(first,more)).toInstant();
|
||||
newTime = newTime.truncatedTo(ChronoUnit.SECONDS);
|
||||
return newTime.atZone(ZoneId.of("GMT"));
|
||||
}
|
||||
|
||||
private void CreateRawRequest(OutputStream stream, String statusCode, String statusMsg, String contentType, byte[] content) {
|
||||
CreateRawRequest(stream, statusCode, statusMsg, contentType, content,null);
|
||||
}
|
||||
@ -82,7 +108,7 @@ public class sigServer {
|
||||
stream.write(("HTTP/1.1 "+statusCode+" "+statusMsg+"\r\n").getBytes());
|
||||
stream.write(("ContentType: "+contentType+"\r\n").getBytes());
|
||||
if (lastModified!=null) {
|
||||
ZonedDateTime date = lastModified.toInstant().atZone(ZoneId.of("GMT"));
|
||||
ZonedDateTime date = lastModified.toInstant().truncatedTo(ChronoUnit.SECONDS).atZone(ZoneId.of("GMT"));
|
||||
stream.write(("Last-Modified: "+date.format(DateTimeFormatter.RFC_1123_DATE_TIME)+"\r\n").getBytes());
|
||||
}
|
||||
stream.write("\r\n".getBytes());
|
||||
@ -100,7 +126,7 @@ public class sigServer {
|
||||
if (statusCode.equals("200")) {
|
||||
if (Files.exists(file)) {
|
||||
if (Files.isDirectory(file)) {
|
||||
CreateRawRequest(clientOutput,statusCode,statusMsg,"text/html",Files.readAllBytes(Paths.get(sigPlace.OUTDIR,string,sigPlace.DIRECTORYLISTING_FILENAME)));
|
||||
CreateRawRequest(clientOutput,statusCode,statusMsg,"text/html",Files.readAllBytes(Paths.get(sigPlace.OUTDIR,string,sigPlace.DIRECTORYLISTING_FILENAME)),Files.getLastModifiedTime(file));
|
||||
clientOutput.write(("<div class=\"generateTime\">Webpage generated in "+(System.currentTimeMillis()-startTime)+"ms</div>\r\n").getBytes());
|
||||
} else {
|
||||
CreateRawRequest(clientOutput,statusCode,statusMsg,Files.probeContentType(file),Files.readAllBytes(file),Files.getLastModifiedTime(file));
|
||||
@ -109,16 +135,18 @@ public class sigServer {
|
||||
clientOutput.write(("<div class=\"generateTime\">Webpage generated in "+(System.currentTimeMillis()-startTime)+"ms</div>\r\n").getBytes());
|
||||
}
|
||||
}
|
||||
System.out.println("Sent "+file+" to client "+client+".");
|
||||
} else {
|
||||
CreateRawRequest(clientOutput,statusCode,statusMsg,"text/html","<!DOCTYPE html>\nWe're sorry, your webpage is in another castle!".getBytes());
|
||||
System.out.println("Sent [404] "+statusMsg+" to client "+client+" for "+file+".");
|
||||
}
|
||||
} else {
|
||||
CreateRawRequest(clientOutput,statusCode,statusMsg,"text/html","<!DOCTYPE html>\nWe're sorry, your webpage exploded!".getBytes());
|
||||
System.out.println("Sent ["+statusCode+"] "+statusMsg+" to client "+client+" for "+file+".");
|
||||
}
|
||||
clientOutput.write("\r\n\r\n".getBytes());
|
||||
clientOutput.flush();
|
||||
client.close();
|
||||
System.out.println("Sent "+file+" to client "+client+".");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user