Skip to content

Commit

Permalink
grizzly-http-server: minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jam01 committed Jul 12, 2020
1 parent 4259948 commit 2861763
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 35 deletions.
8 changes: 4 additions & 4 deletions rule/grizzly-http-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<properties>
<sa.rule.name>grizzly:http-server</sa.rule.name>
<min.version>2.3.35</min.version>
<version.opentracing.grizzly>0.1.4-SNAPSHOT</version.opentracing.grizzly>
<version.opentracing.grizzly>0.2.0</version.opentracing.grizzly>
</properties>
<build>
<plugins>
Expand Down Expand Up @@ -86,9 +86,9 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>fluent-hc</artifactId>
<version>4.5.2</version>
<groupId>org.glassfish.grizzly</groupId>
<artifactId>grizzly-http-client</artifactId>
<version>1.15</version>
<optional>true</optional>
<scope>test</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,57 +1,59 @@
package io.opentracing.contrib.specialagent.rule.grizzly.http.server;

import io.opentracing.contrib.specialagent.Level;
import io.opentracing.contrib.specialagent.Logger;
import io.opentracing.Scope;
import io.opentracing.contrib.specialagent.AgentRule;
import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.implementation.bytecode.assign.Assigner;
import net.bytebuddy.matcher.StringMatcher;
import net.bytebuddy.utility.JavaModule;

import static net.bytebuddy.matcher.ElementMatchers.*;

public class HttpServerFilterRule extends AgentRule {
public static final Logger logger = Logger.getLogger(HttpServerFilterRule.class);
private static final String FILTER_CHAIN_CONTEXT = "org.glassfish.grizzly.filterchain.FilterChainContext";
private static final String HANDLE_READ = "handleRead";

@Override
public AgentBuilder buildAgentChainedGlobal1(final AgentBuilder builder) {
return builder
.type(named("org.glassfish.grizzly.http.HttpServerFilter"))
.transform(new AgentBuilder.Transformer() {
@Override
public DynamicType.Builder<?> transform(final DynamicType.Builder<?> builder, final TypeDescription typeDescription, final ClassLoader classLoader, final JavaModule module) {
return builder.visit(advice(typeDescription).to(HandleReadAdvice.class).on(named("handleRead")
.and(takesArgument(0, named("org.glassfish.grizzly.filterchain.FilterChainContext")))
.and(isPublic())));
return builder.visit(advice(typeDescription).to(HandleReadAdvice.class).on(named(HANDLE_READ)
.and(takesArgument(0, named(FILTER_CHAIN_CONTEXT)))));
}
})
.transform(new AgentBuilder.Transformer() {
@Override
public DynamicType.Builder<?> transform(final DynamicType.Builder<?> builder, final TypeDescription typeDescription, final ClassLoader classLoader, final JavaModule module) {
return builder.visit(advice(typeDescription).to(PrepareResponseAdvice.class).on(named("prepareResponse")
.and(takesArgument(0, named("org.glassfish.grizzly.filterchain.FilterChainContext")))
.and(takesArgument(1, named("org.glassfish.grizzly.http.HttpRequestPacket")))
.and(takesArgument(2, named("org.glassfish.grizzly.http.HttpResponsePacket")))
.and(takesArgument(3, named("org.glassfish.grizzly.http.HttpContent")))
.and(isPrivate())));
.and(takesArgument(0, named(FILTER_CHAIN_CONTEXT)))
.and(takesArgument(2, named("org.glassfish.grizzly.http.HttpResponsePacket")))));
}
})

