Top Description Fields Constructors Methods
io.netty5.handler.codec.http.cors

public Class CorsHandler

extends Object
implements ChannelHandler
Class Inheritance
All Implemented Interfaces
io.netty5.channel.ChannelHandler
Imports
io.netty5.channel.ChannelFutureListeners, .ChannelHandler, .ChannelHandlerContext, io.netty5.handler.codec.http.DefaultFullHttpResponse, .HttpHeaderNames, .HttpHeaderValues, .HttpRequest, .HttpResponse, .HttpUtil, io.netty5.handler.codec.http.headers.HttpHeaders, io.netty5.util.AsciiString, io.netty5.util.concurrent.Future, io.netty5.util.internal.StringUtil, io.netty5.util.internal.logging.InternalLogger, .InternalLoggerFactory, java.util.Collections, .Iterator, .List, .Map.Entry, .Set, .StringJoiner

Handles Cross Origin Resource Sharing (CORS) requests.

This handler can be configured using one or more CorsConfig, please refer to this class for details about the configuration options available.

Field Summary

Modifier and TypeField and Description
private static final String
private CorsConfig
private final List<CorsConfig>
private final boolean
private static final InternalLogger
private static final String
private HttpRequest

Constructor Summary

AccessConstructor and Description
public
CorsHandler(final CorsConfig config)

Creates a new instance with a single CorsConfig.

public
CorsHandler(final List<CorsConfig>
List of CorsConfig
configList
,
boolean
Same as CorsConfig#isShortCircuit but applicable to all supplied configs.
isShortCircuit
)

Creates a new instance with the specified config list.

Method Summary

Modifier and TypeMethod and Description
public void
channelRead(final ChannelHandlerContext ctx, final Object msg)

Overrides default io.netty5.channel.ChannelHandler.channelRead.

Invoked when the current Channel has read a message from the peer.
private void
private static void
forbidden(final ChannelHandlerContext ctx, final HttpRequest request)

private CorsConfig
getForOrigin(CharSequence requestOrigin)

private void
private static boolean
private static void
respond(final ChannelHandlerContext ctx, final HttpRequest request, final HttpResponse response)

private void
private void
setAllowHeaders(final HttpResponse response)

private void
setAllowMethods(final HttpResponse response)

private void
private static void
setAnyOrigin(final HttpResponse response)

private void
private void
setMaxAge(final HttpResponse response)

private static void
setNullOrigin(final HttpResponse response)

private boolean
setOrigin(final HttpResponse response)

private static void
setOrigin(final HttpResponse response, final CharSequence origin)

private void
setPreflightHeaders(final HttpResponse
the HttpResponse to which the preflight response headers should be added.
response
)

This is a non CORS specification feature which enables the setting of preflight response headers that might be required by intermediaries.

private static void
setVaryHeader(final HttpResponse response)

private static CharSequence
toCsv(Iterable<?> headerValues)

public Future<Void>
write(final ChannelHandlerContext
the ChannelHandlerContext for which the write operation is made
ctx
,
final Object
the message to write
msg
)

Overrides default io.netty5.channel.ChannelHandler.write.

Called once a write operation is made.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ANY_ORIGINback to summary
private static final String ANY_ORIGIN
configback to summary
private CorsConfig config
configListback to summary
private final List<CorsConfig> configList
isShortCircuitback to summary
private final boolean isShortCircuit
loggerback to summary
private static final InternalLogger logger
NULL_ORIGINback to summary
private static final String NULL_ORIGIN
requestback to summary
private HttpRequest request

Constructor Detail

CorsHandlerback to summary
public CorsHandler(final CorsConfig config)

Creates a new instance with a single CorsConfig.

CorsHandlerback to summary
public CorsHandler(final List<CorsConfig> configList, boolean isShortCircuit)

Creates a new instance with the specified config list. If more than one config matches a certain origin, the first in the List will be used.

Parameters
configList:List<CorsConfig>

List of CorsConfig

isShortCircuit:boolean

Same as CorsConfig#isShortCircuit but applicable to all supplied configs.

Method Detail

channelReadback to summary
public void channelRead(final ChannelHandlerContext ctx, final Object msg) throws Exception

Overrides default io.netty5.channel.ChannelHandler.channelRead.

Doc from io.netty5.channel.ChannelHandler.channelRead.

Invoked when the current Channel has read a message from the peer.

Annotations
@Override
echoRequestOriginback to summary
private void echoRequestOrigin(final HttpResponse response)
forbiddenback to summary
private static void forbidden(final ChannelHandlerContext ctx, final HttpRequest request) throws Exception
getForOriginback to summary
private CorsConfig getForOrigin(CharSequence requestOrigin)
handlePreflightback to summary
private void handlePreflight(final ChannelHandlerContext ctx, final HttpRequest request) throws Exception
isPreflightRequestback to summary
private static boolean isPreflightRequest(final HttpRequest request)
respondback to summary
private static void respond(final ChannelHandlerContext ctx, final HttpRequest request, final HttpResponse response)
setAllowCredentialsback to summary
private void setAllowCredentials(final HttpResponse response)
setAllowHeadersback to summary
private void setAllowHeaders(final HttpResponse response)
setAllowMethodsback to summary
private void setAllowMethods(final HttpResponse response)
setAllowPrivateNetworkback to summary
private void setAllowPrivateNetwork(final HttpResponse response)
setAnyOriginback to summary
private static void setAnyOrigin(final HttpResponse response)
setExposeHeadersback to summary
private void setExposeHeaders(final HttpResponse response)
setMaxAgeback to summary
private void setMaxAge(final HttpResponse response)
setNullOriginback to summary
private static void setNullOrigin(final HttpResponse response)
setOriginback to summary
private boolean setOrigin(final HttpResponse response)
setOriginback to summary
private static void setOrigin(final HttpResponse response, final CharSequence origin)
setPreflightHeadersback to summary
private void setPreflightHeaders(final HttpResponse response)

This is a non CORS specification feature which enables the setting of preflight response headers that might be required by intermediaries.

Parameters
response:HttpResponse

the HttpResponse to which the preflight response headers should be added.

setVaryHeaderback to summary
private static void setVaryHeader(final HttpResponse response)
toCsvback to summary
private static CharSequence toCsv(Iterable<?> headerValues)
writeback to summary
public Future<Void> write(final ChannelHandlerContext ctx, final Object msg)

Overrides default io.netty5.channel.ChannelHandler.write.

Doc from io.netty5.channel.ChannelHandler.write.

Called once a write operation is made. The write operation will write the messages through the ChannelPipeline. Those are then ready to be flushed to the actual Channel once Channel#flush() is called.

Parameters
ctx:ChannelHandlerContext

the ChannelHandlerContext for which the write operation is made

msg:Object

the message to write

Returns:Future<Void>

the Future which will be notified once the operation completes.

Annotations
@Override