You are viewing a plain text version of this content. The canonical link for it is here.
Posted to slide-dev@jakarta.apache.org by lu...@apache.org on 2004/06/17 19:27:30 UTC

cvs commit: jakarta-slide/src/webdav/server/org/apache/slide/webdav/method AbstractWebdavMethod.java

luetzkendorf    2004/06/17 10:27:30

  Modified:    src/webdav/server/org/apache/slide/webdav/method
                        AbstractWebdavMethod.java
  Log:
  Fix for If-None-Match Bug (29626) and workaround for
  Mircosofts non quoted ETags
  new parseHttpDate method (used 3 times)
  
  Revision  Changes    Path
  1.29      +59 -73    jakarta-slide/src/webdav/server/org/apache/slide/webdav/method/AbstractWebdavMethod.java
  
  Index: AbstractWebdavMethod.java
  ===================================================================
  RCS file: /home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/method/AbstractWebdavMethod.java,v
  retrieving revision 1.28
  retrieving revision 1.29
  diff -u -r1.28 -r1.29
  --- AbstractWebdavMethod.java	14 Jun 2004 12:38:35 -0000	1.28
  +++ AbstractWebdavMethod.java	17 Jun 2004 17:27:30 -0000	1.29
  @@ -963,9 +963,11 @@
       protected boolean checkIfHeaders(HttpServletRequest request,
                                        HttpServletResponse response,
                                        ResourceInfo resourceInfo)
  -        throws IOException {
  -        
  -        String eTag = getETag(resourceInfo, true);
  +        throws IOException 
  +    {
  +        // the ETag without apostrophes ("), we use apostrophes as delimiters
  +        // to because some clients provide If header with out apostrophes
  +        String eTag = getETagValue(resourceInfo, true);
           long lastModified = resourceInfo.date;
           
           StringTokenizer commaTokenizer;
  @@ -977,23 +979,20 @@
           if (headerValue != null) {
               if (headerValue.indexOf("*") == -1) {
                   
  -                commaTokenizer = new StringTokenizer(headerValue, ",");
  -                boolean conditionSatisfied = false;
  +                commaTokenizer = new StringTokenizer(headerValue, ", \"");
  +                boolean matchingTagFound = false;
                   
  -                while (!conditionSatisfied && commaTokenizer.hasMoreTokens()) {
  -                    String currentToken = commaTokenizer.nextToken();
  -                    if (currentToken.trim().equals(eTag))
  -                        conditionSatisfied = true;
  +                while (!matchingTagFound && commaTokenizer.hasMoreTokens()) {
  +                    matchingTagFound = commaTokenizer.nextToken().equals(eTag);
                   }
                   
                   // If none of the given ETags match, 412 Precodition failed is
                   // sent back
  -                if (!conditionSatisfied) {
  -                    response.sendError
  -                        (HttpServletResponse.SC_PRECONDITION_FAILED);
  +                if (!matchingTagFound) {
  +                    response.sendError(
  +                            HttpServletResponse.SC_PRECONDITION_FAILED);
                       return false;
                   }
  -                
               }
           }
           
  @@ -1004,19 +1003,7 @@
               // If an If-None-Match header has been specified, if modified since
               // is ignored.
               if (request.getHeader("If-None-Match") == null) {
  -                
  -                Date date = null;
  -                
  -                // Parsing the HTTP Date
  -                for (int i = 0; (date == null) && (i < formats.length); i++) {
  -                    try {
  -                        synchronized (formats[i]) {
  -                            date = formats[i].parse(headerValue);
  -                        }
  -                    } catch (ParseException e) {
  -                        ;
  -                    }
  -                }
  +                Date date = parseHttpDate(headerValue);
                   
                   if ((date != null)
                       && (lastModified <= (date.getTime() + 1000)) ) {
  @@ -1026,9 +1013,7 @@
                           (HttpServletResponse.SC_NOT_MODIFIED);
                       return false;
                   }
  -                
               }
  -            
           }
           
           // Checking If-None-Match
  @@ -1036,33 +1021,24 @@
           if (headerValue != null) {
               if (headerValue.indexOf("*") == -1) {
                   
  -                commaTokenizer = new StringTokenizer(headerValue, ",");
  -                boolean conditionSatisfied = true;
  -                
  -                while (conditionSatisfied && commaTokenizer.hasMoreTokens()) {
  -                    String currentToken = commaTokenizer.nextToken();
  -                    if (currentToken.trim().equals(eTag))
  -                        conditionSatisfied = false;
  -                }
  -                
  -                if (conditionSatisfied) {
  -                    
  -                    // For GET and HEAD, we should respond with
  -                    // 304 Not Modified.
  -                    // For every other method, 412 Precondition Failed is sent
  -                    // back.
  -                    if ( ("GET".equals(request.getMethod()))
  -                        || ("HEAD".equals(request.getMethod())) ) {
  -                        response.sendError
  -                            (HttpServletResponse.SC_NOT_MODIFIED);
  -                        return false;
  -                    } else {
  -                        response.sendError
  -                            (HttpServletResponse.SC_PRECONDITION_FAILED);
  -                        return false;
  +                commaTokenizer = new StringTokenizer(headerValue, ", \"");
  +                while (commaTokenizer.hasMoreTokens()) {
  +                    if (commaTokenizer.nextToken().equals(eTag)) {
  +                        // For GET and HEAD, we respond with  304 Not Modified.
  +                        // For every other method, 412 Precondition Failed 
  +                        if ( ("GET".equals(request.getMethod()))
  +                            || ("HEAD".equals(request.getMethod())) ) 
  +                        {
  +                            response.sendError(
  +                                    HttpServletResponse.SC_NOT_MODIFIED);
  +                            return false;
  +                        } else {
  +                            response.sendError(
  +                                    HttpServletResponse.SC_PRECONDITION_FAILED);
  +                            return false;
  +                        }
                       }
                   }
  -                
               } else {
                   if (resourceInfo.exists()) {
                       return false;
  @@ -1073,33 +1049,43 @@
           // Checking If-Unmodified-Since
           headerValue = request.getHeader("If-Unmodified-Since");
           if (headerValue != null) {
  -            
  -            Date date = null;
  -            
  -            // Parsing the HTTP Date
  -            for (int i = 0; (date == null) && (i < formats.length); i++) {
  -                try {
  -                    synchronized (formats[i]) {
  -                        date = formats[i].parse(headerValue);
  -                    }
  -                } catch (ParseException e) {
  -                    ;
  -                }
  -            }
  +            Date date = parseHttpDate(headerValue);
               
               if ( (date != null) && (lastModified > date.getTime()) ) {
                   // The entity has not been modified since the date
                   // specified by the client. This is not an error case.
  -                response.sendError
  -                    (HttpServletResponse.SC_PRECONDITION_FAILED);
  +                response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
                   return false;
               }
  -            
           }
  -        
           return true;
       }
       
  +    /**
  +     * Parses the date string given as one of the {@link #formats}. 
  +     * If the date does not fit any of these formats it returns <code>null</code>.
  +     * 
  +     * @param headerValue date string from and HTTP header (e.g. If-Modified)
  +     * @return a Date representing the date given of <code>null</code> if 
  +     *         the string has no valid format.
  +     */
  +    protected Date parseHttpDate(String headerValue) {
  +        Date date = null;
  +        
  +        // Parsing the HTTP Date
  +        for (int i = 0; (date == null) && (i < formats.length); i++) {
  +            try {
  +                synchronized (formats[i]) {
  +                    date = formats[i].parse(headerValue);
  +                }
  +            } catch (ParseException e) {
  +                // ignore the invalid format and try the next
  +            }
  +        }
  +        return date;
  +    }
  +
  +
       /**
        * Get the ETag value associated with a file.
        *
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: slide-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: slide-dev-help@jakarta.apache.org