Java Code Examples for org.apache.tomcat.util.buf.MessageBytes#setBytes()

The following examples show how to use org.apache.tomcat.util.buf.MessageBytes#setBytes() . 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: AjpMessage.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
private void doGetBytes(MessageBytes mb, boolean terminated) {
    int length = getInt();
    if ((length == 0xFFFF) || (length == -1)) {
        mb.recycle();
        return;
    }
    if (terminated) {
        validatePos(pos + length + 1);
    } else {
        validatePos(pos + length);
    }
    mb.setBytes(buf, pos, length);
    mb.getCharChunk().recycle(); // not valid anymore
    pos += length;
    if (terminated) {
        pos++; // Skip the terminating \0
    }
}
 
Example 2
Source File: TestLegacyCookieProcessor.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testV0WithPath() {
    LegacyCookieProcessor cp = new LegacyCookieProcessor();
    cp.setAllowHttpSepsInV0(true);
    cp.setForwardSlashIsSeparator(true);

    MimeHeaders mimeHeaders = new MimeHeaders();
    ServerCookies serverCookies = new ServerCookies(4);

    MessageBytes cookieHeaderValue = mimeHeaders.addValue("Cookie");
    byte[] bytes = "$Version=0;cname=cvalue;$Path=/example".getBytes(StandardCharsets.UTF_8);
    cookieHeaderValue.setBytes(bytes, 0, bytes.length);
    cp.parseCookieHeader(mimeHeaders, serverCookies);
    Assert.assertEquals(1, serverCookies.getCookieCount());
    for (int i = 0; i < 1; i++) {
        ServerCookie actual = serverCookies.getCookie(i);
        Assert.assertEquals(0, actual.getVersion());
        Assert.assertEquals("cname", actual.getName().toString());
        actual.getValue().getByteChunk().setCharset(StandardCharsets.UTF_8);
        Assert.assertEquals("cvalue",
                org.apache.tomcat.util.http.parser.Cookie.unescapeCookieValueRfc2109(
                        actual.getValue().toString()));
        Assert.assertEquals("/example", actual.getPath().toString());
    }
}
 
Example 3
Source File: AjpMessage.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
private void doGetBytes(MessageBytes mb, boolean terminated) {
    int length = getInt();
    if ((length == 0xFFFF) || (length == -1)) {
        mb.recycle();
        return;
    }
    if (terminated) {
        validatePos(pos + length + 1);
    } else {
        validatePos(pos + length);
    }
    mb.setBytes(buf, pos, length);
    mb.getCharChunk().recycle(); // not valid anymore
    pos += length;
    if (terminated) {
        pos++; // Skip the terminating \0
    }
}
 
Example 4
Source File: AjpMessage.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
private void doGetBytes(MessageBytes mb, boolean terminated) {
    int length = getInt();
    if ((length == 0xFFFF) || (length == -1)) {
        mb.recycle();
        return;
    }
    if (terminated) {
        validatePos(pos + length + 1);
    } else {
        validatePos(pos + length);
    }
    mb.setBytes(buf, pos, length);
    mb.getCharChunk().recycle(); // not valid anymore
    pos += length;
    if (terminated) {
        pos++; // Skip the terminating \0
    }
}
 
Example 5
Source File: TomcatService.java    From armeria with Apache License 2.0 6 votes vote down vote up
private static void convertHeaders(HttpHeaders headers, MimeHeaders cHeaders) {
    if (headers.isEmpty()) {
        return;
    }

    for (Entry<AsciiString, String> e : headers) {
        final AsciiString k = e.getKey();
        final String v = e.getValue();

        if (k.isEmpty() || k.byteAt(0) == ':') {
            continue;
        }

        final MessageBytes cValue = cHeaders.addValue(k.array(), k.arrayOffset(), k.length());
        final byte[] valueBytes = v.getBytes(StandardCharsets.US_ASCII);
        cValue.setBytes(valueBytes, 0, valueBytes.length);
    }
}
 
