jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dpfis...@apache.org
Subject svn commit: r1177301 - in /jackrabbit/sandbox/microkernel/src: main/java/org/apache/jackrabbit/mk/client/ main/java/org/apache/jackrabbit/mk/cluster/ main/java/org/apache/jackrabbit/mk/server/ test/java/org/apache/jackrabbit/mk/server/
Date Thu, 29 Sep 2011 13:26:02 GMT
Author: dpfister
Date: Thu Sep 29 13:26:02 2011
New Revision: 1177301

URL: http://svn.apache.org/viewvc?rev=1177301&view=rev
Log:
HTTP interface to microkernel
- add Keep-Alive support

Added:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/cluster/
Modified:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/client/HttpExecutor.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/server/HttpProcessor.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/server/Request.java
    jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/server/BoundaryInputStreamTest.java

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/client/HttpExecutor.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/client/HttpExecutor.java?rev=1177301&r1=1177300&r2=1177301&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/client/HttpExecutor.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/client/HttpExecutor.java
Thu Sep 29 13:26:02 2011
@@ -45,6 +45,8 @@ class HttpExecutor implements Closeable 
     private InputStream socketIn;
     
     private OutputStream socketOut;
+    
+    private boolean connectionClosed;
 
     /**
      * Create a new instance of this class.
@@ -159,6 +161,11 @@ class HttpExecutor implements Closeable 
             reqIn = new BoundedInputStream(socketIn, contentLength);
         }
         
+        String connectionState = headers.get("Connection");
+        if ("close".equalsIgnoreCase(connectionState)) {
+            connectionClosed = true;
+        }
+        
         switch (statusCode) {
         case 200:
             return reqIn;
@@ -171,7 +178,7 @@ class HttpExecutor implements Closeable 
     }
     
     public boolean isAlive() {
-        return !socket.isClosed();
+        return !connectionClosed && !socket.isClosed();
     }
 
     public void close() {

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/server/HttpProcessor.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/server/HttpProcessor.java?rev=1177301&r1=1177300&r2=1177301&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/server/HttpProcessor.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/server/HttpProcessor.java
Thu Sep 29 13:26:02 2011
@@ -32,7 +32,9 @@ class HttpProcessor {
     
     private static final int INITIAL_SO_TIMEOUT = 1000;
     
-    private static final int DEFAULT_SO_TIMEOUT = 15000; 
+    private static final int DEFAULT_SO_TIMEOUT = 30000;
+    
+    private static final int MAX_KEEP_ALIVE_REQUESTS = 30;
 
     private final Socket socket;
     
@@ -63,18 +65,16 @@ class HttpProcessor {
             socketIn = new BufferedInputStream(socket.getInputStream());
             socketOut = new BufferedOutputStream(socket.getOutputStream());
             
-            int timeout = INITIAL_SO_TIMEOUT;
-            socket.setSoTimeout(timeout);
+            socket.setSoTimeout(INITIAL_SO_TIMEOUT);
             
-            for (;;) {
-                if (!process1()) {
+            for (int requestNum = 0; ; requestNum++) {
+                if (!process(requestNum)) {
                     break;
                 }
-                if (timeout != DEFAULT_SO_TIMEOUT) {
-                    socket.setSoTimeout(timeout = DEFAULT_SO_TIMEOUT);
+                if (requestNum == 0) {
+                    socket.setSoTimeout(DEFAULT_SO_TIMEOUT);
                 }
             }
-        
         }  finally {
             IOUtils.closeQuietly(socketOut);
             IOUtils.closeQuietly(socketIn);
@@ -85,27 +85,34 @@ class HttpProcessor {
     /**
      * Process a single request.
      * 
+     * @param requestNum number of this request on the same persistent connection
      * @return <code>true</code> if the connection should be kept alive;
      *         <code>false</code> otherwise
      *         
      * @throws IOException if an I/O error occurs
      */
