Skip to content

Commit

Permalink
fix: manage HTTP/1 to HTTP/2 requests
Browse files Browse the repository at this point in the history
  • Loading branch information
gcusnieux committed Jun 27, 2022
1 parent 3867cda commit 2b96ca2
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -115,7 +114,7 @@ public void request(ExecutionContext context, ProxyRequest request, Handler<Conn

final String host = (port == UNSECURE_PORT || port == SECURE_PORT) ? url.getHost() : url.getHost() + ':' + port;

request.headers().set(HttpHeaders.HOST, host);
translateHeaders(url, request, host);

// Enhance proxy request with endpoint configuration
if (endpoint.getHeaders() != null && !endpoint.getHeaders().isEmpty()) {
Expand Down Expand Up @@ -148,6 +147,8 @@ public void request(ExecutionContext context, ProxyRequest request, Handler<Conn
}
}

protected abstract void translateHeaders(URL url, ProxyRequest request, String host);

protected abstract AbstractHttpConnection<HttpEndpoint> create(ProxyRequest request);

@Override
Expand Down
68 changes: 68 additions & 0 deletions src/main/java/io/gravitee/connector/http/Http2Connector.java
Original file line number Diff line number Diff line change
@@ -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<HttpEndpoint> {

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<HttpEndpoint> create(ProxyRequest request) {
return new HttpConnection<>(endpoint, request);
}
}
10 changes: 10 additions & 0 deletions src/main/java/io/gravitee/connector/http/HttpConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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<HttpEndpoint> create(ProxyRequest request) {
String connectionHeader = request.headers().getFirst(HttpHeaders.CONNECTION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,6 +55,10 @@ public Connector<Connection, ProxyRequest> 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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,7 +26,7 @@
* @author David BRASSELY (david.brassely at graviteesource.com)
* @author GraviteeSource Team
*/
public class GrpcConnector extends AbstractHttpConnector<GrpcEndpoint> {
public class GrpcConnector extends Http2Connector {

public GrpcConnector(GrpcEndpoint endpoint, Configuration configuration) {
super(endpoint, configuration);
Expand All @@ -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;
Expand Down

0 comments on commit 2b96ca2

Please sign in to comment.