From ff7e5b88e75bda0d99614bead4f3b559023ca50a Mon Sep 17 00:00:00 2001 From: ulrich Date: Fri, 26 Mar 2021 16:11:42 +0000 Subject: [PATCH] FileHandler Behandlung gemeinsamer Response Headers --- src/de/uhilger/minsrv/handler/FileHandler.java | 56 +++++++++++++++++++++++--------------------------------- 1 files changed, 23 insertions(+), 33 deletions(-) diff --git a/src/de/uhilger/minsrv/handler/FileHandler.java b/src/de/uhilger/minsrv/handler/FileHandler.java index 46929f6..129cd57 100644 --- a/src/de/uhilger/minsrv/handler/FileHandler.java +++ b/src/de/uhilger/minsrv/handler/FileHandler.java @@ -26,18 +26,23 @@ 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 */ @@ -83,35 +88,16 @@ @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(".")) { 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"; } @@ -132,7 +118,7 @@ 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(); @@ -177,7 +163,7 @@ 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; @@ -195,14 +181,8 @@ sb.append("/"); sb.append(file.length()); resHeaders.add(CONTENT_RANGE_HEADER, sb.toString()); - logger.info(sb.toString()); 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); @@ -221,6 +201,17 @@ sendNotFound(e, file.getName()); } } + + private void setCommonHeaders(Headers resHeaders, File file) throws IOException { + resHeaders.add(ACCEPT_RANGES_HEADER, "bytes"); + String mimeType = Files.probeContentType(file.toPath()); + if(mimeType != null) { + resHeaders.add(CONTENT_TYPE, mimeType); + } + 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)); + } /** * Die Byte-Ranges aus dem Range-Header ermitteln. @@ -237,7 +228,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 @@ -251,7 +242,6 @@ Ziffern 0-9, Bindestrich oder Komma sind. */ rangeHeader = rangeHeader.replaceAll("[^\\d-,]", ""); - logger.info(rangeHeader); /* Die Ranges ermitteln. -- Gitblit v1.9.3