Listeners

Listeners represent the entry point of an Undertow application. All incoming requests will come through a listener, and a listener is responsible for translating a request into an instance of the HttpServerExchange object, and then turning the result into a response that can be sent back to the client.

Undertow provides 3 built in listener types, HTTP/1.1, AJP and HTTP/2. HTTPS is provided by using the HTTP listener with an SSL enabled connection.

Undertow also supports version 1 of the proxy protocol, which can be combined with any of the above listener types by calling setUseProxyProtocol(true) on the Undertow.ListenerBuilder (then added via Undertow.Builder.addListener(ListenerBuilder)).

Options

Undertow listeners can be configured through the use of the org.xnio.Option class. In general options for XNIO that control connection and worker level behaviour are listed in org.xnio.Options. Undertow specific options that control connector level behaviour are listed in io.undertow.UndertowOptions.

XNIO workers

All listeners are tied to an XNIO Worker instance. Usually there will only be a single worker instance that is shared between listeners, however it is possible to create a new worker for each listener.

The worker instance manages the listeners IO threads, and also the default blocking task thread pool. There are several main XNIO worker options that affect listener behaviour. These option can either be specified on the Undertow builder as worker options, or at worker creating time if you are bootstrapping a server manually. These options all reside on the org.xnio.Options class.

WORKER_IO_THREADS

The number of IO threads to create. IO threads perform non blocking tasks, and should never perform blocking operations because they are responsible for multiple connections, so while the operation is blocking other connections will essentially hang. Two IO threads per CPU core is a reasonable default.

WORKER_TASK_CORE_THREADS

The number of threads in the workers blocking task thread pool. When performing blocking operations such as Servlet requests threads from this pool will be used. In general it is hard to give a reasonable default for this, as it depends on the server workload. Generally this should be reasonably high, around 10 per CPU core.

Buffer Pool

All listeners have a buffer pool, which is used to allocate pooled NIO ByteBuffer instances. These buffers are used for IO operations, and the buffer size has a big impact on application performance. For servers the ideal size is generally 16k, as this is usually the maximum amount of data that can be written out via a write() operation (depending on the network setting of the operating system). Smaller systems may want to use smaller buffers to save memory.

In some situations with blocking IO the buffer size will determine if a response is sent using chunked encoding or has a fixed content length. If a response fits completely in the buffer and flush() is not called then a content length can be set automatically.

Common Listener Options

In addition to the worker options the listeners take some other options that control server behaviour. These are all part of the io.undertow.UndertowOptions class. Some of of these only make sense for specific protocols. You can set options with the Undertow.Builder.setServerOption:

MAX_HEADER_SIZE

The maximum size of a HTTP header block, in bytes. If a client sends more data that this as part of the request header then the connection will be closed. Defaults to 1MB (1,048,576 bytes).

MAX_ENTITY_SIZE

The default maximum size of a request entity. If entity body is larger than this limit then a java.io.IOException will be thrown at some point when reading the request (on the first read for fixed length requests, when too much data has been read for chunked requests). This value is only the default size, it is possible for a handler to override this for an individual request by calling io.undertow.server.HttpServerExchange.setMaxEntitySize(long size). Defaults to unlimited.

MULTIPART_MAX_ENTITY_SIZE

The default max entity size when using the Multipart parser. This will generally be larger than MAX_ENTITY_SIZE. Having a seperate setting for this allows for large files to be uploaded, while limiting the size of other requests.

MAX_PARAMETERS

The maximum number of query parameters that are permitted in a request. If a client sends more than this number the connection will be closed. This limit is necessary to protect against hash based denial of service attacks. Defaults to 1000.

MAX_HEADERS

The maximum number of headers that are permitted in a request. If a client sends more than this number the connection will be closed. This limit is necessary to protect against hash based denial of service attacks. Defaults to 200.

MAX_COOKIES

The maximum number of cookies that are permitted in a request. If a client sends more than this number the connection will be closed. This limit is necessary to protect against hash based denial of service attacks. Defaults to 200.

URL_CHARSET

The charset to use to decode the URL and query parameters. Defaults to UTF-8.

