Top Description Inners Fields Constructors Methods
org.eclipse.jetty.server

public Class SizeLimitHandler

extends Wrapper
Class Inheritance
Imports
java.nio.ByteBuffer, org.eclipse.jetty.http.BadMessageException, .HttpException, .HttpField, .HttpFields, .HttpHeader, .HttpStatus, org.eclipse.jetty.io.Content, org.eclipse.jetty.server.handler.gzip.GzipHandler, org.eclipse.jetty.util.Callback

A Handler that can limit the size of message bodies in requests and responses.

The optional request and response limits are imposed by checking the Content-Length header or observing the actual bytes seen by this Handler.

Handler order is important; for example, if this handler is before the GzipHandler, then it will limit compressed sizes, if it as after the GzipHandler then it will limit uncompressed sizes.

If a size limit is exceeded then BadMessageException is thrown with a HttpStatus#PAYLOAD_TOO_LARGE_413 status.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
private class

Field Summary

Modifier and TypeField and Description
private final long
private final long

Constructor Summary

AccessConstructor and Description
public
SizeLimitHandler(long
The request body size limit in bytes or -1 for no limit
requestLimit
,
long
The response body size limit in bytes or -1 for no limit
responseLimit
)

Method Summary

Modifier and TypeMethod and Description
public boolean
handle(Request
the HTTP request to handle
request
,
Response
the HTTP response to handle
response
,
Callback
the callback to complete when the handling is complete
callback
)

Overrides org.eclipse.jetty.server.Handler.Wrapper.handle.

Implements org.eclipse.jetty.server.Request.Handler.handle.

Invoked to handle the passed HTTP request and response.

Inherited from org.eclipse.jetty.server.Handler.Wrapper:
getHandlergetInvocationTypesetHandler

Field Detail

_requestLimitback to summary
private final long _requestLimit
_responseLimitback to summary
private final long _responseLimit

Constructor Detail

SizeLimitHandlerback to summary
public SizeLimitHandler(long requestLimit, long responseLimit)
Parameters
requestLimit:long

The request body size limit in bytes or -1 for no limit

responseLimit:long

The response body size limit in bytes or -1 for no limit

Method Detail

handleback to summary
public boolean handle(Request request, Response response, Callback callback) throws Exception

Overrides org.eclipse.jetty.server.Handler.Wrapper.handle.

Implements org.eclipse.jetty.server.Request.Handler.handle.

Doc from org.eclipse.jetty.server.Request.Handler.handle.

Invoked to handle the passed HTTP request and response.

The request is accepted by returning true, then handling must be concluded by completing the passed callback. The handling may be asynchronous, i.e. this method may return true and complete the given callback later, possibly from a different thread. If this method returns false, then the callback must not be invoked and any mutation on the response reversed.

Exceptions thrown by this method may be subsequently handled by an error Request.Handler, if present, otherwise a default HTTP 500 error is generated and the callback completed while writing the error response.

The simplest implementation is:

public boolean handle(Request request, Response response, Callback callback)
{
    callback.succeeded();
    return true;
}

A HelloWorld implementation is:

public boolean handle(Request request, Response response, Callback callback)
{
    response.write(true, ByteBuffer.wrap("Hello World\n".getBytes(StandardCharsets.UTF_8)), callback);
    return true;
}
Parameters
request:Request

the HTTP request to handle

response:Response

the HTTP response to handle

callback:Callback

the callback to complete when the handling is complete

Returns:boolean

True if and only if the request will be handled, a response generated and the callback eventually called. This may occur within the scope of the call to this method, or asynchronously some time later. If false is returned, then this method must not generate a response, nor complete the callback.

Annotations
@Override
Exceptions
Exception:
if there is a failure during the handling. Catchers cannot assume that the callback will be called and thus should attempt to complete the request as if a false had been returned.
org.eclipse.jetty.server back to summary

private Class SizeLimitHandler.SizeLimitRequestWrapper

extends Wrapper
Class Inheritance

Field Summary

Modifier and TypeField and Description
private long

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public Content.Chunk
Inherited from org.eclipse.jetty.server.Request.Wrapper:
addFailureListeneraddHttpStreamWrapperaddIdleTimeoutListenerasAttributeMapclearAttributesconsumeAvailabledemandfailgetAttributegetAttributeNameSetgetBeginNanoTimegetComponentsgetConnectionMetaDatagetContextgetHeadersgetHeadersNanoTimegetHttpURIgetIdgetLengthgetMethodgetSessiongetTrailersgetTunnelSupportgetWrappedisSecurepushremoveAttributesetAttribute

