commit | author | age
|
069fd4
|
1 |
/* |
90f2d3
|
2 |
http-base - Extensions to jdk.httpserver |
069fd4
|
3 |
Copyright (C) 2021 Ulrich Hilger |
U |
4 |
|
|
5 |
This program is free software: you can redistribute it and/or modify |
|
6 |
it under the terms of the GNU Affero General Public License as |
|
7 |
published by the Free Software Foundation, either version 3 of the |
|
8 |
License, or (at your option) any later version. |
|
9 |
|
|
10 |
This program is distributed in the hope that it will be useful, |
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
GNU Affero General Public License for more details. |
|
14 |
|
|
15 |
You should have received a copy of the GNU Affero General Public License |
|
16 |
along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
17 |
*/ |
|
18 |
package de.uhilger.httpserver.base.handler; |
|
19 |
|
786e8c
|
20 |
import de.uhilger.httpserver.base.HttpResponder; |
069fd4
|
21 |
import com.sun.net.httpserver.Headers; |
U |
22 |
import com.sun.net.httpserver.HttpExchange; |
|
23 |
import com.sun.net.httpserver.HttpHandler; |
73b5b8
|
24 |
import de.uhilger.httpserver.base.HttpHelper; |
786e8c
|
25 |
import de.uhilger.httpserver.base.actor.FileActor; |
069fd4
|
26 |
import java.io.File; |
U |
27 |
import java.io.IOException; |
|
28 |
import java.util.logging.Logger; |
|
29 |
|
|
30 |
/** |
|
31 |
* Die Klasse FileHandler dient zur Auslieferung von Dateiinhalten über |
|
32 |
* HTTP. |
|
33 |
* |
|
34 |
* Für das Streaming über HTTP wird die Auslieferung von Teilinhalten |
|
35 |
* mit dem Accept-Ranges-Header angeboten und via Range-Header unterstützt. |
|
36 |
* (vgl. https://developer.mozilla.org/en-US/docs/Web/HTTP/Range_requests) |
|
37 |
* |
|
38 |
* @author Ulrich Hilger |
90f2d3
|
39 |
* @version 1, 03.06.2021, (seit 25. März 2021) |
069fd4
|
40 |
*/ |
U |
41 |
public class FileHandler implements HttpHandler { |
|
42 |
|
|
43 |
/* Der Logger fuer diesen FileHandler */ |
|
44 |
private static final Logger logger = Logger.getLogger(FileHandler.class.getName()); |
|
45 |
|
|
46 |
/* Headernamen */ |
|
47 |
public static final String RANGE_HEADER = "Range"; |
|
48 |
public static final String CONTENT_RANGE_HEADER = "Content-Range"; |
|
49 |
public static final String CONTENT_TYPE = "Content-Type"; |
|
50 |
public static final String CONTENT_LENGTH = "Content-Length"; |
|
51 |
|
|
52 |
/* Statuscodes */ |
|
53 |
public static final int SC_OK = 200; |
|
54 |
public static final int SC_PARTIAL_CONTENT = 206; |
e88b7b
|
55 |
public static final int SC_FORBIDDEN = 403; |
069fd4
|
56 |
public static final int SC_NOT_FOUND = 404; |
U |
57 |
public static final int SC_METHOD_NOT_ALLOWED = 405; |
|
58 |
public static final int SC_UNPROCESSABLE_ENTITY = 422; |
|
59 |
|
|
60 |
/* String Konstanten */ |
|
61 |
public static final String STR_SLASH = "/"; |
|
62 |
public static final String STR_BLANK = " "; |
|
63 |
public static final String STR_DASH = "-"; |
|
64 |
public static final String STR_COMMA = ","; |
|
65 |
public static final String STR_DOT = "."; |
|
66 |
public static final String RANGE_PATTERN = "[^\\d-,]"; |
|
67 |
public static final String WELCOME_FILE = "index.html"; |
|
68 |
|
|
69 |
/* Ablageort fuer Webinhalte */ |
a488df
|
70 |
public static final String ATTR_FILE_BASE = "fileBase"; |
4e2a31
|
71 |
|
U |
72 |
/* moegliche Dateinamen, wenn kein Name angegeben wurde */ |
73b5b8
|
73 |
public static final String ATTR_WELCOME_FILES = "welcomeFiles"; |
069fd4
|
74 |
|
U |
75 |
/** |
|
76 |
* Die Datei ermitteln, die sich aus dem angefragten URL ergibt, prüfen, |
|
77 |
* ob die Datei existiert und den Inhalt der Datei abhängig davon, ob ein |
|
78 |
* Range-Header vorhanden ist, ganz oder teilweise ausliefern. |
|
79 |
* |
|
80 |
* @param e das Objekt mit Methoden zur Untersuchung der Anfrage sowie zum |
|
81 |
* Anfertigen und Senden der Antwort |
|
82 |
* @throws IOException falls etwas schief geht entsteht dieser Fehler |
|
83 |
*/ |
|
84 |
@Override |
|
85 |
public void handle(HttpExchange e) throws IOException { |
73b5b8
|
86 |
String fName = new HttpHelper().getFileName(e); |
069fd4
|
87 |
if (fName.startsWith(STR_DOT)) { |
U |
88 |
HttpResponder fs = new HttpResponder(); |
|
89 |
fs.sendNotFound(e, fName); |
|
90 |
} else { |
4e2a31
|
91 |
File fileToDeliver = new File(e.getHttpContext().getAttributes().get(ATTR_FILE_BASE).toString(), fName); |
069fd4
|
92 |
Headers headers = e.getRequestHeaders(); |
U |
93 |
if (headers.containsKey(RANGE_HEADER)) { |
4e2a31
|
94 |
new FileActor().serveFileParts(e, fileToDeliver); |
069fd4
|
95 |
} else { |
73b5b8
|
96 |
if (fName.length() < 1 || fName.endsWith(STR_SLASH)) { |
U |
97 |
HttpHelper helper = new HttpHelper(); |
|
98 |
File welcomeFile = helper.tryWelcomeFiles(e, fName); |
|
99 |
if(welcomeFile != null) { |
4e2a31
|
100 |
fileToDeliver = welcomeFile; |
73b5b8
|
101 |
} |
U |
102 |
} |
4e2a31
|
103 |
new HttpResponder().serveFile(e, fileToDeliver); |
069fd4
|
104 |
} |
U |
105 |
} |
|
106 |
} |
4e2a31
|
107 |
} |