Java Code Examples for org.apache.tomcat.util.http.MimeHeaders#getValue()

The following examples show how to use org.apache.tomcat.util.http.MimeHeaders#getValue() . 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: Http11Processor.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
private static boolean isConnectionToken(MimeHeaders headers, String token) throws IOException {
    MessageBytes connection = headers.getValue(Constants.CONNECTION);
    if (connection == null) {
        return false;
    }

    Set<String> tokens = new HashSet<>();
    TokenList.parseTokenList(headers.values(Constants.CONNECTION), tokens);
    return tokens.contains(token);
}
 
Example 2
Source File: AbstractHttp11Processor.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
private boolean isConnectionClose(MimeHeaders headers) {
    MessageBytes connection = headers.getValue(Constants.CONNECTION);
    if (connection == null) {
        return false;
    }
    return connection.equals(Constants.CLOSE);
}
 
Example 3
Source File: AbstractHttp11Processor.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
private boolean isConnectionClose(MimeHeaders headers) {
    MessageBytes connection = headers.getValue(Constants.CONNECTION);
    if (connection == null) {
        return false;
    }
    return connection.equals(Constants.CLOSE);
}
 
Example 4
Source File: AjpProcessor.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
/**
 * When committing the response, we have to validate the set of headers, as
 * well as setup the response filters.
 */
@SuppressWarnings("deprecation")
@Override
protected final void prepareResponse() throws IOException {

    response.setCommitted(true);

    tmpMB.recycle();
    responseMsgPos = -1;
    responseMessage.reset();
    responseMessage.appendByte(Constants.JK_AJP13_SEND_HEADERS);

    // Responses with certain status codes are not permitted to include a
    // response body.
    int statusCode = response.getStatus();
    if (statusCode < 200 || statusCode == 204 || statusCode == 205 ||
            statusCode == 304) {
        // No entity body
        swallowResponse = true;
    }

    // Responses to HEAD requests are not permitted to include a response
    // body.
    MessageBytes methodMB = request.method();
    if (methodMB.equals("HEAD")) {
        // No entity body
        swallowResponse = true;
    }

    // HTTP header contents
    responseMessage.appendInt(statusCode);
    if (sendReasonPhrase) {
        String message = null;
        if (org.apache.coyote.Constants.USE_CUSTOM_STATUS_MSG_IN_HEADER &&
                HttpMessages.isSafeInHttpHeader(response.getMessage())) {
            message = response.getMessage();
        }
        if (message == null) {
            message = HttpMessages.getInstance(
                    response.getLocale()).getMessage(response.getStatus());
        }
        if (message == null) {
            // mod_jk + httpd 2.x fails with a null status message - bug 45026
            message = Integer.toString(response.getStatus());
        }
        tmpMB.setString(message);
    } else {
        // Reason phrase is optional but mod_jk + httpd 2.x fails with a null
        // reason phrase - bug 45026
        tmpMB.setString(Integer.toString(response.getStatus()));
    }
    responseMessage.appendBytes(tmpMB);

    // Special headers
    MimeHeaders headers = response.getMimeHeaders();
    String contentType = response.getContentType();
    if (contentType != null) {
        headers.setValue("Content-Type").setString(contentType);
    }
    String contentLanguage = response.getContentLanguage();
    if (contentLanguage != null) {
        headers.setValue("Content-Language").setString(contentLanguage);
    }
    long contentLength = response.getContentLengthLong();
    if (contentLength >= 0) {
        headers.setValue("Content-Length").setLong(contentLength);
    }

    // Other headers
    int numHeaders = headers.size();
    responseMessage.appendInt(numHeaders);
    for (int i = 0; i < numHeaders; i++) {
        MessageBytes hN = headers.getName(i);
        int hC = Constants.getResponseAjpIndex(hN.toString());
        if (hC > 0) {
            responseMessage.appendInt(hC);
        }
        else {
            responseMessage.appendBytes(hN);
        }
        MessageBytes hV=headers.getValue(i);
        responseMessage.appendBytes(hV);
    }

    // Write to buffer
    responseMessage.end();
    socketWrapper.write(true, responseMessage.getBuffer(), 0, responseMessage.getLen());
    socketWrapper.flush(true);
}
 
