return HttpRequestMultipartFormDataBodyParser.DASH == character;
}
@Override
public IHttpRequestBodyData parse(InputStream inputStream) {
HttpRequestMultipartBodyData multipartBodyData = new HttpRequestMultipartBodyData();
determineBoundary();
boolean stillMorePartsToRead = true;
tryReadInterBoundary(inputStream);
while (stillMorePartsToRead) {
try {
int firstCharacter = inputStream.read();
if (firstCharacter == -1) {
LOGGER.error("Could not read the first of two consecutive characters after inter boundary separator.");
throw new HttpException(HttpStatusCode.STATUS_400_BAD_REQUEST, "");
}
int secondCharacter = inputStream.read();
if (secondCharacter == -1) {
LOGGER.error(String.format("Could not read the second of two consecutive characters after inter boundary separator. Already read: %s.", new String(new int[]{firstCharacter}, 0, 1)));
throw new HttpException(HttpStatusCode.STATUS_400_BAD_REQUEST, "");
}
if (isCR(firstCharacter) && isLF(secondCharacter)) {
stillMorePartsToRead = true;
} else if (isDash(firstCharacter) && isDash(secondCharacter)) {
stillMorePartsToRead = false;
break;
} else {
LOGGER.error(String.format("Read the two consecutive characters after inter boundary separator. Could have been either CRLF or DASHDASH but was: %s (%d %d).", new String(new int[]{firstCharacter, secondCharacter}, 0, 2), firstCharacter, secondCharacter));
throw new HttpException(HttpStatusCode.STATUS_400_BAD_REQUEST, "");
}
}
catch (IOException ioex) {
LOGGER.error("Tried to read the two consecutive characters after inter boundary separator. Could have been either CRLF or DASHDASH.", ioex);
throw new HttpException(HttpStatusCode.STATUS_400_BAD_REQUEST, ioex.getMessage());
}
IHttpHeadersParser headersParser = new HttpHeadersParser();
IHttpHeaders headers = headersParser.parse(inputStream);
ContentDispositionHttpHeader partContentDisposition = (ContentDispositionHttpHeader) headers.getHeader("Content-Disposition");
if (partContentDisposition == null
|| !partContentDisposition.getDispositionType().getType().equalsIgnoreCase("form-data")
|| !partContentDisposition.getDispositionType().getParameters().containsName("name")) {
throw new HttpException(HttpStatusCode.STATUS_400_BAD_REQUEST,
String.format("Bad Content-Disposition: %s.", partContentDisposition.getRawValue()));
}
ContentTypeHttpHeader partContentType = (ContentTypeHttpHeader) headers.getHeader("Content-Type");
BoundaryDelimitedInputStream bdis = new BoundaryDelimitedInputStream(inputStream, boundary);
IHttpRequestBodyParserFactory bodyParserFactory = new HttpRequestBodyParserFactory();
IHttpRequestBodyParser partBodyParser = bodyParserFactory.build(partContentType, partContentDisposition);
IHttpRequestBodyData partBodyData = partBodyParser.parse(bdis);
// HttpBuffer buffer = new HttpBuffer();
// int boundaryPosition = 0;
// int ch = -1;
// try {
// while ((ch = inputStream.read()) != -1) {
// if (ch == boundary[boundaryPosition]) {
// if (boundaryPosition == boundaryLength - 1) {
// break;
// } else {
// boundaryPosition++;
// }
// } else {
// for (int i = 0; i < boundaryPosition; i++) {
// buffer.append(boundary[i]);
// }
// boundaryPosition = 0;
// buffer.append(ch);
// }
// }
// } catch (IOException ex) {
// LOGGER.error("Could not read part from multipart/form-data", ex);
// throw new com.ramforth.utilities.exceptions.IOException(ex);
// }
//
// String name = partContentDisposition.getDispositionType().getValue("name");
// String filename = partContentDisposition.getDispositionType().getValue("filename");
// String mimeType = partContentType.getMediaType().getType();
//
// IHttpRequestBodyData partBodyData = new HttpRequestFileBodyData(name, filename, mimeType, buffer.getCopy());
multipartBodyData.addPart(partBodyData);
}
return multipartBodyData;
}