Java Code Examples for org.apache.tomcat.util.buf.HexUtils#getDec()

The following examples show how to use org.apache.tomcat.util.buf.HexUtils#getDec() . 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: ApplicationPushBuilder.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
private static String decodePercentSequence(String sequence, Charset charset) {
    byte[] bytes = new byte[sequence.length()/3];
    for (int i = 0; i < bytes.length; i += 3) {
        bytes[i] = (byte) ((HexUtils.getDec(sequence.charAt(1 + 3 * i)) << 4) +
                HexUtils.getDec(sequence.charAt(2 + 3 * i)));
    }

    return new String(bytes, charset);
}
 
Example 2
Source File: ChunkedInputFilter.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
/**
 * Parse the header of a chunk.
 * A chunk header can look like one of the following:<br>
 * A10CRLF<br>
 * F23;chunk-extension to be ignoredCRLF
 *
 * <p>
 * The letters before CRLF or ';' (whatever comes first) must be valid hex
 * digits. We should not parse F23IAMGONNAMESSTHISUP34CRLF as a valid
 * header according to the spec.
 * @return <code>true</code> if the chunk header has been
 *  successfully parsed
 * @throws IOException Read error
 */
protected boolean parseChunkHeader() throws IOException {

    int result = 0;
    boolean eol = false;
    int readDigit = 0;
    boolean extension = false;

    while (!eol) {

        if (readChunk == null || readChunk.position() >= readChunk.limit()) {
            if (readBytes() <= 0)
                return false;
        }

        byte chr = readChunk.get(readChunk.position());
        if (chr == Constants.CR || chr == Constants.LF) {
            parseCRLF(false);
            eol = true;
        } else if (chr == Constants.SEMI_COLON && !extension) {
            // First semi-colon marks the start of the extension. Further
            // semi-colons may appear to separate multiple chunk-extensions.
            // These need to be processed as part of parsing the extensions.
            extension = true;
            extensionSize++;
        } else if (!extension) {
            //don't read data after the trailer
            int charValue = HexUtils.getDec(chr);
            if (charValue != -1 && readDigit < 8) {
                readDigit++;
                result = (result << 4) | charValue;
            } else {
                //we shouldn't allow invalid, non hex characters
                //in the chunked header
                return false;
            }
        } else {
            // Extension 'parsing'
            // Note that the chunk-extension is neither parsed nor
            // validated. Currently it is simply ignored.
            extensionSize++;
            if (maxExtensionSize > -1 && extensionSize > maxExtensionSize) {
                throwIOException(sm.getString("chunkedInputFilter.maxExtension"));
            }
        }

        // Parsing the CRLF increments pos
        if (!eol) {
            readChunk.position(readChunk.position() + 1);
        }
    }

    if (readDigit == 0 || result < 0) {
        return false;
    }

    if (result == 0) {
        endChunk = true;
    }

    remaining = result;
    return true;
}
 
Example 3
Source File: AbstractAjpProcessor.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
/**
 * Parse host.
 */
protected void parseHost(MessageBytes valueMB) {

    if (valueMB == null || valueMB.isNull()) {
        // HTTP/1.0
        request.setServerPort(request.getLocalPort());
        try {
            request.serverName().duplicate(request.localName());
        } catch (IOException e) {
            response.setStatus(400);
            setErrorState(ErrorState.CLOSE_CLEAN, e);
        }
        return;
    }

    ByteChunk valueBC = valueMB.getByteChunk();
    byte[] valueB = valueBC.getBytes();
    int valueL = valueBC.getLength();
    int valueS = valueBC.getStart();
    int colonPos = -1;
    if (hostNameC.length < valueL) {
        hostNameC = new char[valueL];
    }

    boolean ipv6 = (valueB[valueS] == '[');
    boolean bracketClosed = false;
    for (int i = 0; i < valueL; i++) {
        char b = (char) valueB[i + valueS];
        hostNameC[i] = b;
        if (b == ']') {
            bracketClosed = true;
        } else if (b == ':') {
            if (!ipv6 || bracketClosed) {
                colonPos = i;
                break;
            }
        }
    }

    if (colonPos < 0) {
        if (request.scheme().equalsIgnoreCase("https")) {
            // 443 - Default HTTPS port
            request.setServerPort(443);
        } else {
            // 80 - Default HTTTP port
            request.setServerPort(80);
        }
        request.serverName().setChars(hostNameC, 0, valueL);
    } else {

        request.serverName().setChars(hostNameC, 0, colonPos);

        int port = 0;
        int mult = 1;
        for (int i = valueL - 1; i > colonPos; i--) {
            int charValue = HexUtils.getDec(valueB[i + valueS]);
            if (charValue == -1) {
                // Invalid character
                // 400 - Bad request
                response.setStatus(400);
                setErrorState(ErrorState.CLOSE_CLEAN, null);
                break;
            }
            port = port + (charValue * mult);
            mult = 10 * mult;
        }
        request.setServerPort(port);
    }
}
 
Example 4
Source File: AbstractHttp11Processor.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
/**
 * Parse host.
 */
