Skip to content

Commit

Permalink
Refactoring for usage in http2
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Bescos Gascon <[email protected]>
  • Loading branch information
jbescos committed Jul 13, 2023
1 parent 79bef69 commit e9738f7
Show file tree
Hide file tree
Showing 17 changed files with 399 additions and 242 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.function.Consumer;
Expand Down Expand Up @@ -68,6 +69,7 @@ class ClientRequestImpl implements Http2ClientRequest {
private int requestPrefetch = 0;
private int maxRedirects;
private ClientConnection explicitConnection;
private Proxy proxy;
private Duration flowControlTimeout = Duration.ofMillis(100);
private Duration timeout = Duration.ofSeconds(10);
private UriFragment fragment = UriFragment.empty();
Expand Down Expand Up @@ -337,7 +339,8 @@ private Http2ClientStream newStream(UriHelper uri) {
priorKnowledge,
tls,
client.dnsResolver(),
client.dnsAddressLookup());
client.dnsAddressLookup(),
proxy);

// this statement locks all threads - must not do anything complicated (just create a new instance)
return CHANNEL_CACHE.computeIfAbsent(connectionKey,
Expand Down Expand Up @@ -375,6 +378,8 @@ public void accept(HeaderValue httpHeader) {

@Override
public Http2ClientRequest proxy(Proxy proxy) {
throw new UnsupportedOperationException("Proxy is not supported in HTTP2");
this.proxy = Objects.requireNonNull(proxy);
return this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.helidon.common.http.Http;
import io.helidon.nima.http.media.ReadableEntity;
import io.helidon.nima.http2.Http2Headers;
import io.helidon.nima.webclient.ClientConnection;
import io.helidon.nima.webclient.UriHelper;

class ClientResponseImpl implements Http2ClientResponse {
Expand Down Expand Up @@ -58,6 +59,11 @@ public URI lastEndpointUri() {
return lastEndpointUri.toUri();
}

@Override
public ClientConnection connection() {
throw new UnsupportedOperationException("Not supported");
}

@Override
public void close() {
if (stream != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
import io.helidon.common.http.Http;
import io.helidon.nima.common.tls.Tls;
import io.helidon.nima.webclient.DnsAddressLookup;
import io.helidon.nima.webclient.Proxy;
import io.helidon.nima.webclient.spi.DnsResolver;

record ConnectionKey(Http.Method method, String scheme, String host, int port, boolean priorKnowledge, Tls tls,
DnsResolver dnsResolver, DnsAddressLookup dnsAddressLookup) {
DnsResolver dnsResolver, DnsAddressLookup dnsAddressLookup, Proxy proxy) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import io.helidon.nima.http2.Http2Settings;
import io.helidon.nima.http2.Http2WindowUpdate;
import io.helidon.nima.http2.WindowSize;
import io.helidon.nima.webclient.Proxy;
import io.helidon.nima.webclient.spi.DnsResolver;

import static java.lang.System.Logger.Level.DEBUG;
Expand Down Expand Up @@ -312,7 +313,7 @@ private void handle() {

private void doConnect() throws IOException {
boolean useTls = "https".equals(connectionKey.scheme()) && connectionKey.tls() != null;

Proxy proxy = connectionKey.proxy();
SSLSocket sslSocket = useTls ? connectionKey.tls().createSocket("h2") : null;
socket = sslSocket == null ? new Socket() : sslSocket;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.helidon.nima.testing.junit5.webserver;

import java.net.Socket;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
Expand Down Expand Up @@ -88,6 +89,11 @@ public void readTimeout(Duration readTimeout) {
//NOOP
}

@Override
public Socket socket() {
throw new UnsupportedOperationException("Socket does not exist in direct connection");
}

private DataWriter writer(ArrayBlockingQueue<byte[]> queue) {
return new DataWriter() {
@Override
Expand Down Expand Up @@ -153,4 +159,5 @@ private void startServer() {
}
});
}

}
4 changes: 4 additions & 0 deletions nima/tests/integration/webclient/webclient/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
<groupId>io.helidon.nima.webclient</groupId>
<artifactId>helidon-nima-webclient</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.nima.http2</groupId>
<artifactId>helidon-nima-http2-webclient</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.nima.webserver</groupId>
<artifactId>helidon-nima-webserver</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion nima/webclient/webclient/etc/spotbugs/exclude.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<!--
This is an implementation of an HTTP client, where a user may want to connect to plain socket.
-->
<Class name="io.helidon.nima.webclient.http1.Http1ClientConnection"/>
<Class name="io.helidon.nima.webclient.ConnectionStrategy"/>
<Bug pattern="UNENCRYPTED_SOCKET"/>
</Match>
<Match>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.helidon.nima.webclient;

import java.net.Socket;
import java.time.Duration;

import io.helidon.common.buffers.DataReader;
Expand Down Expand Up @@ -63,4 +64,11 @@ public interface ClientConnection {
* @param readTimeout connection read timeout
*/
void readTimeout(Duration readTimeout);

/**
* Socket of the connection.
*
* @return socket of the connection
*/
Socket socket();
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,12 @@ default <T extends Source<?>> void source(GenericType<T> sourceType, T source) {
*/
@Override
void close();

/**
* Return the current response connection.
* This is necessary for proxies because they need to re-use the same connection.
*
* @return the connection of the request
*/
ClientConnection connection();
}
Loading

0 comments on commit e9738f7

Please sign in to comment.