Search in sources :

Example 1 with MimeHeaders

use of org.apache.tomcat.util.http.MimeHeaders in project tomcat by apache.

the class FormAuthenticator method restoreRequest.

/**
     * Restore the original request from information stored in our session.
     * If the original request is no longer present (because the session
     * timed out), return <code>false</code>; otherwise, return
     * <code>true</code>.
     *
     * @param request The request to be restored
     * @param session The session containing the saved information
     * @return <code>true</code> if the request was successfully restored
     * @throws IOException if an IO error occurred during the process
     */
protected boolean restoreRequest(Request request, Session session) throws IOException {
    // Retrieve and remove the SavedRequest object from our session
    SavedRequest saved = (SavedRequest) session.getNote(Constants.FORM_REQUEST_NOTE);
    session.removeNote(Constants.FORM_REQUEST_NOTE);
    session.removeNote(Constants.FORM_PRINCIPAL_NOTE);
    if (saved == null) {
        return false;
    }
    // Swallow any request body since we will be replacing it
    // Need to do this before headers are restored as AJP connector uses
    // content length header to determine how much data needs to be read for
    // request body
    byte[] buffer = new byte[4096];
    InputStream is = request.createInputStream();
    while (is.read(buffer) >= 0) {
    // Ignore request body
    }
    // Modify our current request to reflect the original one
    request.clearCookies();
    Iterator<Cookie> cookies = saved.getCookies();
    while (cookies.hasNext()) {
        request.addCookie(cookies.next());
    }
    String method = saved.getMethod();
    MimeHeaders rmh = request.getCoyoteRequest().getMimeHeaders();
    rmh.recycle();
    boolean cacheable = "GET".equalsIgnoreCase(method) || "HEAD".equalsIgnoreCase(method);
    Iterator<String> names = saved.getHeaderNames();
    while (names.hasNext()) {
        String name = names.next();
        // BZ 43687
        if (!("If-Modified-Since".equalsIgnoreCase(name) || (cacheable && "If-None-Match".equalsIgnoreCase(name)))) {
            Iterator<String> values = saved.getHeaderValues(name);
            while (values.hasNext()) {
                rmh.addValue(name).setString(values.next());
            }
        }
    }
    request.clearLocales();
    Iterator<Locale> locales = saved.getLocales();
    while (locales.hasNext()) {
        request.addLocale(locales.next());
    }
    request.getCoyoteRequest().getParameters().recycle();
    request.getCoyoteRequest().getParameters().setQueryStringEncoding(request.getConnector().getURIEncoding());
    ByteChunk body = saved.getBody();
    if (body != null) {
        request.getCoyoteRequest().action(ActionCode.REQ_SET_BODY_REPLAY, body);
        // Set content type
        MessageBytes contentType = MessageBytes.newInstance();
        // If no content type specified, use default for POST
        String savedContentType = saved.getContentType();
        if (savedContentType == null && "POST".equalsIgnoreCase(method)) {
            savedContentType = "application/x-www-form-urlencoded";
        }
        contentType.setString(savedContentType);
        request.getCoyoteRequest().setContentType(contentType);
    }
    request.getCoyoteRequest().method().setString(method);
    return true;
}
Also used : Cookie(javax.servlet.http.Cookie) Locale(java.util.Locale) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) InputStream(java.io.InputStream) MessageBytes(org.apache.tomcat.util.buf.MessageBytes) MimeHeaders(org.apache.tomcat.util.http.MimeHeaders)

Example 2 with MimeHeaders

use of org.apache.tomcat.util.http.MimeHeaders in project tomcat by apache.

the class Http2TestBase method buildSimpleGetRequestPart1.

protected void buildSimpleGetRequestPart1(byte[] frameHeader, ByteBuffer headersPayload, List<Header> headers, int streamId) {
    MimeHeaders mimeHeaders = new MimeHeaders();
    for (Header header : headers) {
        mimeHeaders.addValue(header.getName()).setString(header.getValue());
    }
    hpackEncoder.encode(mimeHeaders, headersPayload);
    headersPayload.flip();
    ByteUtil.setThreeBytes(frameHeader, 0, headersPayload.limit());
    frameHeader[3] = FrameType.HEADERS.getIdByte();
    // Flags. end of stream (0x01)
    frameHeader[4] = 0x01;
    // Stream id
    ByteUtil.set31Bits(frameHeader, 5, streamId);
}
Also used : MimeHeaders(org.apache.tomcat.util.http.MimeHeaders)

Example 3 with MimeHeaders

use of org.apache.tomcat.util.http.MimeHeaders in project tomcat by apache.

the class Http2TestBase method buildPostRequest.

