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/09/28 20:39:11 UTC

httpcomponents-core git commit: Fixed header logging in non-blocking client and server protocol handlers

Repository: httpcomponents-core
Updated Branches:
  refs/heads/master 0451d7e89 -> d8b464eaf


Fixed header logging in non-blocking client and server protocol handlers


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

Branch: refs/heads/master
Commit: d8b464eafb8ddc78050dbc80ed87d09bdd278fbc
Parents: 0451d7e
Author: Oleg Kalnichevski <ol...@apache.org>
Authored: Thu Sep 28 22:31:07 2017 +0200
Committer: Oleg Kalnichevski <ol...@apache.org>
Committed: Thu Sep 28 22:31:07 2017 +0200

----------------------------------------------------------------------
 .../InternalClientHttp1EventHandlerFactory.java |  3 +-
 .../InternalClientHttp2EventHandlerFactory.java |  3 +-
 .../InternalServerHttp1EventHandlerFactory.java |  3 +-
 .../InternalServerHttp2EventHandlerFactory.java |  3 +-
 .../testing/nio/LoggingHttp1StreamListener.java | 97 ++++++++++++++++++++
 .../nio/Http1ServerAndRequesterTest.java        |  5 +-
 .../nio/Http2ServerAndRequesterTest.java        |  5 +-
 7 files changed, 105 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/d8b464ea/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/nio/InternalClientHttp1EventHandlerFactory.java
----------------------------------------------------------------------
diff --git a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/nio/InternalClientHttp1EventHandlerFactory.java b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/nio/InternalClientHttp1EventHandlerFactory.java
index 36b45ff..1735ee9 100644
--- a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/nio/InternalClientHttp1EventHandlerFactory.java
+++ b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/nio/InternalClientHttp1EventHandlerFactory.java
@@ -52,7 +52,6 @@ import org.apache.hc.core5.http.protocol.HttpProcessor;
 import org.apache.hc.core5.reactor.IOEventHandler;
 import org.apache.hc.core5.reactor.IOEventHandlerFactory;
 import org.apache.hc.core5.reactor.TlsCapableIOSession;