protected void parseHost(MessageBytes valueMB) {

    if (valueMB == null || valueMB.isNull()) {
        // HTTP/1.0
        // If no host header, use the port info from the endpoint
        // The host will be obtained lazily from the socket if required
        // using ActionCode#REQ_LOCAL_NAME_ATTRIBUTE
        request.setServerPort(endpoint.getPort());
        return;
    }

    ByteChunk valueBC = valueMB.getByteChunk();
    byte[] valueB = valueBC.getBytes();
    int valueL = valueBC.getLength();
    int valueS = valueBC.getStart();
    int colonPos = -1;
    if (hostNameC.length < valueL) {
        hostNameC = new char[valueL];
    }

    boolean ipv6 = (valueB[valueS] == '[');
    boolean bracketClosed = false;
    for (int i = 0; i < valueL; i++) {
        char b = (char) valueB[i + valueS];
        hostNameC[i] = b;
        if (b == ']') {
            bracketClosed = true;
        } else if (b == ':') {
            if (!ipv6 || bracketClosed) {
                colonPos = i;
                break;
            }
        }
    }

    if (colonPos < 0) {
        if (!endpoint.isSSLEnabled()) {
            // 80 - Default HTTP port
            request.setServerPort(80);
        } else {
            // 443 - Default HTTPS port
            request.setServerPort(443);
        }
        request.serverName().setChars(hostNameC, 0, valueL);
    } else {
        request.serverName().setChars(hostNameC, 0, colonPos);

        int port = 0;
        int mult = 1;
        for (int i = valueL - 1; i > colonPos; i--) {
            int charValue = HexUtils.getDec(valueB[i + valueS]);
            if (charValue == -1 || charValue > 9) {
                // Invalid character
                // 400 - Bad request
                response.setStatus(400);
                setErrorState(ErrorState.CLOSE_CLEAN, null);
                break;
            }
            port = port + (charValue * mult);
            mult = 10 * mult;
        }
        request.setServerPort(port);
    }

}
 
Example 5
Source File: ChunkedInputFilter.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
/**
 * Parse the header of a chunk.
 * A chunk header can look like one of the following:<br />
 * A10CRLF<br />
 * F23;chunk-extension to be ignoredCRLF
 *
 * <p>
 * The letters before CRLF or ';' (whatever comes first) must be valid hex
 * digits. We should not parse F23IAMGONNAMESSTHISUP34CRLF as a valid
 * header according to the spec.
 */
protected boolean parseChunkHeader() throws IOException {

    int result = 0;
    boolean eol = false;
    int readDigit = 0;
    boolean extension = false;

    while (!eol) {

        if (pos >= lastValid) {
            if (readBytes() <= 0)
                return false;
        }

        if (buf[pos] == Constants.CR || buf[pos] == Constants.LF) {
            parseCRLF(false);
            eol = true;
        } else if (buf[pos] == Constants.SEMI_COLON && !extension) {
            // First semi-colon marks the start of the extension. Further
            // semi-colons may appear to separate multiple chunk-extensions.
            // These need to be processed as part of parsing the extensions.
            extension = true;
            extensionSize++;
        } else if (!extension) {
            //don't read data after the trailer
            int charValue = HexUtils.getDec(buf[pos]);
            if (charValue != -1 && readDigit < 8) {
                readDigit++;
                result = (result << 4) | charValue;
            } else {
                //we shouldn't allow invalid, non hex characters
                //in the chunked header
                return false;
            }
        } else {
            // Extension 'parsing'
            // Note that the chunk-extension is neither parsed nor
            // validated. Currently it is simply ignored.
            extensionSize++;
            if (maxExtensionSize > -1 && extensionSize > maxExtensionSize) {
                throwIOException(sm.getString("chunkedInputFilter.maxExtension"));
            }
        }

        // Parsing the CRLF increments pos
        if (!eol) {
            pos++;
        }
    }

    if (readDigit == 0 || result < 0) {
        return false;
    }

    if (result == 0) {
        endChunk = true;
    }

    remaining = result;
    return true;
}
 
Example 6
Source File: AbstractAjpProcessor.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
 * Parse host.
 */
protected void parseHost(MessageBytes valueMB) {

    if (valueMB == null || valueMB.isNull()) {
        // HTTP/1.0
        request.setServerPort(request.getLocalPort());
        try {
            request.serverName().duplicate(request.localName());
        } catch (IOException e) {
            response.setStatus(400);
            setErrorState(ErrorState.CLOSE_CLEAN, e);
        }
        return;
    }

    ByteChunk valueBC = valueMB.getByteChunk();
    byte[] valueB = valueBC.getBytes();
    int valueL = valueBC.getLength();
    int valueS = valueBC.getStart();
    int colonPos = -1;
    if (hostNameC.length < valueL) {
        hostNameC = new char[valueL];
    }

    boolean ipv6 = (valueB[valueS] == '[');
    boolean bracketClosed = false;
    for (int i = 0; i < valueL; i++) {
        char b = (char) valueB[i + valueS];
        hostNameC[i] = b;
        if (b == ']') {
            bracketClosed = true;
        } else if (b == ':') {
            if (!ipv6 || bracketClosed) {
                colonPos = i;
                break;
            }
        }
    }

    if (colonPos < 0) {
        if (request.scheme().equalsIgnoreCase("https")) {
            // 443 - Default HTTPS port
            request.setServerPort(443);
        } else {
            // 80 - Default HTTTP port
            request.setServerPort(80);
        }
        request.serverName().setChars(hostNameC, 0, valueL);
    } else {

        request.serverName().setChars(hostNameC, 0, colonPos);

        int port = 0;
        int mult = 1;
        for (int i = valueL - 1; i > colonPos; i--) {
            int charValue = HexUtils.getDec(valueB[i + valueS]);
            if (charValue == -1) {
                // Invalid character
                // 400 - Bad request
                response.setStatus(400);
                setErrorState(ErrorState.CLOSE_CLEAN, null);
                break;
            }
            port = port + (charValue * mult);
            mult = 10 * mult;
        }
        request.setServerPort(port);
    }
}
 
