Search in sources :

Example 1 with Url

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;
}
Also used : Url(com.hotels.styx.api.Url)

Example 2 with Url

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();
}
Also used : HostAndPort(com.google.common.net.HostAndPort) Url(com.hotels.styx.api.Url)

Example 3 with Url

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(""));
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) Url(com.hotels.styx.api.Url) Test(org.junit.jupiter.api.Test)

Example 4 with Url

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"));
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) Url(com.hotels.styx.api.Url) Test(org.junit.jupiter.api.Test)

Example 5 with Url

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(""));
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) Url(com.hotels.styx.api.Url) Test(org.junit.jupiter.api.Test)

Aggregations

Url (com.hotels.styx.api.Url)8 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)5 Test (org.junit.jupiter.api.Test)5 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 HostAndPort (com.google.common.net.HostAndPort)1 ByteStream (com.hotels.styx.api.ByteStream)1 LiveHttpRequest (com.hotels.styx.api.LiveHttpRequest)1