Search in sources :

Example 1 with MessageBytes

use of org.apache.tomcat.util.buf.MessageBytes in project tomcat70 by apache.

the class Request method getContentLengthLong.

public long getContentLengthLong() {
    if (contentLength > -1)
        return contentLength;
    MessageBytes clB = headers.getUniqueValue("content-length");
    contentLength = (clB == null || clB.isNull()) ? -1 : clB.getLong();
    return contentLength;
}
Also used : MessageBytes(org.apache.tomcat.util.buf.MessageBytes)

Example 2 with MessageBytes

use of org.apache.tomcat.util.buf.MessageBytes in project tomcat70 by apache.

the class AbstractAjpProcessor method prepareResponse.

/**
 * 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());
}
Also used : MimeHeaders(org.apache.tomcat.util.http.MimeHeaders) MessageBytes(org.apache.tomcat.util.buf.MessageBytes) AbstractEndpoint(org.apache.tomcat.util.net.AbstractEndpoint)

Example 3 with MessageBytes

use of org.apache.tomcat.util.buf.MessageBytes in project tomcat70 by apache.

the class InternalInputBuffer method parseHeader.

/**
 * Parse an HTTP header.
 *
 * @return false after reading a blank line (which indicates that the
 * HTTP header parsing is done
 */
// headerValue cannot be null
@SuppressWarnings("null")
private boolean parseHeader() throws IOException {
    // 
    // Check for blank line
    // 
    byte chr = 0;
    while (true) {
        // Read new bytes if needed
        if (pos >= lastValid) {
            if (!fill())
                throw new EOFException(sm.getString("iib.eof.error"));
        }
        chr = buf[pos];
        if (chr == Constants.CR) {
        // Skip
        } else if (chr == Constants.LF) {
            pos++;
            return false;
        } else {
            break;
        }
        pos++;
    }
    // Mark the current buffer position
    int start = pos;
    // 
    // Reading the header name
    // Header name is always US-ASCII
    // 
    boolean colon = false;
    MessageBytes headerValue = null;
    while (!colon) {
        // Read new bytes if needed
        if (pos >= lastValid) {
            if (!fill())
                throw new EOFException(sm.getString("iib.eof.error"));
        }
        if (buf[pos] == Constants.COLON) {
            colon = true;
            headerValue = headers.addValue(buf, start, pos - start);
        } else if (!HttpParser.isToken(buf[pos])) {
            // Non-token characters are illegal in header names
            // Parsing continues so the error can be reported in context
            // skipLine() will handle the error
            skipLine(start);
            return true;
        }
        chr = buf[pos];
        if ((chr >= Constants.A) && (chr <= Constants.Z)) {
            buf[pos] = (byte) (chr - Constants.LC_OFFSET);
        }
        pos++;
    }
    // Mark the current buffer position
    start = pos;
    int realPos = pos;
    // 
    // Reading the header value (which can be spanned over multiple lines)
    // 
    boolean eol = false;
    boolean validLine = true;
    while (validLine) {
        boolean space = true;
        // Skipping spaces
        while (space) {
            // Read new bytes if needed
            if (pos >= lastValid) {
                if (!fill())
                    throw new EOFException(sm.getString("iib.eof.error"));
            }
            if ((buf[pos] == Constants.SP) || (buf[pos] == Constants.HT)) {
                pos++;
            } else {
                space = false;
            }
        }
        int lastSignificantChar = realPos;
        // Reading bytes until the end of the line
        while (!eol) {
            // Read new bytes if needed
            if (pos >= lastValid) {
                if (!fill())
                    throw new EOFException(sm.getString("iib.eof.error"));
            }
            if (buf[pos] == Constants.CR) {
            // Skip
            } else if (buf[pos] == Constants.LF) {
                eol = true;
            } else if (buf[pos] == Constants.SP) {
                buf[realPos] = buf[pos];
                realPos++;
            } else {
                buf[realPos] = buf[pos];
                realPos++;
                lastSignificantChar = realPos;
            }
            pos++;
        }
        realPos = lastSignificantChar;
        // Read new bytes if needed
        if (pos >= lastValid) {
            if (!fill())
                throw new EOFException(sm.getString("iib.eof.error"));
        }
        chr = buf[pos];
        if ((chr != Constants.SP) && (chr != Constants.HT)) {
            validLine = false;
        } else {
            eol = false;
            // Copying one extra space in the buffer (since there must
            // be at least one space inserted between the lines)
            buf[realPos] = chr;
            realPos++;
        }
    }
    // Set the header value
    headerValue.setBytes(buf, start, realPos - start);
    return true;
}
Also used : EOFException(java.io.EOFException) MessageBytes(org.apache.tomcat.util.buf.MessageBytes) AbstractEndpoint(org.apache.tomcat.util.net.AbstractEndpoint)

Example 4 with MessageBytes

use of org.apache.tomcat.util.buf.MessageBytes in project tomcat70 by apache.

the class TestMapper method testPerformanceImpl.

