| | |
| | | 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; |
| | |
| | | /** |
| | | * 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 |
| | | */ |
| | |
| | | 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()); |
| | | if (fName.startsWith(".")) { |
| | | sendNotFound(e, fName); |
| | |
| | | } |
| | | } |
| | | |
| | | public void sendNotFound(HttpExchange e, String fname) throws IOException { |
| | | OutputStream os = e.getResponseBody(); |
| | | String response = fname + " not found."; |
| | | byte[] bytes = response.getBytes(StandardCharsets.UTF_8); |
| | | e.sendResponseHeaders(SC_NOT_FOUND, bytes.length); |
| | | os.write(bytes); |
| | | os.flush(); |
| | | os.close(); |
| | | } |
| | | |
| | | /** |
| | | * Den Inhalt einer Datei ausliefern |
| | | * |
| | |
| | | private void serveFile(HttpExchange e, File file) throws IOException { |
| | | if (file.exists()) { |
| | | OutputStream os = e.getResponseBody(); |
| | | Headers headers = e.getResponseHeaders(); |
| | | setCommonHeaders(headers, file); |
| | | e.sendResponseHeaders(SC_OK, file.length()); |
| | | InputStream in = new FileInputStream(file); |
| | | int b = in.read(); |
| | |
| | | os.close(); |
| | | } else { |
| | | sendNotFound(e, file.getName()); |
| | | /* |
| | | String response = file.getName() + " not found."; |
| | | byte[] bytes = response.getBytes(StandardCharsets.UTF_8); |
| | | e.sendResponseHeaders(SC_NOT_FOUND, bytes.length); |
| | | os.write(bytes); |
| | | */ |
| | | } |
| | | } |
| | | |
| | |
| | | InputStream is = new FileInputStream(file); |
| | | OutputStream os = e.getResponseBody(); |
| | | Headers resHeaders = e.getResponseHeaders(); |
| | | setCommonHeaders(resHeaders, file); |
| | | long responseLength = 0; |
| | | long start = 0; |
| | | long end; |
| | |
| | | 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); |
| | |
| | | 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. |
| | |
| | | private RangeGroup parseRanges(HttpExchange e, File file) { |
| | | RangeGroup ranges = new RangeGroup(); |
| | | String rangeHeader = e.getRequestHeaders().get(RANGE_HEADER).toString(); |
| | | |
| | | |
| | | /* |
| | | Inhalt des Range-Headers von nicht benoetigten Angaben befreien |
| | | |
| | |
| | | Ziffern 0-9, Bindestrich oder Komma sind. |
| | | */ |
| | | rangeHeader = rangeHeader.replaceAll("[^\\d-,]", ""); |
| | | logger.info(rangeHeader); |
| | | |
| | | /* |
| | | Die Ranges ermitteln. |
| | |
| | | } |
| | | |
| | | /** |
| | | * 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 { |
| | | OutputStream os = e.getResponseBody(); |
| | | String response = fname + " not found."; |
| | | byte[] bytes = response.getBytes(StandardCharsets.UTF_8); |
| | | e.sendResponseHeaders(SC_NOT_FOUND, bytes.length); |
| | | os.write(bytes); |
| | | os.flush(); |
| | | os.close(); |
| | | } |
| | | |
| | | /** |
| | | * Eine Range |
| | | */ |
| | | class Range { |