-    private boolean process1() throws IOException {
+    private boolean process(int requestNum) throws IOException {
         Request request = null;
         Response response = null;
         
         try {
             request = Request.parse(socketIn);
-            if (request != null) {
-                boolean keepAlive = request.isKeepAlive();
-                response = new Response(socketOut, keepAlive);
-                servlet.service(request, response);
-                return keepAlive;
+        } catch (IOException e) {
+            if (requestNum == 0) {
+                // ignore errors on the very first request (might be wrong protocol)
+                return false;
             }
+            throw e;
+        }
+        try {
+            boolean keepAlive = request.isKeepAlive();
+            keepAlive &= (requestNum + 1 < MAX_KEEP_ALIVE_REQUESTS);
+            response = new Response(socketOut, keepAlive);
+            servlet.service(request, response);
+            return keepAlive;
         } finally {
             IOUtils.closeQuietly(request);
             IOUtils.closeQuietly(response);
         }
-        return false;
     }
 }

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/server/Request.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/server/Request.java?rev=1177301&r1=1177300&r2=1177301&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/server/Request.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/server/Request.java
Thu Sep 29 13:26:02 2011
@@ -83,7 +83,8 @@ class Request implements Closeable {
         
         String[] parts = requestLine.split(" ");
         if (parts.length != 3) {
-            return null;
+            String msg = String.format("Bad HTTP request line: %s", requestLine);
+            throw new IOException(msg);
         }
         String method = parts[0];
         String uri = parts[1];
@@ -138,7 +139,14 @@ class Request implements Closeable {
     }
     
     private String getContentType() {
-        return headers.get("Content-Type");
+        String ct = headers.get("Content-Type");
+        if (ct != null) {
+            int sep = ct.indexOf(';');
+            if (sep != -1) {
+                ct = ct.substring(0, sep).trim();
+            }
+        }
+        return ct;
     }
     
     private int getContentLength() {
@@ -208,8 +216,8 @@ class Request implements Closeable {
     }
 
     public InputStream getFileParameter(String name) throws IOException {
-        String ctype = getContentType();
-        if (ctype == null || !ctype.startsWith("multipart/form-data")) {
+        String ct = getContentType();
+        if (ct == null || !ct.startsWith("multipart/form-data")) {
             return null;
         }
         if (reqIn != null) {
@@ -225,7 +233,7 @@ class Request implements Closeable {
             if (line.length() == 0) {
                 break;
             }
-            // TODO collect other information (such as mime type)
+            // TODO evaluate other information (such as mime type)
         }
         return new BoundaryInputStream(body, boundary);
     }
@@ -239,7 +247,7 @@ class Request implements Closeable {
         }
     }
     
-    public InputStream getInputStream() throws IOException {
+    public InputStream getInputStream() {
         if (reqIn == null) {
             String encoding = headers.get("Transfer-Encoding");
             if ("chunked".equalsIgnoreCase(encoding)) {
@@ -261,8 +269,10 @@ class Request implements Closeable {
     
     public void close() {
         if (in != null) {
-            IOUtils.closeQuietly(reqIn);
-
+            // Consume a possibly non-empty body by triggering the
+            // creation of our request input stream
+            getInputStream();
+            IOUtils.closeQuietly(reqIn);            
             in = null;
         }
     }

Modified: jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/server/BoundaryInputStreamTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/server/BoundaryInputStreamTest.java?rev=1177301&r1=1177300&r2=1177301&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/server/BoundaryInputStreamTest.java
(original)
+++ jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/server/BoundaryInputStreamTest.java
Thu Sep 29 13:26:02 2011
@@ -27,7 +27,8 @@ import org.apache.jackrabbit.mk.util.IOU
 
 public class BoundaryInputStreamTest extends TestCase {
 
-    private static final String BOUNDARY = "------WebKitFormBoundaryHB5WJrSAPZjfwtqt--";
+    private static final String BOUNDARY = "------ClientFormBoundaryHB5WJrSAPZjfwtqt--";
+    
     private static final Random RANDOM = new Random(System.currentTimeMillis());
 
     @Override



Mime
View raw message