You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by jo...@apache.org on 2007/09/22 22:58:09 UTC

svn commit: r578496 - in /commons/proper/fileupload/trunk/src: changes/changes.xml java/org/apache/commons/fileupload/FileUploadBase.java

Author: jochen
Date: Sat Sep 22 13:58:02 2007
New Revision: 578496

URL: http://svn.apache.org/viewvc?rev=578496&view=rev
Log:
A FileSizeLimitExceededException is now thrown immediately,
if the attachments headers contain a content-length value,
which exceeds the configured limit.

Modified:
    commons/proper/fileupload/trunk/src/changes/changes.xml
    commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/FileUploadBase.java

Modified: commons/proper/fileupload/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/fileupload/trunk/src/changes/changes.xml?rev=578496&r1=578495&r2=578496&view=diff
==============================================================================
--- commons/proper/fileupload/trunk/src/changes/changes.xml (original)
+++ commons/proper/fileupload/trunk/src/changes/changes.xml Sat Sep 22 13:58:02 2007
@@ -70,7 +70,9 @@
       </action>
       <action dev="jochen" type="fix" issue="FILEUPLOAD-145">
         A FileSizeLimitExceededException was deferred until the complete
-        file has been uploaded.
+        file has been uploaded. Additionally, the FileSizeLimitException
+        is now thrown immediately, if the attachments headers contain
+        a content-length value, which exceeds the configured limit.
       </action>
     </release>
 

Modified: commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/FileUploadBase.java
URL: http://svn.apache.org/viewvc/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/FileUploadBase.java?rev=578496&r1=578495&r2=578496&view=diff
==============================================================================
--- commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/FileUploadBase.java (original)
+++ commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/FileUploadBase.java Sat Sep 22 13:58:02 2007
@@ -121,6 +121,11 @@
      */
     public static final String CONTENT_DISPOSITION = "Content-disposition";
 
+    /**
+     * HTTP content length header name.
+     */
+    public static final String CONTENT_LENGTH = "Content-length";
+
 
     /**
      * Content-disposition value for form data.
@@ -711,7 +716,8 @@
              * @param pFormField Whether the item is a form field.
              */
             FileItemStreamImpl(String pName, String pFieldName,
-                    String pContentType, boolean pFormField) {
+                    String pContentType, boolean pFormField,
+                    long contentLength) throws IOException {
                 name = pName;
                 fieldName = pFieldName;
                 contentType = pContentType;
@@ -719,6 +725,15 @@
                 final ItemInputStream itemStream = multi.newInputStream();
                 InputStream istream = itemStream;
                 if (fileSizeMax != -1) {
+                    if (contentLength != -1  &&  contentLength > fileSizeMax) {
+                        FileUploadException e = new FileSizeLimitExceededException(
+                                "The field " + fieldName
+                                + " exceeds its maximum permitted "
+                                + " size of " + fileSizeMax
+                                + " characters.",
+                                contentLength, fileSizeMax);
+                        throw new FileUploadIOException(e);
+                    }
                     istream = new LimitedInputStream(istream, fileSizeMax) {
                         protected void raiseError(long pSizeMax, long pCount)
                                 throws IOException {
@@ -972,7 +987,7 @@
                         String fileName = getFileName(headers);
                         currentItem = new FileItemStreamImpl(fileName,
                                 fieldName, headers.getHeader(CONTENT_TYPE),
-                                fileName == null);
+                                fileName == null, getContentLength(headers));
                         notifier.noteItem();
                         itemValid = true;
                         return true;
@@ -983,13 +998,21 @@
                         currentItem = new FileItemStreamImpl(fileName,
                                 currentFieldName,
                                 headers.getHeader(CONTENT_TYPE),
-                                false);
+                                false, getContentLength(headers));
                         notifier.noteItem();
                         itemValid = true;
                         return true;
                     }
                 }
                 multi.discardBodyData();
+            }
+        }
+
+        private long getContentLength(FileItemHeaders pHeaders) {
+            try {
+                return Long.parseLong(pHeaders.getHeader(CONTENT_LENGTH));
+            } catch (Exception e) {
+                return -1;
             }
         }