private long testPerformanceImpl() throws Exception {
    MappingData mappingData = new MappingData();
    MessageBytes host = MessageBytes.newInstance();
    host.setString("iowejoiejfoiew");
    MessageBytes uri = MessageBytes.newInstance();
    uri.setString("/foo/bar/blah/bobou/foo");
    uri.toChars();
    uri.getCharChunk().setLimit(-1);
    long start = System.currentTimeMillis();
    for (int i = 0; i < 1000000; i++) {
        mappingData.recycle();
        mapper.map(host, uri, null, mappingData);
    }
    long time = System.currentTimeMillis() - start;
    return time;
}
Also used : MessageBytes(org.apache.tomcat.util.buf.MessageBytes)

Example 5 with MessageBytes

use of org.apache.tomcat.util.buf.MessageBytes in project tomcat70 by apache.

the class TestMapper method testContextListConcurrencyBug56653.

@Test
public void testContextListConcurrencyBug56653() throws Exception {
    // "localhost";
    final Object host = new Object();
    // "ROOT";
    final Object contextRoot = new Object();
    // "foo";
    final Object context1 = new Object();
    // "foo#bar";
    final Object context2 = new Object();
    // "foo#bar#bla";
    final Object context3 = new Object();
    // "foo#bar#bla#baz";
    final Object context4 = new Object();
    mapper.addHost("localhost", new String[] { "alias" }, host);
    mapper.setDefaultHostName("localhost");
    mapper.addContextVersion("localhost", host, "", "0", contextRoot, new String[0], null, null, false, false);
    mapper.addContextVersion("localhost", host, "/foo", "0", context1, new String[0], null, null, false, false);
    mapper.addContextVersion("localhost", host, "/foo/bar", "0", context2, new String[0], null, null, false, false);
    mapper.addContextVersion("localhost", host, "/foo/bar/bla", "0", context3, new String[0], null, null, false, false);
    mapper.addContextVersion("localhost", host, "/foo/bar/bla/baz", "0", context4, new String[0], null, null, false, false);
    final AtomicBoolean running = new AtomicBoolean(true);
    Thread t = new Thread() {

        @Override
        public void run() {
            for (int i = 0; i < 100000; i++) {
                mapper.removeContextVersion("localhost", "/foo/bar/bla/baz", "0");
                mapper.addContextVersion("localhost", host, "/foo/bar/bla/baz", "0", context4, new String[0], null, null, false, false);
            }
            running.set(false);
        }
    };
    MappingData mappingData = new MappingData();
    MessageBytes hostMB = MessageBytes.newInstance();
    hostMB.setString("localhost");
    MessageBytes aliasMB = MessageBytes.newInstance();
    aliasMB.setString("alias");
    MessageBytes uriMB = MessageBytes.newInstance();
    char[] uri = "/foo/bar/bla/bobou/foo".toCharArray();
    uriMB.setChars(uri, 0, uri.length);
    mapper.map(hostMB, uriMB, null, mappingData);
    Assert.assertEquals("/foo/bar/bla", mappingData.contextPath.toString());
    mappingData.recycle();
    uriMB.setChars(uri, 0, uri.length);
    mapper.map(aliasMB, uriMB, null, mappingData);
    Assert.assertEquals("/foo/bar/bla", mappingData.contextPath.toString());
    t.start();
    while (running.get()) {
        mappingData.recycle();
        uriMB.setChars(uri, 0, uri.length);
        mapper.map(hostMB, uriMB, null, mappingData);
        Assert.assertEquals("/foo/bar/bla", mappingData.contextPath.toString());
        mappingData.recycle();
        uriMB.setChars(uri, 0, uri.length);
        mapper.map(aliasMB, uriMB, null, mappingData);
        Assert.assertEquals("/foo/bar/bla", mappingData.contextPath.toString());
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MessageBytes(org.apache.tomcat.util.buf.MessageBytes) Test(org.junit.Test) LoggingBaseTest(org.apache.catalina.startup.LoggingBaseTest)

Aggregations

MessageBytes (org.apache.tomcat.util.buf.MessageBytes)73 MimeHeaders (org.apache.tomcat.util.http.MimeHeaders)15 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)14 IOException (java.io.IOException)11 Test (org.junit.Test)11 Context (org.apache.catalina.Context)10 LoggingBaseTest (org.apache.catalina.startup.LoggingBaseTest)8 Pattern (java.util.regex.Pattern)6 AbstractEndpoint (org.apache.tomcat.util.net.AbstractEndpoint)6 Principal (java.security.Principal)5 Wrapper (org.apache.catalina.Wrapper)5 CharChunk (org.apache.tomcat.util.buf.CharChunk)5 Host (org.apache.catalina.Host)4 ServletException (jakarta.servlet.ServletException)3 Cookie (jakarta.servlet.http.Cookie)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 HashSet (java.util.HashSet)3 ServletException (javax.servlet.ServletException)3 Cookie (javax.servlet.http.Cookie)3 Container (org.apache.catalina.Container)3