Returns a map with multipart form data. @param type media type @return map with file names and contents @throws IOException I/O exception @throws QueryException query exception
(final MediaType type)
| 238 | * @throws QueryException query exception |
| 239 | */ |
| 240 | public XQMap multiForm(final MediaType type) throws IOException, QueryException { |
| 241 | // parse boundary, create helper arrays |
| 242 | final byte[] bound = concat(DASHES, boundary(type)), last = concat(bound, DASHES); |
| 243 | |
| 244 | XQMap data = XQMap.empty(); |
| 245 | final ByteList cont = new ByteList(); |
| 246 | int lines = -1; |
| 247 | Str name = Str.EMPTY, filename = Str.EMPTY; |
| 248 | for(byte[] line; (line = readLine()) != null;) { |
| 249 | if(lines >= 0) { |
| 250 | if(startsWith(line, bound)) { |
| 251 | // get old value |
| 252 | Value value = data.get(name); |
| 253 | if(filename.string().length != 0) { |
| 254 | // assign file and contents, join multiple files |
| 255 | final XQMap map = value instanceof final XQMap m ? m : XQMap.empty(); |
| 256 | final B64 contents = B64.get(cont.next()); |
| 257 | final Value files = new ItemList().add(map.get(filename)).add(contents).value(); |
| 258 | value = map.put(filename, files); |
| 259 | } else { |
| 260 | // assign string, join multiple strings |
| 261 | final Str v = Str.get(cont.next()); |
| 262 | value = value == null ? v : new ItemList().add(value).add(v).value(); |
| 263 | } |
| 264 | |
| 265 | if(!name.isEmpty()) data = data.put(name, value); |
| 266 | lines = -1; |
| 267 | if(eq(line, last)) break; |
| 268 | } else { |
| 269 | if(lines++ > 0) cont.add(CRLF); |
| 270 | cont.add(line); |
| 271 | } |
| 272 | } else if(startsWith(lc(line), CONTENT_DISPOSITION)) { |
| 273 | // get key and file name |
| 274 | final String ln = string(line); |
| 275 | name = Str.get(ln.contains("name=") ? ln.replaceAll("^.*?name=\"|\".*", ""). |
| 276 | replaceAll("\\[]", "") : ""); |
| 277 | filename = Str.get(ln.contains("filename=") ? ln.replaceAll("^.*filename=\"|\"$", "") : ""); |
| 278 | } else if(line.length == 0) { |
| 279 | lines = 0; |
| 280 | } |
| 281 | } |
| 282 | return data; |
| 283 | } |
| 284 | |
| 285 | // STATIC METHODS =============================================================================== |
| 286 |