hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject [14/35] httpcomponents-core git commit: fix possible NPE if the connection is closed - and the address is null
Date Tue, 09 May 2017 20:03:43 GMT
fix possible NPE if the connection is closed - and the address is null


git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpcore/branches/4.1.x@1144469
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/34efab70
Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/tree/34efab70
Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/diff/34efab70

Branch: refs/heads/4.1.x
Commit: 34efab70d8b33e2ea81d08785de01b08a1186db3
Parents: 1caa7be
Author: Asankha Chamath Perera <asankha@apache.org>
Authored: Fri Jul 8 20:00:42 2011 +0000
Committer: Asankha Chamath Perera <asankha@apache.org>
Committed: Fri Jul 8 20:00:42 2011 +0000

----------------------------------------------------------------------
 .../impl/nio/DefaultNHttpClientConnection.java  | 20 +++++++---
 .../impl/nio/DefaultNHttpServerConnection.java  | 20 +++++++---
 .../http/impl/nio/reactor/IOSessionImpl.java    | 41 +++++++++++---------
 3 files changed, 51 insertions(+), 30 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/34efab70/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpClientConnection.java
----------------------------------------------------------------------
diff --git a/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpClientConnection.java
b/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpClientConnection.java
index bcca530..78bd87f 100644
--- a/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpClientConnection.java
+++ b/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpClientConnection.java
@@ -29,6 +29,7 @@ package org.apache.http.impl.nio;
 
 import java.io.IOException;
 import java.net.InetSocketAddress;
+import java.net.SocketAddress;
 
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpEntityEnclosingRequest;
@@ -258,11 +259,18 @@ public class DefaultNHttpClientConnection
     public String toString() {
         StringBuilder buf = new StringBuilder();
 
-        if (this.session.getRemoteAddress() instanceof InetSocketAddress &&
-            this.session.getLocalAddress()  instanceof InetSocketAddress) {
+        final SocketAddress remoteAddress = this.session.getRemoteAddress();
+        final SocketAddress localAddress = this.session.getLocalAddress();
 
-            final InetSocketAddress remote = ((InetSocketAddress) this.session.getRemoteAddress());
-            final InetSocketAddress local  = ((InetSocketAddress) this.session.getLocalAddress());
+        if (remoteAddress == null || localAddress == null) {
+            return "[CLOSED]";
+        }
+
+        if (remoteAddress instanceof InetSocketAddress &&
+            localAddress instanceof InetSocketAddress) {
+
+            final InetSocketAddress remote = ((InetSocketAddress) remoteAddress);
+            final InetSocketAddress local  = ((InetSocketAddress) localAddress);
 
             buf.append(local.getAddress() != null ? local.getAddress().getHostAddress() :
local.getAddress())
             .append(':')
@@ -273,9 +281,9 @@ public class DefaultNHttpClientConnection
             .append(remote.getPort());
 
         } else {
-            buf.append(this.session.getLocalAddress())
+            buf.append(localAddress)
             .append("->")
-            .append(this.session.getRemoteAddress());
+            .append(remoteAddress);
         }
 
         return buf.toString();

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/34efab70/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpServerConnection.java
----------------------------------------------------------------------
diff --git a/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpServerConnection.java
b/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpServerConnection.java
index c78ae24..7064b62 100644
--- a/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpServerConnection.java
+++ b/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpServerConnection.java
@@ -29,6 +29,7 @@ package org.apache.http.impl.nio;
 
 import java.io.IOException;
 import java.net.InetSocketAddress;
+import java.net.SocketAddress;
 
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpEntityEnclosingRequest;
@@ -260,11 +261,18 @@ public class DefaultNHttpServerConnection
     public String toString() {
         StringBuilder buf = new StringBuilder();
 
-        if (this.session.getRemoteAddress() instanceof InetSocketAddress &&
-            this.session.getLocalAddress()  instanceof InetSocketAddress) {
+        final SocketAddress remoteAddress = this.session.getRemoteAddress();
+        final SocketAddress localAddress  = this.session.getLocalAddress();
 
-            final InetSocketAddress remote = ((InetSocketAddress) this.session.getRemoteAddress());
-            final InetSocketAddress local  = ((InetSocketAddress) this.session.getLocalAddress());
+        if (remoteAddress == null || localAddress == null) {
+            return "[CLOSED]";
+        }
+
+        if (remoteAddress instanceof InetSocketAddress &&
+            localAddress instanceof InetSocketAddress) {
+
+            final InetSocketAddress remote = ((InetSocketAddress) remoteAddress);
+            final InetSocketAddress local  = ((InetSocketAddress) localAddress);
 
             buf.append(remote.getAddress() != null ? remote.getAddress().getHostAddress()
: remote.getAddress())
             .append(':')
@@ -275,9 +283,9 @@ public class DefaultNHttpServerConnection
             .append(local.getPort());
 
         } else {
-            buf.append(this.session.getRemoteAddress())
+            buf.append(remoteAddress)
             .append("->")
-            .append(this.session.getLocalAddress());
+            .append(localAddress);
         }
 
         return buf.toString();

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/34efab70/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/IOSessionImpl.java
----------------------------------------------------------------------
diff --git a/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/IOSessionImpl.java
b/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/IOSessionImpl.java
index 7e63cde..2c3f802 100644
--- a/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/IOSessionImpl.java
+++ b/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/IOSessionImpl.java
@@ -310,24 +310,29 @@ public class IOSessionImpl implements IOSession {
     public synchronized String toString() {
         StringBuilder buffer = new StringBuilder();
 
-        if (getRemoteAddress() instanceof InetSocketAddress &&
-            getLocalAddress()  instanceof InetSocketAddress) {
-
-            final InetSocketAddress remote = ((InetSocketAddress) getRemoteAddress());
-            final InetSocketAddress local  = ((InetSocketAddress) getLocalAddress());
-
-            buffer.append(local.getAddress() != null ? local.getAddress().getHostAddress()
: local.getAddress())
-            .append(':')
-            .append(local.getPort())
-            .append("<->")
-            .append(remote.getAddress() != null ? remote.getAddress().getHostAddress() :
remote.getAddress())
-            .append(':')
-            .append(remote.getPort());
-
-        } else {
-            buffer.append(getLocalAddress())
-            .append("<->")
-            .append(getRemoteAddress());
+        final SocketAddress remoteAddress = getRemoteAddress();
+        final SocketAddress localAddress = getLocalAddress();
+
+        if (remoteAddress != null && localAddress != null) {
+            if (remoteAddress instanceof InetSocketAddress &&
+                localAddress instanceof InetSocketAddress) {
+
+                final InetSocketAddress remote = ((InetSocketAddress) remoteAddress);
+                final InetSocketAddress local  = ((InetSocketAddress) localAddress);
+
+                buffer.append(local.getAddress() != null ? local.getAddress().getHostAddress()
: local.getAddress())
+                .append(':')
+                .append(local.getPort())
+                .append("<->")
+                .append(remote.getAddress() != null ? remote.getAddress().getHostAddress()
: remote.getAddress())
+                .append(':')
+                .append(remote.getPort());
+
+            } else {
+                buffer.append(localAddress)
+                .append("<->")
+                .append(remoteAddress);
+            }
         }
 
         buffer.append("[");


Mime
View raw message