Java Code Examples for io.netty.util.NetUtil#toSocketAddressString()

The following examples show how to use io.netty.util.NetUtil#toSocketAddressString() . 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: WebSocketClientHandshaker.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
static CharSequence websocketHostValue(URI wsURL) {
    int port = wsURL.getPort();
    if (port == -1) {
        return wsURL.getHost();
    }
    String host = wsURL.getHost();
    if (port == HttpScheme.HTTP.port()) {
        return HttpScheme.HTTP.name().contentEquals(wsURL.getScheme())
                || WebSocketScheme.WS.name().contentEquals(wsURL.getScheme()) ?
                host : NetUtil.toSocketAddressString(host, port);
    }
    if (port == HttpScheme.HTTPS.port()) {
        return HttpScheme.HTTPS.name().contentEquals(wsURL.getScheme())
                || WebSocketScheme.WSS.name().contentEquals(wsURL.getScheme()) ?
                host : NetUtil.toSocketAddressString(host, port);
    }

    // if the port is not standard (80/443) its needed to add the port to the header.
    // See http://tools.ietf.org/html/rfc6454#section-6.2
    return NetUtil.toSocketAddressString(host, port);
}
 
Example 2
Source File: WebSocketClientHandshaker.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
static CharSequence websocketOriginValue(URI wsURL) {
    String scheme = wsURL.getScheme();
    final String schemePrefix;
    int port = wsURL.getPort();
    final int defaultPort;
    if (WebSocketScheme.WSS.name().contentEquals(scheme)
        || HttpScheme.HTTPS.name().contentEquals(scheme)
        || (scheme == null && port == WebSocketScheme.WSS.port())) {

        schemePrefix = HTTPS_SCHEME_PREFIX;
        defaultPort = WebSocketScheme.WSS.port();
    } else {
        schemePrefix = HTTP_SCHEME_PREFIX;
        defaultPort = WebSocketScheme.WS.port();
    }

    // Convert uri-host to lower case (by RFC 6454, chapter 4 "Origin of a URI")
    String host = wsURL.getHost().toLowerCase(Locale.US);

    if (port != defaultPort && port != -1) {
        // if the port is not standard (80/443) its needed to add the port to the header.
        // See http://tools.ietf.org/html/rfc6454#section-6.2
        return schemePrefix + NetUtil.toSocketAddressString(host, port);
    }
    return schemePrefix + host;
}
 
Example 3
Source File: HttpProxyHandler.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Override
protected Object newInitialMessage(ChannelHandlerContext ctx) throws Exception {
    InetSocketAddress raddr = destinationAddress();
    final String host = NetUtil.toSocketAddressString(raddr);
    FullHttpRequest req = new DefaultFullHttpRequest(
            HttpVersion.HTTP_1_1, HttpMethod.CONNECT,
            host,
            Unpooled.EMPTY_BUFFER, false);

    req.headers().set(HttpHeaderNames.HOST, host);

    if (authorization != null) {
        req.headers().set(HttpHeaderNames.PROXY_AUTHORIZATION, authorization);
    }

    if (headers != null) {
        req.headers().add(headers);
    }

    return req;
}
 
Example 4
Source File: UriEndpoint.java    From reactor-netty with Apache License 2.0 6 votes vote down vote up
static String toSocketAddressStringWithoutDefaultPort(SocketAddress address, boolean secure) {
	if (!(address instanceof InetSocketAddress)) {
		throw new IllegalStateException("Only support InetSocketAddress representation");
	}
	String addressString = NetUtil.toSocketAddressString((InetSocketAddress) address);
	if (secure) {
		if (addressString.endsWith(":443")) {
			addressString = addressString.substring(0, addressString.length() - 4);
		}
	}
	else {
		if (addressString.endsWith(":80")) {
			addressString = addressString.substring(0, addressString.length() - 3);
		}
	}
	return addressString;
}