/* http-base - Extensions to jdk.httpserver Copyright (C) 2021 Ulrich Hilger This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ package de.uhilger.httpserver.base.handler; import de.uhilger.httpserver.base.HttpResponder; 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; /** * Die Klasse FileHandler dient zur Auslieferung von Dateiinhalten über * HTTP. * * Für das Streaming über HTTP wird die Auslieferung von Teilinhalten * mit dem Accept-Ranges-Header angeboten und via Range-Header unterstützt. * (vgl. https://developer.mozilla.org/en-US/docs/Web/HTTP/Range_requests) * * @author Ulrich Hilger * @version 1, 03.06.2021, (seit 25. März 2021) */ public class FileHandler implements HttpHandler { /* Headernamen */ public static final String RANGE_HEADER = "Range"; public static final String CONTENT_RANGE_HEADER = "Content-Range"; 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_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 RANGE_PATTERN = "[^\\d-,]"; public static final String WELCOME_FILE = "index.html"; /* Ablageort fuer Webinhalte */ 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, * ob die Datei existiert und den Inhalt der Datei abhängig davon, ob ein * Range-Header vorhanden ist, ganz oder teilweise ausliefern. * * @param e das Objekt mit Methoden zur Untersuchung der Anfrage sowie zum * Anfertigen und Senden der Antwort * @throws IOException falls etwas schief geht entsteht dieser Fehler */ @Override public void handle(HttpExchange e) throws IOException { 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)) { new FileActor().serveFileParts(e, fileToDeliver); } else { if (fName.length() < 1 || fName.endsWith(STR_SLASH)) { HttpHelper helper = new HttpHelper(); File welcomeFile = helper.tryWelcomeFiles(e, fName); if(welcomeFile != null) { fileToDeliver = welcomeFile; } } new HttpResponder().serveFile(e, fileToDeliver); } } } }