changeset 1212:654393655e41

NC file - writing/reading HTTP2
author Devel 2
date Tue, 23 Jun 2020 11:10:59 +0200
parents 13f5f62bca82
children f2fec8fc04e2
files stress-tester/src/main/java/com/passus/st/Protocols.java stress-tester/src/main/java/com/passus/st/reader/nc/NcDataUtils.java stress-tester/src/main/java/com/passus/st/reader/nc/NcHttpPayloadReader.java stress-tester/src/main/java/com/passus/st/reader/nc/NcHttpPayloadWriter.java stress-tester/src/main/java/com/passus/st/source/NcEventDestination.java stress-tester/src/main/java/com/passus/st/source/NcEventSource.java stress-tester/src/main/java/com/passus/st/source/PcapHttp2Listener.java stress-tester/src/main/java/com/passus/st/source/PcapHttp2SessionAnalyzerHook.java stress-tester/src/main/java/com/passus/st/source/PcapHttpSessionAnalyzerHook.java stress-tester/src/main/java/com/passus/st/source/PcapSessionEventSource.java stress-tester/src/main/java/com/passus/st/utils/EventUtils.java stress-tester/src/test/java/com/passus/st/source/NcEventSourceTest.java stress-tester/src/test/java/com/passus/st/utils/Assert.java stress-tester/src/test/resources/pcap/http2/http2_h2c.pcap stress-tester/src/test/resources/pcap/http2/http2_h2c_prior.pcap
diffstat 15 files changed, 135 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/Protocols.java	Tue Jun 23 11:07:47 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/Protocols.java	Tue Jun 23 11:10:59 2020 +0200
@@ -8,22 +8,25 @@
 
     public static final int UNKNOWN = 0;
     public static final int HTTP = 1;
-    public static final int DNS = 2;
-    public static final int NETFLOW = 3;
-    public static final int PGSQL = 4;
-    public static final int MYSQL = 5;
+    public static final int HTTP2 = 2;
+    public static final int DNS = 3;
+    public static final int NETFLOW = 4;
+    public static final int PGSQL = 5;
+    public static final int MYSQL = 6;
 
     private Protocols() {
     }
 
     public static Set<Integer> getAllProtocols() {
-        return ArrayUtils.asSet(HTTP, DNS, NETFLOW, PGSQL, MYSQL);
+        return ArrayUtils.asSet(HTTP, HTTP2, DNS, NETFLOW, PGSQL, MYSQL);
     }
 
     public static String protocolToString(int protocolId) {
         switch (protocolId) {
             case HTTP:
                 return "HTTP";
+            case HTTP2:
+                return "HTTP2";
             case DNS:
                 return "DNS";
             case NETFLOW:
--- a/stress-tester/src/main/java/com/passus/st/reader/nc/NcDataUtils.java	Tue Jun 23 11:07:47 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/reader/nc/NcDataUtils.java	Tue Jun 23 11:10:59 2020 +0200
@@ -24,6 +24,8 @@
 
     public static final byte VERSION_1_1 = 2;
 
+    public static final byte VERSION_2 = 3;
+
     public static final int CUSTOM_HEADER_CODE = 0;
 
     private static final Map<Byte, ByteString> CODE_TO_HEADER;
--- a/stress-tester/src/main/java/com/passus/st/reader/nc/NcHttpPayloadReader.java	Tue Jun 23 11:07:47 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/reader/nc/NcHttpPayloadReader.java	Tue Jun 23 11:10:59 2020 +0200
@@ -31,6 +31,8 @@
             return HttpConsts.VERSION_1_0;
         } else if (b == NcDataUtils.VERSION_1_1) {
             return HttpConsts.VERSION_1_1;
+        } else if (b == NcDataUtils.VERSION_2) {
+            return HttpConsts.VERSION_2;
         }
 
         throw new IOException("Not supported HTTP version '" + b + "'.");
--- a/stress-tester/src/main/java/com/passus/st/reader/nc/NcHttpPayloadWriter.java	Tue Jun 23 11:07:47 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/reader/nc/NcHttpPayloadWriter.java	Tue Jun 23 11:10:59 2020 +0200
@@ -97,6 +97,8 @@
             buff.append(NcDataUtils.VERSION_1_0);
         } else if (HttpConsts.VERSION_1_1 == msg.getVersion()) {
             buff.append(NcDataUtils.VERSION_1_1);
+        } else if (HttpConsts.VERSION_2 == msg.getVersion()) {
+            buff.append(NcDataUtils.VERSION_2);
         } else {
             throw new IOException("Not supported HTTP version '" + msg.getVersion() + "'.");
         }
