remm        2005/04/20 09:13:23

  Modified:    catalina/src/share/org/apache/naming/resources
                        ResourceAttributes.java FileDirContext.java
               http11/src/java/org/apache/coyote/http11
                        InternalAprOutputBuffer.java
                        Http11AprProcessor.java
               util/java/org/apache/tomcat/util/net AprEndpoint.java
               catalina/src/share/org/apache/catalina/connector
                        Request.java
               catalina/src/share/org/apache/catalina/servlets
                        DefaultServlet.java
  Log:
  - Fix setting timeouts on sockets.
  - Add plumbing code for sendfile (disabled by default), which doesn't work 
yet.
  - Apparently, the Sendfile.run algo is bad. APR is black magic to me, so I'll 
let Mladen fix it :)
  
  Revision  Changes    Path
  1.6       +13 -1     
jakarta-tomcat-catalina/catalina/src/share/org/apache/naming/resources/ResourceAttributes.java
  
  Index: ResourceAttributes.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/naming/resources/ResourceAttributes.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ResourceAttributes.java   29 Aug 2004 16:46:15 -0000      1.5
  +++ ResourceAttributes.java   20 Apr 2005 16:13:23 -0000      1.6
  @@ -753,6 +753,18 @@
       }
   
       
  +    /**
  +     * Return the canonical path of the resource, to possibly be used for 
  +     * direct file serving. Implementations which support this should 
override
  +     * it to return the file path.
  +     * 
  +     * @return The canonical path of the resource
  +     */
  +    public String getCanonicalPath() {
  +        return null;
  +    }
  +    
  +    
       // ----------------------------------------------------- Attributes 
Methods
   
   
  
  
  
  1.8       +21 -1     
jakarta-tomcat-catalina/catalina/src/share/org/apache/naming/resources/FileDirContext.java
  
  Index: FileDirContext.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/naming/resources/FileDirContext.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- FileDirContext.java       28 Jul 2004 20:27:01 -0000      1.7
  +++ FileDirContext.java       20 Apr 2005 16:13:23 -0000      1.8
  @@ -997,6 +997,9 @@
           protected boolean accessed = false;
   
   
  +        protected String canonicalPath = null;
  +
  +
           // ----------------------------------------- ResourceAttributes 
Methods
   
   
  @@ -1102,6 +1105,23 @@
               return super.getResourceType();
           }
   
  +        
  +        /**
  +         * Get canonical path.
  +         * 
  +         * @return String the file's canonical path
  +         */
  +        public String getCanonicalPath() {
  +            if (canonicalPath == null) {
  +                try {
  +                    canonicalPath = file.getCanonicalPath();
  +                } catch (IOException e) {
  +                    // Ignore
  +                }
  +            }
  +            return canonicalPath;
  +        }
  +        
   
       }
   
  
  
  
  1.2       +0 -1      
jakarta-tomcat-connectors/http11/src/java/org/apache/coyote/http11/InternalAprOutputBuffer.java
  
  Index: InternalAprOutputBuffer.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-connectors/http11/src/java/org/apache/coyote/http11/InternalAprOutputBuffer.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- InternalAprOutputBuffer.java      14 Apr 2005 18:52:28 -0000      1.1
  +++ InternalAprOutputBuffer.java      20 Apr 2005 16:13:23 -0000      1.2
  @@ -20,7 +20,6 @@
   import java.security.AccessController;
   import java.security.PrivilegedAction;
   
  -import org.apache.tomcat.jni.Error;
   import org.apache.tomcat.jni.Socket;
   import org.apache.tomcat.util.buf.ByteChunk;
   import org.apache.tomcat.util.buf.CharChunk;
  
  
  
  1.8       +34 -2     
