use of reactor.ipc.netty.http.client.HttpClientResponse in project spring-cloud-sleuth by spring-cloud.
the class TraceUserInfoRestTemplateCustomizer method wrapHttpClientRequestSending.
Mono<HttpClientResponse> wrapHttpClientRequestSending(ProceedingJoinPoint pjp, HttpMethod method, String url, Function<? super HttpClientRequest, ? extends Publisher<Void>> handler) throws Throwable {
// add headers and set CS
final Span currentSpan = this.tracer.currentSpan();
final AtomicReference<Span> span = new AtomicReference<>();
Function<HttpClientRequest, Publisher<Void>> combinedFunction = req -> {
try (Tracer.SpanInScope spanInScope = this.tracer.withSpanInScope(currentSpan)) {
io.netty.handler.codec.http.HttpHeaders originalHeaders = req.requestHeaders().copy();
io.netty.handler.codec.http.HttpHeaders tracedHeaders = req.requestHeaders();
span.set(this.handler.handleSend(this.injector, tracedHeaders, req));
io.netty.handler.codec.http.HttpHeaders addedHeaders = tracedHeaders.copy();
originalHeaders.forEach(header -> addedHeaders.remove(header.getKey()));
try (Tracer.SpanInScope clientInScope = this.tracer.withSpanInScope(span.get())) {
if (log.isDebugEnabled()) {
log.debug("Created a new client span for Netty client");
}
return handle(handler, new TracedHttpClientRequest(req, addedHeaders));
}
}
};
// run
Mono<HttpClientResponse> responseMono = (Mono<HttpClientResponse>) pjp.proceed(new Object[] { method, url, combinedFunction });
// get response
return responseMono.doOnSuccessOrError((httpClientResponse, throwable) -> {
try (Tracer.SpanInScope ws = this.tracer.withSpanInScope(span.get())) {
// status codes and CR
this.handler.handleReceive(httpClientResponse, throwable, span.get());
if (log.isDebugEnabled()) {
log.debug("Setting client sent spans");
}
}
});
}
use of reactor.ipc.netty.http.client.HttpClientResponse in project reactor-netty by reactor.
the class HttpServerTests method keepAlive.
@Test
public void keepAlive() throws URISyntaxException {
Path resource = Paths.get(getClass().getResource("/public").toURI());
NettyContext c = HttpServer.create(0).newRouter(routes -> routes.directory("/test", resource)).block(Duration.ofSeconds(30));
HttpResources.set(PoolResources.fixed("http", 1));
HttpClientResponse response0 = HttpClient.create(c.address().getPort()).get("/test/index.html").block(Duration.ofSeconds(30));
HttpClientResponse response1 = HttpClient.create(c.address().getPort()).get("/test/test.css").block(Duration.ofSeconds(30));
HttpClientResponse response2 = HttpClient.create(c.address().getPort()).get("/test/test1.css").block(Duration.ofSeconds(30));
HttpClientResponse response3 = HttpClient.create(c.address().getPort()).get("/test/test2.css").block(Duration.ofSeconds(30));
HttpClientResponse response4 = HttpClient.create(c.address().getPort()).get("/test/test3.css").block(Duration.ofSeconds(30));
HttpClientResponse response5 = HttpClient.create(c.address().getPort()).get("/test/test4.css").block(Duration.ofSeconds(30));
HttpClientResponse response6 = HttpClient.create(opts -> opts.port(c.address().getPort()).disablePool()).get("/test/test5.css").block(Duration.ofSeconds(30));
Assert.assertEquals(response0.channel(), response1.channel());
Assert.assertEquals(response0.channel(), response2.channel());
Assert.assertEquals(response0.channel(), response3.channel());
Assert.assertEquals(response0.channel(), response4.channel());
Assert.assertEquals(response0.channel(), response5.channel());
Assert.assertNotEquals(response0.channel(), response6.channel());
HttpResources.reset();
response0.dispose();
response1.dispose();
response2.dispose();
response3.dispose();
response4.dispose();
response5.dispose();
response6.dispose();
c.dispose();
}
use of reactor.ipc.netty.http.client.HttpClientResponse in project reactor-netty by reactor.
the class HttpServerTests method sendFileSecure.
@Test
public void sendFileSecure() throws CertificateException, SSLException, URISyntaxException {
Path largeFile = Paths.get(getClass().getResource("/largeFile.txt").toURI());
SelfSignedCertificate ssc = new SelfSignedCertificate();
SslContext sslServer = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
SslContext sslClient = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
NettyContext context = HttpServer.create(opt -> opt.sslContext(sslServer)).newHandler((req, resp) -> resp.sendFile(largeFile)).block();
HttpClientResponse response = HttpClient.create(opt -> opt.port(context.address().getPort()).sslContext(sslClient)).get("/foo").block(Duration.ofSeconds(120));
context.dispose();
context.onClose().block();
String body = response.receive().aggregate().asString(StandardCharsets.UTF_8).block();
assertThat(body).startsWith("This is an UTF-8 file that is larger than 1024 bytes. " + "It contains accents like é.").contains("1024 mark here -><- 1024 mark here").endsWith("End of File");
}
use of reactor.ipc.netty.http.client.HttpClientResponse in project reactor-netty by reactor.
the class NettyOptionsTest method afterChannelInit.
@Test
public void afterChannelInit() throws InterruptedException {
List<Channel> initializedChannels = new ArrayList<>();
NettyContext nettyContext = HttpServer.create(opt -> opt.afterChannelInit(initializedChannels::add)).start((req, resp) -> resp.sendNotFound()).getContext();
assertThat(initializedChannels).hasSize(0);
HttpClientResponse resp = HttpClient.create(opt -> opt.connectAddress(() -> nettyContext.address())).get("/", req -> req.failOnClientError(false).send()).block();
assertThat(initializedChannels).hasSize(1).doesNotContain(nettyContext.channel());
resp.dispose();
nettyContext.dispose();
}
use of reactor.ipc.netty.http.client.HttpClientResponse in project reactor-netty by reactor.
the class NettyOptionsTest method channelIsAddedToChannelGroup.
@Test
public void channelIsAddedToChannelGroup() {
// create a ChannelGroup that never removes disconnected channels
ChannelGroup group = new DefaultChannelGroup(null) {
@Override
public boolean remove(Object o) {
System.err.println("removed " + o);
return false;
}
};
NettyContext nettyContext = HttpServer.create(opt -> opt.channelGroup(group)).start((req, resp) -> resp.sendNotFound()).getContext();
HttpClientResponse resp = HttpClient.create(opt -> opt.connectAddress(() -> nettyContext.address())).get("/", req -> req.failOnClientError(false).send()).block();
assertThat((Iterable<Channel>) group).doesNotContain(nettyContext.channel()).hasSize(1);
resp.dispose();
nettyContext.dispose();
}
Aggregations