Package org.eclipse.jetty.server
Class HttpInput
- java.lang.Object
-
- java.io.InputStream
-
- javax.servlet.ServletInputStream
-
- org.eclipse.jetty.server.HttpInput
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,java.lang.Runnable
- Direct Known Subclasses:
HttpInputOverHTTP
public class HttpInput extends javax.servlet.ServletInputStream implements java.lang.Runnable
HttpInput
provides an implementation ofServletInputStream
forHttpChannel
.Content may arrive in patterns such as [content(), content(), messageComplete()] so that this class maintains two states: the content state that tells whether there is content to consume and the EOF state that tells whether an EOF has arrived. Only once the content has been consumed the content state is moved to the EOF state.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
HttpInput.ChainedInterceptor
AnHttpInput.Interceptor
that chains two otherHttpInput.Interceptor
s together.static class
HttpInput.Content
static class
HttpInput.EofContent
protected static class
HttpInput.EOFState
protected static class
HttpInput.ErrorState
static interface
HttpInput.Interceptor
An interceptor for HTTP Request input.static class
HttpInput.SentinelContent
A Sentinel Content, which has zero length content but indicates some other event in the input stream (eg EOF)protected static class
HttpInput.State
-
Field Summary
Fields Modifier and Type Field Description private long
_blockUntil
private HttpChannelState
_channelState
private HttpInput.Content
_content
private long
_contentArrived
private long
_contentConsumed
private long
_firstByteTimeStamp
private java.util.Deque<HttpInput.Content>
_inputQ
private HttpInput.Content
_intercepted
private HttpInput.Interceptor
_interceptor
private javax.servlet.ReadListener
_listener
private byte[]
_oneByteBuffer
private HttpInput.State
_state
private boolean
_waitingForContent
protected static HttpInput.State
AEOF
protected static HttpInput.State
ASYNC
protected static HttpInput.State
EARLY_EOF
(package private) static HttpInput.Content
EARLY_EOF_CONTENT
protected static HttpInput.State
EOF
(package private) static HttpInput.Content
EOF_CONTENT
private static Logger
LOG
protected static HttpInput.State
STREAM
-
Constructor Summary
Constructors Constructor Description HttpInput(HttpChannelState state)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
addContent(HttpInput.Content content)
Adds some content to this input stream.void
addInterceptor(HttpInput.Interceptor interceptor)
Set theHttpInput.Interceptor
, using aHttpInput.ChainedInterceptor
if anHttpInput.Interceptor
is already set.void
asyncReadProduce()
Called by channel when asynchronous IO needs to produce more contentint
available()
protected void
blockForContent()
Blocks until some content or some end-of-file event arrives.private void
consume(HttpInput.Content content)
boolean
consumeAll()
Consume all available content without blocking.boolean
earlyEOF()
This method should be called to signal that an EOF has been detected before all the expected content arrived.boolean
eof()
This method should be called to signal that all the expected content arrived.private java.lang.Throwable
fail(HttpInput.Content content, java.lang.Throwable failure)
boolean
failed(java.lang.Throwable x)
protected int
get(HttpInput.Content content, byte[] buffer, int offset, int length)
Copies the given content into the given byte buffer.private long
getBlockingTimeout()
long
getContentConsumed()
long
getContentReceived()
protected HttpChannelState
getHttpChannelState()
HttpInput.Interceptor
getInterceptor()
boolean
hasContent()
private HttpInput.Content
intercept(HttpInput.Content content)
boolean
isAsync()
boolean
isError()
boolean
isFinished()
boolean
isReady()
protected HttpInput.Content
nextContent()
Get the next content from the inputQ, callingproduceContent()
if need be.protected HttpInput.Content
nextInterceptedContent()
Poll the inputQ for Content or EOF.protected HttpInput.Content
nextNonSentinelContent()
Poll the inputQ for Content.boolean
onIdleTimeout(java.lang.Throwable x)
protected void
produceContent()
Called when derived implementations should attempt to produce more Content and add it viaaddContent(Content)
.protected HttpInput.Content
produceNextContent()
Get the next readable from the inputQ, callingproduceContent()
if need be.int
read()
int
read(byte[] b, int off, int len)
void
recycle()
void
run()
void
setInterceptor(HttpInput.Interceptor interceptor)
Set the interceptor.void
setReadListener(javax.servlet.ReadListener readListener)
java.lang.String
toString()
void
unblock()
protected void
wake()
private boolean
wakeup()
-
-
-
Field Detail
-
LOG
private static final Logger LOG
-
EOF_CONTENT
static final HttpInput.Content EOF_CONTENT
-
EARLY_EOF_CONTENT
static final HttpInput.Content EARLY_EOF_CONTENT
-
_oneByteBuffer
private final byte[] _oneByteBuffer
-
_content
private HttpInput.Content _content
-
_intercepted
private HttpInput.Content _intercepted
-
_inputQ
private final java.util.Deque<HttpInput.Content> _inputQ
-
_channelState
private final HttpChannelState _channelState
-
_listener
private javax.servlet.ReadListener _listener
-
_state
private HttpInput.State _state
-
_firstByteTimeStamp
private long _firstByteTimeStamp
-
_contentArrived
private long _contentArrived
-
_contentConsumed
private long _contentConsumed
-
_blockUntil
private long _blockUntil
-
_waitingForContent
private boolean _waitingForContent
-
_interceptor
private HttpInput.Interceptor _interceptor
-
STREAM
protected static final HttpInput.State STREAM
-
ASYNC
protected static final HttpInput.State ASYNC
-
EARLY_EOF
protected static final HttpInput.State EARLY_EOF
-
EOF
protected static final HttpInput.State EOF
-
AEOF
protected static final HttpInput.State AEOF
-
-
Constructor Detail
-
HttpInput
public HttpInput(HttpChannelState state)
-
-
Method Detail
-
getHttpChannelState
protected HttpChannelState getHttpChannelState()
-
recycle
public void recycle()
-
fail
private java.lang.Throwable fail(HttpInput.Content content, java.lang.Throwable failure)
-
getInterceptor
public HttpInput.Interceptor getInterceptor()
- Returns:
- The current Interceptor, or null if none set
-
setInterceptor
public void setInterceptor(HttpInput.Interceptor interceptor)
Set the interceptor.- Parameters:
interceptor
- The interceptor to use.
-
addInterceptor
public void addInterceptor(HttpInput.Interceptor interceptor)
Set theHttpInput.Interceptor
, using aHttpInput.ChainedInterceptor
if anHttpInput.Interceptor
is already set.- Parameters:
interceptor
- the nextHttpInput.Interceptor
in a chain
-
available
public int available()
- Overrides:
available
in classjava.io.InputStream
-
wake
protected void wake()
-
getBlockingTimeout
private long getBlockingTimeout()
-
read
public int read() throws java.io.IOException
- Specified by:
read
in classjava.io.InputStream
- Throws:
java.io.IOException
-
read
public int read(byte[] b, int off, int len) throws java.io.IOException
- Overrides:
read
in classjava.io.InputStream
- Throws:
java.io.IOException
-
produceContent
protected void produceContent() throws java.io.IOException
Called when derived implementations should attempt to produce more Content and add it viaaddContent(Content)
. For protocols that are constantly producing (eg HTTP2) this can be left as a noop;- Throws:
java.io.IOException
- if unable to produce content
-
asyncReadProduce
public void asyncReadProduce() throws java.io.IOException
Called by channel when asynchronous IO needs to produce more content- Throws:
java.io.IOException
- if unable to produce content
-
nextContent
protected HttpInput.Content nextContent() throws java.io.IOException
Get the next content from the inputQ, callingproduceContent()
if need be. EOF is processed and state changed.- Returns:
- the content or null if none available.
- Throws:
java.io.IOException
- if retrieving the content fails
-
nextNonSentinelContent
protected HttpInput.Content nextNonSentinelContent() throws java.io.IOException
Poll the inputQ for Content. Consumed buffers andHttpInput.SentinelContent
s are removed and EOF state updated if need be.- Returns:
- Content or null
- Throws:
java.io.IOException
-
produceNextContent
protected HttpInput.Content produceNextContent() throws java.io.IOException
Get the next readable from the inputQ, callingproduceContent()
if need be. EOF is NOT processed and state is not changed.- Returns:
- the content or EOF or null if none available.
- Throws:
java.io.IOException
- if retrieving the content fails
-
nextInterceptedContent
protected HttpInput.Content nextInterceptedContent() throws java.io.IOException
Poll the inputQ for Content or EOF. Consumed buffers and non EOFHttpInput.SentinelContent
s are removed. EOF state is not updated. Interception is done within this method.- Returns:
- Content with remaining, a
HttpInput.SentinelContent
, or null - Throws:
java.io.IOException
-
intercept
private HttpInput.Content intercept(HttpInput.Content content) throws java.io.IOException
- Throws:
java.io.IOException
-
consume
private void consume(HttpInput.Content content)
-
get
protected int get(HttpInput.Content content, byte[] buffer, int offset, int length)
Copies the given content into the given byte buffer.- Parameters:
content
- the content to copy frombuffer
- the buffer to copy intooffset
- the buffer offset to start copying fromlength
- the space available in the buffer- Returns:
- the number of bytes actually copied
-
blockForContent
protected void blockForContent() throws java.io.IOException
Blocks until some content or some end-of-file event arrives.- Throws:
java.io.IOException
- if the wait is interrupted
-
addContent
public boolean addContent(HttpInput.Content content)
Adds some content to this input stream.- Parameters:
content
- the content to add- Returns:
- true if content channel woken for read
-
hasContent
public boolean hasContent()
-
unblock
public void unblock()
-
getContentConsumed
public long getContentConsumed()
-
getContentReceived
public long getContentReceived()
-
earlyEOF
public boolean earlyEOF()
This method should be called to signal that an EOF has been detected before all the expected content arrived.Typically this will result in an EOFException being thrown from a subsequent read rather than a -1 return.
- Returns:
- true if content channel woken for read
-
eof
public boolean eof()
This method should be called to signal that all the expected content arrived.- Returns:
- true if content channel woken for read
-
consumeAll
public boolean consumeAll()
Consume all available content without blocking. Raw content is counted in thegetContentReceived()
statistics, but is not intercepted nor counted in thegetContentConsumed()
statistics- Returns:
- True if EOF was reached, false otherwise.
-
isError
public boolean isError()
-
isAsync
public boolean isAsync()
-
isFinished
public boolean isFinished()
- Specified by:
isFinished
in classjavax.servlet.ServletInputStream
-
isReady
public boolean isReady()
- Specified by:
isReady
in classjavax.servlet.ServletInputStream
-
setReadListener
public void setReadListener(javax.servlet.ReadListener readListener)
- Specified by:
setReadListener
in classjavax.servlet.ServletInputStream
-
onIdleTimeout
public boolean onIdleTimeout(java.lang.Throwable x)
-
failed
public boolean failed(java.lang.Throwable x)
-
wakeup
private boolean wakeup()
-
run
public void run()
- Specified by:
run
in interfacejava.lang.Runnable
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-