You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by ms...@locus.apache.org on 2000/11/21 18:38:32 UTC

cvs commit: jakarta-struts/src/share/org/apache/struts/upload DiskMultipartRequestHandler.java MultipartIterator.java

mschachter    00/11/21 09:38:32

  Modified:    src/share/org/apache/struts/upload
                        DiskMultipartRequestHandler.java
                        MultipartIterator.java
  Log:
   - Implemented maxFileSize in DiskMultipartRequestHandler and MultipartIterator
  
  Revision  Changes    Path
  1.4       +37 -3     jakarta-struts/src/share/org/apache/struts/upload/DiskMultipartRequestHandler.java
  
  Index: DiskMultipartRequestHandler.java
  ===================================================================
  RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/upload/DiskMultipartRequestHandler.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- DiskMultipartRequestHandler.java	2000/11/16 22:27:13	1.3
  +++ DiskMultipartRequestHandler.java	2000/11/21 17:38:31	1.4
  @@ -46,10 +46,12 @@
        * A Hashtable representing all elemnents
        */
       protected Hashtable allElements;
  -
  -
  +    
       public void handleRequest(HttpServletRequest request) throws ServletException {
  -        MultipartIterator iterator = new MultipartIterator(request);
  +        
  +        MultipartIterator iterator = new MultipartIterator(request,
  +                                                            servlet.getBufferSize(),
  +                                                            getMaxSizeFromServlet());
           MultipartElement element;
   
           textElements = new Hashtable();
  @@ -208,4 +210,36 @@
           return mapping;
       }
   
  +    /**
  +     * Gets the maximum post data size in bytes from the string
  +     * representation in ActionServlet
  +     */
  +    protected long getMaxSizeFromServlet() throws ServletException{
  +        String stringSize = servlet.getMaxFileSize();
  +        long size = -1;
  +        int multiplier = 1;
  +        
  +        if (stringSize.endsWith("K")) {
  +            multiplier = 1024;
  +            stringSize = stringSize.substring(0, stringSize.length()-1);
  +        }
  +        if (stringSize.endsWith("M")) {
  +            multiplier = 1024*1024;
  +            stringSize = stringSize.substring(0, stringSize.length()-1);
  +        }
  +        else if (stringSize.endsWith("G")) {
  +            multiplier = 1024*1024*1024;
  +            stringSize = stringSize.substring(0, stringSize.length()-1);
  +        }
  +        
  +        try {
  +            size = Long.parseLong(stringSize);
  +        }
  +        catch (NumberFormatException nfe) {
  +            throw new ServletException("Invalid format for maximum file size: \"" +
  +                servlet.getMaxFileSize() + "\"");
  +        }
  +                
  +        return (size * multiplier);
  +    }       
   }
  
  
  
  1.2       +70 -3     jakarta-struts/src/share/org/apache/struts/upload/MultipartIterator.java
  
  Index: MultipartIterator.java
  ===================================================================
  RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/upload/MultipartIterator.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- MultipartIterator.java	2000/11/09 20:08:57	1.1
  +++ MultipartIterator.java	2000/11/21 17:38:31	1.2
  @@ -52,6 +52,11 @@
       protected boolean contentRead = false;
       
       /**
  +     * The maximum file size in bytes allowed. Ignored if -1
  +     */
  +    protected long maxSize = -1;
  +    
  +    /**
        * The amount of data read from a request at a time.
        * This also represents the maximum size in bytes of
        * a line read from the request
  @@ -59,21 +64,57 @@
        */
       protected int bufferSize = 4 * 1024;
   
  -    
  +    /**
  +     * Constructs a MultipartIterator with a default buffer size and no file size
  +     * limit
  +     * 
  +     * @param request The multipart request to iterate
  +     */
       public MultipartIterator(HttpServletRequest request) throws ServletException{
  +        this(request, -1);
  +    }
  +    
  +    /**
  +     * Constructs a MultipartIterator with the specified buffer size and
  +     * no file size limit
  +     *
  +     * @param request The multipart request to iterate
  +     * @param bufferSize The size in bytes that should be read from the input
  +     *                   stream at a times
  +     */
  +    public MultipartIterator(HttpServletRequest request, int bufferSize) throws ServletException {        
  +       this (request, bufferSize, -1);
  +    }
  +    
  +    /**
  +     * Constructs a MultipartIterator with the specified buffer size and
  +     * the specified file size limit in bytes
  +     *
  +     * @param request The multipart request to iterate
  +     * @param bufferSize The size in bytes that should be read from the input
  +     *                   stream at a times
  +     * @param maxSize The maximum size in bytes allowed for a multipart element's data
  +     */
  +    public MultipartIterator(HttpServletRequest request, int bufferSize, long maxSize) 
  +                                                                 throws ServletException {
           this.request = request;
  -        
  +        this.maxSize = maxSize;
  +        if (bufferSize > -1) {
  +            this.bufferSize = bufferSize;
  +        }
           parseRequest();
       }
       
       /**
        * Retrieves the next element in the iterator if one exists.
        *
  +     * @throws a ServletException if the post size exceeds the maximum file size
  +     *         passed in the 3 argument constructor
        * @return a {@link org.apache.struts.upload.MultipartElement MultipartElement}
        *         representing the next element in the request data
        *
        */
  -    public MultipartElement getNextElement() {
  +    public MultipartElement getNextElement() throws ServletException {
           
           //retrieve the "Content-Disposition" header
           //and parse
  @@ -120,10 +161,19 @@
               
               //parse for text data
               line = readLine();
  +            long totalReadLength = (long) line.getBytes().length;
            
               while ((line != null) && (!line.startsWith(boundary))) {
                   textData += line;
                   line = readLine();
  +                totalReadLength += (long) line.getBytes().length;
  +                
  +                if (maxSize > -1) {
  +                    if (totalReadLength > maxSize) {
  +                        throw new ServletException("Multipart data size exceeds the maximum " +
  +                            "allowed post size");
  +                    }
  +                }
               }
               
               //remove the "\r\n" if it's there
  @@ -170,6 +220,23 @@
        */
       public int getBufferSize() {
           return bufferSize;
  +    }
  +    
  +    /**
  +     * Set the maximum post data size allowed for a multipart request
  +     * @param maxSize The maximum post data size in bytes, set to <code>-1</code>
  +     *                for no limit
  +     */
  +    public void setMaxSize(long maxSize) {
  +        this.maxSize = maxSize;
  +    }
  +    
  +    /** 
  +     * Get the maximum post data size allowed for a multipart request
  +     * @return The maximum post data size in bytes
  +     */
  +    public long getMaxSize() {
  +        return maxSize;
       }
       
       /**