Java Code Examples for io.undertow.util.NetworkUtils

The following examples show how to use io.undertow.util.NetworkUtils. These examples are extracted from open source projects. 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 Project: quarkus-http   Source File: JsrWebSocketServerTest.java    License: Apache License 2.0 5 votes vote down vote up
@org.junit.Test
public void testBinaryWithByteBuffer() throws Exception {
    final byte[] payload = "payload".getBytes();
    final AtomicReference<Throwable> cause = new AtomicReference<>();
    final AtomicBoolean connected = new AtomicBoolean(false);
    final CompletableFuture latch = new CompletableFuture<>();

    class TestEndPoint extends Endpoint {
        @Override
        public void onOpen(final Session session, EndpointConfig config) {
            connected.set(true);
            session.addMessageHandler(new MessageHandler.Whole<ByteBuffer>() {
                @Override
                public void onMessage(ByteBuffer message) {
                    ByteBuffer buf = ByteBuffer.allocate(message.remaining());
                    buf.put(message);
                    buf.flip();
                    session.getAsyncRemote().sendBinary(buf);
                }
            });
        }
    }

    ServerWebSocketContainer builder = new ServerWebSocketContainer(TestClassIntrospector.INSTANCE, DefaultServer.getEventLoopSupplier(), Collections.EMPTY_LIST, false, false);

    builder.addEndpoint(ServerEndpointConfig.Builder.create(TestEndPoint.class, "/").configurator(new InstanceConfigurator(new TestEndPoint())).build());
    deployServlet(builder);

    WebSocketTestClient client = new WebSocketTestClient(new URI("ws://" + NetworkUtils.formatPossibleIpv6Address(DefaultServer.getHostAddress("default")) + ":" + DefaultServer.getHostPort("default") + "/"));
    client.connect();
    client.send(new BinaryWebSocketFrame(Unpooled.wrappedBuffer(payload)), new FrameChecker(BinaryWebSocketFrame.class, payload, latch));
    latch.get();
    Assert.assertNull(cause.get());
    client.destroy();
}
 
Example 2
Source Project: quarkus-http   Source File: HttpServerExchange.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Return the host, and also the port if this request was sent to a non-standard port. In general
 * this will just be the value of the Host header.
 * <p>
 * If this resolves to an IPv6 address it *will*  be enclosed by square brackets. The return
 * value of this method is suitable for inclusion in a URL.
 *
 * @return The host and port part of the destination address
 */
public String getHostAndPort() {
    String host = getRequestHeader(HttpHeaderNames.HOST);
    if (host == null) {
        InetSocketAddress address = getDestinationAddress();
        host = NetworkUtils.formatPossibleIpv6Address(address.getHostString());
        int port = address.getPort();
        if (!((getRequestScheme().equals("http") && port == 80)
                || (getRequestScheme().equals("https") && port == 443))) {
            host = host + ":" + port;
        }
    }
    return host;
}
 
Example 3
static InetAddress parseAddress(String addressString, String protocol) throws IOException {
    if (protocol.equals(TCP4)) {
        return NetworkUtils.parseIpv4Address(addressString);
    } else {
        return NetworkUtils.parseIpv6Address(addressString);
    }
}
 
Example 4
Source Project: lams   Source File: HttpServerExchange.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Return the host, and also the port if this request was sent to a non-standard port. In general
 * this will just be the value of the Host header.
 * <p>
 * If this resolves to an IPv6 address it *will*  be enclosed by square brackets. The return
 * value of this method is suitable for inclusion in a URL.
 *
 * @return The host and port part of the destination address
 */
public String getHostAndPort() {
    String host = requestHeaders.getFirst(Headers.HOST);
    if (host == null) {
        InetSocketAddress address = getDestinationAddress();
        host = NetworkUtils.formatPossibleIpv6Address(address.getHostString());
        int port = address.getPort();
        if (!((getRequestScheme().equals("http") && port == 80)
                || (getRequestScheme().equals("https") && port == 443))) {
            host = host + ":" + port;
        }
    }
    return host;
}
 
Example 5
Source Project: light-4j   Source File: CorsUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Determine the default origin, to allow for local access.
 * @param exchange the current HttpExchange.
 * @return the default origin (aka current server).
 */
public static String defaultOrigin(HttpServerExchange exchange) {
    String host = NetworkUtils.formatPossibleIpv6Address(exchange.getHostName());
    String protocol = exchange.getRequestScheme();
    int port = exchange.getHostPort();
    //This browser set header should not need IPv6 escaping
    StringBuilder allowedOrigin = new StringBuilder(256);
    allowedOrigin.append(protocol).append("://").append(host);
    if (!isDefaultPort(port, protocol)) {
        allowedOrigin.append(':').append(port);
    }
    return allowedOrigin.toString();
}
 
Example 6
Source Project: wildfly-core   Source File: CorsUtil.java    License: GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Determine the default origin, to allow for local access.
 * @param exchange the current HttpExchange.
 * @return the default origin (aka current server).
 */
public static String defaultOrigin(HttpServerExchange exchange) {
    String host = NetworkUtils.formatPossibleIpv6Address(exchange.getHostName());
    String protocol = exchange.getRequestScheme();
    int port = exchange.getHostPort();
    //This browser set header should not need IPv6 escaping
    StringBuilder allowedOrigin = new StringBuilder(256);
    allowedOrigin.append(protocol).append("://").append(host);
    if (!isDefaultPort(port, protocol)) {
        allowedOrigin.append(':').append(port);
    }
    return allowedOrigin.toString();
}
 
Example 7
Source Project: quarkus-http   Source File: DefaultServer.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * @return The base URL that can be used to make connections to this server
 */
public static String getDefaultServerURL() {
    return "http://" + NetworkUtils.formatPossibleIpv6Address(getHostAddress(DEFAULT)) + ":" + getHostPort(DEFAULT);
}
 
Example 8
Source Project: quarkus-http   Source File: DefaultServer.java    License: Apache License 2.0 4 votes vote down vote up
public static String getDefaultServerSSLAddress() {
//        if (sslServer == null && !isApacheTest()) {
//            throw new IllegalStateException("SSL Server not started.");
//        }
        return "https://" + NetworkUtils.formatPossibleIpv6Address(getHostAddress(DEFAULT)) + ":" + getHostSSLPort(DEFAULT);
    }