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

public Class CorsHandler

extends ChannelDuplexHandler
Class Inheritance
Imports
io.netty.buffer.Unpooled, io.netty.channel.ChannelDuplexHandler, .ChannelFuture, .ChannelFutureListener, .ChannelHandlerContext, .ChannelPromise, io.netty.handler.codec.http.DefaultFullHttpResponse, .HttpHeaderNames, .HttpHeaderValues, .HttpHeaders, .DefaultHttpHeadersFactory, .HttpRequest, .HttpResponse, .HttpUtil, io.netty.util.internal.logging.InternalLogger, .InternalLoggerFactory, java.util.Collections, .List

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
private void
private static void
forbidden(final ChannelHandlerContext ctx, final HttpRequest request)

private CorsConfig
getForOrigin(String 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 String 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)

public void
write(final ChannelHandlerContext
the ChannelHandlerContext for which the write operation is made
ctx
,
final Object
the message to write
msg
,
final ChannelPromise
the ChannelPromise to notify once the operation completes
promise
)

Overrides io.netty.channel.ChannelDuplexHandler.write.

Implements io.netty.channel.ChannelOutboundHandler.write.

Calls ChannelHandlerContext#write(Object, ChannelPromise) to forward to the next ChannelOutboundHandler in the ChannelPipeline.
Inherited from io.netty.channel.ChannelDuplexHandler:
bindcloseconnectderegisterdisconnectflushread

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 io.netty.channel.ChannelInboundHandlerAdapter.channelRead.

Implements io.netty.channel.ChannelInboundHandler.channelRead.

Doc from io.netty.channel.ChannelInboundHandlerAdapter.channelRead.

Calls ChannelHandlerContext#fireChannelRead(Object) to forward to the next ChannelInboundHandler in the ChannelPipeline. Sub-classes may override this method to change behavior.

Annotations
@Override
echoRequestOriginback to summary
private void echoRequestOrigin(final HttpResponse response)
forbiddenback to summary
private static void forbidden(final ChannelHandlerContext ctx, final HttpRequest request)
getForOriginback to summary
private CorsConfig getForOrigin(String requestOrigin)
handlePreflightback to summary
private void handlePreflight(final ChannelHandlerContext ctx, final HttpRequest request)
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 String 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)
writeback to summary
public void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) throws Exception

Overrides io.netty.channel.ChannelDuplexHandler.write.

Implements io.netty.channel.ChannelOutboundHandler.write.

Doc from io.netty.channel.ChannelDuplexHandler.write.

Calls ChannelHandlerContext#write(Object, ChannelPromise) to forward to the next ChannelOutboundHandler in the ChannelPipeline. Sub-classes may override this method to change behavior.

Parameters
ctx:ChannelHandlerContext

Doc from io.netty.channel.ChannelOutboundHandler.write.

the ChannelHandlerContext for which the write operation is made

msg:Object

Doc from io.netty.channel.ChannelOutboundHandler.write.

the message to write

promise:ChannelPromise

Doc from io.netty.channel.ChannelOutboundHandler.write.

the ChannelPromise to notify once the operation completes

Annotations
@Override
Exceptions
Exception:

Doc from io.netty.channel.ChannelOutboundHandler.write.

thrown if an error occurs