commit | author | age
|
e58690
|
1 |
/* |
U |
2 |
neon - Embeddable HTTP Server based on jdk.httpserver |
|
3 |
Copyright (C) 2024 Ulrich Hilger |
|
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.neon; |
|
19 |
|
|
20 |
import com.sun.net.httpserver.Headers; |
|
21 |
import com.sun.net.httpserver.HttpExchange; |
|
22 |
import java.io.File; |
|
23 |
import java.io.IOException; |
|
24 |
|
|
25 |
/** |
|
26 |
* Objekte der Klasse FileServer liefern Dateien ueber HTTP aus |
|
27 |
* |
|
28 |
* Im Attribut FileServer.ATTR_FILE_BASE in |
|
29 |
* HttpExchange.getContext().getAttributes() wird das |
|
30 |
* Verzeichnis erwartet, aus dem Dateien ausgeliefert |
|
31 |
* werden sollen. |
|
32 |
* |
|
33 |
* Im Attribut FileServer.ATTR_WELCOME_FILES in |
|
34 |
* HttpExchange.getContext().getAttributes() kann eine |
|
35 |
* mit Komma getrennte Liste mit Dateinamen uebergeben |
|
36 |
* werden, die ausgeliefert werden sollen, wenn ein |
|
37 |
* Verzeichnis anstelle einer Datei angefragt ist. |
|
38 |
* |
|
39 |
* @author Ulrich Hilger |
|
40 |
*/ |
|
41 |
public class FileServer { |
|
42 |
|
|
43 |
public static final String RANGE_HEADER = "Range"; |
|
44 |
|
|
45 |
public static final String ATTR_FILE_BASE = "fileBase"; |
|
46 |
public static final String ATTR_WELCOME_FILES = "welcomeFiles"; |
|
47 |
|
|
48 |
public static final String DEFAULT_INDEX_FILE = "index.html"; |
|
49 |
|
|
50 |
public static final String STR_SLASH = "/"; |
|
51 |
public static final String STR_BLANK = " "; |
|
52 |
public static final String STR_DASH = "-"; |
|
53 |
public static final String STR_COMMA = ","; |
|
54 |
public static final String STR_DOT = "."; |
|
55 |
public static final String STR_EMPTY = ""; |
|
56 |
|
|
57 |
public void serveFile(HttpExchange exchange) throws IOException { |
|
58 |
String fName = new HttpHelper().getFileName(exchange); |
|
59 |
if (fName.startsWith(".")) { |
|
60 |
HttpResponder fs = new HttpResponder(); |
|
61 |
fs.sendNotFound(exchange, fName); |
|
62 |
} else { |
|
63 |
File fileToDeliver = new File((String) exchange |
|
64 |
.getHttpContext().getAttributes() |
|
65 |
.getOrDefault(ATTR_FILE_BASE, STR_EMPTY), fName); |
|
66 |
Headers headers = exchange.getRequestHeaders(); |
|
67 |
if (headers.containsKey(RANGE_HEADER)) { |
|
68 |
new PartialFileServer().serveFileParts(exchange, fileToDeliver); |
|
69 |
} else { |
|
70 |
if (fName.length() < 1 || fName.endsWith(STR_SLASH)) { |
|
71 |
//HttpHelper helper = new HttpHelper(); |
|
72 |
File welcomeFile = tryWelcomeFiles(exchange, fName); |
|
73 |
if(welcomeFile != null) { |
|
74 |
fileToDeliver = welcomeFile; |
|
75 |
} |
|
76 |
} |
|
77 |
new HttpResponder().serveFile(exchange, fileToDeliver); |
|
78 |
} |
|
79 |
} |
|
80 |
} |
|
81 |
|
|
82 |
public File tryWelcomeFiles(HttpExchange e, String fName) { |
|
83 |
boolean notFound = true; |
|
84 |
File file = null; |
|
85 |
String fileBase = (String) e.getHttpContext().getAttributes() |
|
86 |
.getOrDefault(ATTR_FILE_BASE, STR_EMPTY); |
|
87 |
Object welcomeFiles = e.getHttpContext().getAttributes().get(ATTR_WELCOME_FILES); |
|
88 |
if(welcomeFiles instanceof String) { |
|
89 |
String[] fileNames = welcomeFiles.toString().split(STR_COMMA); |
|
90 |
int i = -1; |
|
91 |
while(notFound && ++i < fileNames.length) { |
|
92 |
file = new File(fileBase, fName + fileNames[i]); |
|
93 |
if(file.exists()) { |
|
94 |
notFound = false; |
|
95 |
} |
|
96 |
} |
|
97 |
} |
|
98 |
if(notFound) { |
|
99 |
file = new File(fileBase, fName + DEFAULT_INDEX_FILE); |
|
100 |
} |
|
101 |
return file; |
|
102 |
} |
|
103 |
} |