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.FileInputStream; |
|
24 |
import java.io.IOException; |
|
25 |
import java.io.InputStream; |
|
26 |
import java.io.OutputStream; |
|
27 |
import java.nio.file.Files; |
|
28 |
import java.text.SimpleDateFormat; |
|
29 |
import java.util.Date; |
|
30 |
|
|
31 |
/** |
|
32 |
* Helfer zur Beantwortung von HTTP-Anfragen |
|
33 |
* |
|
34 |
* @author Ulrich Hilger |
|
35 |
* @version 1, 03.06.2021 |
|
36 |
*/ |
|
37 |
public class HttpResponder { |
|
38 |
|
|
39 |
/* Headernamen */ |
|
40 |
public static final String ACCEPT_RANGES_HEADER = "Accept-Ranges"; |
|
41 |
public static final String CONTENT_LENGTH = "Content-Length"; |
|
42 |
public static final String CONTENT_TYPE = "Content-Type"; |
|
43 |
public static final String LAST_MODIFIED_DATE_HEADER = "Last-Modified"; |
|
44 |
|
|
45 |
/* Statuscodes */ |
|
46 |
public static final int SC_OK = 200; |
|
47 |
public static final int SC_NOT_FOUND = 404; |
90ccfb
|
48 |
public static final int SC_METHOD_NOT_ALLOWED = 405; |
45e522
|
49 |
public static final int SC_UNPROCESSABLE_ENTITY = 422; |
90ccfb
|
50 |
public static final int SC_INTERNAL_SERVER_ERROR = 500; |
U |
51 |
|
e58690
|
52 |
/* String Konstanten */ |
U |
53 |
public static final String STR_BYTES = "bytes"; |
|
54 |
public static final String STR_NOT_FOUND = " not found."; |
|
55 |
public static final String LM_PATTERN = "EEE, dd MMM yyyy HH:mm:ss zzz"; |
|
56 |
|
|
57 |
/** |
|
58 |
* Den Inhalt einer Datei ausliefern |
|
59 |
* |
|
60 |
* @param e das Objekt mit Methoden zur Untersuchung der Anfrage sowie zum |
|
61 |
* Anfertigen und Senden der Antwort |
|
62 |
* @param file die Datei, deren Inhalt ausgeliefert werden soll |
|
63 |
* @throws IOException falls etwas schief geht entsteht dieser Fehler |
|
64 |
*/ |
|
65 |
public void serveFile(HttpExchange e, File file) throws IOException { |
|
66 |
if (file.exists()) { |
|
67 |
setHeaders(e, file); |
|
68 |
e.getResponseHeaders().set(CONTENT_LENGTH, Long.toString(file.length())); |
|
69 |
e.sendResponseHeaders(SC_OK, file.length()); |
|
70 |
if(HttpHelper.HTTP_GET.equalsIgnoreCase(e.getRequestMethod())) { |
|
71 |
InputStream in = new FileInputStream(file); |
|
72 |
OutputStream os = e.getResponseBody(); |
|
73 |
write(in, os); |
|
74 |
finish(in, os); |
|
75 |
} |
|
76 |
} else { |
|
77 |
sendNotFound(e, file.getName()); |
|
78 |
} |
|
79 |
} |
|
80 |
|
|
81 |
public void write(InputStream in, OutputStream out) throws IOException { |
|
82 |
byte[] b = new byte[4096]; |
|
83 |
int bytesRead = in.read(b); |
|
84 |
while (bytesRead > -1) { |
|
85 |
out.write(b, 0, bytesRead); |
|
86 |
bytesRead = in.read(b); |
|
87 |
} |
|
88 |
} |
|
89 |
|
|
90 |
public void finish(InputStream in, OutputStream out) throws IOException { |
|
91 |
in.close(); |
|
92 |
finish(out); |
|
93 |
} |
|
94 |
|
|
95 |
public void finish(OutputStream out) throws IOException { |
|
96 |
out.flush(); |
|
97 |
out.close(); |
|
98 |
} |
|
99 |
|
|
100 |
/** |
|
101 |
* Die Header erzeugen, die unabhängig davon, ob der ganze |
|
102 |
* Inhalt oder nur Teile davon ausgeliefert werden sollen, in der |
|
103 |
* Antwort stehen sollen |
|
104 |
* |
|
105 |
* @param e das Objekt mit Methoden zur Untersuchung der Anfrage sowie zum |
|
106 |
* Anfertigen und Senden der Antwort |
|
107 |
* @param file die Datei, für die die Header gelten |
|
108 |
* @throws IOException falls etwas schief geht entsteht dieser Fehler |
|
109 |
*/ |
|
110 |
public void setHeaders(HttpExchange e, File file) throws IOException { |
|
111 |
Headers resHeaders = e.getResponseHeaders(); |
|
112 |
resHeaders.add(ACCEPT_RANGES_HEADER, STR_BYTES); |
|
113 |
String mimeType = Files.probeContentType(file.toPath()); |
|
114 |
if (mimeType != null) { |
|
115 |
resHeaders.add(CONTENT_TYPE, mimeType); |
|
116 |
} |
|
117 |
SimpleDateFormat sdf = new SimpleDateFormat(LM_PATTERN); |
|
118 |
Date date = new Date(file.lastModified()); |
|
119 |
resHeaders.add(LAST_MODIFIED_DATE_HEADER, sdf.format(date)); |
|
120 |
} |
|
121 |
|
|
122 |
/** |
|
123 |
* Eine nicht gefunden Antwort senden |
|
124 |
* |
|
125 |
* @param e das Objekt mit Methoden zur Untersuchung der Anfrage sowie zum |
|
126 |
* Anfertigen und Senden der Antwort |
|
127 |
* @param fname Name der Datei, die nicht gefunden wurde |
|
128 |
* @throws IOException falls etwas schief geht entsteht dieser Fehler |
|
129 |
*/ |
|
130 |
public void sendNotFound(HttpExchange e, String fname) throws IOException { |
|
131 |
antwortSenden(e, SC_NOT_FOUND, fname + STR_NOT_FOUND); |
|
132 |
} |
|
133 |
|
|
134 |
public void antwortSenden(HttpExchange exchange, int code, String antwort) throws IOException { |
|
135 |
byte[] bytes = antwort.getBytes(); |
|
136 |
exchange.sendResponseHeaders(code, bytes.length); |
|
137 |
OutputStream os = exchange.getResponseBody(); |
|
138 |
os.write(bytes); |
|
139 |
finish(os); |
|
140 |
} |
|
141 |
|
|
142 |
|
|
143 |
|
|
144 |
} |