.type(hasSuperType(named("org.glassfish.grizzly.filterchain.BaseFilter"))
// common grizzly filters
.and(not(named("org.glassfish.grizzly.filterchain.TransportFilter")))
.and(not(named("org.glassfish.grizzly.nio.transport.TCPNIOTransportFilter")))
// TODO: 7/11/20 Figure out why HttpServerFilter still matches
.and(not(named("org.glassfish.grizzly.http.HttpServerFilter")))
.and(not(named("org.glassfish.grizzly.utils.IdleTimeoutFilter")))
.and(not(named("org.glassfish.grizzly.http.server.FileCacheFilter")))
)
.type(hasSuperClass(named("org.glassfish.grizzly.filterchain.BaseFilter"))
// common http server filters
.and(not(named("org.glassfish.grizzly.filterchain.TransportFilter")
.or(named("org.glassfish.grizzly.nio.transport.TCPNIOTransportFilter"))
.or(named("org.glassfish.grizzly.http.HttpServerFilter"))
.or(named("org.glassfish.grizzly.http.HttpCodecFilter"))
.or(named("org.glassfish.grizzly.utils.IdleTimeoutFilter"))
// common http client filters
.or(named("com.ning.http.client.providers.grizzly.AsyncHttpClientFilter"))
.or(named("org.glassfish.grizzly.websockets.WebSocketClientFilter"))
.or(hasSuperClass(named("org.glassfish.grizzly.http.HttpClientFilter"))))))
.transform(new AgentBuilder.Transformer() {
@Override
public DynamicType.Builder<?> transform(final DynamicType.Builder<?> builder, final TypeDescription typeDescription, final ClassLoader classLoader, final JavaModule module) {
return builder.visit(advice(typeDescription).to(WorkerHandleReadAdvice.class).on(named("handleRead")
.and(takesArgument(0, named("org.glassfish.grizzly.filterchain.FilterChainContext")))
.and(isPublic())));
return builder.visit(advice(typeDescription).to(WorkerHandleReadAdvice.class).on(named(HANDLE_READ)
.and(takesArgument(0, named(FILTER_CHAIN_CONTEXT)))));
}
});
}
Expand All @@ -77,11 +79,8 @@ public static void onEnter(
@Advice.Argument(value = 0, typing = Assigner.Typing.DYNAMIC) final Object ctx,
@Advice.Local("scope") Scope scope) {

// manually filtering HttpServerFilter
if (new StringMatcher("org.glassfish.grizzly.http.HttpServerFilter",
StringMatcher.Mode.EQUALS_FULLY).matches(thiz.getClass().getName())) {
if (hackShouldFilter(thiz))
return;
}

if (isAllowed(className, origin))
scope = WorkerFilterIntercept.onHandleReadEnter(ctx);
Expand All @@ -91,7 +90,12 @@ public static void onEnter(
public static void onExit(
final @ClassName String className,
final @Advice.Origin String origin,
final @Advice.This Object thiz,
@Advice.Local("scope") Scope scope) {

if (hackShouldFilter(thiz))
return;

if (isAllowed(className, origin))
WorkerFilterIntercept.onHandleReadExit(scope);
}
Expand All @@ -104,9 +108,17 @@ public static void onExit(
final @Advice.Origin String origin,
@Advice.Argument(value = 0, typing = Assigner.Typing.DYNAMIC) final Object ctx,
@Advice.Argument(value = 2, typing = Assigner.Typing.DYNAMIC) final Object response) {

if (isAllowed(className, origin))
HttpServerFilterIntercept.onPrepareResponse(ctx, response);
}

}

public static boolean hackShouldFilter(Object thiz) {
// TODO: 7/11/20 figure out why these are not filtered at TypeDescription
logger.log(Level.FINER, "Checking predicate for potential worker filter " + thiz.getClass().getName());
return "com.ning.http.client.providers.grizzly.AsyncHttpClientFilter".equals(thiz.getClass().getName()) ||
"org.glassfish.grizzly.websockets.WebSocketClientFilter".equals(thiz.getClass().getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@
import static org.junit.Assert.*;

import java.io.IOException;
import java.net.URL;
import java.util.List;

import com.ning.http.client.AsyncHttpClient;
import io.opentracing.contrib.grizzly.http.server.AbstractHttpTest;
import org.glassfish.grizzly.http.server.HttpHandler;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.grizzly.http.server.NetworkListener;
Expand All @@ -39,9 +42,7 @@
* @author Jose Montoya
*/
@RunWith(AgentRunner.class)
public class HttpServerTest {
protected static final int PORT = 18906;
protected static final String LOCALHOST = "localhost";
public class HttpServerTest extends AbstractHttpTest {
private HttpServer httpServer;

@Before
Expand Down Expand Up @@ -70,9 +71,11 @@ public void service(final Request request, final Response response) throws Excep
response.setStatus(201);
}
});

final org.apache.http.client.fluent.Response response = org.apache.http.client.fluent.Request.Get("http://" + LOCALHOST + ":" + PORT + "/").addHeader("beep", "boop").execute();
assertEquals(201, response.returnResponse().getStatusLine().getStatusCode());

try (final AsyncHttpClient client = new AsyncHttpClient()) {
final com.ning.http.client.Response response = client.prepareGet(new URL("http", LOCALHOST, PORT, "/").toString()).execute().get();
assertEquals(201, response.getStatusCode());
}

final List<MockSpan> spans = tracer.finishedSpans();
assertEquals(1, spans.size());
Expand Down

0 comments on commit 2861763

Please sign in to comment.