use of jodd.upload.MultipartStreamParser in project jodd by oblac.
the class HttpBase method readBody.
/**
* Parses body.
*/
protected void readBody(BufferedReader reader) {
String bodyString = null;
// first determine if chunked encoding is specified
boolean isChunked = false;
String transferEncoding = header("Transfer-Encoding");
if (transferEncoding != null && transferEncoding.equalsIgnoreCase("chunked")) {
isChunked = true;
}
// content length
String contentLen = contentLength();
int contentLenValue = -1;
if (contentLen != null && !isChunked) {
contentLenValue = Integer.parseInt(contentLen);
if (contentLenValue > 0) {
FastCharArrayWriter fastCharArrayWriter = new FastCharArrayWriter(contentLenValue);
try {
StreamUtil.copy(reader, fastCharArrayWriter, contentLenValue);
} catch (IOException ioex) {
throw new HttpException(ioex);
}
bodyString = fastCharArrayWriter.toString();
}
}
// chunked encoding
if (isChunked) {
FastCharArrayWriter fastCharArrayWriter = new FastCharArrayWriter();
try {
while (true) {
String line = reader.readLine();
int len = Integer.parseInt(line, 16);
if (len > 0) {
StreamUtil.copy(reader, fastCharArrayWriter, len);
reader.readLine();
} else {
// end reached, read trailing headers, if there is any
readHeaders(reader);
break;
}
}
} catch (IOException ioex) {
throw new HttpException(ioex);
}
bodyString = fastCharArrayWriter.toString();
}
// no body yet - special case
if (bodyString == null && contentLenValue != 0) {
// body ends when stream closes
FastCharArrayWriter fastCharArrayWriter = new FastCharArrayWriter();
try {
StreamUtil.copy(reader, fastCharArrayWriter);
} catch (IOException ioex) {
throw new HttpException(ioex);
}
bodyString = fastCharArrayWriter.toString();
}
// BODY READY - PARSE BODY
String charset = this.charset;
if (charset == null) {
charset = StringPool.ISO_8859_1;
}
body = bodyString;
String mediaType = mediaType();
if (mediaType == null) {
mediaType = StringPool.EMPTY;
} else {
mediaType = mediaType.toLowerCase();
}
if (mediaType.equals("application/x-www-form-urlencoded")) {
form = HttpUtil.parseQuery(bodyString, true);
return;
}
if (mediaType.equals("multipart/form-data")) {
form = HttpMultiMap.newCaseInsensitveMap();
MultipartStreamParser multipartParser = new MultipartStreamParser();
try {
byte[] bodyBytes = bodyString.getBytes(StringPool.ISO_8859_1);
ByteArrayInputStream bin = new ByteArrayInputStream(bodyBytes);
multipartParser.parseRequestStream(bin, charset);
} catch (IOException ioex) {
throw new HttpException(ioex);
}
// string parameters
for (String paramName : multipartParser.getParameterNames()) {
String[] values = multipartParser.getParameterValues(paramName);
for (String value : values) {
((HttpMultiMap<Object>) form).add(paramName, value);
}
}
// file parameters
for (String paramName : multipartParser.getFileParameterNames()) {
FileUpload[] uploads = multipartParser.getFiles(paramName);
for (FileUpload upload : uploads) {
((HttpMultiMap<Object>) form).add(paramName, upload);
}
}
return;
}
// body is a simple content
form = null;
}
Aggregations