| | |
| | | import com.sun.net.httpserver.Headers; |
| | | import com.sun.net.httpserver.HttpExchange; |
| | | import com.sun.net.httpserver.HttpHandler; |
| | | import de.uhilger.minsrv.Server; |
| | | import java.io.File; |
| | | import java.io.FileInputStream; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.io.OutputStream; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.nio.file.Files; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.Iterator; |
| | | import java.util.List; |
| | | import java.util.Set; |
| | | import java.util.logging.Logger; |
| | | |
| | | /** |
| | | * 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 0.1, 25. März 2021 |
| | |
| | | private static final Logger logger = Logger.getLogger(FileHandler.class.getName()); |
| | | |
| | | /* Header Namen */ |
| | | final static String RANGE_HEADER = "Range"; |
| | | final static String CONTENT_RANGE_HEADER = "Content-Range"; |
| | | final static String ACCEPT_RANGES_HEADER = "Accept-Ranges"; |
| | | final static String LAST_MODIFIED_DATE_HEADER = "Last-Modified"; |
| | | final static String CONTENT_TYPE = "Content-Type"; |
| | | 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 */ |
| | | public static final int SC_OK = 200; |
| | | public static final int SC_PARTIAL_CONTENT = 206; |
| | | public static final int SC_NOT_FOUND = 404; |
| | | |
| | | /* String Konstanten */ |
| | | public static final String STR_BYTES = "bytes"; |
| | | 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 */ |
| | | private final String basePath; |
| | |
| | | @Override |
| | | public void handle(HttpExchange e) throws IOException { |
| | | String ctxPath = e.getHttpContext().getPath(); |
| | | logger.finer(ctxPath); |
| | | String uriPath = e.getRequestURI().getPath(); |
| | | logger.finer(uriPath); |
| | | logger.info(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(".")) { |
| | | if (fName.startsWith(STR_DOT)) { |
| | | 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"; |
| | | if (fName.endsWith(Server.STR_SLASH)) { |
| | | fName += WELCOME_FILE; |
| | | } |
| | | serveFile(e, new File(basePath, fName)); |
| | | } |
| | |
| | | if (file.exists()) { |
| | | OutputStream os = e.getResponseBody(); |
| | | Headers headers = e.getResponseHeaders(); |
| | | headers.add(ACCEPT_RANGES_HEADER, "bytes"); |
| | | setCommonHeaders(headers, file); |
| | | e.sendResponseHeaders(SC_OK, file.length()); |
| | | InputStream in = new FileInputStream(file); |
| | | int b = in.read(); |
| | |
| | | InputStream is = new FileInputStream(file); |
| | | OutputStream os = e.getResponseBody(); |
| | | Headers resHeaders = e.getResponseHeaders(); |
| | | resHeaders.add(ACCEPT_RANGES_HEADER, "bytes"); |
| | | setCommonHeaders(resHeaders, file); |
| | | long responseLength = 0; |
| | | long start = 0; |
| | | long end; |
| | |
| | | Range range = i.next(); |
| | | start = range.getStart(); |
| | | end = range.getEnd(); |
| | | StringBuilder sb = new StringBuilder(); |
| | | sb.append("bytes "); |
| | | sb.append(range.getStart()); |
| | | sb.append("-"); |
| | | sb.append(range.getEnd()); |
| | | sb.append("/"); |
| | | sb.append(file.length()); |
| | | resHeaders.add(CONTENT_RANGE_HEADER, sb.toString()); |
| | | logger.info(sb.toString()); |
| | | resHeaders.add(CONTENT_RANGE_HEADER, contentRangeHdr(range, file)); |
| | | responseLength += (end - start); |
| | | logger.info("responseLength: " + responseLength); |
| | | } |
| | | resHeaders.add(CONTENT_TYPE, "video/mp4"); |
| | | SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz"); |
| | | Date date = new Date(file.lastModified()); |
| | | resHeaders.add(LAST_MODIFIED_DATE_HEADER, sdf.format(date)); |
| | | e.sendResponseHeaders(SC_PARTIAL_CONTENT, responseLength); |
| | | if (start > 0) { |
| | | is.skip(start); |
| | |
| | | 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 |
| | | |
| | |
| | | Der regulaere Ausdruck "[^\\d-,]" bezeichnet alle Zeichen, die keine |
| | | Ziffern 0-9, Bindestrich oder Komma sind. |
| | | */ |
| | | rangeHeader = rangeHeader.replaceAll("[^\\d-,]", ""); |
| | | logger.info(rangeHeader); |
| | | rangeHeader = rangeHeader.replaceAll(RANGE_PATTERN, ""); |
| | | |
| | | /* |
| | | Die Ranges ermitteln. |
| | |
| | | values.length > 1 und values[0].length < 1: Fall 1 ist gegeben |
| | | ansonsten: Fall 2 ist gegeben |
| | | */ |
| | | String[] rangeArray = rangeHeader.split(","); |
| | | String[] rangeArray = rangeHeader.split(STR_COMMA); |
| | | for (String rangeStr : rangeArray) { |
| | | Range range = new Range(); |
| | | String[] values = rangeStr.split("-"); |
| | | String[] values = rangeStr.split(STR_DASH); |
| | | if (values.length < 2) { |
| | | // Fall 3 |
| | | range.setStart(Long.parseLong(values[0])); |
| | |
| | | } |
| | | |
| | | /** |
| | | * Eine nicht gefunden Antwort senden |
| | | * Einen Content-Range Header erzeugen |
| | | * |
| | | * @param range die Range, aus deren Inhalt der Header erzeugt werden soll |
| | | * @param file die Datei, die den Inhalt liefert, der vom Header |
| | | * bezeichnet wird |
| | | * @return der Inhalt des Content-Range Headers |
| | | */ |
| | | private String contentRangeHdr(Range range, File file) { |
| | | StringBuilder sb = new StringBuilder(); |
| | | sb.append(STR_BYTES); |
| | | sb.append(STR_BLANK); |
| | | sb.append(range.getStart()); |
| | | sb.append(STR_DASH); |
| | | sb.append(range.getEnd()); |
| | | sb.append(Server.STR_SLASH); |
| | | sb.append(file.length()); |
| | | return sb.toString(); |
| | | } |
| | | |
| | | /** |
| | | * Die Header erzeugen, die unabhängig davon, ob der ganze |
| | | * Inhalt oder nur Teile davon ausgeliefert werden sollen, in der |
| | | * Antwort stehen sollen |
| | | * |
| | | * @param resHeaders das Objekt, in das die Header erzeugt werden |
| | | * @param file die Datei, für die die Header gelten |
| | | * @throws IOException falls etwas schief geht entsteht dieser Fehler |
| | | */ |
| | | private void setCommonHeaders(Headers resHeaders, File file) throws IOException { |
| | | resHeaders.add(ACCEPT_RANGES_HEADER, STR_BYTES); |
| | | String mimeType = Files.probeContentType(file.toPath()); |
| | | if (mimeType != null) { |
| | | resHeaders.add(CONTENT_TYPE, mimeType); |
| | | } |
| | | SimpleDateFormat sdf = new SimpleDateFormat(LM_PATTERN); |
| | | Date date = new Date(file.lastModified()); |
| | | resHeaders.add(LAST_MODIFIED_DATE_HEADER, sdf.format(date)); |
| | | } |
| | | |
| | | /** |
| | | * 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 { |
| | | private void sendNotFound(HttpExchange e, String fname) throws IOException { |
| | | OutputStream os = e.getResponseBody(); |
| | | String response = fname + " not found."; |
| | | String response = fname + STR_NOT_FOUND; |
| | | byte[] bytes = response.getBytes(StandardCharsets.UTF_8); |
| | | e.sendResponseHeaders(SC_NOT_FOUND, bytes.length); |
| | | os.write(bytes); |
| | |
| | | } |
| | | |
| | | /** |
| | | * Eine Range |
| | | * Eine Range bezeichnet einen zusammenhängenden Bereich |
| | | * aus Bytes, der sich aus den Bytepositionen des Beginns und Endes |
| | | * des Bereiches ergibt. |
| | | */ |
| | | class Range { |
| | | |