From 9d3717abd59e1672f5d8d7888ce613afdc7fb3c5 Mon Sep 17 00:00:00 2001 From: ulrich Date: Wed, 24 Jan 2024 13:00:11 +0000 Subject: [PATCH] HttpResponder.finish aufgeteilt --- src/de/uhilger/httpserver/base/handler/FileHandler.java | 59 ++++++++++++++++------------------------------------------- 1 files changed, 16 insertions(+), 43 deletions(-) diff --git a/src/de/uhilger/httpserver/base/handler/FileHandler.java b/src/de/uhilger/httpserver/base/handler/FileHandler.java index 9ab3690..90083d0 100644 --- a/src/de/uhilger/httpserver/base/handler/FileHandler.java +++ b/src/de/uhilger/httpserver/base/handler/FileHandler.java @@ -21,10 +21,10 @@ import com.sun.net.httpserver.Headers; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; +import de.uhilger.httpserver.base.HttpHelper; import de.uhilger.httpserver.base.actor.FileActor; import java.io.File; import java.io.IOException; -import java.util.logging.Logger; /** * Die Klasse FileHandler dient zur Auslieferung von Dateiinhalten über @@ -39,49 +39,34 @@ */ public class FileHandler implements HttpHandler { - /* Der Logger fuer diesen FileHandler */ - private static final Logger logger = Logger.getLogger(FileHandler.class.getName()); - /* 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"; public static final String CONTENT_LENGTH = "Content-Length"; /* Statuscodes */ public static final int SC_OK = 200; public static final int SC_PARTIAL_CONTENT = 206; + public static final int SC_FORBIDDEN = 403; public static final int SC_NOT_FOUND = 404; public static final int SC_METHOD_NOT_ALLOWED = 405; public static final int SC_UNPROCESSABLE_ENTITY = 422; /* String Konstanten */ - //public static final String STR_BYTES = "bytes"; public static final String STR_SLASH = "/"; public static final String STR_BLANK = " "; public static final String STR_DASH = "-"; public static final String STR_COMMA = ","; public static final String STR_DOT = "."; - //public static final String STR_NOT_FOUND = " not found."; - //public static final String LM_PATTERN = "EEE, dd MMM yyyy HH:mm:ss zzz"; public static final String RANGE_PATTERN = "[^\\d-,]"; public static final String WELCOME_FILE = "index.html"; /* Ablageort fuer Webinhalte */ - protected final String fileBase; - - /** - * Ein neues Objekt der Klasse FileHandler erzeugen - * - * @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 absoluteDirectoryPathAndName) { - this.fileBase = absoluteDirectoryPathAndName; - } + public static final String ATTR_FILE_BASE = "fileBase"; + + /* moegliche Dateinamen, wenn kein Name angegeben wurde */ + public static final String ATTR_WELCOME_FILES = "welcomeFiles"; /** * Die Datei ermitteln, die sich aus dem angefragten URL ergibt, prüfen, @@ -94,37 +79,25 @@ */ @Override public void handle(HttpExchange e) throws IOException { - String fName = getFileName(e); + String fName = new HttpHelper().getFileName(e); if (fName.startsWith(STR_DOT)) { HttpResponder fs = new HttpResponder(); fs.sendNotFound(e, fName); } else { + File fileToDeliver = new File(e.getHttpContext().getAttributes().get(ATTR_FILE_BASE).toString(), fName); Headers headers = e.getRequestHeaders(); if (headers.containsKey(RANGE_HEADER)) { - FileActor fa = new FileActor(); - fa.serveFileParts(e, new File(fileBase, fName)); + new FileActor().serveFileParts(e, fileToDeliver); } else { if (fName.length() < 1 || fName.endsWith(STR_SLASH)) { - fName += WELCOME_FILE; + HttpHelper helper = new HttpHelper(); + File welcomeFile = helper.tryWelcomeFiles(e, fName); + if(welcomeFile != null) { + fileToDeliver = welcomeFile; + } } - HttpResponder fs = new HttpResponder(); - fs.serveFile(e, new File(fileBase, fName)); + new HttpResponder().serveFile(e, fileToDeliver); } } } - - /** - * Den Namen der gewünschten Datei aus der HTTP-Anfrage ermitteln - * - * @param e das Objekt mit Methoden zur Untersuchung der Anfrage sowie zum - * Anfertigen und Senden der Antwort - * @return Name der gewünschten Datei - */ - protected String getFileName(HttpExchange e) { - String ctxPath = e.getHttpContext().getPath(); - String uriPath = e.getRequestURI().getPath(); - logger.info(uriPath); - return uriPath.substring(ctxPath.length()); - } - -} +} \ No newline at end of file -- Gitblit v1.9.3