Java Code Examples for io.undertow.server.HttpServerExchange#setSourceAddress()

The following examples show how to use io.undertow.server.HttpServerExchange#setSourceAddress() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ForwardedHandler.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
    List<String> forwarded = exchange.getRequestHeaders(HttpHeaderNames.FORWARDED);
    if (forwarded != null) {
        Map<Token, String> values = new HashMap<>();
        for (String val : forwarded) {
            parseHeader(val, values);
        }
        String host = values.get(Token.HOST);
        String proto = values.get(Token.PROTO);
        String by = values.get(Token.BY);
        String forVal = values.get(Token.FOR);

        if (host != null) {
            exchange.setRequestHeader(HttpHeaderNames.HOST, host);
            exchange.setDestinationAddress(InetSocketAddress.createUnresolved(exchange.getHostName(), exchange.getHostPort()));
        } else if (by != null) {
            //we only use 'by' if the host is null
            InetSocketAddress destAddress = parseAddress(by);
            if (destAddress != null) {
                exchange.setDestinationAddress(destAddress);
            }
        }
        if (proto != null) {
            exchange.setRequestScheme(proto);
        }
        if (forVal != null) {
            InetSocketAddress sourceAddress = parseAddress(forVal);
            if (sourceAddress != null) {
                exchange.setSourceAddress(sourceAddress);
            }
        }
    }


    next.handleRequest(exchange);
}
 
Example 2
Source File: AccessLogCompletionListenerTest.java    From galeb with Apache License 2.0 5 votes vote down vote up
@Test
public void getJsonObjectTest() {
    environmentVariables.set("HOSTNAME", "hostname.localenv");
    environmentVariables.set("LOGGING_TAGS", "GALEB,OTHER");

    AccessLogCompletionListener accessLogCompletionListener = new AccessLogCompletionListener();

    HttpServerExchange httpServerExchange = new HttpServerExchange(Mockito.mock(ServerConnection.class), getRequestHeaders(), null, 0);
    httpServerExchange.setSourceAddress(new InetSocketAddress("1.2.3.4", 44444));
    httpServerExchange.setRequestMethod(HttpString.tryFromString("GET"));
    httpServerExchange.setRequestURI("/test");
    httpServerExchange.setProtocol(HttpString.tryFromString("HTTP"));
    httpServerExchange.setStatusCode(200);
    Connectors.setRequestStartTime(httpServerExchange);

    JsonObject jsonObject = accessLogCompletionListener.getJsonObject(httpServerExchange);

    Assert.assertEquals("1", jsonObject.getAsJsonPrimitive("@version").getAsString());
    Assert.assertEquals("hostname.localenv", jsonObject.getAsJsonPrimitive("host").getAsString());
    Assert.assertEquals(AccessLogCompletionListener.SHORT_MESSAGE, jsonObject.getAsJsonPrimitive("short_message").getAsString());
    Assert.assertEquals("vhost.host.virtual", jsonObject.getAsJsonPrimitive("vhost").getAsString());
    Assert.assertEquals("GALEB,OTHER,ACCESS", jsonObject.getAsJsonPrimitive("_tags").getAsString());
    Assert.assertEquals("1.2.3.4", jsonObject.getAsJsonPrimitive("remote_addr").getAsString());
    Assert.assertEquals("GET", jsonObject.getAsJsonPrimitive("request_method").getAsString());
    Assert.assertEquals("/test", jsonObject.getAsJsonPrimitive("request_uri").getAsString());
    Assert.assertEquals("HTTP", jsonObject.getAsJsonPrimitive("server_protocol").getAsString());
    Assert.assertEquals("-", jsonObject.getAsJsonPrimitive("http_referer").getAsString());
    Assert.assertEquals("-", jsonObject.getAsJsonPrimitive("http_x_mobile_group").getAsString());
    Assert.assertEquals("600", jsonObject.getAsJsonPrimitive("status").getAsString());
    Assert.assertNotNull(jsonObject.getAsJsonPrimitive("body_bytes_sent").getAsString());
    Assert.assertNotNull(jsonObject.getAsJsonPrimitive("request_time").getAsString());
    Assert.assertEquals("UNKNOWN_TARGET", jsonObject.getAsJsonPrimitive("upstream_addr").getAsString());
    Assert.assertEquals("200", jsonObject.getAsJsonPrimitive("upstream_status").getAsString());
    Assert.assertEquals("-", jsonObject.getAsJsonPrimitive("upstream_response_length").getAsString());
    Assert.assertEquals("-", jsonObject.getAsJsonPrimitive("http_user_agent").getAsString());
    Assert.assertEquals("-", jsonObject.getAsJsonPrimitive("request_id_final").getAsString());
    Assert.assertEquals("-", jsonObject.getAsJsonPrimitive("http_x_forwarded_for").getAsString());
}
 
Example 3
Source File: ForwardedHandler.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
    HeaderValues forwarded = exchange.getRequestHeaders().get(Headers.FORWARDED);
    if (forwarded != null) {
        Map<Token, String> values = new HashMap<>();
        for (String val : forwarded) {
            parseHeader(val, values);
        }
        String host = values.get(Token.HOST);
        String proto = values.get(Token.PROTO);
        String by = values.get(Token.BY);
        String forVal = values.get(Token.FOR);

        if (host != null) {
            exchange.getRequestHeaders().put(Headers.HOST, host);
            exchange.setDestinationAddress(InetSocketAddress.createUnresolved(exchange.getHostName(), exchange.getHostPort()));
        } else if (by != null) {
            //we only use 'by' if the host is null
            InetSocketAddress destAddress = parseAddress(by);
            if (destAddress != null) {
                exchange.setDestinationAddress(destAddress);
            }
        }
        if (proto != null) {
            exchange.setRequestScheme(proto);
        }
        if (forVal != null) {
            InetSocketAddress sourceAddress = parseAddress(forVal);
            if (sourceAddress != null) {
                exchange.setSourceAddress(sourceAddress);
            }
        }
    }


    next.handleRequest(exchange);
}
 
Example 4
Source File: HttpSource.java    From divolte-collector with Apache License 2.0 5 votes vote down vote up
protected static InetSocketAddress captureAndPersistSourceAddress(final HttpServerExchange exchange) {
    /*
     * The source address can be fetched on-demand from the peer connection, which may
     * no longer be available after the response has been sent. So we materialize it here
     * to ensure it's available further down the chain.
     */
    final InetSocketAddress sourceAddress = exchange.getSourceAddress();
    exchange.setSourceAddress(sourceAddress);
    return sourceAddress;
}
 
Example 5
Source File: HashSourceIpHostSelectorTest.java    From galeb with Apache License 2.0 4 votes vote down vote up
@Override
void changeExchange(HttpServerExchange exchange, int x) {
    InetSocketAddress address = InetSocketAddress.createUnresolved(ipIntToDotted(x), 10000 + (x % 40000));
    exchange.setSourceAddress(address);
}