|
11 | 11 | import java.net.ServerSocket; |
12 | 12 | import java.net.Socket; |
13 | 13 | import java.net.URLDecoder; |
14 | | -import java.time.Instant; |
15 | | -import java.time.ZoneId; |
16 | | -import java.time.ZonedDateTime; |
17 | | -import java.time.format.DateTimeFormatter; |
| 14 | +import java.text.DateFormat; |
| 15 | +import java.text.SimpleDateFormat; |
18 | 16 | import java.util.ArrayList; |
| 17 | +import java.util.Base64; |
19 | 18 | import java.util.Collection; |
20 | 19 | import java.util.HashMap; |
21 | 20 | import java.util.List; |
| 21 | +import java.util.Locale; |
22 | 22 | import java.util.Map; |
| 23 | +import java.util.TimeZone; |
23 | 24 | import java.util.concurrent.ExecutorService; |
24 | 25 | import java.util.concurrent.Executors; |
25 | 26 | import java.util.zip.ZipEntry; |
|
32 | 33 | * @author chrisws |
33 | 34 | */ |
34 | 35 | public abstract class WebServer { |
35 | | - private static final int BUFFER_SIZE = 32768; |
| 36 | + private static final int BUFFER_SIZE = 32768 / 2; |
36 | 37 | private static final int SEND_SIZE = BUFFER_SIZE / 4; |
37 | 38 | private static final int LINE_SIZE = 128; |
38 | 39 | private static final String UTF_8 = "utf-8"; |
@@ -106,6 +107,7 @@ public abstract static class AbstractRequest { |
106 | 107 | final String tokenKey; |
107 | 108 | final List<String> headers; |
108 | 109 | final InputStream inputStream; |
| 110 | + private static final String BASE_64_PREFIX = ";base64,"; |
109 | 111 |
|
110 | 112 | public AbstractRequest(Socket socket, String tokenKey) throws IOException { |
111 | 113 | this.socket = socket; |
@@ -156,16 +158,24 @@ protected Map<String, Collection<String>> getParameters(String url) throws IOExc |
156 | 158 | /** |
157 | 159 | * Parses HTTP POST data from the given input stream |
158 | 160 | */ |
159 | | - protected Map<String, String> getPostData(InputStream inputStream) throws IOException { |
| 161 | + protected Map<String, Object> getPostData(InputStream inputStream) throws IOException { |
160 | 162 | String postData = getLine(inputStream); |
161 | 163 | String[] fields = postData.split("&"); |
162 | | - Map<String, String> result = new HashMap<>(); |
| 164 | + Map<String, Object> result = new HashMap<>(); |
163 | 165 | for (String nextField : fields) { |
164 | 166 | int eq = nextField.indexOf("="); |
165 | 167 | if (eq != -1) { |
166 | 168 | String key = nextField.substring(0, eq); |
167 | | - String value = URLDecoder.decode(nextField.substring(eq + 1), UTF_8); |
168 | | - result.put(key, value); |
| 169 | + String value = nextField.substring(eq + 1); |
| 170 | + int index = value.indexOf(BASE_64_PREFIX); |
| 171 | + if (index != -1) { |
| 172 | + ByteArrayOutputStream data = new ByteArrayOutputStream(); |
| 173 | + value = value.substring(index + BASE_64_PREFIX.length()); |
| 174 | + data.write(Base64.getDecoder().decode(value)); |
| 175 | + result.put(key, data); |
| 176 | + } else { |
| 177 | + result.put(key, URLDecoder.decode(nextField.substring(eq + 1), UTF_8)); |
| 178 | + } |
169 | 179 | } |
170 | 180 | } |
171 | 181 | return result; |
@@ -253,9 +263,11 @@ public FileData(String fileName, String date, long size) { |
253 | 263 | } |
254 | 264 |
|
255 | 265 | public FileData(File file) { |
256 | | - ZonedDateTime zonedDateTime = Instant.ofEpochMilli(file.lastModified()).atZone(ZoneId.of("UTC")); |
| 266 | + final DateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH); |
| 267 | + format.setLenient(false); |
| 268 | + format.setTimeZone(TimeZone.getTimeZone("UTC")); |
257 | 269 | this.fileName = file.getName(); |
258 | | - this.date = DateTimeFormatter.RFC_1123_DATE_TIME.format(zonedDateTime); |
| 270 | + this.date = format.format(file.lastModified()); |
259 | 271 | this.size = file.length(); |
260 | 272 | } |
261 | 273 | } |
@@ -432,8 +444,8 @@ private void handleGet(Map<String, Collection<String>> parameters) throws IOExce |
432 | 444 | /** |
433 | 445 | * Handler for POST requests |
434 | 446 | */ |
435 | | - private void handlePost(Map<String, String> data) throws IOException { |
436 | | - String userToken = data.get(TOKEN); |
| 447 | + private void handlePost(Map<String, Object> data) throws IOException { |
| 448 | + String userToken = (String) data.get(TOKEN); |
437 | 449 | if (userToken == null) { |
438 | 450 | userToken = requestToken; |
439 | 451 | } |
@@ -464,9 +476,9 @@ private void handlePost(Map<String, String> data) throws IOException { |
464 | 476 | /** |
465 | 477 | * Handler for File rename operations |
466 | 478 | */ |
467 | | - private Response handleRename(Map<String, String> data) throws IOException { |
468 | | - String from = data.get("from"); |
469 | | - String to = data.get("to"); |
| 479 | + private Response handleRename(Map<String, Object> data) throws IOException { |
| 480 | + String from = (String) data.get("from"); |
| 481 | + String to = (String) data.get("to"); |
470 | 482 | Response result; |
471 | 483 | try { |
472 | 484 | renameFile(from, to); |
@@ -503,15 +515,15 @@ private Response handleStatus(boolean success, String message) throws IOExceptio |
503 | 515 | /** |
504 | 516 | * Handler for file uploads |
505 | 517 | */ |
506 | | - private Response handleUpload(Map<String, String> data) throws IOException { |
507 | | - String fileName = data.get("fileName"); |
508 | | - String content = data.get("data"); |
| 518 | + private Response handleUpload(Map<String, Object> data) throws IOException { |
| 519 | + String fileName = (String) data.get("fileName"); |
| 520 | + ByteArrayOutputStream content = (ByteArrayOutputStream) data.get("data"); |
509 | 521 | Response result; |
510 | 522 | try { |
511 | 523 | if (fileName == null || content == null) { |
512 | 524 | result = handleStatus(false, "Invalid input"); |
513 | 525 | } else { |
514 | | - saveFile(fileName, content.getBytes(UTF_8)); |
| 526 | + saveFile(fileName, content.toByteArray()); |
515 | 527 | result = handleStatus(true, "File saved"); |
516 | 528 | } |
517 | 529 | } catch (Exception e) { |
|
0 commit comments