From 05a065b411174bc111748be8f64642b28cbc888a Mon Sep 17 00:00:00 2001 From: Doug Hilpipre Date: Wed, 18 Dec 2024 14:52:37 -0600 Subject: [PATCH 1/2] added checks for GRPC because it expects ASCIIString --- .../netty4116/Http2RequestHeaderWrapper.java | 38 +++++++++++++++---- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/instrumentation/netty-4.1.16/src/main/java/com/agent/instrumentation/netty4116/Http2RequestHeaderWrapper.java b/instrumentation/netty-4.1.16/src/main/java/com/agent/instrumentation/netty4116/Http2RequestHeaderWrapper.java index 8bcbefcd35..11c5678549 100644 --- a/instrumentation/netty-4.1.16/src/main/java/com/agent/instrumentation/netty4116/Http2RequestHeaderWrapper.java +++ b/instrumentation/netty-4.1.16/src/main/java/com/agent/instrumentation/netty4116/Http2RequestHeaderWrapper.java @@ -15,6 +15,7 @@ import io.netty.handler.codec.http.cookie.Cookie; import io.netty.handler.codec.http.cookie.ServerCookieDecoder; import io.netty.handler.codec.http2.Http2Headers; +import io.netty.util.AsciiString; import java.util.ArrayList; import java.util.Collections; @@ -28,12 +29,14 @@ public class Http2RequestHeaderWrapper extends ExtendedRequest { private static final Pattern URL_REPLACEMENT_PATTERN = Pattern.compile("(?i)%(?![\\da-f]{2})"); + private static final String GRPCHEADERSUTIL = "io.grpc.netty.GrpcHttp2HeadersUtils.GrpcHttp2RequestHeaders"; private final Set cookies; private final Map> parameters; private final Http2Headers http2Headers; private final CharSequence method; private final CharSequence path; private final CharSequence authority; + private final boolean isGRPCHttp2; public Http2RequestHeaderWrapper(Http2Headers http2Headers) { super(); @@ -43,6 +46,7 @@ public Http2RequestHeaderWrapper(Http2Headers http2Headers) { this.authority = getAuthorityHeader(); this.cookies = getCookies(); this.parameters = getParameters(); + this.isGRPCHttp2 = http2Headers.getClass().getName().equals(GRPCHEADERSUTIL); } private Map> getParameters() { @@ -124,6 +128,13 @@ public String getRequestURI() { @Override public String getHeader(String name) { try { + + if(isGRPCHttp2) { + // GRPC HTTP2 expects an AsciiString + AsciiString asciiString = new AsciiString(name); + return http2Headers.get(asciiString).toString(); + + } // HTTP/2 only supports lowercase headers String lowerCaseHeaderName = name.toLowerCase(); if (lowerCaseHeaderName.equals(HttpHeaderNames.HOST.toString())) { @@ -134,7 +145,10 @@ public String getHeader(String name) { return http2Headers.get(lowerCaseHeaderName).toString(); } } catch (Exception e) { - AgentBridge.getAgent().getLogger().log(Level.FINER, e, "Unable to get Http2Headers header: {0}", e.getMessage()); + String errorMsg = e.getMessage(); + if(!errorMsg.equals("AsciiString expected. Was: java.lang.String")) { + AgentBridge.getAgent().getLogger().log(Level.FINER, e, "Unable to get Http2Headers header: {0}", e.getMessage()); + } } return null; } @@ -205,12 +219,22 @@ public String getHost() { public List getHeaders(String name) { List headers = new ArrayList<>(); try { - // HTTP/2 only supports lowercase headers - String lowerCaseHeaderName = name.toLowerCase(); - List allHeaders = http2Headers.getAll(lowerCaseHeaderName); - for (CharSequence header : allHeaders) { - headers.add(header.toString()); - } + if (!isGRPCHttp2) { + // HTTP/2 only supports lowercase headers + String lowerCaseHeaderName = name.toLowerCase(); + List allHeaders = http2Headers.getAll(lowerCaseHeaderName); + for (CharSequence header : allHeaders) { + headers.add(header.toString()); + } + } else { + // GRPC only accepts AsciiString + AsciiString asciiName = new AsciiString(name); + List allHeaders = http2Headers.getAll(asciiName); + for (CharSequence header : allHeaders) { + headers.add(header.toString()); + } + + } } catch (Exception e) { AgentBridge.getAgent().getLogger().log(Level.FINER, e, "Unable to get Http2Headers headers: {0}", e.getMessage()); } From 3cd9554e59a6575522a862178f78e6ef022a5a17 Mon Sep 17 00:00:00 2001 From: Doug Hilpipre Date: Fri, 20 Dec 2024 11:05:14 -0600 Subject: [PATCH 2/2] changes requested by agent team --- .../netty4116/Http2RequestHeaderWrapper.java | 53 +++++++++---------- 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/instrumentation/netty-4.1.16/src/main/java/com/agent/instrumentation/netty4116/Http2RequestHeaderWrapper.java b/instrumentation/netty-4.1.16/src/main/java/com/agent/instrumentation/netty4116/Http2RequestHeaderWrapper.java index 11c5678549..abb7614b57 100644 --- a/instrumentation/netty-4.1.16/src/main/java/com/agent/instrumentation/netty4116/Http2RequestHeaderWrapper.java +++ b/instrumentation/netty-4.1.16/src/main/java/com/agent/instrumentation/netty4116/Http2RequestHeaderWrapper.java @@ -29,7 +29,7 @@ public class Http2RequestHeaderWrapper extends ExtendedRequest { private static final Pattern URL_REPLACEMENT_PATTERN = Pattern.compile("(?i)%(?![\\da-f]{2})"); - private static final String GRPCHEADERSUTIL = "io.grpc.netty.GrpcHttp2HeadersUtils.GrpcHttp2RequestHeaders"; + private static final String GRPC_HEADER_UTIL_CLASS = "io.grpc.netty.GrpcHttp2HeadersUtils.GrpcHttp2RequestHeaders"; private final Set cookies; private final Map> parameters; private final Http2Headers http2Headers; @@ -46,7 +46,7 @@ public Http2RequestHeaderWrapper(Http2Headers http2Headers) { this.authority = getAuthorityHeader(); this.cookies = getCookies(); this.parameters = getParameters(); - this.isGRPCHttp2 = http2Headers.getClass().getName().equals(GRPCHEADERSUTIL); + this.isGRPCHttp2 = http2Headers.getClass().getName().equals(GRPC_HEADER_UTIL_CLASS); } private Map> getParameters() { @@ -128,13 +128,13 @@ public String getRequestURI() { @Override public String getHeader(String name) { try { - - if(isGRPCHttp2) { - // GRPC HTTP2 expects an AsciiString - AsciiString asciiString = new AsciiString(name); - return http2Headers.get(asciiString).toString(); - - } + + if(isGRPCHttp2) { + // GRPC HTTP2 expects an AsciiString + AsciiString asciiString = new AsciiString(name); + return http2Headers.get(asciiString).toString(); + + } // HTTP/2 only supports lowercase headers String lowerCaseHeaderName = name.toLowerCase(); if (lowerCaseHeaderName.equals(HttpHeaderNames.HOST.toString())) { @@ -145,10 +145,10 @@ public String getHeader(String name) { return http2Headers.get(lowerCaseHeaderName).toString(); } } catch (Exception e) { - String errorMsg = e.getMessage(); - if(!errorMsg.equals("AsciiString expected. Was: java.lang.String")) { - AgentBridge.getAgent().getLogger().log(Level.FINER, e, "Unable to get Http2Headers header: {0}", e.getMessage()); - } + String errorMsg = e.getMessage(); + if(!errorMsg.equals("AsciiString expected. Was: java.lang.String")) { + AgentBridge.getAgent().getLogger().log(Level.FINER, e, "Unable to get Http2Headers header: {0}", e.getMessage()); + } } return null; } @@ -219,22 +219,19 @@ public String getHost() { public List getHeaders(String name) { List headers = new ArrayList<>(); try { + List allHeaders = Collections.emptyList(); if (!isGRPCHttp2) { - // HTTP/2 only supports lowercase headers - String lowerCaseHeaderName = name.toLowerCase(); - List allHeaders = http2Headers.getAll(lowerCaseHeaderName); - for (CharSequence header : allHeaders) { - headers.add(header.toString()); - } - } else { - // GRPC only accepts AsciiString - AsciiString asciiName = new AsciiString(name); - List allHeaders = http2Headers.getAll(asciiName); - for (CharSequence header : allHeaders) { - headers.add(header.toString()); - } - - } + // HTTP/2 only supports lowercase headers + String lowerCaseHeaderName = name.toLowerCase(); + allHeaders = http2Headers.getAll(lowerCaseHeaderName); + } else { + // GRPC only accepts AsciiString + AsciiString asciiName = new AsciiString(name); + allHeaders = http2Headers.getAll(asciiName); + } + for (CharSequence header : allHeaders) { + headers.add(header.toString()); + } } catch (Exception e) { AgentBridge.getAgent().getLogger().log(Level.FINER, e, "Unable to get Http2Headers headers: {0}", e.getMessage()); }