Java Code Examples for org.apache.tomcat.util.http.parser.HttpParser#isToken()

The following examples show how to use org.apache.tomcat.util.http.parser.HttpParser#isToken() . 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: TLSClientHelloExtractor.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
private static boolean isHttp(ByteBuffer bb) {
    // Based on code in Http11InputBuffer
    // Note: The actual request is not important. This code only checks that
    //       the buffer contains a correctly formatted HTTP request line.
    //       The method, target and protocol are not validated.
    byte chr = 0;
    bb.position(0);

    // Skip blank lines
    do {
        if (!bb.hasRemaining()) {
            return false;
        }
        chr = bb.get();
    } while (chr == '\r' || chr == '\n');

    // Read the method
    do {
        if (!HttpParser.isToken(chr) || !bb.hasRemaining()) {
            return false;
        }
        chr = bb.get();
    } while (chr != ' ' && chr != '\t');

    // Whitespace between method and target
    while (chr == ' ' || chr == '\t') {
        if (!bb.hasRemaining()) {
            return false;
        }
        chr = bb.get();
    }

    // Read the target
    while (chr != ' ' && chr != '\t') {
        if (HttpParser.isNotRequestTarget(chr) || !bb.hasRemaining()) {
            return false;
        }
        chr = bb.get();
    }

    // Whitespace between target and protocol
    while (chr == ' ' || chr == '\t') {
        if (!bb.hasRemaining()) {
            return false;
        }
        chr = bb.get();
    }

    // Read protocol
    do {
        if (!HttpParser.isHttpProtocol(chr) || !bb.hasRemaining()) {
            return false;
        }
        chr = bb.get();

    } while (chr != '\r' && chr != '\n');

    return true;
}