Package io.netty.handler.codec.http.cors
Class CorsHandler
java.lang.Object
io.netty.channel.ChannelHandlerAdapter
io.netty.channel.ChannelInboundHandlerAdapter
io.netty.channel.ChannelDuplexHandler
io.netty.handler.codec.http.cors.CorsHandler
- All Implemented Interfaces:
ChannelHandler
,ChannelInboundHandler
,ChannelOutboundHandler
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.
-
Nested Class Summary
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final String
private CorsConfig
private final List
<CorsConfig> private final boolean
private static final InternalLogger
private static final String
private HttpRequest
-
Constructor Summary
ConstructorsConstructorDescriptionCorsHandler
(CorsConfig config) Creates a new instance with a singleCorsConfig
.CorsHandler
(List<CorsConfig> configList, boolean isShortCircuit) Creates a new instance with the specified config list. -
Method Summary
Modifier and TypeMethodDescriptionvoid
channelRead
(ChannelHandlerContext ctx, Object msg) CallsChannelHandlerContext.fireChannelRead(Object)
to forward to the nextChannelInboundHandler
in theChannelPipeline
.private void
echoRequestOrigin
(HttpResponse response) private static void
forbidden
(ChannelHandlerContext ctx, HttpRequest request) private CorsConfig
getForOrigin
(String requestOrigin) private void
handlePreflight
(ChannelHandlerContext ctx, HttpRequest request) private static boolean
isPreflightRequest
(HttpRequest request) private static void
respond
(ChannelHandlerContext ctx, HttpRequest request, HttpResponse response) private void
setAllowCredentials
(HttpResponse response) private void
setAllowHeaders
(HttpResponse response) private void
setAllowMethods
(HttpResponse response) private void
setAllowPrivateNetwork
(HttpResponse response) private static void
setAnyOrigin
(HttpResponse response) private void
setExposeHeaders
(HttpResponse response) private void
setMaxAge
(HttpResponse response) private static void
setNullOrigin
(HttpResponse response) private boolean
setOrigin
(HttpResponse response) private static void
setOrigin
(HttpResponse response, String origin) private void
setPreflightHeaders
(HttpResponse 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
(HttpResponse response) void
write
(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) CallsChannelOutboundInvoker.write(Object, ChannelPromise)
to forward to the nextChannelOutboundHandler
in theChannelPipeline
.Methods inherited from class io.netty.channel.ChannelDuplexHandler
bind, close, connect, deregister, disconnect, flush, read
Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelActive, channelInactive, channelReadComplete, channelRegistered, channelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggered
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, handlerAdded, handlerRemoved, isSharable
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.netty.channel.ChannelHandler
handlerAdded, handlerRemoved
-
Field Details
-
logger
-
ANY_ORIGIN
- See Also:
-
NULL_ORIGIN
- See Also:
-
config
-
request
-
configList
-
isShortCircuit
private final boolean isShortCircuit
-
-
Constructor Details
-
CorsHandler
Creates a new instance with a singleCorsConfig
. -
CorsHandler
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 ofCorsConfig
isShortCircuit
- Same asCorsConfig.isShortCircuit()
but applicable to all supplied configs.
-
-
Method Details
-
channelRead
Description copied from class:ChannelInboundHandlerAdapter
CallsChannelHandlerContext.fireChannelRead(Object)
to forward to the nextChannelInboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
channelRead
in interfaceChannelInboundHandler
- Overrides:
channelRead
in classChannelInboundHandlerAdapter
- Throws:
Exception
-
handlePreflight
-
setPreflightHeaders
This is a non CORS specification feature which enables the setting of preflight response headers that might be required by intermediaries.- Parameters:
response
- the HttpResponse to which the preflight response headers should be added.
-
getForOrigin
-
setOrigin
-
echoRequestOrigin
-
setVaryHeader
-
setAnyOrigin
-
setNullOrigin
-
setOrigin
-
setAllowCredentials
-
isPreflightRequest
-
setExposeHeaders
-
setAllowMethods
-
setAllowHeaders
-
setMaxAge
-
setAllowPrivateNetwork
-
write
Description copied from class:ChannelDuplexHandler
CallsChannelOutboundInvoker.write(Object, ChannelPromise)
to forward to the nextChannelOutboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
write
in interfaceChannelOutboundHandler
- Overrides:
write
in classChannelDuplexHandler
- Parameters:
ctx
- theChannelHandlerContext
for which the write operation is mademsg
- the message to writepromise
- theChannelPromise
to notify once the operation completes- Throws:
Exception
- thrown if an error occurs
-
forbidden
-
respond
-