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

The following examples show how to use org.apache.tomcat.util.buf.ByteChunk#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: AbstractAjpProcessor.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Read bytes into the specified chunk.
 */
@Override
public int doRead(ByteChunk chunk, Request req)
throws IOException {

    if (endOfStream) {
        return -1;
    }
    if (first && req.getContentLengthLong() > 0) {
        // Handle special first-body-chunk
        if (!receive()) {
            return 0;
        }
    } else if (empty) {
        if (!refillReadBuffer()) {
            return -1;
        }
    }
    ByteChunk bc = bodyBytes.getByteChunk();
    chunk.setBytes(bc.getBuffer(), bc.getStart(), bc.getLength());
    empty = true;
    return chunk.getLength();

}
 
Example 2
Source File: AbstractAjpProcessor.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * Read bytes into the specified chunk.
 */
@Override
public int doRead(ByteChunk chunk, Request req)
throws IOException {

    if (endOfStream) {
        return -1;
    }
    if (first && req.getContentLengthLong() > 0) {
        // Handle special first-body-chunk
        if (!receive()) {
            return 0;
        }
    } else if (empty) {
        if (!refillReadBuffer()) {
            return -1;
        }
    }
    ByteChunk bc = bodyBytes.getByteChunk();
    chunk.setBytes(bc.getBuffer(), bc.getStart(), bc.getLength());
    empty = true;
    return chunk.getLength();

}
 
Example 3
Source File: InternalInputBuffer.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Read bytes into the specified chunk.
 */
@Override
public int doRead(ByteChunk chunk, Request req ) 
    throws IOException {

    if (pos >= lastValid) {
        if (!fill())
            return -1;
    }

    int length = lastValid - pos;
    chunk.setBytes(buf, pos, length);
    pos = lastValid;

    return (length);
}
 
Example 4
Source File: InternalAprInputBuffer.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Read bytes into the specified chunk.
 */
@Override
public int doRead(ByteChunk chunk, Request req ) 
    throws IOException {

    if (pos >= lastValid) {
        if (!fill())
            return -1;
    }

    int length = lastValid - pos;
    chunk.setBytes(buf, pos, length);
    pos = lastValid;

    return (length);
}
 
Example 5
Source File: InternalNioInputBuffer.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * Read bytes into the specified chunk.
 */
@Override
public int doRead(ByteChunk chunk, Request req )
    throws IOException {

    if (pos >= lastValid) {
        if (!fill(true,true)) //read body, must be blocking, as the thread is inside the app
            return -1;
    }

    int length = lastValid - pos;
    chunk.setBytes(buf, pos, length);
    pos = lastValid;

    return (length);
}
 
Example 6
Source File: TestBasicAuthParser.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
private void prefix(String method) {
    authHeader = new ByteChunk();
    authHeader.setBytes(HEADER, 0, HEADER.length);
    initialOffset = HEADER.length;

    String methodX = method + " ";
    byte[] methodBytes = methodX.getBytes(StandardCharsets.ISO_8859_1);

    try {
        authHeader.append(methodBytes, 0, methodBytes.length);
    }
    catch (IOException ioe) {
        throw new IllegalStateException("unable to extend ByteChunk:"
                + ioe.getMessage());
    }
}
 
Example 7
Source File: InternalAprInputBuffer.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * Read bytes into the specified chunk.
 */
@Override
public int doRead(ByteChunk chunk, Request req )
    throws IOException {

    if (pos >= lastValid) {
        if (!fill())
            return -1;
    }

    int length = lastValid - pos;
    chunk.setBytes(buf, pos, length);
    pos = lastValid;

    return (length);
}
 
