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 |
|
|
29 |
/** |
|
30 |
* Die Klasse FileHandler dient zur Auslieferung von Dateiinhalten über |
|
31 |
* HTTP. |
|
32 |
* |
|
33 |
* Für das Streaming über HTTP wird die Auslieferung von Teilinhalten |
|
34 |
* mit dem Accept-Ranges-Header angeboten und via Range-Header unterstützt. |
|
35 |
* (vgl. https://developer.mozilla.org/en-US/docs/Web/HTTP/Range_requests) |
|
36 |
* |
|
37 |
* @author Ulrich Hilger |
90f2d3
|
38 |
* @version 1, 03.06.2021, (seit 25. März 2021) |
069fd4
|
39 |
*/ |
U |
40 |
public class FileHandler implements HttpHandler { |
|
41 |
|
|
42 |
/* Headernamen */ |
|
43 |
public static final String RANGE_HEADER = "Range"; |
|
44 |
public static final String CONTENT_RANGE_HEADER = "Content-Range"; |
|
45 |
public static final String CONTENT_TYPE = "Content-Type"; |
|
46 |
public static final String CONTENT_LENGTH = "Content-Length"; |
|
47 |
|
|
48 |
/* Statuscodes */ |
|
49 |
public static final int SC_OK = 200; |
|
50 |
public static final int SC_PARTIAL_CONTENT = 206; |
e88b7b
|
51 |
public static final int SC_FORBIDDEN = 403; |
069fd4
|
52 |
public static final int SC_NOT_FOUND = 404; |
U |
53 |
public static final int SC_METHOD_NOT_ALLOWED = 405; |
|
54 |
public static final int SC_UNPROCESSABLE_ENTITY = 422; |
|
55 |
|
|
56 |
/* String Konstanten */ |
|
57 |
public static final String STR_SLASH = "/"; |
|
58 |
public static final String STR_BLANK = " "; |
|
59 |
public static final String STR_DASH = "-"; |
|
60 |
public static final String STR_COMMA = ","; |
|
61 |
public static final String STR_DOT = "."; |
|
62 |
public static final String RANGE_PATTERN = "[^\\d-,]"; |
|
63 |
public static final String WELCOME_FILE = "index.html"; |
|
64 |
|
|
65 |
/* Ablageort fuer Webinhalte */ |
a488df
|
66 |
public static final String ATTR_FILE_BASE = "fileBase"; |
4e2a31
|
67 |
|
U |
68 |
/* moegliche Dateinamen, wenn kein Name angegeben wurde */ |
73b5b8
|
69 |
public static final String ATTR_WELCOME_FILES = "welcomeFiles"; |
069fd4
|
70 |
|
U |
71 |
/** |
|
72 |
* Die Datei ermitteln, die sich aus dem angefragten URL ergibt, prüfen, |
|
73 |
* ob die Datei existiert und den Inhalt der Datei abhängig davon, ob ein |
|
74 |
* Range-Header vorhanden ist, ganz oder teilweise ausliefern. |
|
75 |
* |
|
76 |
* @param e das Objekt mit Methoden zur Untersuchung der Anfrage sowie zum |
|
77 |
* Anfertigen und Senden der Antwort |
|
78 |
* @throws IOException falls etwas schief geht entsteht dieser Fehler |
|
79 |
*/ |
|
80 |
@Override |
|
81 |
public void handle(HttpExchange e) throws IOException { |
73b5b8
|
82 |
String fName = new HttpHelper().getFileName(e); |
069fd4
|
83 |
if (fName.startsWith(STR_DOT)) { |
U |
84 |
HttpResponder fs = new HttpResponder(); |
|
85 |
fs.sendNotFound(e, fName); |
|
86 |
} else { |
4e2a31
|
87 |
File fileToDeliver = new File(e.getHttpContext().getAttributes().get(ATTR_FILE_BASE).toString(), fName); |
069fd4
|
88 |
Headers headers = e.getRequestHeaders(); |
U |
89 |
if (headers.containsKey(RANGE_HEADER)) { |
4e2a31
|
90 |
new FileActor().serveFileParts(e, fileToDeliver); |
069fd4
|
91 |
} else { |
73b5b8
|
92 |
if (fName.length() < 1 || fName.endsWith(STR_SLASH)) { |
U |
93 |
HttpHelper helper = new HttpHelper(); |
|
94 |
File welcomeFile = helper.tryWelcomeFiles(e, fName); |
|
95 |
if(welcomeFile != null) { |
4e2a31
|
96 |
fileToDeliver = welcomeFile; |
73b5b8
|
97 |
} |
U |
98 |
} |
4e2a31
|
99 |
new HttpResponder().serveFile(e, fileToDeliver); |
069fd4
|
100 |
} |
U |
101 |
} |
|
102 |
} |
4e2a31
|
103 |
} |