-import org.apache.hc.core5.testing.classic.LoggingHttp1StreamListener;
 import org.apache.hc.core5.util.Args;
 
 /**
@@ -116,7 +115,7 @@ class InternalClientHttp1EventHandlerFactory implements IOEventHandlerFactory {
                 requestWriterFactory.create(),
                 DefaultContentLengthStrategy.INSTANCE,
                 DefaultContentLengthStrategy.INSTANCE,
-                LoggingHttp1StreamListener.INSTANCE);
+                LoggingHttp1StreamListener.INSTANCE_CLIENT);
         return new ClientHttp1IOEventHandler(streamDuplexer);
     }
 

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/d8b464ea/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/nio/InternalClientHttp2EventHandlerFactory.java
----------------------------------------------------------------------
diff --git a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/nio/InternalClientHttp2EventHandlerFactory.java b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/nio/InternalClientHttp2EventHandlerFactory.java
index c19387a..567a85c 100644
--- a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/nio/InternalClientHttp2EventHandlerFactory.java
+++ b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/nio/InternalClientHttp2EventHandlerFactory.java
@@ -44,7 +44,6 @@ import org.apache.hc.core5.http2.impl.nio.ClientHttpProtocolNegotiator;
 import org.apache.hc.core5.reactor.IOEventHandler;
 import org.apache.hc.core5.reactor.IOEventHandlerFactory;
 import org.apache.hc.core5.reactor.TlsCapableIOSession;
-import org.apache.hc.core5.testing.classic.LoggingHttp1StreamListener;
 import org.apache.hc.core5.util.Args;
 
 class InternalClientHttp2EventHandlerFactory implements IOEventHandlerFactory {
@@ -83,7 +82,7 @@ class InternalClientHttp2EventHandlerFactory implements IOEventHandlerFactory {
                 httpProcessor != null ? httpProcessor : HttpProcessors.client(),
                 h1Config,
                 charCodingConfig,
-                LoggingHttp1StreamListener.INSTANCE);
+                LoggingHttp1StreamListener.INSTANCE_CLIENT);
         final ClientHttp2StreamMultiplexerFactory http2StreamHandlerFactory = new ClientHttp2StreamMultiplexerFactory(
                 httpProcessor != null ? httpProcessor : Http2Processors.client(),
                 exchangeHandlerFactory,

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/d8b464ea/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/nio/InternalServerHttp1EventHandlerFactory.java
----------------------------------------------------------------------
diff --git a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/nio/InternalServerHttp1EventHandlerFactory.java b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/nio/InternalServerHttp1EventHandlerFactory.java
index 7273231..50e8b29 100644
--- a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/nio/InternalServerHttp1EventHandlerFactory.java
+++ b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/nio/InternalServerHttp1EventHandlerFactory.java
@@ -55,7 +55,6 @@ import org.apache.hc.core5.http.protocol.HttpProcessor;
 import org.apache.hc.core5.reactor.IOEventHandler;
 import org.apache.hc.core5.reactor.IOEventHandlerFactory;
 import org.apache.hc.core5.reactor.TlsCapableIOSession;
-import org.apache.hc.core5.testing.classic.LoggingHttp1StreamListener;
 import org.apache.hc.core5.util.Args;
 
 /**
@@ -125,7 +124,7 @@ class InternalServerHttp1EventHandlerFactory implements IOEventHandlerFactory {
                 responseWriterFactory.create(),
                 DefaultContentLengthStrategy.INSTANCE,
                 DefaultContentLengthStrategy.INSTANCE,
-                LoggingHttp1StreamListener.INSTANCE);
+                LoggingHttp1StreamListener.INSTANCE_SERVER);
         return new ServerHttp1IOEventHandler(streamDuplexer);
     }
 

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/d8b464ea/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/nio/InternalServerHttp2EventHandlerFactory.java
----------------------------------------------------------------------
diff --git a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/nio/InternalServerHttp2EventHandlerFactory.java b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/nio/InternalServerHttp2EventHandlerFactory.java
index 1969912..548f1da 100644
--- a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/nio/InternalServerHttp2EventHandlerFactory.java
+++ b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/nio/InternalServerHttp2EventHandlerFactory.java
@@ -44,7 +44,6 @@ import org.apache.hc.core5.http2.impl.nio.ServerHttpProtocolNegotiator;
 import org.apache.hc.core5.reactor.IOEventHandler;
 import org.apache.hc.core5.reactor.IOEventHandlerFactory;
 import org.apache.hc.core5.reactor.TlsCapableIOSession;
-import org.apache.hc.core5.testing.classic.LoggingHttp1StreamListener;
 import org.apache.hc.core5.util.Args;
 
 class InternalServerHttp2EventHandlerFactory implements IOEventHandlerFactory {
@@ -84,7 +83,7 @@ class InternalServerHttp2EventHandlerFactory implements IOEventHandlerFactory {
                 exchangeHandlerFactory,
                 h1Config,
                 charCodingConfig,
-                LoggingHttp1StreamListener.INSTANCE);
+                LoggingHttp1StreamListener.INSTANCE_SERVER);
         final ServerHttp2StreamMultiplexerFactory http2StreamHandlerFactory = new ServerHttp2StreamMultiplexerFactory(
                 httpProcessor != null ? httpProcessor : Http2Processors.server(),
                 exchangeHandlerFactory,

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/d8b464ea/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/nio/LoggingHttp1StreamListener.java
----------------------------------------------------------------------
diff --git a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/nio/LoggingHttp1StreamListener.java b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/nio/LoggingHttp1StreamListener.java
new file mode 100644
index 0000000..7b05720
--- /dev/null
+++ b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/nio/LoggingHttp1StreamListener.java
@@ -0,0 +1,97 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.hc.core5.testing.nio;
+
+import java.util.Iterator;
+
+import org.apache.hc.core5.http.Header;
+import org.apache.hc.core5.http.HttpConnection;
+import org.apache.hc.core5.http.HttpRequest;
+import org.apache.hc.core5.http.HttpResponse;
+import org.apache.hc.core5.http.impl.Http1StreamListener;
+import org.apache.hc.core5.http.message.RequestLine;
+import org.apache.hc.core5.http.message.StatusLine;
+import org.apache.hc.core5.testing.classic.LoggingSupport;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+public class LoggingHttp1StreamListener implements Http1StreamListener {
+
+    enum Type { CLIENT, SERVER }
+
+    public final static LoggingHttp1StreamListener INSTANCE_CLIENT = new LoggingHttp1StreamListener(Type.CLIENT);
+    public final static LoggingHttp1StreamListener INSTANCE_SERVER = new LoggingHttp1StreamListener(Type.SERVER);
+
+    private final Type type;
+    private final Logger connLog = LogManager.getLogger("org.apache.hc.core5.http.connection");
+    private final Logger headerLog = LogManager.getLogger("org.apache.hc.core5.http.headers");
+
+    private LoggingHttp1StreamListener(final Type type) {
+        this.type = type;
+    }
+
+    private String requestDirection() {
+        return type == Type.CLIENT ? " >> " : " << ";
+    }
+
+    private String responseDirection() {
+        return type == Type.CLIENT ? " << " : " >> ";
+    }
+
+    @Override
+    public void onRequestHead(final HttpConnection connection, final HttpRequest request) {
+        if (headerLog.isDebugEnabled()) {
+            headerLog.debug(LoggingSupport.getId(connection) + requestDirection() + new RequestLine(request));
+            for (final Iterator<Header> it = request.headerIterator(); it.hasNext(); ) {
+                headerLog.debug(LoggingSupport.getId(connection) + requestDirection() + it.next());
+            }
+        }
+    }
+
+    @Override
+    public void onResponseHead(final HttpConnection connection, final HttpResponse response) {
+        if (headerLog.isDebugEnabled()) {
+            headerLog.debug(LoggingSupport.getId(connection) + responseDirection() + new StatusLine(response));
+            for (final Iterator<Header> it = response.headerIterator(); it.hasNext(); ) {
+                headerLog.debug(LoggingSupport.getId(connection) + responseDirection() + it.next());
+            }
+        }
+    }
+
+    @Override
+    public void onExchangeComplete(final HttpConnection connection, final boolean keepAlive) {
+        if (connLog.isDebugEnabled()) {
+            if (keepAlive) {
+                connLog.debug(LoggingSupport.getId(connection) + " Connection is kept alive");
+            } else {
+                connLog.debug(LoggingSupport.getId(connection) + " Connection is not kept alive");
+            }
+        }
+    }
+
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/d8b464ea/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1ServerAndRequesterTest.java
----------------------------------------------------------------------
diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1ServerAndRequesterTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1ServerAndRequesterTest.java
index 39893d5..b258f91 100644
--- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1ServerAndRequesterTest.java
+++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1ServerAndRequesterTest.java
@@ -67,7 +67,6 @@ import org.apache.hc.core5.reactor.ExceptionEvent;
 import org.apache.hc.core5.reactor.IOReactorConfig;
 import org.apache.hc.core5.reactor.ListenerEndpoint;
 import org.apache.hc.core5.testing.classic.LoggingConnPoolListener;
-import org.apache.hc.core5.testing.classic.LoggingHttp1StreamListener;
 import org.apache.hc.core5.util.Timeout;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
@@ -142,7 +141,7 @@ public class Http1ServerAndRequesterTest {
                         }
                     })
                     .setIOSessionListener(LoggingIOSessionListener.INSTANCE)
-                    .setStreamListener(LoggingHttp1StreamListener.INSTANCE)
+                    .setStreamListener(LoggingHttp1StreamListener.INSTANCE_SERVER)
                     .setIOSessionDecorator(LoggingIOSessionDecorator.INSTANCE)
                     .create();
         }
@@ -181,7 +180,7 @@ public class Http1ServerAndRequesterTest {
                             .setSoTimeout(TIMEOUT)
                             .build())
                     .setIOSessionListener(LoggingIOSessionListener.INSTANCE)
-                    .setStreamListener(LoggingHttp1StreamListener.INSTANCE)
+                    .setStreamListener(LoggingHttp1StreamListener.INSTANCE_CLIENT)
                     .setConnPoolListener(LoggingConnPoolListener.INSTANCE)
                     .setIOSessionDecorator(LoggingIOSessionDecorator.INSTANCE)
                     .create();

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/d8b464ea/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http2ServerAndRequesterTest.java
----------------------------------------------------------------------
diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http2ServerAndRequesterTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http2ServerAndRequesterTest.java
index aeab0fa..d36037b 100644
--- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http2ServerAndRequesterTest.java
+++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http2ServerAndRequesterTest.java
@@ -57,7 +57,6 @@ import org.apache.hc.core5.reactor.ExceptionEvent;
 import org.apache.hc.core5.reactor.IOReactorConfig;
 import org.apache.hc.core5.reactor.ListenerEndpoint;
 import org.apache.hc.core5.testing.classic.LoggingConnPoolListener;
-import org.apache.hc.core5.testing.classic.LoggingHttp1StreamListener;
 import org.apache.hc.core5.util.Timeout;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
@@ -106,7 +105,7 @@ public class Http2ServerAndRequesterTest {
                                     .setSoTimeout(TIMEOUT)
                                     .build())
                     .setIOSessionListener(LoggingIOSessionListener.INSTANCE)
-                    .setStreamListener(LoggingHttp1StreamListener.INSTANCE)
+                    .setStreamListener(LoggingHttp1StreamListener.INSTANCE_SERVER)
                     .setIOSessionDecorator(LoggingIOSessionDecorator.INSTANCE)
                     .register("*", new Supplier<AsyncServerExchangeHandler>() {
 
@@ -154,7 +153,7 @@ public class Http2ServerAndRequesterTest {
                             .setSoTimeout(TIMEOUT)
                             .build())
                     .setIOSessionListener(LoggingIOSessionListener.INSTANCE)
-                    .setStreamListener(LoggingHttp1StreamListener.INSTANCE)
+                    .setStreamListener(LoggingHttp1StreamListener.INSTANCE_CLIENT)
                     .setConnPoolListener(LoggingConnPoolListener.INSTANCE)
                     .setIOSessionDecorator(LoggingIOSessionDecorator.INSTANCE)
                     .create();