Field Detail

_readback to summary
private long _read

Constructor Detail

SizeLimitRequestWrapperback to summary
public SizeLimitRequestWrapper(Request wrapped)

Method Detail

readback to summary
public Content.Chunk read()

Overrides org.eclipse.jetty.server.Request.Wrapper.read.

Implements org.eclipse.jetty.server.Request.read.

Doc from org.eclipse.jetty.server.Request.read.

Reads a chunk of content.

See how to use this method idiomatically.

The returned chunk could be:

  • null, to signal that there isn't a chunk of content available
  • an Chunk instance with non null Chunk#getFailure(), to signal that there was a failure trying to produce a chunk of content, or that the content production has been failed externally
  • a Chunk instance, containing the chunk of content.

Once a read returns an Chunk instance with non-null Chunk#getFailure() then if the failure is last further reads will continue to return the same failure chunk instance, otherwise further read() operations may return different non-failure chunks.

Once a read returns a last chunk, further reads will continue to return a last chunk (although the instance may be different).

The content reader code must ultimately arrange for a call to Chunk#release() on the returned Chunk.

Additionally, prior to the ultimate call to Chunk#release(), the reader code may make additional calls to Chunk#retain(), that must ultimately be matched by a correspondent number of calls to Chunk#release().

Concurrent reads from different threads are not recommended, as they are inherently in a race condition.

Reads performed outside the invocation context of a demand callback are allowed. However, reads performed with a pending demand are inherently in a race condition (the thread that reads with the thread that invokes the demand callback).

In addition, the returned Content.Chunk may be a Trailers instance, in case of request content trailers.

Returns:Content.Chunk

Doc from org.eclipse.jetty.io.Content.Source.read.

a chunk of content, possibly a failure instance, or null

Annotations
@Override
org.eclipse.jetty.server back to summary

private Class SizeLimitHandler.SizeLimitResponseWrapper

extends Wrapper
Class Inheritance

Field Summary

Modifier and TypeField and Description
private final HttpFields.Mutable
private long

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public HttpFields.Mutable
public void
write(boolean
whether the ByteBuffer is the last to write
last
,
ByteBuffer
the ByteBuffer to write
content
,
Callback
the callback to notify when the write operation is complete
callback
)

Overrides org.eclipse.jetty.server.Response.Wrapper.write.

Implements org.eclipse.jetty.server.Response.write.

Writes the given ByteBuffer, notifying the Callback when the write is complete.

Inherited from org.eclipse.jetty.server.Response.Wrapper:
getRequestgetStatusgetTrailersSuppliergetWrappedhasLastWriteisCommittedisCompletedSuccessfullyresetsetStatussetTrailersSupplierwriteInterim

Field Detail

_httpFieldsback to summary
private final HttpFields.Mutable _httpFields
_writtenback to summary
private long _written

Constructor Detail

SizeLimitResponseWrapperback to summary
public SizeLimitResponseWrapper(Request request, Response wrapped)

Method Detail

getHeadersback to summary
public HttpFields.Mutable getHeaders()

Overrides org.eclipse.jetty.server.Response.Wrapper.getHeaders.

Implements org.eclipse.jetty.server.Response.getHeaders.

Returns:HttpFields.Mutable

Doc from org.eclipse.jetty.server.Response.getHeaders.

the response HTTP headers

Annotations
@Override
writeback to summary
public void write(boolean last, ByteBuffer content, Callback callback)

Overrides org.eclipse.jetty.server.Response.Wrapper.write.

Implements org.eclipse.jetty.server.Response.write.

Doc from org.eclipse.jetty.server.Response.write.

Writes the given ByteBuffer, notifying the Callback when the write is complete.

Implementations guarantee that calls to this method are safely reentrant so that stack overflows are avoided in the case of mutual recursion between the execution of the Callback and a call to this method.

The invocation of the passed Callback is serialized with previous calls of this method, so that it is not invoked until any invocation of the callback of a previous call to this method has returned.

Thus a Callback should not block waiting for a callback of a future call to this method.

Parameters
last:boolean

whether the ByteBuffer is the last to write

content:ByteBuffer

the ByteBuffer to write

callback:Callback

the callback to notify when the write operation is complete

Annotations
@Override