Example 5
Source File: StreamProcessor.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
static void prepareHeaders(Request coyoteRequest, Response coyoteResponse,
        Http2Protocol protocol, Stream stream) {
    MimeHeaders headers = coyoteResponse.getMimeHeaders();
    int statusCode = coyoteResponse.getStatus();

    // Add the pseudo header for status
    headers.addValue(":status").setString(Integer.toString(statusCode));

    // Check to see if a response body is present
    if (!(statusCode < 200 || statusCode == 204 || statusCode == 205 || statusCode == 304)) {
        String contentType = coyoteResponse.getContentType();
        if (contentType != null) {
            headers.setValue("content-type").setString(contentType);
        }
        String contentLanguage = coyoteResponse.getContentLanguage();
        if (contentLanguage != null) {
            headers.setValue("content-language").setString(contentLanguage);
        }
        // Add a content-length header if a content length has been set unless
        // the application has already added one
        long contentLength = coyoteResponse.getContentLengthLong();
        if (contentLength != -1 && headers.getValue("content-length") == null) {
            headers.addValue("content-length").setLong(contentLength);
        }
    } else {
        if (statusCode == 205) {
            // RFC 7231 requires the server to explicitly signal an empty
            // response in this case
            coyoteResponse.setContentLength(0);
        } else {
            coyoteResponse.setContentLength(-1);
        }
    }

    // Add date header unless it is an informational response or the
    // application has already set one
    if (statusCode >= 200 && headers.getValue("date") == null) {
        headers.addValue("date").setString(FastHttpDateFormat.getCurrentDate());
    }

    if (protocol != null && protocol.useCompression(coyoteRequest, coyoteResponse)) {
        // Enable compression. Headers will have been set. Need to configure
        // output filter at this point.
        stream.addOutputFilter(new GzipOutputFilter());
    }
}
 
Example 6
Source File: AbstractAjpProcessor.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
/**
 * When committing the response, we have to validate the set of headers, as
 * well as setup the response filters.
 */
protected void prepareResponse() throws IOException {

    response.setCommitted(true);

    responseMessage.reset();
    responseMessage.appendByte(Constants.JK_AJP13_SEND_HEADERS);

    // Responses with certain status codes are not permitted to include a
    // response body.
    int statusCode = response.getStatus();
    if (statusCode < 200 || statusCode == 204 || statusCode == 205 ||
            statusCode == 304) {
        // No entity body
        swallowResponse = true;
    }

    // Responses to HEAD requests are not permitted to incude a response
    // body.
    MessageBytes methodMB = request.method();
    if (methodMB.equals("HEAD")) {
        // No entity body
        swallowResponse = true;
    }

    // HTTP header contents
    responseMessage.appendInt(statusCode);
    String message = null;
    if (org.apache.coyote.Constants.USE_CUSTOM_STATUS_MSG_IN_HEADER &&
            HttpMessages.isSafeInHttpHeader(response.getMessage())) {
        message = response.getMessage();
    }
    if (message == null){
        message = HttpMessages.getInstance(
                response.getLocale()).getMessage(response.getStatus());
    }
    if (message == null) {
        // mod_jk + httpd 2.x fails with a null status message - bug 45026
        message = Integer.toString(response.getStatus());
    }
    tmpMB.setString(message);
    responseMessage.appendBytes(tmpMB);

    // Special headers
    MimeHeaders headers = response.getMimeHeaders();
    String contentType = response.getContentType();
    if (contentType != null) {
        headers.setValue("Content-Type").setString(contentType);
    }
    String contentLanguage = response.getContentLanguage();
    if (contentLanguage != null) {
        headers.setValue("Content-Language").setString(contentLanguage);
    }
    long contentLength = response.getContentLengthLong();
    if (contentLength >= 0) {
        headers.setValue("Content-Length").setLong(contentLength);
    }

    // Other headers
    int numHeaders = headers.size();
    responseMessage.appendInt(numHeaders);
    for (int i = 0; i < numHeaders; i++) {
        MessageBytes hN = headers.getName(i);
        int hC = Constants.getResponseAjpIndex(hN.toString());
        if (hC > 0) {
            responseMessage.appendInt(hC);
        }
        else {
            responseMessage.appendBytes(hN);
        }
        MessageBytes hV=headers.getValue(i);
        responseMessage.appendBytes(hV);
    }

    // Write to buffer
    responseMessage.end();
    output(responseMessage.getBuffer(), 0,
            responseMessage.getLen());
}
 