DECODE_URL

Determines if the listener will decode the URL and query parameters, or simply pass it through to the handler chain as is. If this is set url encoded characters will be decoded to the charset specified in URL_CHARSET. Defaults to true.

ALLOW_ENCODED_SLASH

If a request comes in with encoded / characters (i.e. %2F), will these be decoded. This can cause security problems (link:http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2007-0450) if a front end proxy does not perform the same decoding, and as a result this is disabled by default.

DECODE_SLASH

Controls whether encoded slash characters (%2F) in the request path are decoded. If this option is set explicitly it takes precedence over ALLOW_ENCODED_SLASH. As with ALLOW_ENCODED_SLASH, decoding slashes can cause security problems if a front end proxy does not perform the same decoding (see CVE-2007-0450). Defaults to true.

ALLOW_EQUALS_IN_COOKIE_VALUE

If this is true then Undertow will allow non-escaped equals characters in unquoted cookie values. Unquoted cookie values may not contain equals characters. If present the value ends before the equals sign. The remainder of the cookie value will be dropped. Defaults to false.

ALWAYS_SET_DATE

If the server should add a HTTP Date header to all response entities which do not already have one. The server sets the header right before writing the response, if none was set by a handler before. Unlike the DateHandler it will not overwrite the header. The current date string is cached, and is updated every second. Defaults to true.

ALWAYS_SET_KEEP_ALIVE

If a HTTP Connection: keep-alive header should always be set, even for HTTP/1.1 requests that are persistent by default. Even though the spec does not require this header to always be sent it seems safer to always send it. If you are writing some kind of super high performance application and are worried about the extra data being sent over the wire this option allows you to turn it off. Defaults to true.

MAX_BUFFERED_REQUEST_SIZE

The maximum size of a request that can be saved in bytes. Requests are buffered in a few situations, the main ones being SSL renegotiation and saving post data when using FORM based auth. Defaults to 16,384 bytes.

RECORD_REQUEST_START_TIME

If the server should record the start time of a HTTP request. This is necessary if you wish to log or otherwise use the total request time, however has a slight performance impact, as it means that System.nanoTime() must be called for each request. Defaults to false.

IDLE_TIMEOUT

The amount of time a connection can be idle for before it is timed out. An idle connection is a connection that has had no data transfer in the idle timeout period. Note that this is a fairly coarse grained approach, and small values will cause problems for requests with a long processing time.

REQUEST_PARSE_TIMEOUT

How long a request can spend in the parsing phase before it is timed out. This timer is started when the first bytes of a request are read, and finishes once all the headers have been parsed.

NO_REQUEST_TIMEOUT

The amount of time a connection can sit idle without processing a request, before it is closed by the server.

ENABLE_STATISTICS

If this is true then the connector will record statistics such as requests processed and bytes sent/received. This has a performance impact, although it should not be noticeable in most cases. (The older name ENABLE_CONNECTOR_STATISTICS is deprecated in 2.0.x and is simply an alias for ENABLE_STATISTICS.)

TRACK_ACTIVE_REQUESTS

If active request tracking should be enabled, allowing the requests currently being processed to be inspected. This has a slight performance impact and is only honoured if ENABLE_STATISTICS is also enabled. Defaults to false.

BUFFER_PIPELINED_DATA

If the server should buffer pipelined requests, so that multiple pipelined requests can be written out in a single write operation. Defaults to false.

ENABLE_RFC6265_COOKIE_VALIDATION

If this is true then Undertow performs RFC6265 compliant cookie validation for the Set-Cookie header, instead of the legacy, backwards compatible behaviour. Defaults to false.

ALLOW_UNKNOWN_PROTOCOLS

If unknown protocols should be allowed. The known protocols are HTTP/0.9, HTTP/1.0, HTTP/1.1 and HTTP/2.0. If this is false then requests that specify any other protocol will be rejected with a 400. Defaults to false.

REQUIRE_HOST_HTTP11

If this is true then HTTP/1.1 requests that do not include a Host header will be rejected. Defaults to false.

MAX_CACHED_HEADER_SIZE

