Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow compression to be enabled together with HTTP/2 (helidon-2.x) #3705

Merged
merged 3 commits into from Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2020 Oracle and/or its affiliates.
* Copyright (c) 2019, 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -66,7 +66,7 @@ public static void main(final String[] args) {
* @return the created {@link WebServer} instance
*/
static WebServer startServer() {
return startServer(false, false);
return startServer(false, false, false);
}

/**
Expand All @@ -76,7 +76,7 @@ static WebServer startServer() {
* @param http2 Enable http2 support.
* @return the created {@link WebServer} instance
*/
static WebServer startServer(boolean ssl, boolean http2) {
static WebServer startServer(boolean ssl, boolean http2, boolean compression) {
// load logging configuration
LogConfig.configureRuntime();

Expand All @@ -89,12 +89,14 @@ static WebServer startServer(boolean ssl, boolean http2) {
.update(it -> configureJsonSupport(it, config))
.update(it -> configureSsl(it, ssl))
.update(it -> configureHttp2(it, http2))
.enableCompression(compression)
.build();

// Start the server and print some info.
server.start().thenAccept(ws -> {
String url = (ssl ? "https" : "http") + "://localhost:" + ws.port() + SERVICE_PATH;
System.out.println("WEB server is up! " + url + " [ssl=" + ssl + ", http2=" + http2 + "]");
System.out.println("WEB server is up! " + url + " [ssl=" + ssl + ", http2=" + http2
+ ", compression=" + compression + "]");
});

// Server threads are not daemon. NO need to block. Just react.
Expand Down
Expand Up @@ -39,7 +39,7 @@
import static org.junit.jupiter.api.Assertions.fail;

/**
* Tests SSL/TLS with HTTP 2 upgrades.
* Tests SSL/TLS with HTTP 2 upgrades and compression.
*/
public class Http2SslTest {

Expand All @@ -48,7 +48,7 @@ public class Http2SslTest {

@BeforeAll
public static void startServer() throws Exception {
webServer = TestServer.start(true, true);
webServer = TestServer.start(true, true, true);
client = TestServer.newOkHttpClient(true);
}

Expand All @@ -59,40 +59,12 @@ public static void stopServer() throws Exception {

@Test
public void testHelloWorldHttp2Ssl() throws Exception {
Request.Builder builder = TestServer.newRequestBuilder(webServer, "/books", true);

Request getBooks = builder.build();
try (Response getBooksRes = client.newCall(getBooks).execute()) {
assertEquals(getBooksRes.code(), 200);
assertEquals(getBooksRes.protocol(), Protocol.HTTP_2);
}

Request postBook = builder.post(
RequestBody.create(APPLICATION_JSON, TestServer.getBookAsJson())).build();
try (Response postBookRes = client.newCall(postBook).execute()) {
assertEquals(postBookRes.code(), 200);
assertEquals(postBookRes.protocol(), Protocol.HTTP_2);
}

builder = TestServer.newRequestBuilder(webServer, "/books/123456", true);
Request getBook = builder.build();
try (Response getBookRes = client.newCall(getBook).execute()) {
assertEquals(getBookRes.code(), 200);
assertEquals(getBookRes.protocol(), Protocol.HTTP_2);
}

Request deleteBook = builder.delete().build();
try (Response deleteBookRes = client.newCall(deleteBook).execute()) {
assertEquals(deleteBookRes.code(), 200);
assertEquals(deleteBookRes.protocol(), Protocol.HTTP_2);

}
testHelloWorld(false);
}

Request getNoBook = builder.build();
try (Response getNoBookRes = client.newCall(getNoBook).execute()) {
assertEquals(getNoBookRes.code(), 404);
assertEquals(getNoBookRes.protocol(), Protocol.HTTP_2);
}
@Test
public void testHelloWorldHttp2SslCompression() throws Exception {
testHelloWorld(true);
}

@Test
Expand Down Expand Up @@ -130,4 +102,41 @@ public void testHelloWorldHttp2SslConcurrent() throws Exception {
}
});
}

private void testHelloWorld(boolean compression) throws Exception {
Request.Builder builder = TestServer.newRequestBuilder(webServer, "/books", true, compression);

Request getBooks = builder.build();
try (Response getBooksRes = client.newCall(getBooks).execute()) {
assertEquals(getBooksRes.code(), 200);
assertEquals(getBooksRes.protocol(), Protocol.HTTP_2);
}

Request postBook = builder.post(
RequestBody.create(APPLICATION_JSON, TestServer.getBookAsJson())).build();
try (Response postBookRes = client.newCall(postBook).execute()) {
assertEquals(postBookRes.code(), 200);
assertEquals(postBookRes.protocol(), Protocol.HTTP_2);
}

builder = TestServer.newRequestBuilder(webServer, "/books/123456", true, compression);
Request getBook = builder.build();
try (Response getBookRes = client.newCall(getBook).execute()) {
assertEquals(getBookRes.code(), 200);
assertEquals(getBookRes.protocol(), Protocol.HTTP_2);
}

Request deleteBook = builder.delete().build();
try (Response deleteBookRes = client.newCall(deleteBook).execute()) {
assertEquals(deleteBookRes.code(), 200);
assertEquals(deleteBookRes.protocol(), Protocol.HTTP_2);

}

Request getNoBook = builder.build();
try (Response getNoBookRes = client.newCall(getNoBook).execute()) {
assertEquals(getNoBookRes.code(), 404);
assertEquals(getNoBookRes.protocol(), Protocol.HTTP_2);
}
}
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -42,7 +42,7 @@ public class MainTest {

@BeforeAll
public static void startServer() throws Exception {
webServer = TestServer.start(false, false);
webServer = TestServer.start(false, false, false);
client = TestServer.newOkHttpClient(false);
}

Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -39,7 +39,7 @@ public class SslTest {

@BeforeAll
public static void startServer() throws Exception {
webServer = TestServer.start(true, false);
webServer = TestServer.start(true, false, false);
client = TestServer.newOkHttpClient(true);
}

Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,9 @@

package io.helidon.tests.apps.bookstore.se;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -27,12 +30,7 @@
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import io.helidon.webserver.WebServer;

import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
Expand Down Expand Up @@ -60,8 +58,8 @@ public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
};

static WebServer start(boolean ssl, boolean http2) throws Exception {
WebServer webServer = Main.startServer(ssl, http2);
static WebServer start(boolean ssl, boolean http2, boolean compression) throws Exception {
WebServer webServer = Main.startServer(ssl, http2, compression);

long timeout = 2000; // 2 seconds should be enough to start the server
long now = System.currentTimeMillis();
Expand Down Expand Up @@ -101,8 +99,15 @@ static SSLContext setupSSLTrust() throws Exception {
}

static Request.Builder newRequestBuilder(WebServer webServer, String path, boolean ssl) throws Exception {
return newRequestBuilder(webServer, path, ssl, false);
}

static Request.Builder newRequestBuilder(WebServer webServer, String path, boolean ssl, boolean compression)
throws Exception {
URL url = new URL((ssl ? "https" : "http") + "://localhost:" + webServer.port() + path);
return new Request.Builder().url(url);
Request.Builder builder = new Request.Builder().url(url);
builder.addHeader("Accept-Encoding", compression ? "gzip" : "none");
return builder;
}

static class LoggingInterceptor implements Interceptor {
Expand Down
Expand Up @@ -200,12 +200,12 @@ public void initChannel(SocketChannel ch) {
// Uncomment the following line if you don't want to handle HttpChunks.
// p.addLast(new HttpObjectAggregator(1048576));
p.addLast(new HttpResponseEncoder());
}

// Enable compression via "Accept-Encoding" header if configured
if (serverConfig.enableCompression()) {
LOGGER.finer(() -> log("Compression negotiation enabled (gzip, deflate)", ch));
p.addLast(new HttpContentCompressor());
}
// Enable compression via "Accept-Encoding" header if configured
if (serverConfig.enableCompression()) {
LOGGER.finer(() -> log("Compression negotiation enabled (gzip, deflate)", ch));
p.addLast(new HttpContentCompressor());
}

// Helidon's forwarding handler
Expand Down
Expand Up @@ -798,7 +798,7 @@ public Builder tls(WebServerTls webServerTls) {

@Override
public Builder enableCompression(boolean value) {
this.defaultSocketBuilder.enableCompression(true);
this.defaultSocketBuilder.enableCompression(value);
return this;
}
}
Expand Down