Package org.apache.coyote.http11.filters

# Class ChunkedInputFilter

java.lang.Object
org.apache.coyote.http11.filters.ChunkedInputFilter

All Implemented Interfaces:
  `InputFilter`, `InputBuffer`, `ApplicationBufferHandler`

public class ChunkedInputFilter extends Object implements InputFilter, ApplicationBufferHandler
Chunked input filter. Parses chunked data according to http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.6.1

Author:
  Remy Maucherat

## Field Summary

Fields
Modifier and Type
Field
Description
`protected InputBuffer`
`buffer`
Next buffer in the pipeline.
`protected static final ByteChunk`
`ENCODING`
`protected static final String`
`ENCODING_NAME`
`protected boolean`
`endChunk`
Flag set to true when the end chunk has been read.
`protected boolean`
`needCRLFParse`
Flag set to true if the next call to doRead() must parse a CRLF pair before doing anything else.
`protected ByteBuffer`
`readChunk`
Byte chunk used to read bytes.
`protected int`
`remaining`
Number of bytes remaining in the current chunk.
`protected final ByteChunk`
`trailingHeaders`
Byte chunk used to store trailing headers.

### Fields inherited from interface org.apache.tomcat.util.net.ApplicationBufferHandler

`EMPTY, EMPTY_BUFFER`

## Constructor Summary

Constructors
Constructor
Description
`ChunkedInputFilter(int maxTrailerSize, Set<String> allowedTrailerHeaders, int maxExtensionSize, int maxSwallowSize)`

## Method Summary

Modifier and Type
Method
Description
`int`
`available()`
Amount of bytes still available in a buffer.
`int`
`doRead(ApplicationBufferHandler handler)`
Read from the input stream into the ByteBuffer provided by the ApplicationBufferHandler.
`long`
`end()`
End the current request.
`void`
`expand(int size)`
`ByteBuffer`
`getByteBuffer()`
`ByteChunk`
`getEncodingName()`
Return the name of the associated encoding; Here, the value is "identity".
`boolean`
`isFinished()`
Has the request body been read fully?
`protected boolean`
`parseChunkHeader()`
Parse the header of a chunk.
`protected void`
`parseCRLF(boolean tolerant)`
Parse CRLF at end of chunk.
`protected void`
`parseEndChunk()`
Parse end chunk data.
`protected int`
`readBytes()`
Read bytes from the previous buffer.
`void`
`recycle()`
Make the filter ready to process the next request.
`void`
`setBuffer(InputBuffer buffer)`
Set the next buffer in the filter pipeline.
`void`
`setByteBuffer(ByteBuffer buffer)`
`void`
`setRequest(Request request)`
Read the content length from the request.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Field Details

### ENCODING_NAME

protected static final String ENCODING_NAME

See Also:
  - Constant Field Values

### ENCODING

protected static final ByteChunk ENCODING

### buffer

protected InputBuffer buffer
Next buffer in the pipeline.

### remaining

protected int remaining
Number of bytes remaining in the current chunk.

### readChunk

protected ByteBuffer readChunk
Byte chunk used to read bytes.

### endChunk

protected boolean endChunk
Flag set to true when the end chunk has been read.

### trailingHeaders

protected final ByteChunk trailingHeaders
Byte chunk used to store trailing headers.

### needCRLFParse

protected boolean needCRLFParse
Flag set to true if the next call to doRead() must parse a CRLF pair before doing anything else.

## Constructor Details

### ChunkedInputFilter

public ChunkedInputFilter(int maxTrailerSize, Set<String> allowedTrailerHeaders, int maxExtensionSize, int maxSwallowSize)

## Method Details

### doRead

public int doRead(ApplicationBufferHandler handler) throws IOException
Description copied from interface: `InputBuffer`
Read from the input stream into the ByteBuffer provided by the ApplicationBufferHandler. IMPORTANT: the current model assumes that the protocol will 'own' the ByteBuffer and return a pointer to it.

Specified by:
  `doRead` in interface `InputBuffer`
Parameters:
  `handler` - ApplicationBufferHandler that provides the buffer to read data into.
Returns:
  The number of bytes that have been added to the buffer or -1 for end of stream
Throws:
  `IOException` - If an I/O error occurs reading from the input stream

### setRequest

public void setRequest(Request request)
Read the content length from the request.

Specified by:
  `setRequest` in interface `InputFilter`
Parameters:
  `request` - The request to be associated with this filter

### end

public long end() throws IOException
End the current request.

Specified by:
  `end` in interface `InputFilter`
Returns:
  0 is the expected return value. A positive value indicates that too many bytes were read. This method is allowed to use buffer.doRead to consume extra bytes. The result of this method can't be negative (if an error happens, an IOException should be thrown instead).
Throws:
  `IOException` - If an error happens

### available

public int available()
Amount of bytes still available in a buffer.

Specified by:
  `available` in interface `InputBuffer`
Returns:
  The number of bytes that can be read without blocking

### setBuffer

public void setBuffer(InputBuffer buffer)
Set the next buffer in the filter pipeline.

Specified by:
  `setBuffer` in interface `InputFilter`
Parameters:
  `buffer` - The next buffer

### recycle

public void recycle()
Make the filter ready to process the next request.

Specified by:
  `recycle` in interface `InputFilter`

### getEncodingName

public ByteChunk getEncodingName()
Return the name of the associated encoding; Here, the value is "identity".

Specified by:
  `getEncodingName` in interface `InputFilter`
Returns:
  The encoding name as a byte chunk to facilitate comparison with the value read from the HTTP headers which will also be a ByteChunk

### isFinished

public boolean isFinished()
Description copied from interface: `InputFilter`
Has the request body been read fully?

Specified by:
  `isFinished` in interface `InputFilter`
Returns:
  `true` if the request body has been fully read, otherwise `false`

### readBytes

protected int readBytes() throws IOException
Read bytes from the previous buffer.

Returns:
  The byte count which has been read
Throws:
  `IOException` - Read error

### parseChunkHeader

protected boolean parseChunkHeader() throws IOException
Parse the header of a chunk. A chunk header can look like one of the following:
A10CRLF
F23;chunk-extension to be ignoredCRLF

The letters before CRLF or ';' (whatever comes first) must be valid hex digits. We should not parse F23IAMGONNAMESSTHISUP34CRLF as a valid header according to the spec.

Returns:
  `true` if the chunk header has been successfully parsed
Throws:
  `IOException` - Read error

### parseCRLF

protected void parseCRLF(boolean tolerant) throws IOException
Parse CRLF at end of chunk.

Parameters:
  `tolerant` - Should tolerant parsing (LF and CRLF) be used? This is recommended (RFC2616, section 19.3) for message headers.
Throws:
  `IOException` - An error occurred parsing CRLF

### parseEndChunk

protected void parseEndChunk() throws IOException
Parse end chunk data.

Throws:
  `IOException` - Error propagation

### setByteBuffer

public void setByteBuffer(ByteBuffer buffer)

Specified by:
  `setByteBuffer` in interface `ApplicationBufferHandler`

### getByteBuffer

public ByteBuffer getByteBuffer()

Specified by:
  `getByteBuffer` in interface `ApplicationBufferHandler`

### expand

public void expand(int size)

Specified by:
  `expand` in interface `ApplicationBufferHandler`