The maximum size of a header name + value combination that is cached in the per connection header cache. Defaults to 150.

HTTP_HEADERS_CACHE_SIZE

The maximum number of headers that are cached per connection. If this is set to zero the cache is disabled. Defaults to 15.

SSL_USER_CIPHER_SUITES_ORDER

If the SSLEngine should prefer the server’s cipher suite order rather than the client’s.

ALLOW_UNESCAPED_CHARACTERS_IN_URL

If this is true then Undertow will accept unescaped characters in the URL that are not permitted by the HTTP specification. This can be a security risk and is disabled by default.

ENDPOINT_IDENTIFICATION_ALGORITHM

The endpoint identification algorithm to use for SSL connections. See javax.net.ssl.SSLParameters.setEndpointIdentificationAlgorithm(String).

SSL_SNI_HOSTNAME

Forces UndertowXnioSsl to use a specific name as the javax.net.ssl.SNIHostName for a client connection. If this is not set, the URL hostname is used when available. This is useful when connecting via IP address but an SNI hostname value is still required.

SHUTDOWN_TIMEOUT

The server shutdown timeout in milliseconds, after which the executor will be forcefully shut down, interrupting tasks that are still executing. There is no timeout by default.

ENABLE_SPDY

Deprecated. SPDY is no longer supported; use HTTP/2 (ENABLE_HTTP2) instead.

MEMORY_STORAGE_THRESHOLD

The minimum size in bytes for storing multipart form content in memory before it is persisted to disk. If a file part exceeds the configured file size threshold and no filename is specified in the form, the content is kept in memory as long as its size is less than or equal to this value, otherwise it is written to disk. Defaults to 16 KB (16,384 bytes).

WEB_SOCKETS_READ_TIMEOUT

Configures a read timeout for a web socket, in milliseconds. If set, it overrides org.xnio.Options.READ_TIMEOUT. If the given amount of time elapses without a successful read, the socket’s next read throws a ReadTimeoutException.

WEB_SOCKETS_WRITE_TIMEOUT

Configures a write timeout for a web socket, in milliseconds. If set, it overrides org.xnio.Options.WRITE_TIMEOUT. If the given amount of time elapses without a successful write, the socket’s next write throws a WriteTimeoutException.

ALLOW_ID_LESS_MATRIX_PARAMETERS

Deprecated. If true, matrix parameters that have no associated path segment id are allowed. Defaults to false (to comply with the specification).

ALPN

io.undertow.server.protocol.http.AlpnOpenListener

The HTTP/2 connector requires the use of ALPN when running over SSL.

Undertow 2.3 requires JDK 11 or higher, and all supported JDKs provide native ALPN support, so ALPN works out of the box with the JDK SSL implementation.

Alternatively you can use the Wildfly OpenSSL project to provide ALPN, which may perform better than the JDK SSL implementation.

HTTP Listener

io.undertow.server.protocol.http.HttpOpenListener

The HTTP listener is the most commonly used listener type, and deals with HTTP/1.0 and HTTP/1.1. It only takes one additional option.

ENABLE_HTTP2

If this is true then the connection can be processed as a HTTP/2 'prior knowledge' connection. If a HTTP/2 client connects directly to the listener with a HTTP/2 connection preface then the HTTP/2 protocol will be used instead of HTTP/1.1.

AJP Listener

io.undertow.server.protocol.ajp.AjpOpenListener

The AJP listener allows the use of the AJP protocol, as used by the apache modules mod_jk and mod_proxy_ajp. It is a binary protocol that is slightly more efficient protocol than HTTP, as some common strings are replaced by integers. If the front end load balancer supports it then it is recommended to use HTTP2 instead, as it is both a standard protocol and more efficient.

This listener has the following specific options:

MAX_AJP_PACKET_SIZE

Controls the maximum size of an AJP packet. This setting must match on both the load balancer and backend server. Defaults to 8192 bytes.

AJP_ALLOWED_REQUEST_ATTRIBUTES_PATTERN

The AJP protocol supports passing arbitrary request attributes. Unrecognised request attributes are ignored unless the entire attribute name matches this regular expression. If not specified, the default value is null (no unrecognised attributes are accepted).

