Author: fhanik
Date: Tue Nov 25 19:39:03 2008
New Revision: 720724

URL: http://svn.apache.org/viewvc?rev=720724&view=rev
Log:
First attempt for SSL send file

Modified:
    tomcat/trunk/java/org/apache/coyote/http11/Http11NioProtocol.java
    tomcat/trunk/java/org/apache/tomcat/util/net/NioChannel.java
    tomcat/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java
    tomcat/trunk/java/org/apache/tomcat/util/net/SecureNioChannel.java

Modified: tomcat/trunk/java/org/apache/coyote/http11/Http11NioProtocol.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/coyote/http11/Http11NioProtocol.java?rev=720724&r1=720723&r2=720724&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/coyote/http11/Http11NioProtocol.java (original)
+++ tomcat/trunk/java/org/apache/coyote/http11/Http11NioProtocol.java Tue Nov 
25 19:39:03 2008
@@ -309,7 +309,6 @@
     public void setUseSendfile(boolean useSendfile) {
         ep.setUseSendfile(useSendfile);
     }
-
     
     // -------------------- Tcp setup --------------------
 

Modified: tomcat/trunk/java/org/apache/tomcat/util/net/NioChannel.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/net/NioChannel.java?rev=720724&r1=720723&r2=720724&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/net/NioChannel.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/util/net/NioChannel.java Tue Nov 25 
19:39:03 2008
@@ -47,6 +47,8 @@
     protected ApplicationBufferHandler bufHandler;
 
     protected Poller poller;
+    
+    protected boolean sendFile = false;
 
     public NioChannel(SocketChannel channel, ApplicationBufferHandler 
bufHandler) throws IOException {
         this.sc = channel;
@@ -56,6 +58,7 @@
     public void reset() throws IOException {
         bufHandler.getReadBuffer().clear();
         bufHandler.getWriteBuffer().clear();
+        this.sendFile = false;
     }
     
     public int getBufferSize() {
@@ -191,5 +194,22 @@
     public String toString() {
         return super.toString()+":"+this.sc.toString();
     }
+    
+    public int getOutboundRemaining() {
+        return 0;
+    }
+    
+    public void flushOutbound() throws IOException {
+        
+    }
+    
+    public boolean isSendFile() {
+        return sendFile;
+    }
+    
+    public void setSendFile(boolean s) {
+        this.sendFile = s;
+    }
+    
 
 }

Modified: tomcat/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java?rev=720724&r1=720723&r2=720724&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java Tue Nov 25 
19:39:03 2008
@@ -30,6 +30,7 @@
 import java.nio.channels.Selector;
 import java.nio.channels.ServerSocketChannel;
 import java.nio.channels.SocketChannel;
+import java.nio.channels.WritableByteChannel;
 import java.security.KeyStore;
 import java.util.Collection;
 import java.util.Iterator;
@@ -644,7 +645,6 @@
     }
 
     public void setUseSendfile(boolean useSendfile) {
-
         this.useSendfile = useSendfile;
     }
 
@@ -958,8 +958,7 @@
     }
 
     public boolean getUseSendfile() {
-        //send file doesn't work with SSL
-        return useSendfile && (!isSSLEnabled());
+        return useSendfile;
     }
 
     public int getOomParachute() {
@@ -1328,10 +1327,10 @@
                     } else {
                         cancel = true;
                     }
-                    if ( cancel ) 
getPoller0().cancelledKey(key,SocketStatus.ERROR,false);
+                    if ( cancel ) 
socket.getPoller().cancelledKey(key,SocketStatus.ERROR,false);
                 }catch (CancelledKeyException ckx) {
                     try {
-                        
getPoller0().cancelledKey(key,SocketStatus.DISCONNECT,true);
+                        
socket.getPoller().cancelledKey(key,SocketStatus.DISCONNECT,true);
                     }catch (Exception ignore) {}
                 }
             }//end if
@@ -1627,6 +1626,7 @@
         }
         
         public boolean processSendfile(SelectionKey sk, KeyAttachment 
attachment, boolean reg, boolean event) {
+            NioChannel sc = null;
             try {
                 //unreg(sk,attachment);//only do this if we do process send 
file on a separate thread
                 SendfileData sd = attachment.getSendfileData();
@@ -1638,13 +1638,20 @@
                     }
                     sd.fchannel = new FileInputStream(f).getChannel();
                 }
-                SocketChannel sc = attachment.getChannel().getIOChannel();
-                long written = sd.fchannel.transferTo(sd.pos,sd.length,sc);
-                if ( written > 0 ) {
-                    sd.pos += written;
-                    sd.length -= written;
+                sc = attachment.getChannel();
+                sc.setSendFile(true);
+                WritableByteChannel wc =(WritableByteChannel) ((sc instanceof 
SecureNioChannel)?sc:sc.getIOChannel());
+                
+                if (sc.getOutboundRemaining()>0) {
+                    sc.flushOutbound();
+                } else {
+                    long written = sd.fchannel.transferTo(sd.pos,sd.length,wc);
+                    if ( written > 0 ) {
+                        sd.pos += written;
+                        sd.length -= written;
+                    }
                 }
-                if ( sd.length <= 0 ) {
+                if ( sd.length <= 0 && sc.getOutboundRemaining()<=0) {
                     if (log.isDebugEnabled()) {
                         log.debug("Send file complete for:"+sd.fileName);
                     }
@@ -1684,6 +1691,8 @@
                 log.error("",t);
                 cancelledKey(sk, SocketStatus.ERROR, false);
                 return false;
+            }finally {
+                if (sc!=null) sc.setSendFile(false);
             }
             return true;
         }

Modified: tomcat/trunk/java/org/apache/tomcat/util/net/SecureNioChannel.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/net/SecureNioChannel.java?rev=720724&r1=720723&r2=720724&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/net/SecureNioChannel.java 
(original)
+++ tomcat/trunk/java/org/apache/tomcat/util/net/SecureNioChannel.java Tue Nov 
25 19:39:03 2008
@@ -400,7 +400,7 @@
             return written;
         } else {
             //make sure we can handle expand, and that we only use on buffer
-            if ( src != bufHandler.getWriteBuffer() ) throw new 
IllegalArgumentException("You can only write using the application write buffer 
provided by the handler.");
+            if ( (!this.isSendFile()) && (src != bufHandler.getWriteBuffer()) 
) throw new IllegalArgumentException("You can only write using the application 
write buffer provided by the handler.");
             //are we closing or closed?
             if ( closing || closed) throw new IOException("Channel is in 
closing state.");
 
@@ -434,6 +434,17 @@
         }
     }
     
+    @Override
+    public int getOutboundRemaining() {
+        return netOutBuffer.remaining();
+    }
+    
+    @Override
+    public void flushOutbound() throws IOException {
+        flush(netOutBuffer);
+    }
+
+    
     /**
      * Callback interface to be able to expand buffers
      * when buffer overflow exceptions happen



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

Reply via email to