jakarta-tomcat-connectors/http11/src/java/org/apache/coyote/http11/Http11AprProcessor.java
  
  Index: Http11AprProcessor.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-connectors/http11/src/java/org/apache/coyote/http11/Http11AprProcessor.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- Http11AprProcessor.java   18 Apr 2005 16:47:47 -0000      1.7
  +++ Http11AprProcessor.java   20 Apr 2005 16:13:23 -0000      1.8
  @@ -158,6 +158,12 @@
   
   
       /**
  +     * Sendfile data.
  +     */
  +    protected AprEndpoint.SendfileData sendfileData = null;
  +
  +
  +    /**
        * Content delimitator for the request (if false, the connection will
        * be closed at the end of the request).
        */
  @@ -753,7 +759,7 @@
               // Parsing the request header
               try {
                   if( !disableUploadTimeout && keptAlive && soTimeout > 0 ) {
  -                    Socket.timeoutSet(socket, soTimeout);
  +                    Socket.timeoutSet(socket, soTimeout * 1000);
                   }
                   if (!inputBuffer.parseRequestLine()) {
                       // This means that no data is available right now
  @@ -769,7 +775,7 @@
                   thrA.setParam(endpoint, request.requestURI());
                   keptAlive = true;
                   if (!disableUploadTimeout) {
  -                    Socket.timeoutSet(socket, timeout);
  +                    Socket.timeoutSet(socket, timeout * 1000);
                   }
                   inputBuffer.parseHeaders();
               } catch (IOException e) {
  @@ -864,6 +870,13 @@
               inputBuffer.nextRequest();
               outputBuffer.nextRequest();
   
  +            // Do sendfile as needed: add socket to sendfile and end
  +            if (sendfileData != null) {
  +                keepAlive = false;
  +                sendfileData.pool = pool;
  +                endpoint.getSendfile().add(socket, sendfileData);
  +            }
  +            
           }
   
           rp.setStage(org.apache.coyote.Constants.STAGE_ENDED);
  @@ -1141,6 +1154,7 @@
           http09 = false;
           contentDelimitation = false;
           expectation = false;
  +        sendfileData = null;
           if (sslSupport != null) {
               request.scheme().setString("https");
           }
  @@ -1307,6 +1321,11 @@
           if (!contentDelimitation)
               keepAlive = false;
   
  +        // Advertise sendfile support through a request attribute
  +        if (endpoint.getUseSendfile()) {
  +            request.setAttribute("sendfile.support", Boolean.TRUE);
  +        }
  +        
       }
   
   
  @@ -1483,6 +1502,19 @@
               contentDelimitation = true;
           }
   
  +        // Sendfile support
  +        String fileName = (String) request.getAttribute("sendfile.filename");
  +        if (fileName != null) {
  +            // No entity body sent here
  +            outputBuffer.addActiveFilter
  +                (outputFilters[Constants.VOID_FILTER]);
  +            contentDelimitation = true;
  +            sendfileData = new AprEndpoint.SendfileData();
  +            sendfileData.fileName = fileName;
  +            sendfileData.start = ((Long) 
request.getAttribute("sendfile.start")).longValue();
  +            sendfileData.end = ((Long) 
request.getAttribute("sendfile.end")).longValue();
  +        }
  +        
           // Check for compression
           boolean useCompression = false;
           if (entityBody && (compressionLevel > 0)) {
  
  
  
  1.14      +9 -1      
jakarta-tomcat-connectors/util/java/org/apache/tomcat/util/net/AprEndpoint.java
  
  Index: AprEndpoint.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-connectors/util/java/org/apache/tomcat/util/net/AprEndpoint.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- AprEndpoint.java  20 Apr 2005 00:13:24 -0000      1.13
  +++ AprEndpoint.java  20 Apr 2005 16:13:23 -0000      1.14
  @@ -282,6 +282,14 @@
   
   
       /**
  +     * Use endfile for sending static files.
  +     */
  +    protected boolean useSendfile = false;
  +    public void setUseSendfile(boolean useSendfile) { this.useSendfile = 
useSendfile; }
  +    public boolean getUseSendfile() { return useSendfile; }
  +
  +
  +    /**
        * Number of keepalive sockets.
        */
       protected int keepAliveCount = 0;
  @@ -546,7 +554,7 @@
           if (tcpNoDelay)
               Socket.optSet(socket, Socket.APR_TCP_NODELAY, (tcpNoDelay ? 1 : 
0));
           if (soTimeout > 0)
  -            Socket.timeoutSet(socket, soTimeout);
  +            Socket.timeoutSet(socket, soTimeout * 1000);
       }
   
   
  
  
  
  1.22      +6 -1      
jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/connector/Request.java
  
  Index: Request.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/connector/Request.java,v
  retrieving revision 1.21
  retrieving revision 1.22
  diff -u -r1.21 -r1.22
  --- Request.java      1 Apr 2005 11:36:52 -0000       1.21
  +++ Request.java      20 Apr 2005 16:13:23 -0000      1.22
  @@ -1366,6 +1366,11 @@
               replaced = true;
           }
   
  +        // Pass special attributes to the native layer
  +        if (name.startsWith("sendfile.")) {
  +            coyoteRequest.setAttribute(name, value);
  +        }
  +        
           // Notify interested application event listeners
           Object listeners[] = context.getApplicationEventListeners();
           if ((listeners == null) || (listeners.length == 0))
  
  
  
  1.32      +34 -3     
jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/servlets/DefaultServlet.java
  
  Index: DefaultServlet.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/servlets/DefaultServlet.java,v
  retrieving revision 1.31
  retrieving revision 1.32
  diff -u -r1.31 -r1.32
  --- DefaultServlet.java       18 Dec 2004 12:04:40 -0000      1.31
  +++ DefaultServlet.java       20 Apr 2005 16:13:23 -0000      1.32
  @@ -818,7 +818,8 @@
                       // Silent catch
                   }
                   if (ostream != null) {
  -                    copy(cacheEntry, renderResult, ostream);
  +                    if (!checkSendfile(request, response, cacheEntry, 
contentLength, null))
  +                        copy(cacheEntry, renderResult, ostream);
                   } else {
                       copy(cacheEntry, renderResult, writer);
                   }
  @@ -856,7 +857,8 @@
                           // Silent catch
                       }
                       if (ostream != null) {
  -                        copy(cacheEntry, ostream, range);
  +                        if (!checkSendfile(request, response, cacheEntry, 
range.end - range.start + 1, range))
  +                            copy(cacheEntry, ostream, range);
                       } else {
                           copy(cacheEntry, writer, range);
                       }
  @@ -1480,6 +1482,35 @@
   
   
       /**
  +     * Check if sendfile can be used.
  +     */
  +    private boolean checkSendfile(HttpServletRequest request,
  +                                  HttpServletResponse response,
  +                                  CacheEntry entry,
  +                                  long length, Range range) {
  +        if ((entry.resource != null) 
  +            && ((length > 256 * 1024) || (entry.resource.getContent() == 
null))
  +            && (entry.attributes.getCanonicalPath() != null)
  +            && (Boolean.TRUE == request.getAttribute("sendfile.support"))
  +            && 
(request.getClass().getName().equals("org.apache.catalina.connector.RequestFacade"))
  +            && 
(response.getClass().getName().equals("org.apache.catalina.connector.ResponseFacade")))
 {
  +            request.setAttribute("sendfile.filename", 
entry.attributes.getCanonicalPath());
  +            if (range == null) {
  +                request.setAttribute("sendfile.start", new Long(0L));
  +                request.setAttribute("sendfile.end", new Long(length));
  +            } else {
  +                request.setAttribute("sendfile.start", new 
Long(range.start));
  +                request.setAttribute("sendfile.end", new Long(range.end + 
1));
  +            }
  +            request.setAttribute("sendfile.token", this);
  +            return true;
  +        } else {
  +            return false;
  +        }
  +    }
  +    
  +    
  +    /**
        * Check if the if-match condition is satisfied.
        *
        * @param request The servlet request we are processing
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to