Example 6
Source File: TestCoyoteAdapter.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
private void doTestNormalize(String input, String expected) {
    MessageBytes mb = MessageBytes.newInstance();
    byte[] b = input.getBytes(StandardCharsets.UTF_8);
    mb.setBytes(b, 0, b.length);

    boolean result = CoyoteAdapter.normalize(mb);
    mb.toString();

    if (expected == null) {
        Assert.assertFalse(result);
    } else {
        Assert.assertTrue(result);
        Assert.assertEquals(expected, mb.toString());
    }
}
 
Example 7
Source File: TestCookies.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
private void test(boolean useRfc6265, String header, Cookie... expected) {
    MimeHeaders mimeHeaders = new MimeHeaders();
    ServerCookies serverCookies = new ServerCookies(4);
    CookieProcessor cookieProcessor;

    if (useRfc6265) {
        cookieProcessor = new Rfc6265CookieProcessor();
    } else {
        cookieProcessor = new LegacyCookieProcessor();
    }
    MessageBytes cookieHeaderValue = mimeHeaders.addValue("Cookie");
    byte[] bytes = header.getBytes(StandardCharsets.UTF_8);
    cookieHeaderValue.setBytes(bytes, 0, bytes.length);
    cookieProcessor.parseCookieHeader(mimeHeaders, serverCookies);
    Assert.assertEquals(expected.length, serverCookies.getCookieCount());
    for (int i = 0; i < expected.length; i++) {
        Cookie cookie = expected[i];
        ServerCookie actual = serverCookies.getCookie(i);
        Assert.assertEquals(cookie.getVersion(), actual.getVersion());
        Assert.assertEquals(cookie.getName(), actual.getName().toString());
        actual.getValue().getByteChunk().setCharset(StandardCharsets.UTF_8);
        Assert.assertEquals(cookie.getValue(),
                org.apache.tomcat.util.http.parser.Cookie.unescapeCookieValueRfc2109(
                        actual.getValue().toString()));
        if (cookie.getVersion() == 1) {
            Assert.assertEquals(cookie.getDomain(), actual.getDomain().toString());
            Assert.assertEquals(cookie.getPath(), actual.getPath().toString());
        }
    }
}
 
Example 8
Source File: TesterCookiesPerformance.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Test
public void testPerformance01() throws Exception {
    final int cookieCount = 100;
    final int parsingLoops = 200000;

    MimeHeaders mimeHeaders = new MimeHeaders();

    StringBuilder cookieHeader = new StringBuilder();
    // Create cookies
    for (int i = 0; i < cookieCount; i++) {
        cookieHeader.append("name");
        cookieHeader.append(i);
        cookieHeader.append('=');
        cookieHeader.append("value");
        cookieHeader.append(i);
        cookieHeader.append(';');
    }

    byte[] cookieHeaderBytes = cookieHeader.toString().getBytes("UTF-8");

    MessageBytes headerValue = mimeHeaders.addValue("Cookie");
    headerValue.setBytes(cookieHeaderBytes, 0, cookieHeaderBytes.length);
    ServerCookies serverCookies = new ServerCookies(4);

    LegacyCookieProcessor originalCookieProcessor = new LegacyCookieProcessor();
    Rfc6265CookieProcessor rfc6265CookieProcessor = new Rfc6265CookieProcessor();

    // warm up
    for (int i = 0; i < parsingLoops; i++) {
        originalCookieProcessor.parseCookieHeader(mimeHeaders, serverCookies);
        Assert.assertEquals(cookieCount, serverCookies.getCookieCount());
        serverCookies.recycle();
    }

    long oldStart = System.nanoTime();
    for (int i = 0; i < parsingLoops; i++) {
        originalCookieProcessor.parseCookieHeader(mimeHeaders, serverCookies);
        Assert.assertEquals(cookieCount, serverCookies.getCookieCount());
        serverCookies.recycle();
    }
    long oldDuration = System.nanoTime() - oldStart;

    long newStart = System.nanoTime();
    for (int i = 0; i < parsingLoops; i++) {
        rfc6265CookieProcessor.parseCookieHeader(mimeHeaders, serverCookies);
        Assert.assertEquals(cookieCount, serverCookies.getCookieCount());
        serverCookies.recycle();
    }
    long newDuration = System.nanoTime() - newStart;

    System.out.println("Original duration: " + oldDuration);
    System.out.println("RFC6265 duration:  " + newDuration);
}