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.StringANY_ORIGINprivate CorsConfigconfigprivate java.util.List<CorsConfig>configListprivate booleanisShortCircuitprivate static InternalLoggerloggerprivate static java.lang.StringNULL_ORIGINprivate HttpRequestrequest
-
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 voidchannelRead(ChannelHandlerContext ctx, java.lang.Object msg)CallsChannelHandlerContext.fireChannelRead(Object)to forward to the nextChannelInboundHandlerin theChannelPipeline.private voidechoRequestOrigin(HttpResponse response)private static voidforbidden(ChannelHandlerContext ctx, HttpRequest request)private CorsConfiggetForOrigin(java.lang.String requestOrigin)private voidhandlePreflight(ChannelHandlerContext ctx, HttpRequest request)private static booleanisPreflightRequest(HttpRequest request)private static voidrespond(ChannelHandlerContext ctx, HttpRequest request, HttpResponse response)private voidsetAllowCredentials(HttpResponse response)private voidsetAllowHeaders(HttpResponse response)private voidsetAllowMethods(HttpResponse response)private static voidsetAnyOrigin(HttpResponse response)private voidsetExposeHeaders(HttpResponse response)private voidsetMaxAge(HttpResponse response)private static voidsetNullOrigin(HttpResponse response)private booleansetOrigin(HttpResponse response)private static voidsetOrigin(HttpResponse response, java.lang.String origin)private voidsetPreflightHeaders(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 voidsetVaryHeader(HttpResponse response)voidwrite(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise)CallsChannelOutboundInvoker.write(Object, ChannelPromise)to forward to the nextChannelOutboundHandlerin 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 ofCorsConfigisShortCircuit- Same asCorsConfig.shortCircuitbut 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:ChannelInboundHandlerAdapterCallsChannelHandlerContext.fireChannelRead(Object)to forward to the nextChannelInboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
channelReadin interfaceChannelInboundHandler- Overrides:
channelReadin 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:ChannelDuplexHandlerCallsChannelOutboundInvoker.write(Object, ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
writein interfaceChannelOutboundHandler- Overrides:
writein classChannelDuplexHandler- Parameters:
ctx- theChannelHandlerContextfor which the write operation is mademsg- the message to writepromise- theChannelPromiseto 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)
-
-