Skip to content

Commit

Permalink
update Configuration cors
Browse files Browse the repository at this point in the history
  • Loading branch information
zd925 committed Mar 22, 2024
1 parent c6248da commit e90071e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
27 changes: 23 additions & 4 deletions src/main/java/com/corundumstudio/socketio/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
*/
package com.corundumstudio.socketio;

import java.io.InputStream;
import java.util.Arrays;
import java.util.List;

import com.corundumstudio.socketio.handler.SuccessAuthorizationListener;
import com.corundumstudio.socketio.listener.DefaultExceptionListener;
import com.corundumstudio.socketio.listener.ExceptionListener;
Expand All @@ -27,6 +23,9 @@
import com.corundumstudio.socketio.store.StoreFactory;

import javax.net.ssl.KeyManagerFactory;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;

public class Configuration {

Expand Down Expand Up @@ -84,6 +83,8 @@ public class Configuration {

private String origin;

private boolean enableCors = true;

private boolean httpCompression = true;

private boolean websocketCompression = true;
Expand Down Expand Up @@ -154,6 +155,7 @@ public Configuration() {

setAddVersionHeader(conf.isAddVersionHeader());
setOrigin(conf.getOrigin());
setEnableCors(conf.isEnableCors());
setAllowHeaders(conf.getAllowHeaders());
setSSLProtocol(conf.getSSLProtocol());

Expand Down Expand Up @@ -517,13 +519,30 @@ public boolean isAddVersionHeader() {
public void setOrigin(String origin) {
this.origin = origin;
}

public String getOrigin() {
return origin;
}

/**
* cors dispose
* <p>
* Default is <code>true</code>
*
* @param enableCors enableCors
*/
public void setEnableCors(boolean enableCors) {
this.enableCors = enableCors;
}

public boolean isEnableCors() {
return enableCors;
}

public boolean isUseLinuxNativeEpoll() {
return useLinuxNativeEpoll;
}

public void setUseLinuxNativeEpoll(boolean useLinuxNativeEpoll) {
this.useLinuxNativeEpoll = useLinuxNativeEpoll;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ private void write(XHROptionsMessage msg, ChannelHandlerContext ctx, ChannelProm
HttpResponse res = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.OK);

res.headers().add(HttpHeaderNames.SET_COOKIE, "io=" + msg.getSessionId())
.add(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE)
.add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS, HttpHeaderNames.CONTENT_TYPE);
.add(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE)
.add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS, HttpHeaderNames.CONTENT_TYPE);

String origin = ctx.channel().attr(ORIGIN).get();
addOriginHeaders(origin, res);
Expand All @@ -136,7 +136,7 @@ private void sendMessage(HttpMessage msg, Channel channel, ByteBuf out, String t
HttpResponse res = new DefaultHttpResponse(HTTP_1_1, status);

res.headers().add(HttpHeaderNames.CONTENT_TYPE, type)
.add(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
.add(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
if (msg.getSessionId() != null) {
res.headers().add(HttpHeaderNames.SET_COOKIE, "io=" + msg.getSessionId());
}
Expand Down Expand Up @@ -188,19 +188,21 @@ private void addOriginHeaders(String origin, HttpResponse res) {
res.headers().add(HttpHeaderNames.SERVER, version);
}

if (configuration.getOrigin() != null) {
res.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN, configuration.getOrigin());
res.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_CREDENTIALS, Boolean.TRUE);
} else {
if (origin != null) {
res.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN, origin);
if (configuration.isEnableCors()) {
if (configuration.getOrigin() != null) {
res.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN, configuration.getOrigin());
res.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_CREDENTIALS, Boolean.TRUE);
} else {
res.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN, "*");
if (origin != null) {
res.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN, origin);
res.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_CREDENTIALS, Boolean.TRUE);
} else {
res.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN, "*");
}
}
if (configuration.getAllowHeaders() != null) {
res.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS, configuration.getAllowHeaders());
}
}
if(configuration.getAllowHeaders() != null){
res.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS, configuration.getAllowHeaders());
}
}

Expand Down

0 comments on commit e90071e

Please sign in to comment.