From f3f50e5171e870e41edb6afd8be8a0f01b5dfc55 Mon Sep 17 00:00:00 2001 From: "sigonasr2, Sig, Sigo" Date: Fri, 6 May 2022 18:16:42 +0000 Subject: [PATCH] Implement proper caching of files --- out/DIRECTORY_LISTING | 12 ++--- out/articles/DIRECTORY_LISTING | 10 ++-- out/articles/test articles/DIRECTORY_LISTING | 12 ++--- out/codeBackground | 30 +++++------ sigServer.java | 56 +++++++++++++++----- 5 files changed, 74 insertions(+), 46 deletions(-) diff --git a/out/DIRECTORY_LISTING b/out/DIRECTORY_LISTING index 304094e..40b193e 100644 --- a/out/DIRECTORY_LISTING +++ b/out/DIRECTORY_LISTING @@ -18,12 +18,12 @@
-

Directory Listing for /

📁articles 2022-05-06T16:14:07.054613Z gitpod 190
-
🗎codeBackground 2022-05-06T16:14:07.010613Z gitpod 2635
-
🗎otherpage.html 2022-05-06T16:14:07.038613Z gitpod 8892
-
🗎reset.css 2022-05-06T16:14:07.014613Z gitpod 1093
-
🗎sig.css 2022-05-06T16:14:07.022613Z gitpod 4702
-
🗎testfile.html 2022-05-06T16:14:07.038613Z gitpod 829
+

Directory Listing for /

📁articles 2022-05-06T18:16:24.073441Z gitpod 190
+
🗎codeBackground 2022-05-06T18:16:24.001441Z gitpod 2635
+
🗎otherpage.html 2022-05-06T18:16:24.045441Z gitpod 8892
+
🗎reset.css 2022-05-06T18:16:24.009441Z gitpod 1093
+
🗎sig.css 2022-05-06T18:16:24.021441Z gitpod 4702
+
🗎testfile.html 2022-05-06T18:16:24.049441Z gitpod 829
diff --git a/out/articles/DIRECTORY_LISTING b/out/articles/DIRECTORY_LISTING index fd35661..9369f6e 100644 --- a/out/articles/DIRECTORY_LISTING +++ b/out/articles/DIRECTORY_LISTING @@ -18,11 +18,11 @@
-

Directory Listing for /articles/

🗎curabitur vel tempus ex.article 2022-05-06T16:14:06.994613Z gitpod 579
-
🗎sigplace1.article 2022-05-06T16:14:06.998613Z gitpod 3807
-
📁test articles 2022-05-06T16:14:07.050613Z gitpod 147
-
🗎curabitur vel tempus ex.article.html 2022-05-06T16:14:07.042613Z gitpod 1296
-
🗎sigplace1.article.html 2022-05-06T16:14:07.046613Z gitpod 4524
+

Directory Listing for /articles/

🗎curabitur vel tempus ex.article 2022-05-06T18:16:23.981441Z gitpod 579
+
🗎sigplace1.article 2022-05-06T18:16:23.989441Z gitpod 3807
+
📁test articles 2022-05-06T18:16:24.061441Z gitpod 147
+
🗎curabitur vel tempus ex.article.html 2022-05-06T18:16:24.053441Z gitpod 1296
+
🗎sigplace1.article.html 2022-05-06T18:16:24.057441Z gitpod 4524
diff --git a/out/articles/test articles/DIRECTORY_LISTING b/out/articles/test articles/DIRECTORY_LISTING index affdcad..b3910d1 100644 --- a/out/articles/test articles/DIRECTORY_LISTING +++ b/out/articles/test articles/DIRECTORY_LISTING @@ -18,12 +18,12 @@
-

Directory Listing for /articles/test articles/

🗎test1.article 2022-05-06T16:14:07.002613Z gitpod 1068
-
🗎test2.article 2022-05-06T16:14:07.002613Z gitpod 764
-
🗎test3.article 2022-05-06T16:14:07.002613Z gitpod 977
-
🗎test1.article.html 2022-05-06T16:14:07.046613Z gitpod 1785
-
🗎test2.article.html 2022-05-06T16:14:07.046613Z gitpod 1481
-
🗎test3.article.html 2022-05-06T16:14:07.050613Z gitpod 1694
+

Directory Listing for /articles/test articles/

🗎test1.article 2022-05-06T18:16:23.989441Z gitpod 1068
+
🗎test2.article 2022-05-06T18:16:23.993441Z gitpod 764
+
🗎test3.article 2022-05-06T18:16:23.993441Z gitpod 977
+
🗎test1.article.html 2022-05-06T18:16:24.057441Z gitpod 1785
+
🗎test2.article.html 2022-05-06T18:16:24.061441Z gitpod 1481
+
🗎test3.article.html 2022-05-06T18:16:24.061441Z gitpod 1694
diff --git a/out/codeBackground b/out/codeBackground index b1ffc55..37dbdbe 100644 --- a/out/codeBackground +++ b/out/codeBackground @@ -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); diff --git a/sigServer.java b/sigServer.java index 8398570..f5381af 100644 --- a/sigServer.java +++ b/sigServer.java @@ -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(("
Webpage generated in "+(System.currentTimeMillis()-startTime)+"ms
\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(("
Webpage generated in "+(System.currentTimeMillis()-startTime)+"ms
\r\n").getBytes()); } } + System.out.println("Sent "+file+" to client "+client+"."); } else { CreateRawRequest(clientOutput,statusCode,statusMsg,"text/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","\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(); }