--- a/stress-tester/src/main/java/com/passus/st/source/NcEventDestination.java	Tue Jun 23 11:07:47 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/source/NcEventDestination.java	Tue Jun 23 11:10:59 2020 +0200
@@ -142,6 +142,7 @@
     private void write(SessionPayloadEvent event) throws IOException {
         switch (event.getProtocolId()) {
             case HTTP:
+            case HTTP2:
                 httpWriter.write(event, writer);
                 break;
             case DNS:
--- a/stress-tester/src/main/java/com/passus/st/source/NcEventSource.java	Tue Jun 23 11:07:47 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/source/NcEventSource.java	Tue Jun 23 11:10:59 2020 +0200
@@ -287,6 +287,7 @@
                 ReqRespPair messages;
                 switch (payloadBlock.proto()) {
                     case HTTP:
+                    case HTTP2:
                         messages = httpReader.read(payload);
                         break;
                     case DNS:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/source/PcapHttp2Listener.java	Tue Jun 23 11:10:59 2020 +0200
@@ -0,0 +1,55 @@
+package com.passus.st.source;
+
+import com.passus.net.http.HttpMessage;
+import com.passus.net.http.HttpRequest;
+import com.passus.net.session.SessionContext;
+import com.passus.st.client.EventHandler;
+import com.passus.st.emitter.SessionInfo;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import static com.passus.st.Protocols.HTTP2;
+
+public class PcapHttp2Listener extends BaseSessionAnalyzerListener<HttpMessage> {
+
+    private final Map<SessionInfo, HttpRequest> lastRequests;
+
+    public PcapHttp2Listener(String sourceName, EventHandler eventHandler,
+                             boolean collectMetric, PcapSessionEventSourceMetric metric, int maxSessionNum) {
+        super(sourceName, eventHandler, collectMetric, metric, HTTP2);
+        lastRequests = new HashMap<>(maxSessionNum);
+    }
+
+    @Override
+    public void onMessageReceived(SessionContext context, HttpMessage msg, long timestamp) {
+        SessionInfo session = SessionInfo.create(context, protocolId);
+        if (msg.isRequest()) {
+            HttpRequest lastRequest = lastRequests.put(session, (HttpRequest) msg);
+            if (lastRequest != null) {
+                firePayloadEvent(lastRequest, null, session, timestamp);
+            }
+        } else {
+            HttpRequest req = lastRequests.remove(session);
+            firePayloadEvent(req, msg, session, timestamp);
+        }
+    }
+
+    @Override
+    protected void onSessionClosed(SessionContext context, int status, long timestamp) {
+        SessionInfo session = SessionInfo.create(context, protocolId);
+        HttpRequest req = lastRequests.remove(session);
+        if (req != null) {
+            firePayloadEvent(req, null, session, timestamp);
+        }
+    }
+
+    public void onLoopEnd(long timestamp) {
+        if (!lastRequests.isEmpty()) {
+            lastRequests.forEach((session, req) -> {
+                firePayloadEvent(req, null, session, timestamp);
+            });
+            lastRequests.clear();
+        }
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/source/PcapHttp2SessionAnalyzerHook.java	Tue Jun 23 11:10:59 2020 +0200
@@ -0,0 +1,49 @@
+package com.passus.st.source;
+
+import com.passus.net.http2.session.Http2SessionAnalyzer;
+import com.passus.net.session.SessionAnalyzer;
+import com.passus.net.session.TcpSessionProcessor;
+
+public class PcapHttp2SessionAnalyzerHook extends PcapSessionAnalyzerHook {
+
+    @Override
+    public boolean supports(Class<? extends SessionAnalyzer> clazz) {
+        return Http2SessionAnalyzer.class.isAssignableFrom(clazz);
+    }
+
+    @Override
+    protected void doAttach(SessionAnalyzer analyzer, PcapSessionAnalyzerHookContext context) {
+        TcpSessionProcessor tcpProc = context.getTcpProcessor();
+        PcapHttp2Listener listener = new PcapHttp2Listener(context.getSourceName(),
+                context.getEventHandler(),
+                context.isCollectMetric(),
+                context.getMetric(),
+                context.getTcpProcessor().getMaxSessionNumber());
+
+        analyzer.setListener(listener);
+        tcpProc.addAnalyzer(analyzer);
+    }
+
+    @Override
+    protected void doDetach(SessionAnalyzer analyzer, PcapSessionAnalyzerHookContext context) {
+        TcpSessionProcessor tcpProc = context.getTcpProcessor();
+        analyzer.setListener(null);
+        tcpProc.removeAnalyzer(analyzer);
+    }
+
+    @Override
+    public void onLoopEnd(SessionAnalyzer analyzer, PcapSessionAnalyzerHookContext context) {
+        PcapHttp2Listener listener = (PcapHttp2Listener) analyzer.getListener();
+        if (listener != null) {
+            listener.onLoopEnd(System.currentTimeMillis());
+        }
+    }
+
+    @Override
+    public void onDataEnd(SessionAnalyzer analyzer, PcapSessionAnalyzerHookContext context) {
+        PcapHttp2Listener listener = (PcapHttp2Listener) analyzer.getListener();
+        if (listener != null) {
+            listener.onLoopEnd(System.currentTimeMillis());
+        }
+    }
+}
--- a/stress-tester/src/main/java/com/passus/st/source/PcapHttpSessionAnalyzerHook.java	Tue Jun 23 11:07:47 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/source/PcapHttpSessionAnalyzerHook.java	Tue Jun 23 11:10:59 2020 +0200
@@ -1,7 +1,6 @@
 package com.passus.st.source;
 
 import com.passus.net.http.session.HttpSessionAnalyzer;
-import com.passus.net.http2.session.Http2SessionAnalyzer;
 import com.passus.net.session.SessionAnalyzer;
 import com.passus.net.session.TcpSessionProcessor;
 
@@ -9,8 +8,7 @@
 
     @Override
     public boolean supports(Class<? extends SessionAnalyzer> clazz) {
-        return HttpSessionAnalyzer.class.isAssignableFrom(clazz)
-                || Http2SessionAnalyzer.class.isAssignableFrom(clazz);
+        return HttpSessionAnalyzer.class.isAssignableFrom(clazz);
     }
 
     @Override
--- a/stress-tester/src/main/java/com/passus/st/source/PcapSessionEventSource.java	Tue Jun 23 11:07:47 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/source/PcapSessionEventSource.java	Tue Jun 23 11:10:59 2020 +0200
@@ -88,6 +88,7 @@
         this.name = UniqueIdGenerator.generate();
 
         hooks.add(new PcapHttpSessionAnalyzerHook());
+        hooks.add(new PcapHttp2SessionAnalyzerHook());
         hooks.add(new PcapDnsSessionAnalyzerHook());
         hooks.add(new PcapNetflowSessionAnalyzerHook());
         hooks.add(new PcapPgSqlSessionAnalyzerHook());
--- a/stress-tester/src/main/java/com/passus/st/utils/EventUtils.java	Tue Jun 23 11:07:47 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/utils/EventUtils.java	Tue Jun 23 11:10:59 2020 +0200
@@ -5,6 +5,7 @@
 import com.passus.net.PortRangeSet;
 import com.passus.net.dns.session.DnsUdpSessionAnalyzer;
 import com.passus.net.http.session.HttpSessionAnalyzer;
+import com.passus.net.http2.session.Http2SessionAnalyzer;
 import com.passus.net.mysql.MySqlTcpSessionAnalyzer;
 import com.passus.net.netflow.NetflowUdpSessionAnalyzer;
 import com.passus.net.pgsql.PgSqlTcpSessionAnalyzer;
@@ -36,6 +37,7 @@
         return readEvents(pcapFile, props);
     }
 
+    @SuppressWarnings("unchecked")
     public static List<Event> readEvents(File pcapFile, Map<String, Object> props) {
         try {
             boolean allowPartialSession = false;
@@ -65,6 +67,12 @@
                 src.addAnalyzer(analyzer);
             }
 
+            if (protocols.contains(Protocols.HTTP2)) {
+                SessionAnalyzer analyzer = new Http2SessionAnalyzer();
+                analyzer.getPortsRange().add(8090);
+                src.addAnalyzer(analyzer);
+            }
+
             if (protocols.contains(Protocols.DNS)) {
                 SessionAnalyzer analyzer = new DnsUdpSessionAnalyzer();
                 analyzer.getPortsRange().add(53);
--- a/stress-tester/src/test/java/com/passus/st/source/NcEventSourceTest.java	Tue Jun 23 11:07:47 2020 +0200
+++ b/stress-tester/src/test/java/com/passus/st/source/NcEventSourceTest.java	Tue Jun 23 11:10:59 2020 +0200
@@ -2,6 +2,7 @@
 
 import com.passus.data.PooledByteBuffAllocator;
 import com.passus.net.PortRangeSet;
+import com.passus.st.Log4jConfigurationFactory;
 import com.passus.st.Protocols;
 import com.passus.st.client.ArrayListEventHandler;
 import com.passus.st.client.Event;
@@ -50,6 +51,7 @@
                 {"pcap/http/http_ndiag_tcp_conn.pcap", HTTP},
                 {"pcap/http/basic_digest.pcap", HTTP},
                 {"pcap/http/http_get_png.pcap", HTTP},
+                {"pcap/http2/http2_h2c_prior.pcap", HTTP2},
                 {"pcap/http/http_1.pcap", HTTP},
                 {"pcap/dns/dns_A_req_resp.pcap", DNS},
                 {"pcap/netflow/netflow_v5.pcap", NETFLOW},
--- a/stress-tester/src/test/java/com/passus/st/utils/Assert.java	Tue Jun 23 11:07:47 2020 +0200
+++ b/stress-tester/src/test/java/com/passus/st/utils/Assert.java	Tue Jun 23 11:10:59 2020 +0200
@@ -17,6 +17,7 @@
 import java.util.stream.Collectors;
 
 import static com.passus.st.Protocols.HTTP;
+import static com.passus.st.Protocols.HTTP2;
 import static com.passus.st.utils.HttpMessageAssert.assertMessages;
 import static com.passus.st.utils.HttpMessageAssert.assertMessagesContent;
 
@@ -71,10 +72,10 @@
                 SessionPayloadEvent expectedPayloadEvent = (SessionPayloadEvent) expectedEvent;
                 SessionPayloadEvent payloadEvent = (SessionPayloadEvent) event;
 
-                int protocolId = ((SessionPayloadEvent) event).getProtocolId();
+                int protocolId = payloadEvent.getProtocolId();
                 assertEquals(expectedProtocolId, protocolId);
 
-                if (((SessionPayloadEvent) event).getProtocolId() == HTTP) {
+                if (payloadEvent.getProtocolId() == HTTP || payloadEvent.getProtocolId() == HTTP2) {
                     assertMessages(expectedPayloadEvent.getRequest(), payloadEvent.getRequest());
                     assertMessagesContent(expectedPayloadEvent.getRequest(), payloadEvent.getRequest());
                     assertMessages(expectedPayloadEvent.getResponse(), payloadEvent.getResponse());
Binary file stress-tester/src/test/resources/pcap/http2/http2_h2c.pcap has changed
Binary file stress-tester/src/test/resources/pcap/http2/http2_h2c_prior.pcap has changed