You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2017/05/09 20:03:50 UTC

[21/35] httpcomponents-core git commit: I/O event dispatchers to shut down the I/O session in case of a runtime exception (merged from trunk)

I/O event dispatchers to shut down the I/O session in case of a runtime exception (merged from trunk)

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpcore/branches/4.1.x@1157057 13f79535-47bb-0310-9956-ffa450edef68


Project: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/commit/cc013c33
Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/tree/cc013c33
Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/diff/cc013c33

Branch: refs/heads/4.1.x
Commit: cc013c33718bc443393b78d9b642a46f57f80cdb
Parents: f4a290e
Author: Oleg Kalnichevski <ol...@apache.org>
Authored: Fri Aug 12 11:30:47 2011 +0000
Committer: Oleg Kalnichevski <ol...@apache.org>
Committed: Fri Aug 12 11:30:47 2011 +0000

----------------------------------------------------------------------
 .../impl/nio/DefaultClientIOEventDispatch.java  |  71 +++++++---
 .../impl/nio/DefaultServerIOEventDispatch.java  |  54 ++++---
 .../impl/nio/ssl/SSLClientIOEventDispatch.java  | 139 +++++++++++--------
 .../impl/nio/ssl/SSLServerIOEventDispatch.java  | 136 ++++++++++--------
 4 files changed, 241 insertions(+), 159 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/cc013c33/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultClientIOEventDispatch.java
