org.apache.tomcat.util.http.HttpMessages Java Examples

The following examples show how to use org.apache.tomcat.util.http.HttpMessages. 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: Http11OutputBuffer.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
protected Http11OutputBuffer(Response response, int headerBufferSize, boolean sendReasonPhrase) {

        this.response = response;
        this.sendReasonPhrase = sendReasonPhrase;

        headerBuffer = ByteBuffer.allocate(headerBufferSize);

        filterLibrary = new OutputFilter[0];
        activeFilters = new OutputFilter[0];
        lastActiveFilter = -1;

        responseFinished = false;

        outputStreamOutputBuffer = new SocketOutputBuffer();

        if (sendReasonPhrase) {
            // Cause loading of HttpMessages
            HttpMessages.getInstance(response.getLocale()).getMessage(200);
        }
    }
 
Example #2
Source File: InternalAprOutputBuffer.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Default constructor.
 */
public InternalAprOutputBuffer(Response response, int headerBufferSize) {

    this.response = response;

    buf = new byte[headerBufferSize];
    if (headerBufferSize < (8 * 1024)) {
        bbuf = ByteBuffer.allocateDirect(6 * 1500);
    } else {
        bbuf = ByteBuffer.allocateDirect((headerBufferSize / 1500 + 1) * 1500);
    }

    outputStreamOutputBuffer = new SocketOutputBuffer();

    filterLibrary = new OutputFilter[0];
    activeFilters = new OutputFilter[0];
    lastActiveFilter = -1;

    committed = false;
    finished = false;

    // Cause loading of HttpMessages
    HttpMessages.getInstance(response.getLocale()).getMessage(200);

}
 
Example #3
Source File: InternalAprOutputBuffer.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * Default constructor.
 */
public InternalAprOutputBuffer(Response response, int headerBufferSize) {

    this.response = response;

    buf = new byte[headerBufferSize];
    if (headerBufferSize < (8 * 1024)) {
        bbuf = ByteBuffer.allocateDirect(6 * 1500);
    } else {
        bbuf = ByteBuffer.allocateDirect((headerBufferSize / 1500 + 1) * 1500);
    }

    outputStreamOutputBuffer = new SocketOutputBuffer();

    filterLibrary = new OutputFilter[0];
    activeFilters = new OutputFilter[0];
    lastActiveFilter = -1;

    committed = false;
    finished = false;

    // Cause loading of HttpMessages
    HttpMessages.getInstance(response.getLocale()).getMessage(200);

}
 
Example #4
Source File: SSIMediator.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
protected String encode(String value, String encoding) {
    String retVal = null;
    if (encoding.equalsIgnoreCase("url")) {
        retVal = urlEncoder.encode(value);
    } else if (encoding.equalsIgnoreCase("none")) {
        retVal = value;
    } else if (encoding.equalsIgnoreCase("entity")) {
        retVal = HttpMessages.filter(value);
    } else {
        //This shouldn't be possible
        throw new IllegalArgumentException("Unknown encoding: " + encoding);
    }
    return retVal;
}
 
Example #5
Source File: SSIMediator.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
protected String encode(String value, String encoding) {
    String retVal = null;
    if (encoding.equalsIgnoreCase("url")) {
        retVal = urlEncoder.encode(value, "UTF-8");
    } else if (encoding.equalsIgnoreCase("none")) {
        retVal = value;
    } else if (encoding.equalsIgnoreCase("entity")) {
        retVal = HttpMessages.filter(value);
    } else {
        //This shouldn't be possible
        throw new IllegalArgumentException("Unknown encoding: " + encoding);
    }
    return retVal;
}
 
Example #6
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 #7
Source File: Http11OutputBuffer.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
/**
 * Send the response status line.
 */
@SuppressWarnings("deprecation")
public void sendStatus() {
    // Write protocol name
    write(Constants.HTTP_11_BYTES);
    headerBuffer.put(Constants.SP);

    // Write status code
    int status = response.getStatus();
    switch (status) {
    case 200:
        write(Constants._200_BYTES);
        break;
    case 400:
        write(Constants._400_BYTES);
        break;
    case 404:
        write(Constants._404_BYTES);
        break;
    default:
        write(status);
    }

    headerBuffer.put(Constants.SP);

    if (sendReasonPhrase) {
        // Write message
        String message = null;
        if (org.apache.coyote.Constants.USE_CUSTOM_STATUS_MSG_IN_HEADER &&
                HttpMessages.isSafeInHttpHeader(response.getMessage())) {
            message = response.getMessage();
        }
        if (message == null) {
            write(HttpMessages.getInstance(
                    response.getLocale()).getMessage(status));
        } else {
            write(message);
        }
    } else {
        // The reason phrase is optional but the space before it is not. Skip
        // sending the reason phrase. Clients should ignore it (RFC 7230) and it
        // just wastes bytes.
    }

    headerBuffer.put(Constants.CR).put(Constants.LF);
}
 
