use of reactor.netty.http.HttpProtocol in project reactor-netty by reactor.
the class HttpClientConfig method protocols.
void protocols(HttpProtocol... protocols) {
this.protocols = protocols;
int _protocols = 0;
for (HttpProtocol p : protocols) {
if (p == HttpProtocol.HTTP11) {
_protocols |= h11;
} else if (p == HttpProtocol.H2) {
_protocols |= h2;
} else if (p == HttpProtocol.H2C) {
_protocols |= h2c;
}
}
this._protocols = _protocols;
}
use of reactor.netty.http.HttpProtocol in project reactor-netty by reactor.
the class HttpServerTests method doTestIssue1978.
private void doTestIssue1978(HttpProtocol[] serverProtocols, HttpProtocol[] clientProtocols, @Nullable Http2SslContextSpec serverCtx, @Nullable Http2SslContextSpec clientCtx, long serverDelay, long clientDelay) throws Exception {
int count = 5;
CountDownLatch latch = new CountDownLatch(count);
Mono<String> mono = Mono.just("testIssue1978");
Mono<String> serverResponse = serverDelay == 0 ? mono : mono.delayElement(Duration.ofMillis(serverDelay));
HttpServer mainServer = HttpServer.create().protocol(serverProtocols).httpRequestDecoder(spec -> spec.h2cMaxContentLength(8 * 1024));
HttpServer server = serverCtx != null ? mainServer.secure(sslContextSpec -> sslContextSpec.sslContext(serverCtx)) : mainServer;
disposableServer = server.handle((req, res) -> {
req.withConnection(conn -> conn.channel().closeFuture().addListener(f -> latch.countDown()));
return res.sendString(serverResponse);
}).bindNow();
byte[] content = new byte[1024];
Random rndm = new Random();
rndm.nextBytes(content);
String strContent = new String(content, Charset.defaultCharset());
Flux<String> flux = Flux.just(strContent, strContent, strContent, strContent);
Flux<String> clientRequest = clientDelay == 0 ? flux : flux.delayElements(Duration.ofMillis(clientDelay));
HttpClient mainClient = HttpClient.create().port(disposableServer.port()).protocol(clientProtocols);
HttpClient client = clientCtx != null ? mainClient.secure(sslContextSpec -> sslContextSpec.sslContext(clientCtx)) : mainClient;
Flux.range(0, count).flatMap(i -> client.post().uri("/").send(ByteBufFlux.fromString(clientRequest)).responseContent().aggregate().asString()).blockLast(Duration.ofSeconds(10));
assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue();
}
use of reactor.netty.http.HttpProtocol in project reactor-netty by reactor.
the class DefaultPooledConnectionProviderTest method doTestIssue1982.
private void doTestIssue1982(HttpProtocol[] serverProtocols, HttpProtocol[] clientProtocols, @Nullable Http2SslContextSpec serverCtx, @Nullable Http2SslContextSpec clientCtx) throws Exception {
HttpServer server = serverCtx != null ? HttpServer.create().secure(sslContextSpec -> sslContextSpec.sslContext(serverCtx)) : HttpServer.create();
disposableServer = server.protocol(serverProtocols).http2Settings(h2 -> h2.maxConcurrentStreams(20)).handle((req, res) -> res.sendString(Mono.just("testIssue1982").delayElement(Duration.ofMillis(100)))).bindNow();
DefaultPooledConnectionProvider provider = (DefaultPooledConnectionProvider) ConnectionProvider.create("", 5);
CountDownLatch latch = new CountDownLatch(1);
AtomicInteger counter = new AtomicInteger();
HttpClient mainClient = clientCtx != null ? HttpClient.create(provider).port(disposableServer.port()).secure(sslContextSpec -> sslContextSpec.sslContext(clientCtx)) : HttpClient.create(provider).port(disposableServer.port());
HttpClient client = mainClient.protocol(clientProtocols).observe((conn, state) -> {
if (state == ConnectionObserver.State.CONNECTED) {
counter.incrementAndGet();
}
if (state == ConnectionObserver.State.RELEASED && counter.decrementAndGet() == 0) {
latch.countDown();
}
});
try {
Flux.range(0, 80).flatMap(i -> client.get().uri("/").responseContent().aggregate().asString()).blockLast(Duration.ofSeconds(10));
assertThat(latch.await(5, TimeUnit.SECONDS)).isTrue();
@SuppressWarnings({ "unchecked", "rawtypes" }) InstrumentedPool<DefaultPooledConnectionProvider.PooledConnection> channelPool = provider.channelPools.values().toArray(new InstrumentedPool[0])[0];
InstrumentedPool.PoolMetrics metrics = channelPool.metrics();
assertThat(metrics.acquiredSize()).isEqualTo(0);
assertThat(metrics.allocatedSize()).isEqualTo(metrics.idleSize());
} finally {
provider.disposeLater().block(Duration.ofSeconds(5));
}
}
use of reactor.netty.http.HttpProtocol in project reactor-netty by reactor.
the class HttpServerConfig method protocols.
void protocols(HttpProtocol... protocols) {
this.protocols = protocols;
int _protocols = 0;
for (HttpProtocol p : protocols) {
if (p == HttpProtocol.HTTP11) {
_protocols |= h11;
} else if (p == HttpProtocol.H2) {
_protocols |= h2;
} else if (p == HttpProtocol.H2C) {
_protocols |= h2c;
}
}
this._protocols = _protocols;
}
use of reactor.netty.http.HttpProtocol in project reactor-netty by reactor.
the class HttpServerPostFormTests method doTestPostForm.
private void doTestPostForm(HttpServer server, HttpClient client, Consumer<HttpServerFormDecoderProvider.Builder> provider, boolean configOnServer, boolean multipart, boolean streaming, @Nullable String expectedResponse) throws Exception {
AtomicReference<List<HttpData>> originalHttpData1 = new AtomicReference<>(new ArrayList<>());
AtomicReference<List<HttpData>> originalHttpData2 = new AtomicReference<>(new ArrayList<>());
AtomicReference<Map<String, CompositeByteBuf>> copiedHttpData = new AtomicReference<>(new HashMap<>());
server = (configOnServer ? server.httpFormDecoder(provider) : server).handle((req, res) -> res.sendString((configOnServer ? req.receiveForm() : req.receiveForm(provider)).flatMap(data -> {
if ("0".equals(req.path())) {
originalHttpData1.get().add(data);
if (streaming) {
CompositeByteBuf copy = copiedHttpData.get().computeIfAbsent(data.getName(), k -> Unpooled.compositeBuffer());
try {
// In case of streaming this is not a blocking call
copy.writeBytes(data.get());
} catch (IOException e) {
return Mono.error(e);
}
}
} else {
originalHttpData2.get().add(data);
}
return Mono.just('[' + data.getName() + ' ' + data.getClass().getSimpleName() + ' ' + data.isCompleted() + "] ");
}).onErrorResume(t -> Mono.just(t.getCause().getMessage())).log()));
disposableServer = server.bindNow();
List<Tuple2<Integer, String>> responses;
Path file = Paths.get(getClass().getResource("/largeFile1.txt").toURI());
responses = Flux.range(0, 2).flatMap(i -> client.port(disposableServer.port()).post().uri("/" + i).sendForm((req, form) -> form.multipart(multipart).file("test1", "largeFile1.txt", file.toFile(), null).attr("attr1", "attr2").file("test2", "largeFile1.txt", file.toFile(), null)).responseSingle((r, buf) -> buf.asString().map(s -> Tuples.of(r.status().code(), s)))).collectList().block(Duration.ofSeconds(30));
assertThat(responses).as("response").isNotNull();
for (Tuple2<Integer, String> response : responses) {
assertThat(response.getT1()).as("status code").isEqualTo(200);
if (expectedResponse != null) {
assertThat(response.getT2()).as("response body reflecting request").contains(expectedResponse);
}
}
assertThat(originalHttpData1.get()).allMatch(data -> data.refCnt() == 0);
assertThat(originalHttpData2.get()).allMatch(data -> data.refCnt() == 0);
if (streaming) {
if (expectedResponse == null) {
assertThat(copiedHttpData.get()).hasSize(3);
byte[] fileBytes = Files.readAllBytes(file);
testContent(copiedHttpData.get().get("test1"), fileBytes);
testContent(copiedHttpData.get().get("test2"), fileBytes);
copiedHttpData.get().forEach((s, buffer) -> buffer.release());
} else {
List<HttpProtocol> serverProtocols = Arrays.asList(server.configuration().protocols());
if (serverProtocols.size() == 1 && serverProtocols.get(0).equals(HttpProtocol.HTTP11)) {
assertThat(copiedHttpData.get()).hasSize(1);
copiedHttpData.get().forEach((s, buffer) -> buffer.release());
} else {
assertThat(copiedHttpData.get()).hasSize(0);
}
}
}
}
Aggregations