Java Code Examples for org.apache.tomcat.util.buf.ByteChunk#getBytes()

The following examples show how to use org.apache.tomcat.util.buf.ByteChunk#getBytes() . 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: Stream.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
private void prepareRequest() {
    MessageBytes hostValueMB = coyoteRequest.getMimeHeaders().getUniqueValue("host");
    if (hostValueMB == null) {
        throw new IllegalArgumentException();
    }
    // This processing expects bytes. Server push will have used a String
    // to trigger a conversion if required.
    hostValueMB.toBytes();
    ByteChunk valueBC = hostValueMB.getByteChunk();
    byte[] valueB = valueBC.getBytes();
    int valueL = valueBC.getLength();
    int valueS = valueBC.getStart();

    int colonPos = Host.parse(hostValueMB);
    if (colonPos != -1) {
        int port = 0;
        for (int i = colonPos + 1; i < valueL; i++) {
            char c = (char) valueB[i + valueS];
            if (c < '0' || c > '9') {
                throw new IllegalArgumentException();
            }
            port = port * 10 + c - '0';
        }
        coyoteRequest.setServerPort(port);

        // Only need to copy the host name up to the :
        valueL = colonPos;
    }

    // Extract the host name
    char[] hostNameC = new char[valueL];
    for (int i = 0; i < valueL; i++) {
        hostNameC[i] = (char) valueB[i + valueS];
    }
    coyoteRequest.serverName().setChars(hostNameC, 0, valueL);
}
 
Example 2
Source File: AbstractProcessor.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
protected void parseHost(MessageBytes valueMB) {
    if (valueMB == null || valueMB.isNull()) {
        populateHost();
        populatePort();
        return;
    } else if (valueMB.getLength() == 0) {
        // Empty Host header so set sever name to empty string
        request.serverName().setString("");
        populatePort();
        return;
    }

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

    try {
        // Validates the host name
        int colonPos = Host.parse(valueMB);

        // Extract the port information first, if any
        if (colonPos != -1) {
            int port = 0;
            for (int i = colonPos + 1; i < valueL; i++) {
                char c = (char) valueB[i + valueS];
                if (c < '0' || c > '9') {
                    response.setStatus(400);
                    setErrorState(ErrorState.CLOSE_CLEAN, null);
                    return;
                }
                port = port * 10 + c - '0';
            }
            request.setServerPort(port);

            // Only need to copy the host name up to the :
            valueL = colonPos;
        }

        // Extract the host name
        for (int i = 0; i < valueL; i++) {
            hostNameC[i] = (char) valueB[i + valueS];
        }
        request.serverName().setChars(hostNameC, 0, valueL);

    } catch (IllegalArgumentException e) {
        // IllegalArgumentException indicates that the host name is invalid
        UserDataHelper.Mode logMode = userDataHelper.getNextMode();
        if (logMode != null) {
            String message = sm.getString("abstractProcessor.hostInvalid", valueMB.toString());
            switch (logMode) {
                case INFO_THEN_DEBUG:
                    message += sm.getString("abstractProcessor.fallToDebug");
                    //$FALL-THROUGH$
                case INFO:
                    getLog().info(message, e);
                    break;
                case DEBUG:
                    getLog().debug(message, e);
            }
        }

        response.setStatus(400);
        setErrorState(ErrorState.CLOSE_CLEAN, e);
    }
}
 
Example 3
Source File: Host.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
public MessageBytesReader(MessageBytes mb) {
    ByteChunk bc = mb.getByteChunk();
    bytes = bc.getBytes();
    pos = bc.getOffset();
    end = bc.getEnd();
}
 
Example 4
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 5
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 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);
    }

}