Skip to content
This repository was archived by the owner on Jul 31, 2022. It is now read-only.

Commit 1d4e36a

Browse files
committed
Update SimpleHttpExchangeImpl.java
1 parent d50320d commit 1d4e36a

1 file changed

Lines changed: 44 additions & 38 deletions

File tree

src/ktt/lib/httpserver/server/SimpleHttpExchangeImpl.java

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ abstract class SimpleHttpExchangeImpl {
3434
* @since 02.00.00
3535
* @author Ktt Development
3636
*/
37+
@SuppressWarnings({"rawtypes","unchecked"})
3738
static SimpleHttpExchange createSimpleHttpExchange(final HttpExchange exchange){
3839
return new SimpleHttpExchange() {
3940

@@ -63,21 +64,25 @@ static SimpleHttpExchange createSimpleHttpExchange(final HttpExchange exchange){
6364

6465
private boolean closed = false;
6566

67+
//
68+
6669
private final Function<String,HashMap<String,String>> parseWwwFormEnc = s -> {
67-
final LinkedHashMap<String,String> OUT = new LinkedHashMap<>();
68-
final String[] pairs = s.split("&");
69-
70-
for(final String pair : pairs){
71-
if(pair.contains("=")){
72-
final String[] kv = pair.split("=");
73-
OUT.put(
74-
URLDecoder.decode(kv[0],StandardCharsets.UTF_8),
75-
kv.length == 2 ? URLDecoder.decode(kv[1],StandardCharsets.UTF_8) : null
76-
);
77-
}
78-
}
79-
return OUT;
80-
};
70+
final LinkedHashMap<String,String> OUT = new LinkedHashMap<>();
71+
final String[] pairs = s.split("&");
72+
73+
for(final String pair : pairs){
74+
if(pair.contains("=")){
75+
final String[] kv = pair.split("=");
76+
OUT.put(
77+
URLDecoder.decode(kv[0],StandardCharsets.UTF_8),
78+
kv.length == 2 ? URLDecoder.decode(kv[1],StandardCharsets.UTF_8) : null
79+
);
80+
}
81+
}
82+
return OUT;
83+
};
84+
85+
//
8186

8287
{
8388
httpServer = (httpContext = exchange.getHttpContext()).getServer();
@@ -135,38 +140,38 @@ static SimpleHttpExchange createSimpleHttpExchange(final HttpExchange exchange){
135140
hasPost = (rawPost = OUT) != null;
136141

137142
// region todo: optimize this
138-
final String content_type = requestHeaders.getFirst("Content-type");
139-
if(content_type != null && content_type.startsWith("multipart/form-data")){
140-
final Pattern hpat = Pattern.compile("(.*): (.*?)(?:$|; )(.*)");
141-
final Pattern vpat = Pattern.compile("(.*?)=\"(.*?)\"(?:; |$)");
143+
if(hasPost){
144+
final String content_type = requestHeaders.getFirst("Content-type");
145+
if(content_type != null && content_type.startsWith("multipart/form-data")){
146+
final Pattern boundaryHeaderPattern = Pattern.compile("(.*): (.*?)(?:$|; )(.*)"); // returns the headers listed after each webkit boundary
147+
final Pattern contentDispositionKVPPattern = Pattern.compile("(.*?)=\"(.*?)\"(?:; |$)"); // returns the keys, values, and parameters for the content disposition header
142148

143-
final String webkitBoundary = content_type.substring(content_type.indexOf("; boundary=") + 11);
144-
final String startBoundary = "--" + webkitBoundary + "\r\n";
145-
final String endBoundary = "--" + webkitBoundary + "--\r\n";
149+
final String webkitBoundary = content_type.substring(content_type.indexOf("; boundary=") + 11);
150+
final String startBoundary = "--" + webkitBoundary + "\r\n";
151+
final String endBoundary = "--" + webkitBoundary + "--\r\n"; // the final boundary in the request
146152

147-
postMap = new HashMap<>();
148-
/* Parse multipart/form-data */ {
153+
postMap = new HashMap<>();
149154
final String[] pairs = OUT.replace(endBoundary,"").split(Pattern.quote(startBoundary));
150-
for (String pair : pairs) {
155+
for(String pair : pairs){
151156
final HashMap<String, HashMap> postHeaders = new HashMap<>();
152-
if (pair.contains("\r\n\r\n")) {
157+
if(pair.contains("\r\n\r\n")){
153158
final String[] headers = pair.substring(0, pair.indexOf("\r\n\r\n")).split("\r\n");
154159

155160
for (String header : headers) {
156-
final HashMap hmap = new HashMap<>();
161+
final HashMap headerMap = new HashMap<>();
157162
final HashMap<String, String> val = new HashMap<>();
158163

159-
final Matcher hmatch = hpat.matcher(header);
160-
if (hmatch.find()) {
161-
final Matcher vmatch = vpat.matcher(hmatch.group(3));
162-
while (vmatch.find()) {
163-
val.put(vmatch.group(1), vmatch.group(2));
164-
}
165-
hmap.put("header-name", hmatch.group(1));
166-
hmap.put("header-value", hmatch.group(2));
167-
hmap.put("parameter", val);
164+
final Matcher headerMatcher = boundaryHeaderPattern.matcher(header);
165+
if (headerMatcher.find()) {
166+
final Matcher contentDispositionKVPMatcher = contentDispositionKVPPattern.matcher(headerMatcher.group(3));
167+
while (contentDispositionKVPMatcher.find())
168+
val.put(contentDispositionKVPMatcher.group(1), contentDispositionKVPMatcher.group(2));
169+
170+
headerMap.put("header-name", headerMatcher.group(1));
171+
headerMap.put("header-value", headerMatcher.group(2));
172+
headerMap.put("parameter", val);
168173
}
169-
postHeaders.put((String) hmap.get("header-name"), hmap);
174+
postHeaders.put((String) headerMap.get("header-name"), headerMap);
170175
}
171176

172177
final HashMap row = new HashMap();
@@ -177,12 +182,13 @@ static SimpleHttpExchange createSimpleHttpExchange(final HttpExchange exchange){
177182
((HashMap<String, String>) postHeaders.get("Content-Disposition").get("parameter")).get("name"),
178183
row
179184
);
180-
181185
}
182186
}
187+
}else{
188+
postMap = parseWwwFormEnc.apply(rawPost);
183189
}
184190
}else{
185-
postMap = parseWwwFormEnc.apply(rawPost);
191+
postMap = new HashMap();
186192
}
187193
// endregion
188194

0 commit comments

Comments
 (0)