Example 7
Source File: CrossSubdomainSessionValve.java    From scipio-erp with Apache License 2.0 4 votes vote down vote up
protected void replaceCookie(Request request, Response response, Cookie cookie) {

        Delegator delegator = (Delegator) request.getAttribute("delegator");
        // copy the existing session cookie, but use a different domain (only if domain is valid)
        String cookieDomain = null;
        cookieDomain = EntityUtilProperties.getPropertyValue("url", "cookie.domain", "", delegator);

        if (UtilValidate.isEmpty(cookieDomain)) {
            String serverName = request.getServerName();
            String[] domainArray = serverName.split("\\.");
            // check that the domain isn't an IP address
            if (domainArray.length == 4) {
                boolean isIpAddress = true;
                for (String domainSection : domainArray) {
                    if (!UtilValidate.isIntegerInRange(domainSection, 0, 255)) {
                        isIpAddress = false;
                        break;
                    }
                }
                if (isIpAddress) {
                    return;
                }
            }
            if (domainArray.length > 2) {
                cookieDomain = "." + domainArray[domainArray.length - 2] + "." + domainArray[domainArray.length - 1];
            }
        }


        if (UtilValidate.isNotEmpty(cookieDomain)) {
            Cookie newCookie = new Cookie(cookie.getName(), cookie.getValue());
            if (cookie.getPath() != null) {
                newCookie.setPath(cookie.getPath());
            }
            newCookie.setDomain(cookieDomain);
            newCookie.setMaxAge(cookie.getMaxAge());
            newCookie.setVersion(cookie.getVersion());
            if (cookie.getComment() != null) {
                newCookie.setComment(cookie.getComment());
            }
            newCookie.setSecure(cookie.getSecure());
            newCookie.setHttpOnly(cookie.isHttpOnly());

            // if the response has already been committed, our replacement strategy will have no effect
            if (response.isCommitted()) {
                Debug.logError("CrossSubdomainSessionValve: response was already committed!", module);
            }

            // find the Set-Cookie header for the existing cookie and replace its value with new cookie
            MimeHeaders mimeHeaders = request.getCoyoteRequest().getMimeHeaders();
            for (int i = 0, size = mimeHeaders.size(); i < size; i++) {
                if (mimeHeaders.getName(i).equals("Set-Cookie")) {
                    MessageBytes value = mimeHeaders.getValue(i);
                    if (value.indexOf(cookie.getName()) >= 0) {
                        String newCookieValue = request.getContext().getCookieProcessor().generateHeader(newCookie);
                        if (Debug.verboseOn()) Debug.logVerbose("CrossSubdomainSessionValve: old Set-Cookie value: " + value.toString(), module);
                        if (Debug.verboseOn()) Debug.logVerbose("CrossSubdomainSessionValve: new Set-Cookie value: " + newCookieValue, module);
                        value.setString(newCookieValue);
                    }
                }
            }
        }
    }
 
Example 8
Source File: AbstractAjpProcessor.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
 * When committing the response, we have to validate the set of headers, as
 * well as setup the response filters.
 */
protected void prepareResponse() throws IOException {

    response.setCommitted(true);

    responseMessage.reset();
    responseMessage.appendByte(Constants.JK_AJP13_SEND_HEADERS);

    // Responses with certain status codes are not permitted to include a
    // response body.
    int statusCode = response.getStatus();
    if (statusCode < 200 || statusCode == 204 || statusCode == 205 ||
            statusCode == 304) {
        // No entity body
        swallowResponse = true;
    }

    // Responses to HEAD requests are not permitted to incude a response
    // body.
    MessageBytes methodMB = request.method();
    if (methodMB.equals("HEAD")) {
        // No entity body
        swallowResponse = true;
    }

    // HTTP header contents
    responseMessage.appendInt(statusCode);
    String message = null;
    if (org.apache.coyote.Constants.USE_CUSTOM_STATUS_MSG_IN_HEADER &&
            HttpMessages.isSafeInHttpHeader(response.getMessage())) {
        message = response.getMessage();
    }
    if (message == null){
        message = HttpMessages.getInstance(
                response.getLocale()).getMessage(response.getStatus());
    }
    if (message == null) {
        // mod_jk + httpd 2.x fails with a null status message - bug 45026
        message = Integer.toString(response.getStatus());
    }
    tmpMB.setString(message);
    responseMessage.appendBytes(tmpMB);

    // Special headers
    MimeHeaders headers = response.getMimeHeaders();
    String contentType = response.getContentType();
    if (contentType != null) {
        headers.setValue("Content-Type").setString(contentType);
    }
    String contentLanguage = response.getContentLanguage();
    if (contentLanguage != null) {
        headers.setValue("Content-Language").setString(contentLanguage);
    }
    long contentLength = response.getContentLengthLong();
    if (contentLength >= 0) {
        headers.setValue("Content-Length").setLong(contentLength);
    }

    // Other headers
    int numHeaders = headers.size();
    responseMessage.appendInt(numHeaders);
    for (int i = 0; i < numHeaders; i++) {
        MessageBytes hN = headers.getName(i);
        int hC = Constants.getResponseAjpIndex(hN.toString());
        if (hC > 0) {
            responseMessage.appendInt(hC);
        }
        else {
            responseMessage.appendBytes(hN);
        }
        MessageBytes hV=headers.getValue(i);
        responseMessage.appendBytes(hV);
    }

    // Write to buffer
    responseMessage.end();
    output(responseMessage.getBuffer(), 0,
            responseMessage.getLen());
}