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.HttpExchange; |
|
21 |
import java.io.BufferedReader; |
|
22 |
import java.io.IOException; |
|
23 |
import java.io.InputStream; |
|
24 |
import java.io.InputStreamReader; |
|
25 |
import java.util.Arrays; |
|
26 |
import java.util.HashMap; |
|
27 |
import java.util.List; |
|
28 |
import java.util.Map; |
|
29 |
|
|
30 |
/** |
|
31 |
* Helfer fuer HTTP-Methoden |
|
32 |
* |
|
33 |
* @author Ulrich Hilger |
|
34 |
* @version 1, 01.06.2021 |
|
35 |
*/ |
|
36 |
public class HttpHelper { |
|
37 |
|
|
38 |
/* HTTP Methoden */ |
|
39 |
public static final String HTTP_GET = "GET"; |
|
40 |
public static final String HTTP_POST = "POST"; |
|
41 |
public static final String HTTP_PUT = "PUT"; |
|
42 |
public static final String HTTP_DELETE = "DELETE"; |
|
43 |
|
|
44 |
public static final String STR_AMP = "&"; |
|
45 |
public static final String STR_EQUAL = "="; |
|
46 |
|
|
47 |
//public static final String CONTENT_TYPE = "Content-Type"; |
|
48 |
|
|
49 |
public static final String CT_JSON = "application/json; charset=UTF-8"; |
|
50 |
public static final String CT_TEXT_HTML = "text/html"; |
|
51 |
|
|
52 |
/** |
|
53 |
* Den Namen der gewünschten Datei aus der HTTP-Anfrage ermitteln |
|
54 |
* |
|
55 |
* @param e das Objekt mit Methoden zur Untersuchung der Anfrage sowie zum |
|
56 |
* Anfertigen und Senden der Antwort |
|
57 |
* @return Name der gewünschten Datei |
19c3c5
|
58 |
* @throws IllegalArgumentException wenn der Dateiname ungueltige Zeichen |
U |
59 |
* enthaelt, z.B. ../ |
e58690
|
60 |
*/ |
19c3c5
|
61 |
public String getFileName(HttpExchange e) throws IllegalArgumentException { |
e58690
|
62 |
String ctxPath = e.getHttpContext().getPath(); |
U |
63 |
String uriPath = e.getRequestURI().getPath(); |
a1027d
|
64 |
return inspectFileName(uriPath.substring(ctxPath.length())); |
19c3c5
|
65 |
} |
U |
66 |
|
a1027d
|
67 |
public String inspectFileName(String fileName) throws IllegalArgumentException { |
19c3c5
|
68 |
if (fileName == null |
a1027d
|
69 |
|| fileName.contains("..")) { |
U |
70 |
//|| fileName.contains("/") |
|
71 |
//|| fileName.contains("\\")) { |
19c3c5
|
72 |
throw new IllegalArgumentException("Invalid file name"); |
U |
73 |
} |
|
74 |
return fileName; |
e58690
|
75 |
} |
U |
76 |
|
|
77 |
public String bodyLesen(HttpExchange exchange) throws IOException { |
|
78 |
StringBuilder sb = new StringBuilder(); |
|
79 |
InputStream is = exchange.getRequestBody(); |
|
80 |
BufferedReader in = new BufferedReader(new InputStreamReader(is)); |
|
81 |
String line = in.readLine(); |
|
82 |
while (line != null) { |
|
83 |
sb.append(line); |
|
84 |
line = in.readLine(); |
|
85 |
} |
|
86 |
return sb.toString(); |
|
87 |
} |
|
88 |
|
|
89 |
public Map<String, String> getQueryMap(HttpExchange exchange) { |
|
90 |
if(exchange.getRequestMethod().equalsIgnoreCase("GET")) { |
|
91 |
return getQueryMap(exchange.getRequestURI().getQuery()); |
|
92 |
} else { |
|
93 |
try { |
|
94 |
return getQueryMap(bodyLesen(exchange)); |
|
95 |
} catch (IOException ex) { |
|
96 |
return new HashMap<>(); |
|
97 |
} |
|
98 |
} |
|
99 |
} |
|
100 |
|
|
101 |
public Map<String, String> getQueryMap(String query) { |
|
102 |
Map<String, String> map = new HashMap<>(); |
63bcde
|
103 |
try { |
U |
104 |
if(query instanceof String) { |
|
105 |
String[] params = query.split(STR_AMP); |
|
106 |
for (String param : params) { |
|
107 |
String name = param.split(STR_EQUAL)[0]; |
|
108 |
String value = param.split(STR_EQUAL)[1]; |
|
109 |
map.put(name, value); |
|
110 |
} |
|
111 |
} else { |
|
112 |
// map bleibt leer |
|
113 |
} |
|
114 |
} catch(Exception ex) { |
|
115 |
|
|
116 |
} finally { |
|
117 |
return map; |
e58690
|
118 |
} |
U |
119 |
} |
|
120 |
|
|
121 |
public String getRouteString(HttpExchange exchange) { |
|
122 |
return getFileName(exchange); |
|
123 |
} |
|
124 |
|
|
125 |
public List<String> getRouteList(String routeString) { |
|
126 |
return Arrays.asList(routeString.split("/")); |
19c3c5
|
127 |
} |
e58690
|
128 |
} |