Skip to content

Adding capability to take max upload size per request. #188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions src/main/java/org/apache/commons/fileupload/FileUploadBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,16 @@ public long getSizeMax() {
return sizeMax;
}

/**
* Gets allowed max size for current upload, subclasses can override this method
* to calculate allowed size per request
* @param ctx requestContext - provides information related to current upload request.
* @return max size allowed for current request
*/
public long getSizeMaxForThisRequest(RequestContext ctx) {
return sizeMax;
}

/**
* Sets the maximum allowed size of a complete request, as opposed
* to {@link #setFileSizeMax(long)}.
Expand Down Expand Up @@ -969,15 +979,16 @@ public void setHeaders(FileItemHeaders pHeaders) {
// CHECKSTYLE:ON

InputStream input; // N.B. this is eventually closed in MultipartStream processing
if (sizeMax >= 0) {
if (requestSize != -1 && requestSize > sizeMax) {
long sizeMaxForThisRequest = getSizeMaxForThisRequest(ctx);
if (sizeMaxForThisRequest >= 0) {
if (requestSize != -1 && requestSize > sizeMaxForThisRequest) {
throw new SizeLimitExceededException(
format("the request was rejected because its size (%s) exceeds the configured maximum (%s)",
Long.valueOf(requestSize), Long.valueOf(sizeMax)),
requestSize, sizeMax);
Long.valueOf(requestSize), Long.valueOf(sizeMaxForThisRequest)),
requestSize, sizeMaxForThisRequest);
}
// N.B. this is eventually closed in MultipartStream processing
input = new LimitedInputStream(ctx.getInputStream(), sizeMax) {
input = new LimitedInputStream(ctx.getInputStream(), sizeMaxForThisRequest) {
@Override
protected void raiseError(long pSizeMax, long pCount)
throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,13 @@ public String toString() {
this.getContentType());
}

/**
* Gets underlying HttpServletRequest, this method can be used in subclasses of @{@link FileUploadBase}
* while adding support for max upload size per request.
* @return
*/
public HttpServletRequest getRequest(){
return this.request;
}

}