Java Code Examples for io.netty.util.internal.StringUtil#split()

The following examples show how to use io.netty.util.internal.StringUtil#split() . 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: WebSocketServerHandshaker.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
/**
 * Constructor specifying the destination web socket location
 *
 * @param version
 *            the protocol version
 * @param uri
 *            URL for web socket communications. e.g "ws://myhost.com/mypath". Subsequent web socket frames will be
 *            sent to this URL.
 * @param subprotocols
 *            CSV of supported protocols. Null if sub protocols not supported.
 * @param maxFramePayloadLength
 *            Maximum length of a frame's payload
 */
protected WebSocketServerHandshaker(
        WebSocketVersion version, String uri, String subprotocols,
        int maxFramePayloadLength) {
    this.version = version;
    this.uri = uri;
    if (subprotocols != null) {
        String[] subprotocolArray = StringUtil.split(subprotocols, ',');
        for (int i = 0; i < subprotocolArray.length; i++) {
            subprotocolArray[i] = subprotocolArray[i].trim();
        }
        this.subprotocols = subprotocolArray;
    } else {
        this.subprotocols = EmptyArrays.EMPTY_STRINGS;
    }
    this.maxFramePayloadLength = maxFramePayloadLength;
}
 
Example 2
Source File: WebSocketServerHandshaker.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
/**
 * Selects the first matching supported sub protocol
 *
 * @param requestedSubprotocols
 *            CSV of protocols to be supported. e.g. "chat, superchat"
 * @return First matching supported sub protocol. Null if not found.
 */
protected String selectSubprotocol(String requestedSubprotocols) {
    if (requestedSubprotocols == null || subprotocols.length == 0) {
        return null;
    }

    String[] requestedSubprotocolArray = StringUtil.split(requestedSubprotocols, ',');
    for (String p: requestedSubprotocolArray) {
        String requestedSubprotocol = p.trim();

        for (String supportedSubprotocol: subprotocols) {
            if (SUB_PROTOCOL_WILDCARD.equals(supportedSubprotocol)
                    || requestedSubprotocol.equals(supportedSubprotocol)) {
                selectedSubprotocol = requestedSubprotocol;
                return requestedSubprotocol;
            }
        }
    }

    // No match found
    return null;
}
 
Example 3
Source File: WampServerWebsocketHandler.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
private boolean isUpgradeRequest(FullHttpRequest request) {
    if (!request.getDecoderResult().isSuccess()) {
        return false;
    }
    
    String connectionHeaderValue = request.headers().get(HttpHeaders.Names.CONNECTION);
    if (connectionHeaderValue == null) {
        return false;
    }
    String[] connectionHeaderFields = StringUtil.split(connectionHeaderValue.toLowerCase(), ',');
    boolean hasUpgradeField = false;
    for (String s : connectionHeaderFields) {
        if (s.trim().equals(HttpHeaders.Values.UPGRADE.toLowerCase())) {
            hasUpgradeField = true;
            break;
        }
    }
    if (!hasUpgradeField) {
        return false;
    }
    
    if (!request.headers().contains(HttpHeaders.Names.UPGRADE, HttpHeaders.Values.WEBSOCKET, true)){
        return false;
    }
    
    return request.getUri().equals(websocketPath);
}
 
Example 4
Source File: HttpPostMultipartRequestDecoder.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
/**
 * Split one header in Multipart
 *
 * @return an array of String where rank 0 is the name of the header,
 *         follows by several values that were separated by ';' or ','
 */
private static String[] splitMultipartHeader(String sb) {
    ArrayList<String> headers = new ArrayList<String>(1);
    int nameStart;
    int nameEnd;
    int colonEnd;
    int valueStart;
    int valueEnd;
    nameStart = HttpPostBodyUtil.findNonWhitespace(sb, 0);
    for (nameEnd = nameStart; nameEnd < sb.length(); nameEnd++) {
        char ch = sb.charAt(nameEnd);
        if (ch == ':' || Character.isWhitespace(ch)) {
            break;
        }
    }
    for (colonEnd = nameEnd; colonEnd < sb.length(); colonEnd++) {
        if (sb.charAt(colonEnd) == ':') {
            colonEnd++;
            break;
        }
    }
    valueStart = HttpPostBodyUtil.findNonWhitespace(sb, colonEnd);
    valueEnd = HttpPostBodyUtil.findEndOfString(sb);
    headers.add(sb.substring(nameStart, nameEnd));
    String svalue = sb.substring(valueStart, valueEnd);
    String[] values;
    if (svalue.indexOf(';') >= 0) {
        values = splitMultipartHeaderValues(svalue);
    } else {
        values = StringUtil.split(svalue, ',');
    }
    for (String value : values) {
        headers.add(value.trim());
    }
    String[] array = new String[headers.size()];
    for (int i = 0; i < headers.size(); i++) {
        array[i] = headers.get(i);
    }
    return array;
}
 
