Skip to content

Commit

Permalink
start implementing session routing
Browse files Browse the repository at this point in the history
  • Loading branch information
patriot1burke committed May 21, 2024
1 parent 36c8881 commit 5f338a7
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 9 deletions.
11 changes: 11 additions & 0 deletions extensions/vertx-http/devspace-proxy-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public class ProxySession {
volatile long lastPoll;
AtomicLong requestId = new AtomicLong(System.currentTimeMillis());


ProxySession(ServiceProxy proxy, String sessionId, String who) {
timerId = vertx.setPeriodic(POLL_TIMEOUT, this::timerCallback);
this.proxy = proxy;
Expand Down Expand Up @@ -223,15 +224,15 @@ void shutdown() {
}

public static final String CLIENT_API_PATH = "/_dev_proxy_client_";
public static final String GLOBAL_PROXY_SESSION = "_depot_global";
public static final String SESSION_HEADER = "X-Depot-Proxy-Session";
public static final String HEADER_FORWARD_PREFIX = "X-Depot-Fwd-";
public static final String STATUS_CODE_HEADER = "X-Depot-Status-Code";
public static final String METHOD_HEADER = "X-Depot-Method";
public static final String URI_HEADER = "X-Depot-Uri";
public static final String REQUEST_ID_HEADER = "X-Depot-Request-Id";
public static final String RESPONSE_LINK = "X-Depot-Response-Path";
public static final String POLL_LINK = "X-Depot-Poll-Path";
public static final String GLOBAL_PROXY_SESSION = "_devspace_global";
public static final String SESSION_HEADER = "X-DevSpace-Session";
public static final String HEADER_FORWARD_PREFIX = "X-DevSpace-Fwd-";
public static final String STATUS_CODE_HEADER = "X-DevSpace-Status-Code";
public static final String METHOD_HEADER = "X-DevSpace-Method";
public static final String URI_HEADER = "X-DevSpace-Uri";
public static final String REQUEST_ID_HEADER = "X-DevSpace-Request-Id";
public static final String RESPONSE_LINK = "X-DevSpace-Response-Path";
public static final String POLL_LINK = "X-DevSpace-Poll-Path";

protected long POLL_TIMEOUT = 5000;
protected static final Logger log = Logger.getLogger(DevProxyServer.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.quarkus.devspace.server;

import io.vertx.ext.web.RoutingContext;

public class HeaderParamSessionRouter implements RequestSessionRouter {
private final String name;

public HeaderParamSessionRouter(String name) {
this.name = name;
}

@Override
public String match(RoutingContext ctx) {
return ctx.request().headers().get(name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.quarkus.devspace.server;

import io.vertx.core.http.HttpServerRequest;
import io.vertx.ext.web.RoutingContext;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PathParamSessionRouter implements RequestSessionRouter {
private final Pattern pattern;

public PathParamSessionRouter(String pathExpression) {
String regex = pathExpression.replace("<service>", "(?<service>[^/]+)");
pattern = Pattern.compile(regex + ".*");
}

@Override
public String match(RoutingContext ctx) {
String path = ctx.normalizedPath();
return match(path);
}

public String match(String path) {
if (path == null)
return null;
Matcher matcher = pattern.matcher(path);
if (matcher.matches()) {
return matcher.group("service");
} else {
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.quarkus.devspace.server;

import io.vertx.ext.web.RoutingContext;

public class QueryParamSessionRouter implements RequestSessionRouter {
private final String name;

public QueryParamSessionRouter(String name) {
this.name = name;
}

@Override
public String match(RoutingContext ctx) {
return ctx.queryParams().get(name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.quarkus.devspace.server;

import io.vertx.core.http.HttpServerRequest;
import io.vertx.ext.web.RoutingContext;

/**
*
*/
public interface RequestSessionRouter {
String match(RoutingContext ctx);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.quarkus.devspace.test;

import io.quarkus.devspace.server.PathParamSessionRouter;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;

public class MatchTest {
@Test
public void testPathParam() {
PathParamSessionRouter router = new PathParamSessionRouter("/foo/bar/<service>");
Assertions.assertEquals("bill", router.match("/foo/bar/bill"));
Assertions.assertEquals("bill", router.match("/foo/bar/bill/other/stuff"));
Assertions.assertEquals("bill", router.match("/foo/bar/bill/"));
}

}

0 comments on commit 5f338a7

Please sign in to comment.