protected void buildPostRequest(byte[] headersFrameHeader, ByteBuffer headersPayload, boolean useExpectation, byte[] dataFrameHeader, ByteBuffer dataPayload, byte[] padding, byte[] trailersFrameHeader, ByteBuffer trailersPayload, int streamId) {
    MimeHeaders headers = new MimeHeaders();
    headers.addValue(":method").setString("POST");
    headers.addValue(":path").setString("/simple");
    headers.addValue(":authority").setString("localhost:" + getPort());
    if (useExpectation) {
        headers.addValue("expect").setString("100-continue");
    }
    hpackEncoder.encode(headers, headersPayload);
    headersPayload.flip();
    ByteUtil.setThreeBytes(headersFrameHeader, 0, headersPayload.limit());
    headersFrameHeader[3] = FrameType.HEADERS.getIdByte();
    // Flags. end of headers (0x04)
    headersFrameHeader[4] = 0x04;
    // Stream id
    ByteUtil.set31Bits(headersFrameHeader, 5, streamId);
    // Data
    if (padding != null) {
        dataPayload.put((byte) (padding.length & 0xFF));
        dataPayload.limit(dataPayload.capacity() - padding.length);
    }
    while (dataPayload.hasRemaining()) {
        dataPayload.put((byte) 'x');
    }
    if (padding != null && padding.length > 0) {
        dataPayload.limit(dataPayload.capacity());
        dataPayload.put(padding);
    }
    dataPayload.flip();
    // Size
    ByteUtil.setThreeBytes(dataFrameHeader, 0, dataPayload.limit());
    // Flags: End of stream 1, Padding 8
    if (trailersPayload == null) {
        dataFrameHeader[4] = 0x01;
    } else {
        dataFrameHeader[4] = 0x00;
    }
    if (padding != null) {
        dataFrameHeader[4] += 0x08;
    }
    ByteUtil.set31Bits(dataFrameHeader, 5, streamId);
    // Trailers
    if (trailersPayload != null) {
        MimeHeaders trailerHeaders = new MimeHeaders();
        trailerHeaders.addValue(TRAILER_HEADER_NAME).setString(TRAILER_HEADER_VALUE);
        hpackEncoder.encode(trailerHeaders, trailersPayload);
        trailersPayload.flip();
        ByteUtil.setThreeBytes(trailersFrameHeader, 0, trailersPayload.limit());
        trailersFrameHeader[3] = FrameType.HEADERS.getIdByte();
        // Flags. end of headers (0x04) and end of stream (0x01)
        trailersFrameHeader[4] = 0x05;
        // Stream id
        ByteUtil.set31Bits(trailersFrameHeader, 5, streamId);
    }
}
Also used : MimeHeaders(org.apache.tomcat.util.http.MimeHeaders)

Example 4 with MimeHeaders

use of org.apache.tomcat.util.http.MimeHeaders in project tomcat by apache.

the class Http2TestBase method buildSimpleGetRequestPart2.

protected void buildSimpleGetRequestPart2(byte[] frameHeader, ByteBuffer headersPayload, List<Header> headers, int streamId) {
    MimeHeaders mimeHeaders = new MimeHeaders();
    for (Header header : headers) {
        mimeHeaders.addValue(header.getName()).setString(header.getValue());
    }
    hpackEncoder.encode(mimeHeaders, headersPayload);
    headersPayload.flip();
    ByteUtil.setThreeBytes(frameHeader, 0, headersPayload.limit());
    frameHeader[3] = FrameType.CONTINUATION.getIdByte();
    // Flags. end of headers (0x04)
    frameHeader[4] = 0x04;
    // Stream id
    ByteUtil.set31Bits(frameHeader, 5, streamId);
}
Also used : MimeHeaders(org.apache.tomcat.util.http.MimeHeaders)

Example 5 with MimeHeaders

use of org.apache.tomcat.util.http.MimeHeaders in project tomcat by apache.

the class TestHpack method testEncode.

@Test
public void testEncode() throws Exception {
    MimeHeaders headers = new MimeHeaders();
    headers.setValue("header1").setString("value1");
    headers.setValue(":status").setString("200");
    headers.setValue("header2").setString("value2");
    ByteBuffer output = ByteBuffer.allocate(512);
    HpackEncoder encoder = new HpackEncoder();
    encoder.encode(headers, output);
    output.flip();
    // Size is supposed to be 33 without huffman, or 27 with it
    // TODO: use the HpackHeaderFunction to enable huffman predictably
    Assert.assertEquals(27, output.remaining());
    output.clear();
    encoder.encode(headers, output);
    output.flip();
    // Size is now 3 after using the table
    Assert.assertEquals(3, output.remaining());
}
Also used : MimeHeaders(org.apache.tomcat.util.http.MimeHeaders) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

MimeHeaders (org.apache.tomcat.util.http.MimeHeaders)18 MessageBytes (org.apache.tomcat.util.buf.MessageBytes)6 ByteBuffer (java.nio.ByteBuffer)3 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)3 Test (org.junit.Test)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 Locale (java.util.Locale)1 Pattern (java.util.regex.Pattern)1 Cookie (javax.servlet.http.Cookie)1 BufferedInputFilter (org.apache.coyote.http11.filters.BufferedInputFilter)1 ChunkedInputFilter (org.apache.coyote.http11.filters.ChunkedInputFilter)1 ChunkedOutputFilter (org.apache.coyote.http11.filters.ChunkedOutputFilter)1 GzipOutputFilter (org.apache.coyote.http11.filters.GzipOutputFilter)1 IdentityInputFilter (org.apache.coyote.http11.filters.IdentityInputFilter)1 IdentityOutputFilter (org.apache.coyote.http11.filters.IdentityOutputFilter)1 SavedRequestInputFilter (org.apache.coyote.http11.filters.SavedRequestInputFilter)1 VoidInputFilter (org.apache.coyote.http11.filters.VoidInputFilter)1