diff --git a/src/main/java/io/gravitee/connector/http/AbstractHttpConnector.java b/src/main/java/io/gravitee/connector/http/AbstractHttpConnector.java index 0366780d..d9673616 100644 --- a/src/main/java/io/gravitee/connector/http/AbstractHttpConnector.java +++ b/src/main/java/io/gravitee/connector/http/AbstractHttpConnector.java @@ -15,7 +15,6 @@ */ package io.gravitee.connector.http; -import io.gravitee.common.http.HttpHeaders; import io.gravitee.common.util.MultiValueMap; import io.gravitee.connector.api.AbstractConnector; import io.gravitee.connector.api.Connection; @@ -115,7 +114,7 @@ public void request(ExecutionContext context, ProxyRequest request, Handler create(ProxyRequest request); @Override diff --git a/src/main/java/io/gravitee/connector/http/Http2Connector.java b/src/main/java/io/gravitee/connector/http/Http2Connector.java new file mode 100644 index 00000000..3dc5194b --- /dev/null +++ b/src/main/java/io/gravitee/connector/http/Http2Connector.java @@ -0,0 +1,68 @@ +/** + * 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.connector.http; + +import io.gravitee.connector.api.EndpointException; +import io.gravitee.connector.http.endpoint.HttpEndpoint; +import io.gravitee.gateway.api.proxy.ProxyRequest; +import io.gravitee.node.api.configuration.Configuration; +import io.netty.handler.codec.http.HttpHeaderNames; +import io.vertx.core.http.HttpClientOptions; +import io.vertx.core.http.HttpVersion; +import java.net.URL; + +/** + * @author GraviteeSource Team + */ +public class Http2Connector extends AbstractHttpConnector { + + public Http2Connector(HttpEndpoint endpoint, Configuration configuration) { + super(endpoint, configuration); + } + + @Override + public HttpClientOptions createHttpClientOptions() throws EndpointException { + HttpClientOptions options = super.createHttpClientOptions(); + + // Force HTTP/2 protocol + options.setProtocolVersion(HttpVersion.HTTP_2); + + return options; + } + + @Override + protected void translateHeaders(URL url, ProxyRequest request, String host) { + // TODO: support HTTP2 headers + // request.headers().set(":authority", host); + // if (!request.headers().contains(":scheme")) { + // request.headers().set(":scheme", request.headers().get("X-Forwarded-Proto")); + // } + // if (!request.headers().contains(":path")) { + // request.headers().set(":path", url.getFile()); + // } + // Strip all headers made illegal in HTTP/2 messages: + request.headers().remove(HttpHeaderNames.HOST); + request.headers().remove(HttpHeaderNames.UPGRADE); + request.headers().remove(HttpHeaderNames.CONNECTION); + request.headers().remove(HttpHeaderNames.PROXY_CONNECTION); + request.headers().remove(HttpHeaderNames.TRANSFER_ENCODING); + } + + @Override + protected AbstractHttpConnection create(ProxyRequest request) { + return new HttpConnection<>(endpoint, request); + } +} diff --git a/src/main/java/io/gravitee/connector/http/HttpConnector.java b/src/main/java/io/gravitee/connector/http/HttpConnector.java index 53b1bfec..4a445887 100644 --- a/src/main/java/io/gravitee/connector/http/HttpConnector.java +++ b/src/main/java/io/gravitee/connector/http/HttpConnector.java @@ -22,6 +22,7 @@ import io.gravitee.gateway.api.proxy.ProxyRequest; import io.gravitee.node.api.configuration.Configuration; import io.netty.handler.codec.http.HttpHeaderValues; +import java.net.URL; /** * @author David BRASSELY (david.brassely at graviteesource.com) @@ -33,6 +34,15 @@ public HttpConnector(HttpEndpoint endpoint, Configuration configuration) { super(endpoint, configuration); } + @Override + protected void translateHeaders(URL url, ProxyRequest request, String host) { + request.headers().set(HttpHeaders.HOST, host); + // Strip all HTTP/2 headers + request.headers().remove(":authority"); + request.headers().remove(":scheme"); + request.headers().remove(":path"); + } + @Override protected AbstractHttpConnection create(ProxyRequest request) { String connectionHeader = request.headers().getFirst(HttpHeaders.CONNECTION); diff --git a/src/main/java/io/gravitee/connector/http/HttpConnectorFactory.java b/src/main/java/io/gravitee/connector/http/HttpConnectorFactory.java index 93deff93..1fa582ad 100644 --- a/src/main/java/io/gravitee/connector/http/HttpConnectorFactory.java +++ b/src/main/java/io/gravitee/connector/http/HttpConnectorFactory.java @@ -17,6 +17,7 @@ import io.gravitee.connector.api.*; import io.gravitee.connector.http.endpoint.HttpEndpoint; +import io.gravitee.connector.http.endpoint.ProtocolVersion; import io.gravitee.connector.http.endpoint.factory.HttpEndpointFactory; import io.gravitee.connector.http.grpc.GrpcConnector; import io.gravitee.connector.http.grpc.GrpcEndpoint; @@ -54,6 +55,10 @@ public Connector create(String target, String configur ); } + if (httpEndpoint.getHttpClientOptions().getVersion().equals(ProtocolVersion.HTTP_2)) { + return new Http2Connector(httpEndpoint, builder.getConfiguration()); + } + return new HttpConnector(httpEndpoint, builder.getConfiguration()); } diff --git a/src/main/java/io/gravitee/connector/http/grpc/GrpcConnector.java b/src/main/java/io/gravitee/connector/http/grpc/GrpcConnector.java index 3ae9d9d6..bdc66438 100644 --- a/src/main/java/io/gravitee/connector/http/grpc/GrpcConnector.java +++ b/src/main/java/io/gravitee/connector/http/grpc/GrpcConnector.java @@ -16,7 +16,7 @@ package io.gravitee.connector.http.grpc; import io.gravitee.connector.api.EndpointException; -import io.gravitee.connector.http.AbstractHttpConnector; +import io.gravitee.connector.http.Http2Connector; import io.gravitee.gateway.api.proxy.ProxyRequest; import io.gravitee.node.api.configuration.Configuration; import io.vertx.core.http.HttpClientOptions; @@ -26,7 +26,7 @@ * @author David BRASSELY (david.brassely at graviteesource.com) * @author GraviteeSource Team */ -public class GrpcConnector extends AbstractHttpConnector { +public class GrpcConnector extends Http2Connector { public GrpcConnector(GrpcEndpoint endpoint, Configuration configuration) { super(endpoint, configuration); @@ -36,7 +36,7 @@ public GrpcConnector(GrpcEndpoint endpoint, Configuration configuration) { public HttpClientOptions createHttpClientOptions() throws EndpointException { HttpClientOptions options = super.createHttpClientOptions(); - // For GRPC, force HTTP/2 protocol + // Force HTTP/2 protocol options.setProtocolVersion(HttpVersion.HTTP_2).setHttp2ClearTextUpgrade(false); return options;