Example 8
Source File: Http11InputBuffer.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 *
 * @deprecated Unused. Will be removed in Tomcat 9. Use
 *             {@link #doRead(ApplicationBufferHandler)}
 */
@Deprecated
@Override
public int doRead(ByteChunk chunk) throws IOException {

    if (byteBuffer.position() >= byteBuffer.limit()) {
        // The application is reading the HTTP request body which is
        // always a blocking operation.
        /**
         * 读取HttpBody的时候是会阻塞的。
         */
        if (!fill(true))
            return -1;
    }

    int length = byteBuffer.remaining();
    chunk.setBytes(byteBuffer.array(), byteBuffer.position(), length);
    byteBuffer.position(byteBuffer.limit());

    return length;
}
 
Example 9
Source File: BufferedInputFilter.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Fills the given ByteChunk with the buffered request body.
 *
 * @deprecated Unused. Will be removed in Tomcat 9. Use
 *             {@link #doRead(ApplicationBufferHandler)}
 */
@Deprecated
@Override
public int doRead(ByteChunk chunk) throws IOException {
    if (isFinished()) {
        return -1;
    }

    chunk.setBytes(buffered.array(), buffered.arrayOffset() + buffered.position(),
            buffered.remaining());
    hasRead = true;
    return chunk.getLength();
}
 
Example 10
Source File: LegacyCookieProcessor.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Unescapes any double quotes in the given cookie value.
 *
 * @param bc The cookie value to modify
 */
private static final void unescapeDoubleQuotes(ByteChunk bc) {

    if (bc == null || bc.getLength() == 0 || bc.indexOf('"', 0) == -1) {
        return;
    }

    // Take a copy of the buffer so the original cookie header is not
    // modified by this unescaping.
    byte[] original = bc.getBuffer();
    int len = bc.getLength();

    byte[] copy = new byte[len];
    System.arraycopy(original, bc.getStart(), copy, 0, len);

    int src = 0;
    int dest = 0;

    while (src < len) {
        if (copy[src] == '\\' && src < len && copy[src+1]  == '"') {
            src++;
        }
        copy[dest] = copy[src];
        dest ++;
        src ++;
    }
    bc.setBytes(copy, 0, dest);
}
 
Example 11
Source File: IdentityOutputFilter.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * @deprecated Unused. Will be removed in Tomcat 9. Use
 *             {@link #doWrite(ByteBuffer)}
 */
@Deprecated
@Override
public int doWrite(ByteChunk chunk) throws IOException {

    int result = -1;

    if (contentLength >= 0) {
        if (remaining > 0) {
            result = chunk.getLength();
            if (result > remaining) {
                // The chunk is longer than the number of bytes remaining
                // in the body; changing the chunk length to the number
                // of bytes remaining
                chunk.setBytes(chunk.getBytes(), chunk.getStart(),
                               (int) remaining);
                result = (int) remaining;
                remaining = 0;
            } else {
                remaining = remaining - result;
            }
            buffer.doWrite(chunk);
        } else {
            // No more bytes left to be written : return -1 and clear the
            // buffer
            chunk.recycle();
            result = -1;
        }
    } else {
        // If no content length was set, just write the bytes
        buffer.doWrite(chunk);
        result = chunk.getLength();
    }

    return result;

}
 
Example 12
Source File: IdentityInputFilter.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * @deprecated Unused. Will be removed in Tomcat 9. Use
 *             {@link #doRead(ApplicationBufferHandler)}
 */
@Deprecated
@Override
public int doRead(ByteChunk chunk) throws IOException {

    int result = -1;

    if (contentLength >= 0) {
        if (remaining > 0) {
            int nRead = buffer.doRead(chunk);
            if (nRead > remaining) {
                // The chunk is longer than the number of bytes remaining
                // in the body; changing the chunk length to the number
                // of bytes remaining
                chunk.setBytes(chunk.getBytes(), chunk.getStart(),
                               (int) remaining);
                result = (int) remaining;
            } else {
                result = nRead;
            }
            if (nRead > 0) {
                remaining = remaining - nRead;
            }
        } else {
            // No more bytes left to be read : return -1 and clear the
            // buffer
            chunk.recycle();
            result = -1;
        }
    }

    return result;

}
 
Example 13
Source File: Tomcat90InputBuffer.java    From armeria with Apache License 2.0 5 votes vote down vote up
public int doRead(ByteChunk chunk) {
    if (!isNeedToRead()) {
        // Read only once.
        return -1;
    }

    read = true;

    final int readableBytes = content.length();
    chunk.setBytes(content.array(), 0, readableBytes);

    return readableBytes;
}
 
Example 14
Source File: IdentityInputFilter.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Read bytes.
 * 
 * @return If the filter does request length control, this value is
 * significant; it should be the number of bytes consumed from the buffer,
 * up until the end of the current request body, or the buffer length, 
 * whichever is greater. If the filter does not do request body length
 * control, the returned value should be -1.
 */
@Override
public int doRead(ByteChunk chunk, Request req)
    throws IOException {

    int result = -1;

    if (contentLength >= 0) {
        if (remaining > 0) {
            int nRead = buffer.doRead(chunk, req);
            if (nRead > remaining) {
                // The chunk is longer than the number of bytes remaining
                // in the body; changing the chunk length to the number
                // of bytes remaining
                chunk.setBytes(chunk.getBytes(), chunk.getStart(), 
                               (int) remaining);
                result = (int) remaining;
            } else {
                result = nRead;
            }
            if (nRead > 0) {
                remaining = remaining - nRead;
            }
        } else {
            // No more bytes left to be read : return -1 and clear the 
            // buffer
            chunk.recycle();
            result = -1;
        }
    }

    return result;

}
 
Example 15
Source File: IdentityOutputFilter.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Write some bytes.
 * 
 * @return number of bytes written by the filter
 */
@Override
public int doWrite(ByteChunk chunk, Response res)
    throws IOException {

    int result = -1;

    if (contentLength >= 0) {
        if (remaining > 0) {
            result = chunk.getLength();
            if (result > remaining) {
                // The chunk is longer than the number of bytes remaining
                // in the body; changing the chunk length to the number
                // of bytes remaining
                chunk.setBytes(chunk.getBytes(), chunk.getStart(), 
                               (int) remaining);
                result = (int) remaining;
                remaining = 0;
            } else {
                remaining = remaining - result;
            }
            buffer.doWrite(chunk, res);
        } else {
            // No more bytes left to be written : return -1 and clear the 
            // buffer
            chunk.recycle();
            result = -1;
        }
    } else {
        // If no content length was set, just write the bytes
        buffer.doWrite(chunk, res);
        result = chunk.getLength();
    }

    return result;

}
 
Example 16
Source File: IdentityInputFilter.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Read bytes.
 * 
 * @return If the filter does request length control, this value is
 * significant; it should be the number of bytes consumed from the buffer,
 * up until the end of the current request body, or the buffer length, 
 * whichever is greater. If the filter does not do request body length
 * control, the returned value should be -1.
 */
@Override
public int doRead(ByteChunk chunk, Request req)
    throws IOException {

    int result = -1;

    if (contentLength >= 0) {
        if (remaining > 0) {
            int nRead = buffer.doRead(chunk, req);
            if (nRead > remaining) {
                // The chunk is longer than the number of bytes remaining
                // in the body; changing the chunk length to the number
                // of bytes remaining
                chunk.setBytes(chunk.getBytes(), chunk.getStart(), 
                               (int) remaining);
                result = (int) remaining;
            } else {
                result = nRead;
            }
            if (nRead > 0) {
                remaining = remaining - nRead;
            }
        } else {
            // No more bytes left to be read : return -1 and clear the 
            // buffer
            chunk.recycle();
            result = -1;
        }
    }

    return result;

}
 
Example 17
Source File: BufferedInputFilter.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Fills the given ByteChunk with the buffered request body.
 */
@Override
public int doRead(ByteChunk chunk, Request request) throws IOException {
    if (hasRead || buffered.getLength() <= 0) {
        return -1;
    }

    chunk.setBytes(buffered.getBytes(), buffered.getStart(),
            buffered.getLength());
    hasRead = true;
    return chunk.getLength();
}
 
Example 18
Source File: CoyoteAdapter.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
/**
 * Extract the path parameters from the request. This assumes parameters are
 * of the form /path;name=value;name2=value2/ etc. Currently only really
 * interested in the session ID that will be in this form. Other parameters
 * can safely be ignored.
 *
 * @param req
 * @param request
 */
protected void parsePathParameters(org.apache.coyote.Request req,
        Request request) {

    // Process in bytes (this is default format so this is normally a NO-OP
    req.decodedURI().toBytes();

    ByteChunk uriBC = req.decodedURI().getByteChunk();
    int semicolon = uriBC.indexOf(';', 0);

    // What encoding to use? Some platforms, eg z/os, use a default
    // encoding that doesn't give the expected result so be explicit
    String enc = connector.getURIEncoding();
    if (enc == null) {
        enc = "ISO-8859-1";
    }
    Charset charset = null;
    try {
        charset = B2CConverter.getCharset(enc);
    } catch (UnsupportedEncodingException e1) {
        log.warn(sm.getString("coyoteAdapter.parsePathParam",
                enc));
    }

    if (log.isDebugEnabled()) {
        log.debug(sm.getString("coyoteAdapter.debug", "uriBC",
                uriBC.toString()));
        log.debug(sm.getString("coyoteAdapter.debug", "semicolon",
                String.valueOf(semicolon)));
        log.debug(sm.getString("coyoteAdapter.debug", "enc", enc));
    }

    while (semicolon > -1) {
        // Parse path param, and extract it from the decoded request URI
        int start = uriBC.getStart();
        int end = uriBC.getEnd();

        int pathParamStart = semicolon + 1;
        int pathParamEnd = ByteChunk.findBytes(uriBC.getBuffer(),
                start + pathParamStart, end,
                new byte[] {';', '/'});

        String pv = null;

        if (pathParamEnd >= 0) {
            if (charset != null) {
                pv = new String(uriBC.getBuffer(), start + pathParamStart,
                            pathParamEnd - pathParamStart, charset);
            }
            // Extract path param from decoded request URI
            byte[] buf = uriBC.getBuffer();
            for (int i = 0; i < end - start - pathParamEnd; i++) {
                buf[start + semicolon + i]
                    = buf[start + i + pathParamEnd];
            }
            uriBC.setBytes(buf, start,
                    end - start - pathParamEnd + semicolon);
        } else {
            if (charset != null) {
                pv = new String(uriBC.getBuffer(), start + pathParamStart,
                            (end - start) - pathParamStart, charset);
            }
            uriBC.setEnd(start + semicolon);
        }

        if (log.isDebugEnabled()) {
            log.debug(sm.getString("coyoteAdapter.debug", "pathParamStart",
                    String.valueOf(pathParamStart)));
            log.debug(sm.getString("coyoteAdapter.debug", "pathParamEnd",
                    String.valueOf(pathParamEnd)));
            log.debug(sm.getString("coyoteAdapter.debug", "pv", pv));
        }

        if (pv != null) {
            int equals = pv.indexOf('=');
            if (equals > -1) {
                String name = pv.substring(0, equals);
                String value = pv.substring(equals + 1);
                request.addPathParameter(name, value);
                if (log.isDebugEnabled()) {
                    log.debug(sm.getString("coyoteAdapter.debug", "equals",
                            String.valueOf(equals)));
                    log.debug(sm.getString("coyoteAdapter.debug", "name",
                            name));
                    log.debug(sm.getString("coyoteAdapter.debug", "value",
                            value));
                }
            }
        }

        semicolon = uriBC.indexOf(';', semicolon);
    }
}
 
Example 19
Source File: CoyoteAdapter.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
 * Extract the path parameters from the request. This assumes parameters are
 * of the form /path;name=value;name2=value2/ etc. Currently only really
 * interested in the session ID that will be in this form. Other parameters
 * can safely be ignored.
 *
 * @param req
 * @param request
 */
protected void parsePathParameters(org.apache.coyote.Request req,
        Request request) {

    // Process in bytes (this is default format so this is normally a NO-OP
    req.decodedURI().toBytes();

    ByteChunk uriBC = req.decodedURI().getByteChunk();
    int semicolon = uriBC.indexOf(';', 0);

    // What encoding to use? Some platforms, eg z/os, use a default
    // encoding that doesn't give the expected result so be explicit
    String enc = connector.getURIEncoding();
    if (enc == null) {
        enc = "ISO-8859-1";
    }
    Charset charset = null;
    try {
        charset = B2CConverter.getCharset(enc);
    } catch (UnsupportedEncodingException e1) {
        log.warn(sm.getString("coyoteAdapter.parsePathParam",
                enc));
    }

    if (log.isDebugEnabled()) {
        log.debug(sm.getString("coyoteAdapter.debug", "uriBC",
                uriBC.toString()));
        log.debug(sm.getString("coyoteAdapter.debug", "semicolon",
                String.valueOf(semicolon)));
        log.debug(sm.getString("coyoteAdapter.debug", "enc", enc));
    }

    while (semicolon > -1) {
        // Parse path param, and extract it from the decoded request URI
        int start = uriBC.getStart();
        int end = uriBC.getEnd();

        int pathParamStart = semicolon + 1;
        int pathParamEnd = ByteChunk.findBytes(uriBC.getBuffer(),
                start + pathParamStart, end,
                new byte[] {';', '/'});

        String pv = null;

        if (pathParamEnd >= 0) {
            if (charset != null) {
                pv = new String(uriBC.getBuffer(), start + pathParamStart,
                            pathParamEnd - pathParamStart, charset);
            }
            // Extract path param from decoded request URI
            byte[] buf = uriBC.getBuffer();
            for (int i = 0; i < end - start - pathParamEnd; i++) {
                buf[start + semicolon + i]
                    = buf[start + i + pathParamEnd];
            }
            uriBC.setBytes(buf, start,
                    end - start - pathParamEnd + semicolon);
        } else {
            if (charset != null) {
                pv = new String(uriBC.getBuffer(), start + pathParamStart,
                            (end - start) - pathParamStart, charset);
            }
            uriBC.setEnd(start + semicolon);
        }

        if (log.isDebugEnabled()) {
            log.debug(sm.getString("coyoteAdapter.debug", "pathParamStart",
                    String.valueOf(pathParamStart)));
            log.debug(sm.getString("coyoteAdapter.debug", "pathParamEnd",
                    String.valueOf(pathParamEnd)));
            log.debug(sm.getString("coyoteAdapter.debug", "pv", pv));
        }

        if (pv != null) {
            int equals = pv.indexOf('=');
            if (equals > -1) {
                String name = pv.substring(0, equals);
                String value = pv.substring(equals + 1);
                request.addPathParameter(name, value);
                if (log.isDebugEnabled()) {
                    log.debug(sm.getString("coyoteAdapter.debug", "equals",
                            String.valueOf(equals)));
                    log.debug(sm.getString("coyoteAdapter.debug", "name",
                            name));
                    log.debug(sm.getString("coyoteAdapter.debug", "value",
                            value));
                }
            }
        }

        semicolon = uriBC.indexOf(';', semicolon);
    }
}
 
Example 20
Source File: Stream.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
/**
 * @deprecated Unused. Will be removed in Tomcat 9. Use
 *             {@link #doRead(ApplicationBufferHandler)}
 */
@Deprecated
@Override
public int doRead(ByteChunk chunk) throws IOException {

    ensureBuffersExist();

    int written = -1;

    // Ensure that only one thread accesses inBuffer at a time
    synchronized (inBuffer) {
        boolean canRead = false;
        while (inBuffer.position() == 0 && (canRead = isActive() && !isInputFinished())) {
            // Need to block until some data is written
            try {
                if (log.isDebugEnabled()) {
                    log.debug(sm.getString("stream.inputBuffer.empty"));
                }

                long readTimeout = handler.getProtocol().getStreamReadTimeout();
                if (readTimeout < 0) {
                    inBuffer.wait();
                } else {
                    inBuffer.wait(readTimeout);
                }

                if (resetReceived) {
                    throw new IOException(sm.getString("stream.inputBuffer.reset"));
                }

                if (inBuffer.position() == 0) {
                    String msg = sm.getString("stream.inputBuffer.readTimeout");
                    StreamException se = new StreamException(
                            msg, Http2Error.ENHANCE_YOUR_CALM, getIdAsInt());
                    // Trigger a reset once control returns to Tomcat
                    coyoteResponse.setError();
                    streamOutputBuffer.reset = se;
                    throw new CloseNowException(msg, se);
                }
            } catch (InterruptedException e) {
                // Possible shutdown / rst or similar. Use an
                // IOException to signal to the client that further I/O
                // isn't possible for this Stream.
                throw new IOException(e);
            }
        }

        if (inBuffer.position() > 0) {
            // Data is available in the inBuffer. Copy it to the
            // outBuffer.
            inBuffer.flip();
            written = inBuffer.remaining();
            if (log.isDebugEnabled()) {
                log.debug(sm.getString("stream.inputBuffer.copy",
                        Integer.toString(written)));
            }
            inBuffer.get(outBuffer, 0, written);
            inBuffer.clear();
        } else if (!canRead) {
            return -1;
        } else {
            // Should never happen
            throw new IllegalStateException();
        }
    }

    chunk.setBytes(outBuffer, 0,  written);

    // Increment client-side flow control windows by the number of bytes
    // read
    handler.writeWindowUpdate(Stream.this, written, true);

    return written;
}