|
1 | 1 | package net.sourceforge.smallbasic; |
2 | 2 |
|
| 3 | +import android.util.Base64; |
| 4 | + |
3 | 5 | import java.io.BufferedOutputStream; |
4 | 6 | import java.io.ByteArrayInputStream; |
5 | 7 | import java.io.ByteArrayOutputStream; |
|
14 | 16 | import java.text.DateFormat; |
15 | 17 | import java.text.SimpleDateFormat; |
16 | 18 | import java.util.ArrayList; |
17 | | -import java.util.Base64; |
18 | 19 | import java.util.Collection; |
19 | 20 | import java.util.HashMap; |
20 | 21 | import java.util.List; |
@@ -107,7 +108,6 @@ public abstract static class AbstractRequest { |
107 | 108 | final String tokenKey; |
108 | 109 | final List<String> headers; |
109 | 110 | final InputStream inputStream; |
110 | | - private static final String BASE_64_PREFIX = ";base64,"; |
111 | 111 |
|
112 | 112 | public AbstractRequest(Socket socket, String tokenKey) throws IOException { |
113 | 113 | this.socket = socket; |
@@ -158,24 +158,16 @@ protected Map<String, Collection<String>> getParameters(String url) throws IOExc |
158 | 158 | /** |
159 | 159 | * Parses HTTP POST data from the given input stream |
160 | 160 | */ |
161 | | - protected Map<String, Object> getPostData(InputStream inputStream) throws IOException { |
| 161 | + protected Map<String, FormField> getPostData(InputStream inputStream) throws IOException { |
162 | 162 | String postData = getLine(inputStream); |
163 | 163 | String[] fields = postData.split("&"); |
164 | | - Map<String, Object> result = new HashMap<>(); |
| 164 | + Map<String, FormField> result = new HashMap<>(); |
165 | 165 | for (String nextField : fields) { |
166 | 166 | int eq = nextField.indexOf("="); |
167 | 167 | if (eq != -1) { |
168 | 168 | String key = nextField.substring(0, eq); |
169 | 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 | | - } |
| 170 | + result.put(key, new FormField(key, value)); |
179 | 171 | } |
180 | 172 | } |
181 | 173 | return result; |
@@ -272,6 +264,37 @@ public FileData(File file) { |
272 | 264 | } |
273 | 265 | } |
274 | 266 |
|
| 267 | + /** |
| 268 | + * Holder for POST form data |
| 269 | + */ |
| 270 | + public static class FormField { |
| 271 | + private static final String BASE_64_PREFIX = ";base64,"; |
| 272 | + private final String string; |
| 273 | + private final byte[] bytes; |
| 274 | + |
| 275 | + public FormField(String key, String value) throws IOException { |
| 276 | + int index = value.indexOf(BASE_64_PREFIX); |
| 277 | + if (index != -1 && "data".equals(key)) { |
| 278 | + ByteArrayOutputStream data = new ByteArrayOutputStream(); |
| 279 | + String base64Value = value.substring(index + BASE_64_PREFIX.length()); |
| 280 | + data.write(Base64.decode(base64Value, Base64.DEFAULT)); |
| 281 | + this.string = null; |
| 282 | + this.bytes = data.toByteArray(); |
| 283 | + } else { |
| 284 | + this.string = URLDecoder.decode(value, UTF_8); |
| 285 | + this.bytes = null; |
| 286 | + } |
| 287 | + } |
| 288 | + |
| 289 | + public String getString() { |
| 290 | + return string; |
| 291 | + } |
| 292 | + |
| 293 | + public byte[] toByteArray() { |
| 294 | + return bytes; |
| 295 | + } |
| 296 | + } |
| 297 | + |
275 | 298 | /** |
276 | 299 | * Build JSON string |
277 | 300 | */ |
@@ -369,6 +392,16 @@ private Collection<String> getAllFileNames() throws IOException { |
369 | 392 | return result; |
370 | 393 | } |
371 | 394 |
|
| 395 | + private byte[] getData(Map<String, FormField> data) { |
| 396 | + FormField field = data.get("data"); |
| 397 | + return field == null ? null : field.toByteArray(); |
| 398 | + } |
| 399 | + |
| 400 | + private String getString(Map<String, FormField> data, String key) { |
| 401 | + FormField field = data.get(key); |
| 402 | + return field == null ? null : field.toString(); |
| 403 | + } |
| 404 | + |
372 | 405 | /** |
373 | 406 | * Download files button handler |
374 | 407 | */ |
@@ -444,8 +477,8 @@ private void handleGet(Map<String, Collection<String>> parameters) throws IOExce |
444 | 477 | /** |
445 | 478 | * Handler for POST requests |
446 | 479 | */ |
447 | | - private void handlePost(Map<String, Object> data) throws IOException { |
448 | | - String userToken = (String) data.get(TOKEN); |
| 480 | + private void handlePost(Map<String, FormField> data) throws IOException { |
| 481 | + String userToken = getString(data, TOKEN); |
449 | 482 | if (userToken == null) { |
450 | 483 | userToken = requestToken; |
451 | 484 | } |
@@ -476,9 +509,9 @@ private void handlePost(Map<String, Object> data) throws IOException { |
476 | 509 | /** |
477 | 510 | * Handler for File rename operations |
478 | 511 | */ |
479 | | - private Response handleRename(Map<String, Object> data) throws IOException { |
480 | | - String from = (String) data.get("from"); |
481 | | - String to = (String) data.get("to"); |
| 512 | + private Response handleRename(Map<String, FormField> data) throws IOException { |
| 513 | + String from = getString(data, "from"); |
| 514 | + String to = getString(data, "to"); |
482 | 515 | Response result; |
483 | 516 | try { |
484 | 517 | renameFile(from, to); |
@@ -515,15 +548,15 @@ private Response handleStatus(boolean success, String message) throws IOExceptio |
515 | 548 | /** |
516 | 549 | * Handler for file uploads |
517 | 550 | */ |
518 | | - private Response handleUpload(Map<String, Object> data) throws IOException { |
519 | | - String fileName = (String) data.get("fileName"); |
520 | | - ByteArrayOutputStream content = (ByteArrayOutputStream) data.get("data"); |
| 551 | + private Response handleUpload(Map<String, FormField> data) throws IOException { |
| 552 | + String fileName = getString(data, "fileName"); |
| 553 | + byte[] content = getData(data); |
521 | 554 | Response result; |
522 | 555 | try { |
523 | 556 | if (fileName == null || content == null) { |
524 | 557 | result = handleStatus(false, "Invalid input"); |
525 | 558 | } else { |
526 | | - saveFile(fileName, content.toByteArray()); |
| 559 | + saveFile(fileName, content); |
527 | 560 | result = handleStatus(true, "File saved"); |
528 | 561 | } |
529 | 562 | } catch (Exception e) { |
|
0 commit comments