| | |
| | | import java.util.Date; |
| | | import java.util.Iterator; |
| | | import java.util.List; |
| | | import java.util.Set; |
| | | import java.util.logging.Logger; |
| | | |
| | | /** |
| | |
| | | @Override |
| | | public void handle(HttpExchange e) throws IOException { |
| | | String ctxPath = e.getHttpContext().getPath(); |
| | | logger.finer(ctxPath); |
| | | String uriPath = e.getRequestURI().getPath(); |
| | | logger.finer(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"; |
| | | } |
| | | serveFile(e, new File(basePath, 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(); |
| | | } |
| | | |
| | | /** |
| | |
| | | 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; |
| | |
| | | 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 |
| | | |
| | |
| | | } |
| | | |
| | | /** |
| | | * 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 { |