Example 7
Source File: AbstractHttp11Processor.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
 * Parse host.
 */
protected void parseHost(MessageBytes valueMB) {

    if (valueMB == null || valueMB.isNull()) {
        // HTTP/1.0
        // If no host header, use the port info from the endpoint
        // The host will be obtained lazily from the socket if required
        // using ActionCode#REQ_LOCAL_NAME_ATTRIBUTE
        request.setServerPort(endpoint.getPort());
        return;
    }

    ByteChunk valueBC = valueMB.getByteChunk();
    byte[] valueB = valueBC.getBytes();
    int valueL = valueBC.getLength();
    int valueS = valueBC.getStart();
    int colonPos = -1;
    if (hostNameC.length < valueL) {
        hostNameC = new char[valueL];
    }

    boolean ipv6 = (valueB[valueS] == '[');
    boolean bracketClosed = false;
    for (int i = 0; i < valueL; i++) {
        char b = (char) valueB[i + valueS];
        hostNameC[i] = b;
        if (b == ']') {
            bracketClosed = true;
        } else if (b == ':') {
            if (!ipv6 || bracketClosed) {
                colonPos = i;
                break;
            }
        }
    }

    if (colonPos < 0) {
        if (!endpoint.isSSLEnabled()) {
            // 80 - Default HTTP port
            request.setServerPort(80);
        } else {
            // 443 - Default HTTPS port
            request.setServerPort(443);
        }
        request.serverName().setChars(hostNameC, 0, valueL);
    } else {
        request.serverName().setChars(hostNameC, 0, colonPos);

        int port = 0;
        int mult = 1;
        for (int i = valueL - 1; i > colonPos; i--) {
            int charValue = HexUtils.getDec(valueB[i + valueS]);
            if (charValue == -1 || charValue > 9) {
                // Invalid character
                // 400 - Bad request
                response.setStatus(400);
                setErrorState(ErrorState.CLOSE_CLEAN, null);
                break;
            }
            port = port + (charValue * mult);
            mult = 10 * mult;
        }
        request.setServerPort(port);
    }

}
 
Example 8
Source File: ChunkedInputFilter.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
 * Parse the header of a chunk.
 * A chunk header can look like one of the following:<br />
 * A10CRLF<br />
 * F23;chunk-extension to be ignoredCRLF
 *
 * <p>
 * The letters before CRLF or ';' (whatever comes first) must be valid hex
 * digits. We should not parse F23IAMGONNAMESSTHISUP34CRLF as a valid
 * header according to the spec.
 */
protected boolean parseChunkHeader() throws IOException {

    int result = 0;
    boolean eol = false;
    int readDigit = 0;
    boolean extension = false;

    while (!eol) {

        if (pos >= lastValid) {
            if (readBytes() <= 0)
                return false;
        }

        if (buf[pos] == Constants.CR || buf[pos] == Constants.LF) {
            parseCRLF(false);
            eol = true;
        } else if (buf[pos] == Constants.SEMI_COLON && !extension) {
            // First semi-colon marks the start of the extension. Further
            // semi-colons may appear to separate multiple chunk-extensions.
            // These need to be processed as part of parsing the extensions.
            extension = true;
            extensionSize++;
        } else if (!extension) {
            //don't read data after the trailer
            int charValue = HexUtils.getDec(buf[pos]);
            if (charValue != -1 && readDigit < 8) {
                readDigit++;
                result = (result << 4) | charValue;
            } else {
                //we shouldn't allow invalid, non hex characters
                //in the chunked header
                return false;
            }
        } else {
            // Extension 'parsing'
            // Note that the chunk-extension is neither parsed nor
            // validated. Currently it is simply ignored.
            extensionSize++;
            if (maxExtensionSize > -1 && extensionSize > maxExtensionSize) {
                throwIOException(sm.getString("chunkedInputFilter.maxExtension"));
            }
        }

        // Parsing the CRLF increments pos
        if (!eol) {
            pos++;
        }
    }

    if (readDigit == 0 || result < 0) {
        return false;
    }

    if (result == 0) {
        endChunk = true;
    }

    remaining = result;
    return true;
}