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 |
|
58 |
*/ |
|
59 |
public String getFileName(HttpExchange e) { |
|
60 |
String ctxPath = e.getHttpContext().getPath(); |
|
61 |
String uriPath = e.getRequestURI().getPath(); |
|
62 |
return uriPath.substring(ctxPath.length()); |
|
63 |
} |
|
64 |
|
|
65 |
public String bodyLesen(HttpExchange exchange) throws IOException { |
|
66 |
StringBuilder sb = new StringBuilder(); |
|
67 |
InputStream is = exchange.getRequestBody(); |
|
68 |
BufferedReader in = new BufferedReader(new InputStreamReader(is)); |
|
69 |
String line = in.readLine(); |
|
70 |
while (line != null) { |
|
71 |
sb.append(line); |
|
72 |
line = in.readLine(); |
|
73 |
} |
|
74 |
return sb.toString(); |
|
75 |
} |
|
76 |
|
|
77 |
/*public String getAttrStr(Map attributes, String key, String defaultValue) { |
|
78 |
Object value = attributes.get(key); |
|
79 |
if(value instanceof String) { |
|
80 |
return value.toString(); |
|
81 |
} else { |
|
82 |
return defaultValue; |
|
83 |
} |
|
84 |
} */ |
|
85 |
|
|
86 |
public Map<String, String> getQueryMap(HttpExchange exchange) { |
|
87 |
if(exchange.getRequestMethod().equalsIgnoreCase("GET")) { |
|
88 |
return getQueryMap(exchange.getRequestURI().getQuery()); |
|
89 |
} else { |
|
90 |
try { |
|
91 |
return getQueryMap(bodyLesen(exchange)); |
|
92 |
} catch (IOException ex) { |
|
93 |
return new HashMap<>(); |
|
94 |
} |
|
95 |
} |
|
96 |
} |
|
97 |
|
|
98 |
public Map<String, String> getQueryMap(String query) { |
|
99 |
Map<String, String> map = new HashMap<>(); |
|
100 |
if(query instanceof String) { |
|
101 |
String[] params = query.split(STR_AMP); |
|
102 |
for (String param : params) { |
|
103 |
String name = param.split(STR_EQUAL)[0]; |
|
104 |
String value = param.split(STR_EQUAL)[1]; |
|
105 |
map.put(name, value); |
|
106 |
} |
|
107 |
} else { |
|
108 |
// map bleibt leer |
|
109 |
} |
|
110 |
return map; |
|
111 |
} |
|
112 |
|
|
113 |
/*public String getRouteString(HttpExchange exchange) { |
|
114 |
return exchange.getRequestURI().getPath() |
|
115 |
.substring(exchange.getHttpContext().getPath().length()); |
|
116 |
}*/ |
|
117 |
|
|
118 |
public String getRouteString(HttpExchange exchange) { |
|
119 |
return getFileName(exchange); |
|
120 |
} |
|
121 |
|
|
122 |
public List<String> getRouteList(String routeString) { |
|
123 |
return Arrays.asList(routeString.split("/")); |
|
124 |
} |
|
125 |
|
|
126 |
/* |
|
127 |
public File tryWelcomeFiles(HttpExchange e, String fName) { |
|
128 |
boolean notFound = true; |
|
129 |
File file = null; |
|
130 |
String fileBase = e.getHttpContext().getAttributes().get(FileHandler.ATTR_FILE_BASE).toString(); |
|
131 |
Object welcomeFiles = e.getHttpContext().getAttributes().get(FileHandler.ATTR_WELCOME_FILES); |
|
132 |
if(welcomeFiles instanceof String) { |
|
133 |
String[] fileNames = welcomeFiles.toString().split(FileHandler.STR_COMMA); |
|
134 |
int i = -1; |
|
135 |
while(notFound && ++i < fileNames.length) { |
|
136 |
file = new File(fileBase, fName + fileNames[i]); |
|
137 |
if(file.exists()) { |
|
138 |
notFound = false; |
|
139 |
} |
|
140 |
} |
|
141 |
} |
|
142 |
if(notFound) { |
|
143 |
file = new File(fileBase, fName + FileHandler.WELCOME_FILE); |
|
144 |
} |
|
145 |
return file; |
|
146 |
} |
|
147 |
*/ |
|
148 |
|
|
149 |
} |