in commons-fileupload2-core/src/main/java/org/apache/commons/fileupload2/core/FileItemInputIteratorImpl.java [229:277]
protected void init(final AbstractFileUpload<?, ?, ?> fileUploadBase, final RequestContext initContext) throws FileUploadException, IOException {
final var contentType = requestContext.getContentType();
if (null == contentType || !contentType.toLowerCase(Locale.ENGLISH).startsWith(AbstractFileUpload.MULTIPART)) {
throw new FileUploadContentTypeException(String.format("the request doesn't contain a %s or %s stream, content type header is %s",
AbstractFileUpload.MULTIPART_FORM_DATA, AbstractFileUpload.MULTIPART_MIXED, contentType), contentType);
}
final var contentLengthInt = requestContext.getContentLength();
// @formatter:off
final var requestSize = RequestContext.class.isAssignableFrom(requestContext.getClass())
// Inline conditional is OK here CHECKSTYLE:OFF
? requestContext.getContentLength()
: contentLengthInt;
// CHECKSTYLE:ON
// @formatter:on
final InputStream inputStream; // N.B. this is eventually closed in MultipartInput processing
if (sizeMax >= 0) {
if (requestSize != -1 && requestSize > sizeMax) {
throw new FileUploadSizeException(
String.format("the request was rejected because its size (%s) exceeds the configured maximum (%s)", requestSize, sizeMax), sizeMax,
requestSize);
}
// N.B. this is eventually closed in MultipartInput processing
inputStream = new BoundedInputStream(requestContext.getInputStream(), sizeMax) {
@Override
protected void onMaxLength(final long maxLen, final long count) throws IOException {
throw new FileUploadSizeException(
String.format("The request was rejected because its size (%s) exceeds the configured maximum (%s)", count, maxLen), maxLen, count);
}
};
} else {
inputStream = requestContext.getInputStream();
}
final var charset = Charsets.toCharset(fileUploadBase.getHeaderCharset(), requestContext.getCharset());
multiPartBoundary = fileUploadBase.getBoundary(contentType);
if (multiPartBoundary == null) {
IOUtils.closeQuietly(inputStream); // avoid possible resource leak
throw new FileUploadException("the request was rejected because no multipart boundary was found");
}
progressNotifier = new MultipartInput.ProgressNotifier(fileUploadBase.getProgressListener(), requestSize);
try {
multiPartInput = MultipartInput.builder().setInputStream(inputStream).setBoundary(multiPartBoundary).setProgressNotifier(progressNotifier).get();
} catch (final IllegalArgumentException e) {
IOUtils.closeQuietly(inputStream); // avoid possible resource leak
throw new FileUploadContentTypeException(String.format("The boundary specified in the %s header is too long", AbstractFileUpload.CONTENT_TYPE), e);
}
multiPartInput.setHeaderCharset(charset);
}