----------------------------------------------------------------------
diff --git a/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultClientIOEventDispatch.java b/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultClientIOEventDispatch.java
index bf2a61d..2215cc7 100644
--- a/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultClientIOEventDispatch.java
+++ b/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultClientIOEventDispatch.java
@@ -120,23 +120,33 @@ public class DefaultClientIOEventDispatch implements IOEventDispatch {
      * @return newly created HTTP connection.
      */
     protected NHttpClientIOTarget createConnection(final IOSession session) {
-        return new DefaultNHttpClientConnection(
-                session,
-                createHttpResponseFactory(),
-                this.allocator,
-                this.params);
+    	try {
+            return new DefaultNHttpClientConnection(
+                    session,
+                    createHttpResponseFactory(),
+                    this.allocator,
+                    this.params);
+    	} catch (RuntimeException ex) {
+    		session.shutdown();
+    		throw ex;
+    	}
     }
 
     public void connected(final IOSession session) {
-        NHttpClientIOTarget conn = createConnection(session);
-        Object attachment = session.getAttribute(IOSession.ATTACHMENT_KEY);
-        session.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
-        this.handler.connected(conn, attachment);
+    	try {
+            NHttpClientIOTarget conn = createConnection(session);
+            Object attachment = session.getAttribute(IOSession.ATTACHMENT_KEY);
+            session.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
+            this.handler.connected(conn, attachment);
+    	} catch (RuntimeException ex) {
+    		session.shutdown();
+    		throw ex;
+    	}
     }
 
     public void disconnected(final IOSession session) {
-        NHttpClientIOTarget conn =
-            (NHttpClientIOTarget) session.getAttribute(ExecutionContext.HTTP_CONNECTION);
+        NHttpClientIOTarget conn = (NHttpClientIOTarget) session.getAttribute(
+        		ExecutionContext.HTTP_CONNECTION);
         if (conn != null) {
             this.handler.closed(conn);
         }
@@ -149,24 +159,39 @@ public class DefaultClientIOEventDispatch implements IOEventDispatch {
     }
 
     public void inputReady(final IOSession session) {
-        NHttpClientIOTarget conn =
-            (NHttpClientIOTarget) session.getAttribute(ExecutionContext.HTTP_CONNECTION);
-        ensureNotNull(conn);
-        conn.consumeInput(this.handler);
+    	try {
+            NHttpClientIOTarget conn = (NHttpClientIOTarget) session.getAttribute(
+            		ExecutionContext.HTTP_CONNECTION);
+            ensureNotNull(conn);
+            conn.consumeInput(this.handler);
+    	} catch (RuntimeException ex) {
+    		session.shutdown();
+    		throw ex;
+    	}
     }
 
     public void outputReady(final IOSession session) {
-        NHttpClientIOTarget conn =
-            (NHttpClientIOTarget) session.getAttribute(ExecutionContext.HTTP_CONNECTION);
-        ensureNotNull(conn);
-        conn.produceOutput(this.handler);
+    	try {
+            NHttpClientIOTarget conn = (NHttpClientIOTarget) session.getAttribute(
+            		ExecutionContext.HTTP_CONNECTION);
+            ensureNotNull(conn);
+            conn.produceOutput(this.handler);
+    	} catch (RuntimeException ex) {
+    		session.shutdown();
+    		throw ex;
+    	}
     }
 
     public void timeout(final IOSession session) {
-        NHttpClientIOTarget conn =
-            (NHttpClientIOTarget) session.getAttribute(ExecutionContext.HTTP_CONNECTION);
-        ensureNotNull(conn);
-        this.handler.timeout(conn);
+    	try {
+            NHttpClientIOTarget conn = (NHttpClientIOTarget) session.getAttribute(
+            		ExecutionContext.HTTP_CONNECTION);
+            ensureNotNull(conn);
+            this.handler.timeout(conn);
+    	} catch (RuntimeException ex) {
+    		session.shutdown();
+    		throw ex;
+    	}
     }
 
 }

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/cc013c33/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultServerIOEventDispatch.java
----------------------------------------------------------------------
diff --git a/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultServerIOEventDispatch.java b/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultServerIOEventDispatch.java
index ed573ce..1dfe6f9 100644
--- a/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultServerIOEventDispatch.java
+++ b/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultServerIOEventDispatch.java
@@ -128,14 +128,19 @@ public class DefaultServerIOEventDispatch implements IOEventDispatch {
     }
 
     public void connected(final IOSession session) {
-        NHttpServerIOTarget conn = createConnection(session);
-        session.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
-        this.handler.connected(conn);
+    	try {
+            NHttpServerIOTarget conn = createConnection(session);
+            session.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
+            this.handler.connected(conn);
+    	} catch (RuntimeException ex) {
+    		session.shutdown();
+    		throw ex;
+    	}
     }
 
     public void disconnected(final IOSession session) {
-        NHttpServerIOTarget conn =
-            (NHttpServerIOTarget) session.getAttribute(ExecutionContext.HTTP_CONNECTION);
+        NHttpServerIOTarget conn = (NHttpServerIOTarget) session.getAttribute(
+        		ExecutionContext.HTTP_CONNECTION);
         if (conn != null) {
             this.handler.closed(conn);
         }
@@ -148,24 +153,39 @@ public class DefaultServerIOEventDispatch implements IOEventDispatch {
     }
 
     public void inputReady(final IOSession session) {
-        NHttpServerIOTarget conn =
-            (NHttpServerIOTarget) session.getAttribute(ExecutionContext.HTTP_CONNECTION);
-        ensureNotNull(conn);
-        conn.consumeInput(this.handler);
+    	try {
+            NHttpServerIOTarget conn = (NHttpServerIOTarget) session.getAttribute(
+            		ExecutionContext.HTTP_CONNECTION);
+            ensureNotNull(conn);
+            conn.consumeInput(this.handler);
+    	} catch (RuntimeException ex) {
+    		session.shutdown();
+    		throw ex;
+    	}
     }
 
     public void outputReady(final IOSession session) {
-        NHttpServerIOTarget conn =
-            (NHttpServerIOTarget) session.getAttribute(ExecutionContext.HTTP_CONNECTION);
-        ensureNotNull(conn);
-        conn.produceOutput(this.handler);
+    	try {
+            NHttpServerIOTarget conn = (NHttpServerIOTarget) session.getAttribute(
+            		ExecutionContext.HTTP_CONNECTION);
+            ensureNotNull(conn);
+            conn.produceOutput(this.handler);
+    	} catch (RuntimeException ex) {
+    		session.shutdown();
+    		throw ex;
+    	}
     }
 
     public void timeout(final IOSession session) {
-        NHttpServerIOTarget conn =
-            (NHttpServerIOTarget) session.getAttribute(ExecutionContext.HTTP_CONNECTION);
-        ensureNotNull(conn);
-        this.handler.timeout(conn);
+    	try {
+            NHttpServerIOTarget conn = (NHttpServerIOTarget) session.getAttribute(
+            		ExecutionContext.HTTP_CONNECTION);
+            ensureNotNull(conn);
+            this.handler.timeout(conn);
+    	} catch (RuntimeException ex) {
+    		session.shutdown();
+    		throw ex;
+    	}
     }
 
 }

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/cc013c33/httpcore-nio/src/main/java/org/apache/http/impl/nio/ssl/SSLClientIOEventDispatch.java
----------------------------------------------------------------------
diff --git a/httpcore-nio/src/main/java/org/apache/http/impl/nio/ssl/SSLClientIOEventDispatch.java b/httpcore-nio/src/main/java/org/apache/http/impl/nio/ssl/SSLClientIOEventDispatch.java
index 1fbf622..03471fc 100644
--- a/httpcore-nio/src/main/java/org/apache/http/impl/nio/ssl/SSLClientIOEventDispatch.java
+++ b/httpcore-nio/src/main/java/org/apache/http/impl/nio/ssl/SSLClientIOEventDispatch.java
@@ -186,32 +186,36 @@ public class SSLClientIOEventDispatch implements IOEventDispatch {
     }
 
     public void connected(final IOSession session) {
+    	try {
+            SSLIOSession sslSession = createSSLIOSession(
+                    session,
+                    this.sslcontext,
+                    this.sslHandler);
 
-        SSLIOSession sslSession = createSSLIOSession(
-                session,
-                this.sslcontext,
-                this.sslHandler);
-
-        NHttpClientIOTarget conn = createConnection(
-                sslSession);
+            NHttpClientIOTarget conn = createConnection(
+                    sslSession);
 
-        session.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
-        session.setAttribute(SSL_SESSION, sslSession);
+            session.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
+            session.setAttribute(SSL_SESSION, sslSession);
 
-        Object attachment = session.getAttribute(IOSession.ATTACHMENT_KEY);
-        this.handler.connected(conn, attachment);
+            Object attachment = session.getAttribute(IOSession.ATTACHMENT_KEY);
+            this.handler.connected(conn, attachment);
 
-        try {
-            sslSession.bind(SSLMode.CLIENT, this.params);
-        } catch (SSLException ex) {
-            this.handler.exception(conn, ex);
-            sslSession.shutdown();
-        }
+            try {
+                sslSession.bind(SSLMode.CLIENT, this.params);
+            } catch (SSLException ex) {
+                this.handler.exception(conn, ex);
+                sslSession.shutdown();
+            }
+    	} catch (RuntimeException ex) {
+    		session.shutdown();
+    		throw ex;
+    	}
     }
 
     public void disconnected(final IOSession session) {
-        NHttpClientIOTarget conn =
-            (NHttpClientIOTarget) session.getAttribute(ExecutionContext.HTTP_CONNECTION);
+        NHttpClientIOTarget conn = (NHttpClientIOTarget) session.getAttribute(
+        		ExecutionContext.HTTP_CONNECTION);
         if (conn != null) {
             this.handler.closed(conn);
         }
@@ -230,58 +234,73 @@ public class SSLClientIOEventDispatch implements IOEventDispatch {
     }
 
     public void inputReady(final IOSession session) {
-        NHttpClientIOTarget conn =
-            (NHttpClientIOTarget) session.getAttribute(ExecutionContext.HTTP_CONNECTION);
-        ensureNotNull(conn);
-        SSLIOSession sslSession =
-            (SSLIOSession) session.getAttribute(SSL_SESSION);
-        ensureNotNull(sslSession);
+    	try {
+	        NHttpClientIOTarget conn = (NHttpClientIOTarget) session.getAttribute(
+	        		ExecutionContext.HTTP_CONNECTION);
+            ensureNotNull(conn);
+            SSLIOSession sslSession =
+                (SSLIOSession) session.getAttribute(SSL_SESSION);
+            ensureNotNull(sslSession);
 
-        try {
-            if (sslSession.isAppInputReady()) {
-                conn.consumeInput(this.handler);
+            try {
+                if (sslSession.isAppInputReady()) {
+                    conn.consumeInput(this.handler);
+                }
+                sslSession.inboundTransport();
+            } catch (IOException ex) {
+                this.handler.exception(conn, ex);
+                sslSession.shutdown();
             }
-            sslSession.inboundTransport();
-        } catch (IOException ex) {
-            this.handler.exception(conn, ex);
-            sslSession.shutdown();
-        }
+    	} catch (RuntimeException ex) {
+    		session.shutdown();
+    		throw ex;
+    	}
     }
 
     public void outputReady(final IOSession session) {
-        NHttpClientIOTarget conn =
-            (NHttpClientIOTarget) session.getAttribute(ExecutionContext.HTTP_CONNECTION);
-        ensureNotNull(conn);
-        SSLIOSession sslSession =
-            (SSLIOSession) session.getAttribute(SSL_SESSION);
-        ensureNotNull(sslSession);
+    	try {
+	        NHttpClientIOTarget conn = (NHttpClientIOTarget) session.getAttribute(
+	        		ExecutionContext.HTTP_CONNECTION);
+            ensureNotNull(conn);
+            SSLIOSession sslSession =
+                (SSLIOSession) session.getAttribute(SSL_SESSION);
+            ensureNotNull(sslSession);
 
-        try {
-            if (sslSession.isAppOutputReady()) {
-                conn.produceOutput(this.handler);
+            try {
+                if (sslSession.isAppOutputReady()) {
+                    conn.produceOutput(this.handler);
+                }
+                sslSession.outboundTransport();
+            } catch (IOException ex) {
+                this.handler.exception(conn, ex);
+                sslSession.shutdown();
             }
-            sslSession.outboundTransport();
-        } catch (IOException ex) {
-            this.handler.exception(conn, ex);
-            sslSession.shutdown();
-        }
+    	} catch (RuntimeException ex) {
+    		session.shutdown();
+    		throw ex;
+    	}
     }
 
     public void timeout(final IOSession session) {
-        NHttpClientIOTarget conn =
-            (NHttpClientIOTarget) session.getAttribute(ExecutionContext.HTTP_CONNECTION);
-        ensureNotNull(conn);
-        SSLIOSession sslSession =
-            (SSLIOSession) session.getAttribute(SSL_SESSION);
-        ensureNotNull(sslSession);
+    	try {
+	        NHttpClientIOTarget conn = (NHttpClientIOTarget) session.getAttribute(
+	        		ExecutionContext.HTTP_CONNECTION);
+	        ensureNotNull(conn);
+	        SSLIOSession sslSession =
+	            (SSLIOSession) session.getAttribute(SSL_SESSION);
+	        ensureNotNull(sslSession);
 
-        this.handler.timeout(conn);
-        synchronized (sslSession) {
-            if (sslSession.isOutboundDone() && !sslSession.isInboundDone()) {
-                // The session failed to terminate cleanly
-                sslSession.shutdown();
-            }
-        }
+	        this.handler.timeout(conn);
+	        synchronized (sslSession) {
+	            if (sslSession.isOutboundDone() && !sslSession.isInboundDone()) {
+	                // The session failed to terminate cleanly
+	                sslSession.shutdown();
+	            }
+	        }
+    	} catch (RuntimeException ex) {
+    		session.shutdown();
+    		throw ex;
+    	}
     }
 
 }

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/cc013c33/httpcore-nio/src/main/java/org/apache/http/impl/nio/ssl/SSLServerIOEventDispatch.java
----------------------------------------------------------------------
diff --git a/httpcore-nio/src/main/java/org/apache/http/impl/nio/ssl/SSLServerIOEventDispatch.java b/httpcore-nio/src/main/java/org/apache/http/impl/nio/ssl/SSLServerIOEventDispatch.java
index d43c1fe..d4839a5 100644
--- a/httpcore-nio/src/main/java/org/apache/http/impl/nio/ssl/SSLServerIOEventDispatch.java
+++ b/httpcore-nio/src/main/java/org/apache/http/impl/nio/ssl/SSLServerIOEventDispatch.java
@@ -186,32 +186,35 @@ public class SSLServerIOEventDispatch implements IOEventDispatch {
     }
 
     public void connected(final IOSession session) {
+    	try {
+            SSLIOSession sslSession = createSSLIOSession(
+                    session,
+                    this.sslcontext,
+                    this.sslHandler);
 
-        SSLIOSession sslSession = createSSLIOSession(
-                session,
-                this.sslcontext,
-                this.sslHandler);
-
-        NHttpServerIOTarget conn = createConnection(
-                sslSession);
+            NHttpServerIOTarget conn = createConnection(
+                    sslSession);
 
-        session.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
-        session.setAttribute(SSL_SESSION, sslSession);
+            session.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
+            session.setAttribute(SSL_SESSION, sslSession);
 
-        this.handler.connected(conn);
+            this.handler.connected(conn);
 
-        try {
-            sslSession.bind(SSLMode.SERVER, this.params);
-        } catch (SSLException ex) {
-            this.handler.exception(conn, ex);
-            sslSession.shutdown();
-        }
+            try {
+                sslSession.bind(SSLMode.SERVER, this.params);
+            } catch (SSLException ex) {
+                this.handler.exception(conn, ex);
+                sslSession.shutdown();
+            }
+    	} catch (RuntimeException ex) {
+    		session.shutdown();
+    		throw ex;
+    	}
     }
 
     public void disconnected(final IOSession session) {
-        NHttpServerIOTarget conn =
-            (NHttpServerIOTarget) session.getAttribute(ExecutionContext.HTTP_CONNECTION);
-
+    	NHttpServerIOTarget conn = (NHttpServerIOTarget) session.getAttribute(
+        		ExecutionContext.HTTP_CONNECTION);
         if (conn != null) {
             this.handler.closed(conn);
         }
@@ -230,58 +233,73 @@ public class SSLServerIOEventDispatch implements IOEventDispatch {
     }
 
     public void inputReady(final IOSession session) {
-        NHttpServerIOTarget conn =
-            (NHttpServerIOTarget) session.getAttribute(ExecutionContext.HTTP_CONNECTION);
-        ensureNotNull(conn);
-        SSLIOSession sslSession =
-            (SSLIOSession) session.getAttribute(SSL_SESSION);
-        ensureNotNull(sslSession);
+    	try {
+        	NHttpServerIOTarget conn = (NHttpServerIOTarget) session.getAttribute(
+            		ExecutionContext.HTTP_CONNECTION);
+            ensureNotNull(conn);
+            SSLIOSession sslSession =
+                (SSLIOSession) session.getAttribute(SSL_SESSION);
+            ensureNotNull(sslSession);
 
-        try {
-            if (sslSession.isAppInputReady()) {
-                conn.consumeInput(this.handler);
+            try {
+                if (sslSession.isAppInputReady()) {
+                    conn.consumeInput(this.handler);
+                }
+                sslSession.inboundTransport();
+            } catch (IOException ex) {
+                this.handler.exception(conn, ex);
+                sslSession.shutdown();
             }
-            sslSession.inboundTransport();
-        } catch (IOException ex) {
-            this.handler.exception(conn, ex);
-            sslSession.shutdown();
-        }
+    	} catch (RuntimeException ex) {
+    		session.shutdown();
+    		throw ex;
+    	}
     }
 
     public void outputReady(final IOSession session) {
-        NHttpServerIOTarget conn =
-            (NHttpServerIOTarget) session.getAttribute(ExecutionContext.HTTP_CONNECTION);
-        ensureNotNull(conn);
-        SSLIOSession sslSession =
-            (SSLIOSession) session.getAttribute(SSL_SESSION);
-        ensureNotNull(sslSession);
+    	try {
+        	NHttpServerIOTarget conn = (NHttpServerIOTarget) session.getAttribute(
+            		ExecutionContext.HTTP_CONNECTION);
+            ensureNotNull(conn);
+            SSLIOSession sslSession =
+                (SSLIOSession) session.getAttribute(SSL_SESSION);
+            ensureNotNull(sslSession);
 
-        try {
-            if (sslSession.isAppOutputReady()) {
-                conn.produceOutput(this.handler);
+            try {
+                if (sslSession.isAppOutputReady()) {
+                    conn.produceOutput(this.handler);
+                }
+                sslSession.outboundTransport();
+            } catch (IOException ex) {
+                this.handler.exception(conn, ex);
+                sslSession.shutdown();
             }
-            sslSession.outboundTransport();
-        } catch (IOException ex) {
-            this.handler.exception(conn, ex);
-            sslSession.shutdown();
-        }
+    	} catch (RuntimeException ex) {
+    		session.shutdown();
+    		throw ex;
+    	}
     }
 
     public void timeout(final IOSession session) {
-        NHttpServerIOTarget conn =
-            (NHttpServerIOTarget) session.getAttribute(ExecutionContext.HTTP_CONNECTION);
-        ensureNotNull(conn);
-        SSLIOSession sslSession =
-            (SSLIOSession) session.getAttribute(SSL_SESSION);
-        ensureNotNull(sslSession);
+    	try {
+        	NHttpServerIOTarget conn = (NHttpServerIOTarget) session.getAttribute(
+            		ExecutionContext.HTTP_CONNECTION);
+            ensureNotNull(conn);
+            SSLIOSession sslSession =
+                (SSLIOSession) session.getAttribute(SSL_SESSION);
+            ensureNotNull(sslSession);
 
-        this.handler.timeout(conn);
-        synchronized (sslSession) {
-            if (sslSession.isOutboundDone() && !sslSession.isInboundDone()) {
-                // The session failed to cleanly terminate
-                sslSession.shutdown();
+            this.handler.timeout(conn);
+            synchronized (sslSession) {
+                if (sslSession.isOutboundDone() && !sslSession.isInboundDone()) {
+                    // The session failed to cleanly terminate
+                    sslSession.shutdown();
+                }
             }
-        }
+    	} catch (RuntimeException ex) {
+    		session.shutdown();
+    		throw ex;
+    	}
     }
 
 }