Skip to content
This repository has been archived by the owner on Dec 8, 2021. It is now read-only.

Commit

Permalink
fix(websocket): Websocket support is configured from gravitee.yml ins…
Browse files Browse the repository at this point in the history
…tead of system properties

Closes gravitee-io/issues#2374
  • Loading branch information
brasseld authored and NicolasGeraud committed Aug 26, 2019
1 parent 68587fa commit b87aa62
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 35 deletions.
Expand Up @@ -16,6 +16,7 @@
package io.gravitee.gateway.standalone.vertx;

import io.gravitee.gateway.reactor.Reactor;
import io.gravitee.gateway.standalone.vertx.ws.VertxWebSocketReactorHandler;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.Vertx;
Expand Down Expand Up @@ -53,14 +54,25 @@ public class ReactorVerticle extends AbstractVerticle {
@Value("${http.requestTimeout:0}")
private long requestTimeout;

@Value("${http.websocket.enabled:false}")
private boolean websocketEnabled;

@Override
public void start(Future<Void> startFuture) throws Exception {
if (requestTimeout > 0) {
httpServer.requestHandler(new VertxReactorTimeoutHandler(vertx, reactor, requestTimeout));
VertxReactorHandler handler;

if (websocketEnabled) {
handler = new VertxWebSocketReactorHandler(reactor);
} else {
httpServer.requestHandler(new VertxReactorHandler(reactor));
handler = new VertxReactorHandler(reactor);
}

if (requestTimeout > 0) {
handler = new VertxReactorTimeoutHandler(reactor, handler, vertx, requestTimeout);
}

httpServer.requestHandler(handler);

httpServer.listen(res -> {
if (res.succeeded()) {
logger.info("HTTP listener ready to accept requests on port {}",
Expand Down
Expand Up @@ -66,6 +66,9 @@ public class VertxHttpServerConfiguration {
@Value("${http.maxChunkSize:8192}")
private int maxChunkSize;

@Value("${http.websocket.enabled:false}")
private boolean websocketEnabled;

public int getPort() {
return port;
}
Expand Down Expand Up @@ -177,4 +180,12 @@ public int getMaxChunkSize() {
public void setMaxChunkSize(int maxChunkSize) {
this.maxChunkSize = maxChunkSize;
}

public boolean isWebsocketEnabled() {
return websocketEnabled;
}

public void setWebsocketEnabled(boolean websocketEnabled) {
this.websocketEnabled = websocketEnabled;
}
}
Expand Up @@ -76,6 +76,9 @@ public HttpServer getObject() throws Exception {
options.setMaxChunkSize(httpServerConfiguration.getMaxChunkSize());
options.setMaxHeaderSize(httpServerConfiguration.getMaxHeaderSize());

// Configure websocket
System.setProperty("vertx.disableWebsockets", Boolean.toString(!httpServerConfiguration.isWebsocketEnabled()));

return vertx.createHttpServer(options);
}

Expand Down
Expand Up @@ -15,15 +15,10 @@
*/
package io.gravitee.gateway.standalone.vertx;

import io.gravitee.common.http.HttpHeaders;
import io.gravitee.gateway.api.Request;
import io.gravitee.gateway.api.Response;
import io.gravitee.gateway.reactor.Reactor;
import io.gravitee.gateway.standalone.vertx.ws.VertxWebSocketServerRequest;
import io.gravitee.gateway.standalone.vertx.ws.VertxWebSocketServerResponse;
import io.netty.handler.codec.http.HttpHeaderValues;
import io.vertx.core.Handler;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServerRequest;

/**
Expand All @@ -35,36 +30,19 @@ public class VertxReactorHandler implements Handler<HttpServerRequest> {

private final Reactor reactor;

VertxReactorHandler(final Reactor reactor) {
public VertxReactorHandler(final Reactor reactor) {
this.reactor = reactor;
}

@Override
public void handle(HttpServerRequest httpServerRequest) {
Request request;
Response response;

if (isWebSocket(httpServerRequest)) {
request = new VertxWebSocketServerRequest(httpServerRequest);
response = new VertxWebSocketServerResponse(httpServerRequest, request);
} else {
request = new VertxHttpServerRequest(httpServerRequest);
response = new VertxHttpServerResponse(httpServerRequest, request.metrics());
}
Request request = new VertxHttpServerRequest(httpServerRequest);
Response response = new VertxHttpServerResponse(httpServerRequest, request.metrics());

route(request, response);
}

protected void route(final Request request, final Response response) {
reactor.route(request, response, __ -> {});
}

private boolean isWebSocket(HttpServerRequest httpServerRequest) {
String connectionHeader = httpServerRequest.getHeader(HttpHeaders.CONNECTION);
String upgradeHeader = httpServerRequest.getHeader(HttpHeaders.UPGRADE);

return httpServerRequest.method() == HttpMethod.GET &&
HttpHeaderValues.UPGRADE.contentEqualsIgnoreCase(connectionHeader) &&
HttpHeaderValues.WEBSOCKET.contentEqualsIgnoreCase(upgradeHeader);
}
}
Expand Up @@ -27,12 +27,15 @@
*/
public class VertxReactorTimeoutHandler extends VertxReactorHandler {

private final VertxReactorHandler handler;

private final Vertx vertx;

private final long timeout;

VertxReactorTimeoutHandler(final Vertx vertx, final Reactor reactor, final long timeout) {
super( reactor );
VertxReactorTimeoutHandler(final Reactor reactor, final VertxReactorHandler handler, final Vertx vertx, final long timeout) {
super(reactor);
this.handler = handler;
this.vertx = vertx;
this.timeout = timeout;
}
Expand All @@ -47,9 +50,9 @@ protected void route(final Request request, final Response response) {
});

// Release timeout when response ends
super.route(request, new TimeoutServerResponse(vertx, response, timeoutId));
handler.route(request, new TimeoutServerResponse(vertx, response, timeoutId));
} else {
super.route(request, response);
handler.route(request, response);
}
}
}
@@ -0,0 +1,60 @@
/**
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.gateway.standalone.vertx.ws;

import io.gravitee.common.http.HttpHeaders;
import io.gravitee.gateway.api.Request;
import io.gravitee.gateway.api.Response;
import io.gravitee.gateway.reactor.Reactor;
import io.gravitee.gateway.standalone.vertx.VertxReactorHandler;
import io.netty.handler.codec.http.HttpHeaderValues;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServerRequest;

/**
* @author David BRASSELY (david.brassely at graviteesource.com)
* @author Azize ELAMRANI (azize.elamrani at graviteesource.com)
* @author GraviteeSource Team
*/
public class VertxWebSocketReactorHandler extends VertxReactorHandler {

public VertxWebSocketReactorHandler(final Reactor reactor) {
super(reactor);
}

@Override
public void handle(HttpServerRequest httpServerRequest) {
Request request;
Response response;

if (isWebSocket(httpServerRequest)) {
request = new VertxWebSocketServerRequest(httpServerRequest);
response = new VertxWebSocketServerResponse(httpServerRequest, request);
route(request, response);
} else {
super.handle(httpServerRequest);
}
}

private boolean isWebSocket(HttpServerRequest httpServerRequest) {
String connectionHeader = httpServerRequest.getHeader(HttpHeaders.CONNECTION);
String upgradeHeader = httpServerRequest.getHeader(HttpHeaders.UPGRADE);

return httpServerRequest.method() == HttpMethod.GET &&
HttpHeaderValues.UPGRADE.contentEqualsIgnoreCase(connectionHeader) &&
HttpHeaderValues.WEBSOCKET.contentEqualsIgnoreCase(upgradeHeader);
}
}
Expand Up @@ -28,7 +28,6 @@
import io.gravitee.plugin.core.api.ConfigurablePluginManager;
import io.gravitee.plugin.policy.PolicyPlugin;
import io.netty.util.internal.logging.InternalLoggerFactory;
import io.netty.util.internal.logging.Log4JLoggerFactory;
import io.netty.util.internal.logging.Slf4JLoggerFactory;
import org.junit.BeforeClass;
import org.junit.Rule;
Expand Down
Expand Up @@ -127,7 +127,6 @@ if [ -z "$daemon" ] ; then
-Dvertx.disableFileCaching=true \
-Dvertx.disableFileCPResolving=true \
-Dvertx.disableContextTiming=true \
-Dvertx.disableWebsockets=true \
-Dvertx.logger-delegate-factory-class-name=io.vertx.core.logging.SLF4JLogDelegateFactory \
-cp "$GRAVITEE_BOOT_CLASSPATH" \
$GRAVITEE_OPTS \
Expand All @@ -152,7 +151,6 @@ else
-Dvertx.disableFileCaching=true \
-Dvertx.disableFileCPResolving=true \
-Dvertx.disableContextTiming=true \
-Dvertx.disableWebsockets=true \
-Dvertx.logger-delegate-factory-class-name=io.vertx.core.logging.SLF4JLogDelegateFactory \
-cp "$GRAVITEE_BOOT_CLASSPATH" \
$GRAVITEE_OPTS \
Expand Down
Expand Up @@ -32,6 +32,8 @@
# truststore:
# path: ${gravitee.home}/security/truststore.jks
# password: secret
# websocket:
# enabled: false

# Plugins repository
#plugins:
Expand Down

0 comments on commit b87aa62

Please sign in to comment.