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
public class CorsHandler extends ChannelDuplexHandler
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
Fields Modifier and Type Field Description private static java.lang.String
ANY_ORIGIN
private CorsConfig
config
private java.util.List<CorsConfig>
configList
private boolean
isShortCircuit
private static InternalLogger
logger
private static java.lang.String
NULL_ORIGIN
private HttpRequest
request
-
Constructor Summary
Constructors Constructor Description CorsHandler(CorsConfig config)
Creates a new instance with a singleCorsConfig
.CorsHandler(java.util.List<CorsConfig> configList, boolean isShortCircuit)
Creates a new instance with the specified config list.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
channelRead(ChannelHandlerContext ctx, java.lang.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(java.lang.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 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, java.lang.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, java.lang.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 Detail
-
logger
private static final InternalLogger logger
-
ANY_ORIGIN
private static final java.lang.String ANY_ORIGIN
- See Also:
- Constant Field Values
-
NULL_ORIGIN
private static final java.lang.String NULL_ORIGIN
- See Also:
- Constant Field Values
-
config
private CorsConfig config
-
request
private HttpRequest request
-
configList
private final java.util.List<CorsConfig> configList
-
isShortCircuit
private boolean isShortCircuit
-
-
Constructor Detail
-
CorsHandler
public CorsHandler(CorsConfig config)
Creates a new instance with a singleCorsConfig
.
-
CorsHandler
public CorsHandler(java.util.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 ofCorsConfig
isShortCircuit
- Same asCorsConfig.shortCircuit
but applicable to all supplied configs.
-
-
Method Detail
-
channelRead
public void channelRead(ChannelHandlerContext ctx, java.lang.Object msg) throws java.lang.Exception
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:
java.lang.Exception
-
handlePreflight
private void handlePreflight(ChannelHandlerContext ctx, HttpRequest request)
-
setPreflightHeaders
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.- Parameters:
response
- the HttpResponse to which the preflight response headers should be added.
-
getForOrigin
private CorsConfig getForOrigin(java.lang.String requestOrigin)
-
setOrigin
private boolean setOrigin(HttpResponse response)
-
echoRequestOrigin
private void echoRequestOrigin(HttpResponse response)
-
setVaryHeader
private static void setVaryHeader(HttpResponse response)
-
setAnyOrigin
private static void setAnyOrigin(HttpResponse response)
-
setNullOrigin
private static void setNullOrigin(HttpResponse response)
-
setOrigin
private static void setOrigin(HttpResponse response, java.lang.String origin)
-
setAllowCredentials
private void setAllowCredentials(HttpResponse response)
-
isPreflightRequest
private static boolean isPreflightRequest(HttpRequest request)
-
setExposeHeaders
private void setExposeHeaders(HttpResponse response)
-
setAllowMethods
private void setAllowMethods(HttpResponse response)
-
setAllowHeaders
private void setAllowHeaders(HttpResponse response)
-
setMaxAge
private void setMaxAge(HttpResponse response)
-
write
public void write(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise) throws java.lang.Exception
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:
java.lang.Exception
- thrown if an error occurs
-
forbidden
private static void forbidden(ChannelHandlerContext ctx, HttpRequest request)
-
respond
private static void respond(ChannelHandlerContext ctx, HttpRequest request, HttpResponse response)
-
-