From 2eeb9e441b99e390067cb5573d858c8bd72902f1 Mon Sep 17 00:00:00 2001
From: ulrich
Date: Sat, 27 Mar 2021 11:29:28 +0000
Subject: [PATCH] Unterscheidung HEAD/GET eingebaut

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

diff --git a/src/de/uhilger/minsrv/handler/FileHandler.java b/src/de/uhilger/minsrv/handler/FileHandler.java
index f1eb4ef..035ad25 100644
--- a/src/de/uhilger/minsrv/handler/FileHandler.java
+++ b/src/de/uhilger/minsrv/handler/FileHandler.java
@@ -51,18 +51,21 @@
   /* Der Logger fuer diesen FileHandler */
   private static final Logger logger = Logger.getLogger(FileHandler.class.getName());
 
-  /* Header Namen */
+  /* Headernamen */
   public static final String RANGE_HEADER = "Range";
   public static final String CONTENT_RANGE_HEADER = "Content-Range";
   public static final String ACCEPT_RANGES_HEADER = "Accept-Ranges";
   public static final String LAST_MODIFIED_DATE_HEADER = "Last-Modified";
   public static final String CONTENT_TYPE = "Content-Type";
 
-  /* Status Codes */
+  /* Statuscodes */
   public static final int SC_OK = 200;
   public static final int SC_PARTIAL_CONTENT = 206;
   public static final int SC_NOT_FOUND = 404;
 
+  /* HTTP Methoden */
+  public static final String HTTP_GET = "GET";
+  
   /* String Konstanten */
   public static final String STR_BYTES = "bytes";
   public static final String STR_BLANK = " ";
@@ -75,16 +78,17 @@
   public static final String WELCOME_FILE = "index.html";
 
   /* Ablageort fuer Webinhalte */
-  private final String basePath;
+  private final String fileBase;
 
   /**
    * Ein neues Objekt der Klasse FileHandler erzeugen
    *
-   * @param basePath der Pfad zu Inhalten, die von diesem Handler ausgeliefert
-   * werden
+   * @param absoluteDirectoryPathAndName der absolute Pfad und Name des 
+   * Ordners im Dateisystem, der die Inhalte enthaelt, die von diesem 
+   * Handler ausgeliefert werden sollen
    */
