From 51e1d5cc34ea03c3689700f5f5738d92eb1d9d5b Mon Sep 17 00:00:00 2001
From: ulrich
Date: Fri, 26 Mar 2021 15:41:25 +0000
Subject: [PATCH] FileHandler Logging erweitert

---
 src/de/uhilger/minsrv/handler/FileHandler.java |   62 ++++++++++++++++++++++---------
 1 files changed, 44 insertions(+), 18 deletions(-)

diff --git a/src/de/uhilger/minsrv/handler/FileHandler.java b/src/de/uhilger/minsrv/handler/FileHandler.java
index 033fa58..46929f6 100644
--- a/src/de/uhilger/minsrv/handler/FileHandler.java
+++ b/src/de/uhilger/minsrv/handler/FileHandler.java
@@ -31,6 +31,7 @@
 import java.util.Date;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Set;
 import java.util.logging.Logger;
 
 /**
@@ -82,31 +83,41 @@
   @Override
   public void handle(HttpExchange e) throws IOException {
     String ctxPath = e.getHttpContext().getPath();
+    logger.finer(ctxPath);
     String uriPath = e.getRequestURI().getPath();
+    logger.finer(uriPath);
     String fName = uriPath.substring(ctxPath.length());
+    logger.finer(fName);
+    
+    Headers headers = e.getRequestHeaders();
+    Set keys = headers.keySet();
+    Iterator i = keys.iterator();
+    StringBuilder sb = new StringBuilder();
+    while(i.hasNext()) {
+      String key = i.next().toString();
+      sb.append("   ");
+      sb.append(key);
+      sb.append(": ");
+      sb.append(headers.getFirst(key));
+      sb.append("\r\n");
+    }
+    logger.finer(sb.toString());
+    
     if (fName.startsWith(".")) {
       sendNotFound(e, fName);
     } else {
-      Headers headers = e.getRequestHeaders();
+      //Headers headers = e.getRequestHeaders();
       if (headers.containsKey(RANGE_HEADER)) {
+        logger.finer("has range header");
         serveFileParts(e, new File(basePath, fName));
       } else {
+        logger.finer("no range header");        
         if (fName.endsWith("/")) {
           fName += "index.html";
         }
         serveFile(e, new File(basePath, fName));
       }
     }
-  }
-
-  public void sendNotFound(HttpExchange e, String fname) throws IOException {
-    OutputStream os = e.getResponseBody();
-    String response = fname + " not found.";
-    byte[] bytes = response.getBytes(StandardCharsets.UTF_8);
-    e.sendResponseHeaders(SC_NOT_FOUND, bytes.length);
-    os.write(bytes);
-    os.flush();
-    os.close();
   }
 
   /**
@@ -120,6 +131,8 @@
   private void serveFile(HttpExchange e, File file) throws IOException {
     if (file.exists()) {
       OutputStream os = e.getResponseBody();
+      Headers headers = e.getResponseHeaders();
+      headers.add(ACCEPT_RANGES_HEADER, "bytes");
       e.sendResponseHeaders(SC_OK, file.length());
       InputStream in = new FileInputStream(file);
       int b = in.read();
@@ -132,12 +145,6 @@
       os.close();
     } else {
       sendNotFound(e, file.getName());
-      /*
-      String response = file.getName() + " not found.";
-      byte[] bytes = response.getBytes(StandardCharsets.UTF_8);
-      e.sendResponseHeaders(SC_NOT_FOUND, bytes.length);
-      os.write(bytes);
-       */
     }
   }
 
@@ -170,6 +177,7 @@
       InputStream is = new FileInputStream(file);
       OutputStream os = e.getResponseBody();
       Headers resHeaders = e.getResponseHeaders();
+      resHeaders.add(ACCEPT_RANGES_HEADER, "bytes");
       long responseLength = 0;
       long start = 0;
       long end;
@@ -229,7 +237,7 @@
   private RangeGroup parseRanges(HttpExchange e, File file) {
     RangeGroup ranges = new RangeGroup();
     String rangeHeader = e.getRequestHeaders().get(RANGE_HEADER).toString();
-
+    logger.info(rangeHeader);
     /*
       Inhalt des Range-Headers von nicht benoetigten Angaben befreien
     
@@ -288,6 +296,24 @@
   }
 
   /**
+   * Eine nicht gefunden Antwort senden
+   * 
+   * @param e das Objekt mit Methoden zur Untersuchung der Anfrage sowie zum
+   * Anfertigen und Senden der Antwort
+   * @param fname Name der Datei, die nicht gefunden wurde
+   * @throws IOException falls etwas schief geht entsteht dieser Fehler
+   */
+  public void sendNotFound(HttpExchange e, String fname) throws IOException {
+    OutputStream os = e.getResponseBody();
+    String response = fname + " not found.";
+    byte[] bytes = response.getBytes(StandardCharsets.UTF_8);
+    e.sendResponseHeaders(SC_NOT_FOUND, bytes.length);
+    os.write(bytes);
+    os.flush();
+    os.close();
+  }
+
+  /**
    * Eine Range
    */
   class Range {

--
Gitblit v1.9.3