Example 5
Source File: SpdyOrHttpChooser.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
/**
 * Return the {@link SelectedProtocol} for the {@link SSLEngine}. If its not known yet implementations MUST return
 * {@link SelectedProtocol#UNKNOWN}.
 *
 */
protected SelectedProtocol getProtocol(SSLEngine engine) {
    String[] protocol = StringUtil.split(engine.getSession().getProtocol(), ':');
    if (protocol.length < 2) {
        // Use HTTP/1.1 as default
        return SelectedProtocol.HTTP_1_1;
    }
    SelectedProtocol selectedProtocol = SelectedProtocol.protocol(protocol[1]);
    return selectedProtocol;
}
 
Example 6
Source File: WebSocketClientHandshaker.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
/**
 * Validates and finishes the opening handshake initiated by {@link #handshake}}.
 *
 * @param channel
 *            Channel
 * @param response
 *            HTTP response containing the closing handshake details
 */
public final void finishHandshake(Channel channel, FullHttpResponse response) {
    verify(response);

    // Verify the subprotocol that we received from the server.
    // This must be one of our expected subprotocols - or null/empty if we didn't want to speak a subprotocol
    String receivedProtocol = response.headers().get(HttpHeaders.Names.SEC_WEBSOCKET_PROTOCOL);
    receivedProtocol = receivedProtocol != null ? receivedProtocol.trim() : null;
    String expectedProtocol = expectedSubprotocol != null ? expectedSubprotocol : "";
    boolean protocolValid = false;

    if (expectedProtocol.isEmpty() && receivedProtocol == null) {
        // No subprotocol required and none received
        protocolValid = true;
        setActualSubprotocol(expectedSubprotocol); // null or "" - we echo what the user requested
    } else if (!expectedProtocol.isEmpty() && receivedProtocol != null && !receivedProtocol.isEmpty()) {
        // We require a subprotocol and received one -> verify it
        for (String protocol : StringUtil.split(expectedSubprotocol, ',')) {
            if (protocol.trim().equals(receivedProtocol)) {
                protocolValid = true;
                setActualSubprotocol(receivedProtocol);
                break;
            }
        }
    } // else mixed cases - which are all errors

    if (!protocolValid) {
        throw new WebSocketHandshakeException(String.format(
                "Invalid subprotocol. Actual: %s. Expected one of: %s",
                receivedProtocol, expectedSubprotocol));
    }

    setHandshakeComplete();

    ChannelPipeline p = channel.pipeline();
    // Remove decompressor from pipeline if its in use
    HttpContentDecompressor decompressor = p.get(HttpContentDecompressor.class);
    if (decompressor != null) {
        p.remove(decompressor);
    }

    // Remove aggregator if present before
    HttpObjectAggregator aggregator = p.get(HttpObjectAggregator.class);
    if (aggregator != null) {
        p.remove(aggregator);
    }

    ChannelHandlerContext ctx = p.context(HttpResponseDecoder.class);
    if (ctx == null) {
        ctx = p.context(HttpClientCodec.class);
        if (ctx == null) {
            throw new IllegalStateException("ChannelPipeline does not contain " +
                    "a HttpRequestEncoder or HttpClientCodec");
        }
        p.replace(ctx.name(), "ws-decoder", newWebsocketDecoder());
    } else {
        if (p.get(HttpRequestEncoder.class) != null) {
            p.remove(HttpRequestEncoder.class);
        }
        p.replace(ctx.name(),
                "ws-decoder", newWebsocketDecoder());
    }
}
 
Example 7
Source File: HttpContentCompressor.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("FloatingPointEquality")
protected ZlibWrapper determineWrapper(String acceptEncoding) {
    float starQ = -1.0f;
    float gzipQ = -1.0f;
    float deflateQ = -1.0f;
    for (String encoding: StringUtil.split(acceptEncoding, ',')) {
        float q = 1.0f;
        int equalsPos = encoding.indexOf('=');
        if (equalsPos != -1) {
            try {
                q = Float.valueOf(encoding.substring(equalsPos + 1));
            } catch (NumberFormatException e) {
                // Ignore encoding
                q = 0.0f;
            }
        }
        if (encoding.contains("*")) {
            starQ = q;
        } else if (encoding.contains("gzip") && q > gzipQ) {
            gzipQ = q;
        } else if (encoding.contains("deflate") && q > deflateQ) {
            deflateQ = q;
        }
    }
    if (gzipQ > 0.0f || deflateQ > 0.0f) {
        if (gzipQ >= deflateQ) {
            return ZlibWrapper.GZIP;
        } else {
            return ZlibWrapper.ZLIB;
        }
    }
    if (starQ > 0.0f) {
        if (gzipQ == -1.0f) {
            return ZlibWrapper.GZIP;
        }
        if (deflateQ == -1.0f) {
            return ZlibWrapper.ZLIB;
        }
    }
    return null;
}