-  public FileHandler(String basePath) {
-    this.basePath = basePath;
+  public FileHandler(String absoluteDirectoryPathAndName) {
+    this.fileBase = absoluteDirectoryPathAndName;
   }
 
   /**
@@ -98,25 +102,29 @@
    */
   @Override
   public void handle(HttpExchange e) throws IOException {
-    String ctxPath = e.getHttpContext().getPath();
-    String uriPath = e.getRequestURI().getPath();
-    logger.info(uriPath);
-    String fName = uriPath.substring(ctxPath.length());
+    String fName = getFileName(e);
     if (fName.startsWith(STR_DOT)) {
       sendNotFound(e, fName);
     } else {
       Headers headers = e.getRequestHeaders();
       if (headers.containsKey(RANGE_HEADER)) {
-        serveFileParts(e, new File(basePath, fName));
+        serveFileParts(e, new File(fileBase, fName));
       } else {
         if (fName.endsWith(Server.STR_SLASH)) {
           fName += WELCOME_FILE;
         }
-        serveFile(e, new File(basePath, fName));
+        serveFile(e, new File(fileBase, fName));
       }
     }
   }
 
+  protected String getFileName(HttpExchange e) {
+    String ctxPath = e.getHttpContext().getPath();
+    String uriPath = e.getRequestURI().getPath();
+    logger.info(uriPath);
+    return uriPath.substring(ctxPath.length());
+  }
+  
   /**
    * Den Inhalt einer Datei ausliefern
    *
@@ -125,21 +133,22 @@
    * @param file die Datei, deren Inhalt ausgeliefert werden soll
    * @throws IOException falls etwas schief geht entsteht dieser Fehler
    */
-  private void serveFile(HttpExchange e, File file) throws IOException {
+  protected void serveFile(HttpExchange e, File file) throws IOException {
     if (file.exists()) {
       OutputStream os = e.getResponseBody();
-      Headers headers = e.getResponseHeaders();
-      setCommonHeaders(headers, file);
+      setHeaders(e, file);
       e.sendResponseHeaders(SC_OK, file.length());
-      InputStream in = new FileInputStream(file);
-      int b = in.read();
-      while (b > -1) {
-        os.write(b);
-        b = in.read();
+      if(HTTP_GET.equalsIgnoreCase(e.getRequestMethod())) {
+        InputStream in = new FileInputStream(file);
+        int b = in.read();
+        while (b > -1) {
+          os.write(b);
+          b = in.read();
+        }
+        in.close();
+        os.flush();
+        os.close();
       }
-      in.close();
-      os.flush();
-      os.close();
     } else {
       sendNotFound(e, file.getName());
     }
@@ -169,17 +178,17 @@
    */
   /*
    */
-  private void serveFileParts(HttpExchange e, File file) throws IOException {
+  protected void serveFileParts(HttpExchange e, File file) throws IOException {
     if (file.exists()) {
       InputStream is = new FileInputStream(file);
       OutputStream os = e.getResponseBody();
-      Headers resHeaders = e.getResponseHeaders();
-      setCommonHeaders(resHeaders, file);
+      setHeaders(e, file);
       long responseLength = 0;
       long start = 0;
       long end;
       RangeGroup rangeGroup = parseRanges(e, file);
       Iterator<Range> i = rangeGroup.getRanges();
+      Headers resHeaders = e.getResponseHeaders();
       while (i.hasNext()) {
         Range range = i.next();
         start = range.getStart();
@@ -188,19 +197,21 @@
         responseLength += (end - start);
       }
       e.sendResponseHeaders(SC_PARTIAL_CONTENT, responseLength);
-      if (start > 0) {
-        is.skip(start);
+      if(HTTP_GET.equalsIgnoreCase(e.getRequestMethod())) {
+        if (start > 0) {
+          is.skip(start);
+        }
+        long count = 0;
+        int byteRead = is.read();
+        while (byteRead > -1 && count < responseLength) {
+          ++count;
+          os.write(byteRead);
+          byteRead = is.read();
+        }
+        os.flush();
+        os.close();
+        is.close();
       }
-      long count = 0;
-      int byteRead = is.read();
-      while (byteRead > -1 && count < responseLength) {
-        ++count;
-        os.write(byteRead);
-        byteRead = is.read();
-      }
-      os.flush();
-      os.close();
-      is.close();
     } else {
       sendNotFound(e, file.getName());
     }
@@ -218,7 +229,7 @@
    * @param file die Datei, deren Inhalt ausgeliefert werden soll
    * @return die angefragten Byte-Ranges
    */
-  private RangeGroup parseRanges(HttpExchange e, File file) {
+  protected RangeGroup parseRanges(HttpExchange e, File file) {
     RangeGroup ranges = new RangeGroup();
     String rangeHeader = e.getRequestHeaders().get(RANGE_HEADER).toString();
 
@@ -286,7 +297,7 @@
    * bezeichnet wird
    * @return der Inhalt des Content-Range Headers
    */
-  private String contentRangeHdr(Range range, File file) {
+  protected String contentRangeHdr(Range range, File file) {
     StringBuilder sb = new StringBuilder();
     sb.append(STR_BYTES);
     sb.append(STR_BLANK);
@@ -303,11 +314,13 @@
    * Inhalt oder nur Teile davon ausgeliefert werden sollen, in der 
    * Antwort stehen sollen 
    * 
-   * @param resHeaders das Objekt, in das die Header erzeugt werden
+   * @param e das Objekt mit Methoden zur Untersuchung der Anfrage sowie zum
+   * Anfertigen und Senden der Antwort
    * @param file  die Datei, f&uuml;r die die Header gelten
    * @throws IOException falls etwas schief geht entsteht dieser Fehler
    */
-  private void setCommonHeaders(Headers resHeaders, File file) throws IOException {
+  protected void setHeaders(HttpExchange e, File file) throws IOException {
+    Headers resHeaders = e.getResponseHeaders();
     resHeaders.add(ACCEPT_RANGES_HEADER, STR_BYTES);
     String mimeType = Files.probeContentType(file.toPath());
     if (mimeType != null) {
@@ -326,7 +339,7 @@
    * @param fname Name der Datei, die nicht gefunden wurde
    * @throws IOException falls etwas schief geht entsteht dieser Fehler
    */
-  private void sendNotFound(HttpExchange e, String fname) throws IOException {
+  protected void sendNotFound(HttpExchange e, String fname) throws IOException {
     OutputStream os = e.getResponseBody();
     String response = fname + STR_NOT_FOUND;
     byte[] bytes = response.getBytes(StandardCharsets.UTF_8);

--
Gitblit v1.9.3