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

Wire up Server Push #11082

Merged
merged 1 commit into from
Jul 30, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package io.quarkus.undertow.test.push;

import java.io.File;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.quarkus.test.common.http.TestHTTPResource;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpClientOptions;
import io.vertx.core.http.HttpClientRequest;
import io.vertx.core.http.HttpClientResponse;
import io.vertx.core.http.HttpVersion;
import io.vertx.core.net.JdkSSLEngineOptions;

public class Http2ServerPushTestCase {

@TestHTTPResource(value = "/push", ssl = true)
URL sslUrl;

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(ServerPushServlet.class, MessageServlet.class)
.addAsResource(new File("src/test/resources/ssl-jks.conf"), "application.properties")
.addAsResource(new File("src/test/resources/server-keystore.jks"), "server-keystore.jks"));

@Test
public void testServerPush() throws Exception {
Assumptions.assumeTrue(JdkSSLEngineOptions.isAlpnAvailable()); //don't run on JDK8
Vertx vertx = Vertx.vertx();
try {
HttpClientOptions options = new HttpClientOptions().setSsl(true).setUseAlpn(true)
.setProtocolVersion(HttpVersion.HTTP_2).setVerifyHost(false).setTrustAll(true);

final CompletableFuture<String> pushedPath = new CompletableFuture<>();
final CompletableFuture<String> pushedBody = new CompletableFuture<>();

HttpClientRequest request = vertx.createHttpClient(options)
.get(sslUrl.getPort(), sslUrl.getHost(), sslUrl.getPath());
request.pushHandler(new Handler<HttpClientRequest>() {
@Override
public void handle(HttpClientRequest event) {
pushedPath.complete(event.path());
event.handler(new Handler<HttpClientResponse>() {
@Override
public void handle(HttpClientResponse event) {
event.bodyHandler(new Handler<Buffer>() {
@Override
public void handle(Buffer event) {
pushedBody.complete(new String(event.getBytes(), StandardCharsets.UTF_8));
}
});
event.exceptionHandler(new Handler<Throwable>() {
@Override
public void handle(Throwable event) {
pushedBody.completeExceptionally(event);
pushedPath.completeExceptionally(event);
}
});
}
});
}
});
request.handler(new Handler<HttpClientResponse>() {
@Override
public void handle(HttpClientResponse event) {
event.endHandler(new Handler<Void>() {
@Override
public void handle(Void event) {

}
});
}
});
request.end();
Assertions.assertEquals("/pushed", pushedPath.get(10, TimeUnit.SECONDS));
Assertions.assertEquals("pushed-body", pushedBody.get(10, TimeUnit.SECONDS));

} finally {
vertx.close();
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.quarkus.undertow.test.push;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns = "/pushed")
public class MessageServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().print("pushed-body");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.quarkus.undertow.test.push;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns = "/push")
public class ServerPushServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.newPushBuilder()
.path("pushed")
.push();
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Enable SSL, configure the key store
quarkus.http.ssl.certificate.key-store-file=server-keystore.jks
quarkus.http.ssl.certificate.key-store-password=secret
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import io.quarkus.security.identity.CurrentIdentityAssociation;
import io.quarkus.vertx.http.runtime.CurrentVertxRequest;
import io.quarkus.vertx.http.runtime.HttpConfiguration;
import io.quarkus.vertx.http.runtime.VertxHttpRecorder;
import io.quarkus.vertx.http.runtime.security.QuarkusHttpUser;
import io.undertow.httpcore.BufferAllocator;
import io.undertow.httpcore.StatusCodes;
Expand Down Expand Up @@ -378,6 +379,7 @@ public void handle(RoutingContext event) {
event.remove(QuarkusHttpUser.AUTH_FAILURE_HANDLER);
VertxHttpExchange exchange = new VertxHttpExchange(event.request(), allocator, executorService, event,
event.getBody());
exchange.setPushHandler(VertxHttpRecorder.getRootHandler());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be sure: at this point we are entirely sure that the root handler is set in VertxHttpRecorder?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. At this point the server is processing requests, if the root handler was not set then no request processing would be happening.

Optional<MemorySize> maxBodySize = httpConfiguration.limits.maxBodySize;
if (maxBodySize.isPresent()) {
exchange.setMaxEntitySize(maxBodySize.get().asLongValue());
Expand Down