| | |
| | | import java.util.Date; |
| | | import java.util.Iterator; |
| | | import java.util.List; |
| | | import java.util.Set; |
| | | import java.util.logging.Logger; |
| | | |
| | | /** |
| | |
| | | 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(); |
| | | headers.add(ACCEPT_RANGES_HEADER, "bytes"); |
| | | 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(); |
| | | resHeaders.add(ACCEPT_RANGES_HEADER, "bytes"); |
| | | 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"); |
| | |
| | | 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 { |