|
|
@ -15,6 +15,7 @@ |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
package com.fr.third.socketio.handler; |
|
|
|
package com.fr.third.socketio.handler; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import static com.fr.third.springframework.util.StringUtils.isEmpty; |
|
|
|
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1; |
|
|
|
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
import java.io.IOException; |
|
|
@ -121,7 +122,7 @@ public class EncoderHandler extends ChannelOutboundHandlerAdapter { |
|
|
|
.add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS, HttpHeaderNames.CONTENT_TYPE); |
|
|
|
.add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS, HttpHeaderNames.CONTENT_TYPE); |
|
|
|
|
|
|
|
|
|
|
|
String origin = ctx.channel().attr(ORIGIN).get(); |
|
|
|
String origin = ctx.channel().attr(ORIGIN).get(); |
|
|
|
addOriginHeaders(origin, res); |
|
|
|
addOriginAndAllowHeaders(origin, res); |
|
|
|
|
|
|
|
|
|
|
|
ByteBuf out = encoder.allocateBuffer(ctx.alloc()); |
|
|
|
ByteBuf out = encoder.allocateBuffer(ctx.alloc()); |
|
|
|
sendMessage(msg, ctx.channel(), out, res, promise); |
|
|
|
sendMessage(msg, ctx.channel(), out, res, promise); |
|
|
@ -143,7 +144,7 @@ public class EncoderHandler extends ChannelOutboundHandlerAdapter { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
String origin = channel.attr(ORIGIN).get(); |
|
|
|
String origin = channel.attr(ORIGIN).get(); |
|
|
|
addOriginHeaders(origin, res); |
|
|
|
addOriginAndAllowHeaders(origin, res); |
|
|
|
|
|
|
|
|
|
|
|
HttpUtil.setContentLength(res, out.readableBytes()); |
|
|
|
HttpUtil.setContentLength(res, out.readableBytes()); |
|
|
|
|
|
|
|
|
|
|
@ -185,10 +186,7 @@ public class EncoderHandler extends ChannelOutboundHandlerAdapter { |
|
|
|
sendMessage(errorMsg, ctx.channel(), encBuf, "application/json", promise, HttpResponseStatus.BAD_REQUEST); |
|
|
|
sendMessage(errorMsg, ctx.channel(), encBuf, "application/json", promise, HttpResponseStatus.BAD_REQUEST); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void addOriginHeaders(String origin, HttpResponse res) { |
|
|
|
private void addOriginAndAllowHeaders(String origin, HttpResponse res) { |
|
|
|
|
|
|
|
|
|
|
|
// 使WebSocket连接时可添加fine_username和fine_auth_token的Header
|
|
|
|
|
|
|
|
res.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS, "fine_username,fine_auth_token"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (version != null) { |
|
|
|
if (version != null) { |
|
|
|
res.headers().add(HttpHeaderNames.SERVER, version); |
|
|
|
res.headers().add(HttpHeaderNames.SERVER, version); |
|
|
@ -196,12 +194,17 @@ public class EncoderHandler extends ChannelOutboundHandlerAdapter { |
|
|
|
|
|
|
|
|
|
|
|
if (origin != null) { |
|
|
|
if (origin != null) { |
|
|
|
String configOrigin = configuration.getOrigin(); |
|
|
|
String configOrigin = configuration.getOrigin(); |
|
|
|
if (configOrigin != null && !"".equals(configOrigin) && !configOrigin.contains(origin)) { |
|
|
|
if (!isEmpty(configOrigin) && !configOrigin.contains(origin)) { |
|
|
|
throw new IllegalArgumentException(); |
|
|
|
throw new IllegalArgumentException(); |
|
|
|
} |
|
|
|
} |
|
|
|
res.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN, origin); |
|
|
|
res.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN, origin); |
|
|
|
res.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_CREDENTIALS, Boolean.TRUE); |
|
|
|
res.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_CREDENTIALS, Boolean.TRUE); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String allowHeaders = configuration.getAccessControlAllowHeaders(); |
|
|
|
|
|
|
|
if (!isEmpty(allowHeaders)) { |
|
|
|
|
|
|
|
res.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS, configuration.getAccessControlAllowHeaders()); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|