use of com.hotels.styx.api.Url in project styx by ExpediaGroup.
the class RewriteRuleset method rewrite.
/**
* Rewrites the URL of the request according to the rewrite rules.
*
* @param request a request
* @return a rewritten request
*/
public LiveHttpRequest rewrite(LiveHttpRequest request) {
String path = request.path();
String newPath = newPath(path);
if (!Objects.equals(newPath, path)) {
Url newUrl = request.url().newBuilder().path(newPath).build();
return request.newBuilder().url(newUrl).build();
}
return request;
}
use of com.hotels.styx.api.Url in project styx by ExpediaGroup.
the class StyxHttpClient method originFromRequest.
private static Origin originFromRequest(LiveHttpRequest request, Boolean isHttps) {
String hostAndPort = request.header(HOST).orElseGet(() -> {
checkArgument(request.url().isAbsolute(), "host header is not set for request=%s", request);
return request.url().authority().map(Url.Authority::hostAndPort).orElseThrow(() -> new IllegalArgumentException("Cannot send request " + request + " as URL is not absolute and no HOST header is present"));
});
HostAndPort host = HostAndPort.fromString(hostAndPort);
if (host.getPortOrDefault(-1) < 0) {
host = host.withDefaultPort(isHttps ? DEFAULT_HTTPS_PORT : DEFAULT_HTTP_PORT);
}
return newOriginBuilder(host.getHost(), host.getPort()).build();
}
use of com.hotels.styx.api.Url in project styx by ExpediaGroup.
the class UrlDecoderTest method decodesOriginFormWithUppercaseHostHeader.
// Demonstrates that Host header handling is case insensitive.
@Test
public void decodesOriginFormWithUppercaseHostHeader() {
DefaultFullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, GET, "/foo");
request.headers().add("HOST", "example.com");
Url url = UrlDecoder.decodeUrl(x -> x, request);
assertThat(url.authority(), is(Optional.empty()));
assertThat(url.path(), is("/foo"));
assertThat(url.encodedUrl(), is("/foo"));
assertThat(url.isAbsolute(), is(false));
assertThat(url.isRelative(), is(true));
assertThat(url.host(), is(Optional.empty()));
assertThat(url.queryParams(), is(emptyMap()));
assertThat(url.scheme(), is(""));
}
use of com.hotels.styx.api.Url in project styx by ExpediaGroup.
the class UrlDecoderTest method shouldEncodeSquareBrackets.
@Test
public void shouldEncodeSquareBrackets() {
DefaultFullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, GET, "/foo?bob[]=a");
request.headers().add(HOST, "example.com");
Url url = UrlDecoder.decodeUrl(x -> x, request);
assertThat(url.toString(), is("/foo?bob%5B%5D=a"));
}
use of com.hotels.styx.api.Url in project styx by ExpediaGroup.
the class UrlDecoderTest method exposesPathComponentsWithDoubleSlashSeparators.
// From GitHub issue #391.
@Test
public void exposesPathComponentsWithDoubleSlashSeparators() {
DefaultFullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, GET, "//www.abc.com//abc123Z");
request.headers().add(HOST, "example.com");
Url url = UrlDecoder.decodeUrl(x -> x, request);
assertThat(url.authority(), is(Optional.empty()));
assertThat(url.path(), is("//www.abc.com//abc123Z"));
assertThat(url.encodedUrl(), is("//www.abc.com//abc123Z"));
assertThat(url.isAbsolute(), is(false));
assertThat(url.isRelative(), is(true));
assertThat(url.host(), is(Optional.empty()));
assertThat(url.queryParams(), is(emptyMap()));
assertThat(url.scheme(), is(""));
}
Aggregations