2016-02-19 20:28:26 +01:00
|
|
|
/*
|
|
|
|
|
* The MIT License (MIT)
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2015 Ziver Koc
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
* THE SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package zutil.net.http.multipart;
|
|
|
|
|
|
2016-07-08 16:23:29 +02:00
|
|
|
import zutil.io.BufferedBoundaryInputStream;
|
|
|
|
|
import zutil.io.IOUtil;
|
2016-07-06 17:36:57 +02:00
|
|
|
import zutil.log.LogUtil;
|
2016-02-19 20:28:26 +01:00
|
|
|
import zutil.net.http.HttpHeader;
|
2016-07-06 17:36:57 +02:00
|
|
|
import zutil.net.http.HttpHeaderParser;
|
2016-02-19 20:28:26 +01:00
|
|
|
|
2016-07-16 22:53:56 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStream;
|
2016-07-06 17:36:57 +02:00
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
import java.util.logging.Level;
|
|
|
|
|
import java.util.logging.Logger;
|
2016-02-19 20:28:26 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Parses a multipart/form-data http request,
|
|
|
|
|
* saves files to temporary location.
|
|
|
|
|
*
|
|
|
|
|
* http://www.ietf.org/rfc/rfc1867.txt
|
|
|
|
|
*
|
|
|
|
|
* @author Ziver
|
|
|
|
|
*
|
|
|
|
|
*/
|
2016-07-06 17:36:57 +02:00
|
|
|
public class MultipartParser implements Iterable<MultipartField>{
|
|
|
|
|
private static final Logger logger = LogUtil.getLogger();
|
2016-07-13 17:22:11 +02:00
|
|
|
protected static final String HEADER_CONTENT_DISPOSITION = "Content-Disposition".toUpperCase();
|
|
|
|
|
protected static final String HEADER_CONTENT_TYPE = "Content-Type".toUpperCase();
|
2016-07-06 17:36:57 +02:00
|
|
|
|
2016-02-19 20:28:26 +01:00
|
|
|
/** This is the delimiter that will separate the fields */
|
|
|
|
|
private String delimiter;
|
|
|
|
|
/** The length of the HTTP Body */
|
|
|
|
|
private long contentLength;
|
|
|
|
|
/** This is the input stream */
|
2016-07-08 16:23:29 +02:00
|
|
|
private InputStream in;
|
2016-02-19 20:28:26 +01:00
|
|
|
|
2016-07-06 17:36:57 +02:00
|
|
|
private MultiPartIterator iterator;
|
2016-02-19 20:28:26 +01:00
|
|
|
|
|
|
|
|
|
2016-07-08 16:23:29 +02:00
|
|
|
public MultipartParser(InputStream in, String delimiter, long length){
|
2016-07-06 17:36:57 +02:00
|
|
|
this.in = in;
|
|
|
|
|
this.delimiter = delimiter;
|
|
|
|
|
this.contentLength = length;
|
|
|
|
|
}
|
2016-07-13 17:22:11 +02:00
|
|
|
public MultipartParser(HttpHeader header){
|
|
|
|
|
this(header.getInputStream(),
|
2016-07-06 17:36:57 +02:00
|
|
|
parseDelimiter(header.getHeader("Content-type")),
|
2016-07-13 17:22:11 +02:00
|
|
|
Long.parseLong(header.getHeader("Content-Length")));
|
2016-02-19 20:28:26 +01:00
|
|
|
}
|
2016-07-14 17:50:39 +02:00
|
|
|
|
2016-07-06 17:36:57 +02:00
|
|
|
private static String parseDelimiter(String contentTypeHeader){
|
|
|
|
|
String delimiter = contentTypeHeader.split(" *; *")[1];
|
|
|
|
|
delimiter = delimiter.split(" *= *")[1];
|
|
|
|
|
return delimiter;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-19 20:28:26 +01:00
|
|
|
public long getContentLength(){
|
|
|
|
|
return contentLength;
|
|
|
|
|
}
|
2016-07-06 17:36:57 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Iterator<MultipartField> iterator() {
|
|
|
|
|
if (iterator == null)
|
|
|
|
|
iterator = new MultiPartIterator();
|
|
|
|
|
return iterator;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected class MultiPartIterator implements Iterator<MultipartField>{
|
2016-07-08 16:23:29 +02:00
|
|
|
private BufferedBoundaryInputStream boundaryIn;
|
2016-07-12 17:22:30 +02:00
|
|
|
private boolean firstIteration;
|
2016-07-06 17:36:57 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
protected MultiPartIterator(){
|
2016-07-08 16:23:29 +02:00
|
|
|
this.boundaryIn = new BufferedBoundaryInputStream(in);
|
|
|
|
|
|
|
|
|
|
this.boundaryIn.setBoundary("--"+delimiter);
|
2016-07-12 17:22:30 +02:00
|
|
|
firstIteration = true;
|
2016-07-06 17:36:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-07-13 17:22:11 +02:00
|
|
|
/**
|
2016-07-15 16:48:47 +02:00
|
|
|
* @return if there is more data after the closest boundary.
|
|
|
|
|
* Note that if all data until the next boundary has
|
|
|
|
|
* not been read then this method can only estimate
|
|
|
|
|
* if there is a next element as the next boundary or
|
|
|
|
|
* end of stream might be out of reach.
|
2016-07-13 17:22:11 +02:00
|
|
|
*/
|
2016-07-06 17:36:57 +02:00
|
|
|
@Override
|
|
|
|
|
public boolean hasNext() {
|
2016-07-08 16:23:29 +02:00
|
|
|
try {
|
2016-07-15 16:48:47 +02:00
|
|
|
// check is the last characters are "--"
|
|
|
|
|
if (boundaryIn.hasNext() && boundaryIn.isOnBoundary()) {
|
|
|
|
|
boundaryIn.mark(delimiter.length() + 10);
|
|
|
|
|
boundaryIn.next();
|
|
|
|
|
boolean ret = ! ('-' == boundaryIn.read() && '-' == boundaryIn.read());
|
|
|
|
|
boundaryIn.reset();
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
// Or just guess
|
2016-07-11 23:55:47 +02:00
|
|
|
return boundaryIn.hasNext();
|
|
|
|
|
} catch (IOException e) {
|
2016-07-06 17:36:57 +02:00
|
|
|
logger.log(Level.SEVERE, null, e);
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-08 16:23:29 +02:00
|
|
|
|
2016-07-06 17:36:57 +02:00
|
|
|
@Override
|
|
|
|
|
public MultipartField next() {
|
|
|
|
|
try {
|
2016-07-11 18:05:21 +02:00
|
|
|
boundaryIn.next();
|
2016-07-12 17:22:30 +02:00
|
|
|
if (firstIteration){
|
|
|
|
|
this.boundaryIn.setBoundary("\n--"+delimiter); // Add new-line to boundary after the first iteration
|
|
|
|
|
firstIteration = false;
|
|
|
|
|
}
|
2016-07-13 17:22:11 +02:00
|
|
|
String tmp = IOUtil.readLine(boundaryIn); // read the new line after the delimiter
|
2016-07-11 18:05:21 +02:00
|
|
|
if (tmp == null || tmp.equals("--"))
|
2016-07-10 22:09:56 +02:00
|
|
|
return null;
|
|
|
|
|
|
2016-07-13 17:22:11 +02:00
|
|
|
// Read Headers
|
|
|
|
|
HashMap<String,String> headers = new HashMap<>();
|
|
|
|
|
while ((tmp=IOUtil.readLine(boundaryIn)) != null && !tmp.isEmpty())
|
|
|
|
|
HttpHeaderParser.parseHeaderLine(headers, tmp);
|
|
|
|
|
|
|
|
|
|
// Parse
|
|
|
|
|
String disposition = headers.get(HEADER_CONTENT_DISPOSITION);
|
2016-07-06 17:36:57 +02:00
|
|
|
if (disposition != null){
|
2016-07-13 17:22:11 +02:00
|
|
|
HttpHeaderParser.parseHeaderValue(headers, disposition);
|
|
|
|
|
if (headers.containsKey("form-data")){
|
|
|
|
|
if (headers.containsKey("filename")){
|
|
|
|
|
MultipartFileField field = new MultipartFileField(headers, boundaryIn);
|
2016-07-06 17:36:57 +02:00
|
|
|
return field;
|
|
|
|
|
}
|
|
|
|
|
else{
|
2016-07-13 17:22:11 +02:00
|
|
|
MultipartStringField field = new MultipartStringField(headers, boundaryIn);
|
2016-07-06 17:36:57 +02:00
|
|
|
return field;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
2016-07-08 16:23:29 +02:00
|
|
|
logger.warning("Only multipart form-data is supported");
|
2016-07-06 17:36:57 +02:00
|
|
|
return this.next(); // find next field
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
logger.log(Level.SEVERE, null, e);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void remove() {
|
|
|
|
|
throw new UnsupportedOperationException("Remove not supported in read only stream.");
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-19 20:28:26 +01:00
|
|
|
}
|