HTTP2 Listener

HTTP/2 support is implemented on top of HTTP/1.1 (it is not possible to have a HTTP/2 server that does not also support HTTP/1). There are three different ways a HTTP/2 connection can be established:

ALPN

This is the most common way (and the only way many browsers currently support). It requires HTTPS, and uses the application layer protocol negotiation SSL extension to negotiate that connection will use HTTP/2.

Prior Knowledge

This involves the client simply sending a HTTP/2 connection preface and assuming the server will support it. This is not generally used on the open internet, but it’s useful for things like load balancers when you know the backend server will support HTTP/2.

HTTP Upgrade

This involves the client sending an Upgrade: h2c header in the initial request. If this upgrade is accepted then the server will initiate a HTTP/2 connection, and send back the response to the initial request using HTTP/2.

Depending on the way HTTP/2 is being used the setup for the listeners is slightly different.

If you are using the Undertow builder all that is required is to call setServerOption(ENABLE_HTTP2, true), and HTTP/2 support will be automatically added for all HTTP and HTTPS listeners.

Undertow uses the ALPN implementation provided natively by the JDK (Undertow 2.3 requires JDK 11 or higher).

The following options are supported:

HTTP2_SETTINGS_HEADER_TABLE_SIZE

The size of the header table that is used for compression. Increasing this will use more memory per connection, but potentially decrease the amount of data that is sent over the wire. Defaults to 4096.

HTTP2_SETTINGS_ENABLE_PUSH

If server push is enabled for this connection.

HTTP2_SETTINGS_MAX_CONCURRENT_STREAMS

The maximum number of streams a client is allowed to have open at any one time.

HTTP2_SETTINGS_INITIAL_WINDOW_SIZE

The initial flow control window size.

HTTP2_SETTINGS_MAX_FRAME_SIZE

The maximum frame size.

HTTP2_SETTINGS_MAX_HEADER_LIST_SIZE

The maximum size of the headers that this server is prepared to accept. This option is deprecated in 2.0.x as it is effectively a duplicate of MAX_HEADER_SIZE.

HTTP2_PADDING_SIZE

The maximum amount of padding to send in a HTTP/2 frame. The actual amount is randomly determined. Defaults to zero.

HTTP2_HUFFMAN_CACHE_SIZE

Undertow keeps an LRU cache of common Huffman encodings. This sets the maximum size of that cache; setting it to 0 disables the caching.

MAX_CONCURRENT_REQUESTS_PER_CONNECTION

The maximum number of concurrent requests that will be processed at a time. Unlike HTTP2_SETTINGS_MAX_CONCURRENT_STREAMS this value is not sent to the remote client; requests in excess of this number are queued and processed using a priority queue based on HTTP/2 stream priority. If this is less than or equal to zero then the maximum concurrent streams value determines how many streams can run at once.

MAX_QUEUED_READ_BUFFERS

The maximum number of buffers that will be queued before reads are paused in framed protocols (such as HTTP/2). Defaults to 10.

QUEUED_FRAMES_HIGH_WATER_MARK

The maximum number of frames that can be queued before writes are suspended. Once this number is reached writes will not be resumed until the queue drops to QUEUED_FRAMES_LOW_WATER_MARK. Defaults to 50.

QUEUED_FRAMES_LOW_WATER_MARK

The point at which frame writes will resume after hitting the high water mark. Defaults to 10.

MAX_RST_FRAMES_PER_WINDOW

The maximum number of HTTP/2 RST_STREAM frames the server will accept within RST_FRAMES_TIME_WINDOW. If more than this many are received during the window the server sends a GO_AWAY frame with error code 11 (ENHANCE_YOUR_CALM) and closes the connection. This mitigates the HTTP/2 "Rapid Reset" denial of service attack (CVE-2023-44487). Defaults to 200.

RST_FRAMES_TIME_WINDOW

The window of time, in milliseconds, over which received HTTP/2 RST_STREAM frames are counted against MAX_RST_FRAMES_PER_WINDOW. Defaults to 30000 (30 seconds).