Example #8
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 #9
Source File: AbstractOutputBuffer.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
/**
 * Send the response status line.
 */
public void sendStatus() {

    // Write protocol name
    write(Constants.HTTP_11_BYTES);
    buf[pos++] = Constants.SP;

    // Write status code
    int status = response.getStatus();
    switch (status) {
    case 200:
        write(Constants._200_BYTES);
        break;
    case 400:
        write(Constants._400_BYTES);
        break;
    case 404:
        write(Constants._404_BYTES);
        break;
    default:
        write(status);
    }

    buf[pos++] = Constants.SP;

    // Write message
    String message = null;
    if (org.apache.coyote.Constants.USE_CUSTOM_STATUS_MSG_IN_HEADER &&
            HttpMessages.isSafeInHttpHeader(response.getMessage())) {
        message = response.getMessage();
    }
    if (message == null) {
        write(HttpMessages.getInstance(
                response.getLocale()).getMessage(status));
    } else {
        write(message);
    }

    // End the response status line
    if (org.apache.coyote.Constants.IS_SECURITY_ENABLED){
       AccessController.doPrivileged(
            new PrivilegedAction<Void>(){
                @Override
                public Void run(){
                    buf[pos++] = Constants.CR;
                    buf[pos++] = Constants.LF;
                    return null;
                }
            }
       );
    } else {
        buf[pos++] = Constants.CR;
        buf[pos++] = Constants.LF;
    }

}
 
Example #10
Source File: InternalNioOutputBuffer.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
/**
 * Default constructor.
 */
public InternalNioOutputBuffer(Response response, int headerBufferSize) {

    this.response = response;

    buf = new byte[headerBufferSize];
    
    outputStreamOutputBuffer = new SocketOutputBuffer();

    filterLibrary = new OutputFilter[0];
    activeFilters = new OutputFilter[0];
    lastActiveFilter = -1;

    committed = false;
    finished = false;
    
    // Cause loading of HttpMessages
    HttpMessages.getInstance(response.getLocale()).getMessage(200);

}
 
Example #11
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());
}
 
Example #12
Source File: AbstractOutputBuffer.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
 * Send the response status line.
 */
public void sendStatus() {

    // Write protocol name
    write(Constants.HTTP_11_BYTES);
    buf[pos++] = Constants.SP;

    // Write status code
    int status = response.getStatus();
    switch (status) {
    case 200:
        write(Constants._200_BYTES);
        break;
    case 400:
        write(Constants._400_BYTES);
        break;
    case 404:
        write(Constants._404_BYTES);
        break;
    default:
        write(status);
    }

    buf[pos++] = Constants.SP;

    // Write message
    String message = null;
    if (org.apache.coyote.Constants.USE_CUSTOM_STATUS_MSG_IN_HEADER &&
            HttpMessages.isSafeInHttpHeader(response.getMessage())) {
        message = response.getMessage();
    }
    if (message == null) {
        write(HttpMessages.getInstance(
                response.getLocale()).getMessage(status));
    } else {
        write(message);
    }

    // End the response status line
    if (org.apache.coyote.Constants.IS_SECURITY_ENABLED){
       AccessController.doPrivileged(
            new PrivilegedAction<Void>(){
                @Override
                public Void run(){
                    buf[pos++] = Constants.CR;
                    buf[pos++] = Constants.LF;
                    return null;
                }
            }
       );
    } else {
        buf[pos++] = Constants.CR;
        buf[pos++] = Constants.LF;
    }

}
 
Example #13
Source File: InternalNioOutputBuffer.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
 * Default constructor.
 */
public InternalNioOutputBuffer(Response response, int headerBufferSize) {

    this.response = response;

    buf = new byte[headerBufferSize];

    outputStreamOutputBuffer = new SocketOutputBuffer();

    filterLibrary = new OutputFilter[0];
    activeFilters = new OutputFilter[0];
    lastActiveFilter = -1;

    committed = false;
    finished = false;

    // Cause loading of HttpMessages
    HttpMessages.getInstance(response.getLocale()).getMessage(200);

}