Skip to content

Commit

Permalink
fix(): save option and vary bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawven committed Apr 11, 2024
1 parent 8137167 commit 791c905
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ private boolean reject(HttpResponse response) {
public static boolean isPreFlight(HttpRequest request) {
// preflight request is a OPTIONS request with Access-Control-Request-Method header
return request.method().equals(HttpMethods.OPTIONS.name())
&& request.header(RestConstants.ORIGIN) != null
&& request.header(RestConstants.ACCESS_CONTROL_REQUEST_METHOD) != null;
}

Expand Down Expand Up @@ -162,18 +163,25 @@ private boolean isCorsRequest(HttpRequest request) {
private void setVaryHeaders(HttpResponse response) {
List<String> varyHeaders = response.headerValues(RestConstants.VARY);
if (varyHeaders == null) {
response.addHeader(RestConstants.VARY, RestConstants.ORIGIN);
response.addHeader(RestConstants.VARY, RestConstants.ACCESS_CONTROL_REQUEST_METHOD);
response.addHeader(RestConstants.VARY, RestConstants.ACCESS_CONTROL_REQUEST_HEADERS);

response.addHeader(
RestConstants.VARY,
RestConstants.ORIGIN + "," + RestConstants.ACCESS_CONTROL_REQUEST_METHOD + ","
+ RestConstants.ACCESS_CONTROL_REQUEST_HEADERS);
} else {
StringBuilder builder = new StringBuilder();
if (!varyHeaders.contains(RestConstants.ORIGIN)) {
response.addHeader(RestConstants.VARY, RestConstants.ORIGIN);
builder.append(RestConstants.ORIGIN).append(",");
}
if (!varyHeaders.contains(RestConstants.ACCESS_CONTROL_REQUEST_METHOD)) {
response.addHeader(RestConstants.VARY, RestConstants.ACCESS_CONTROL_REQUEST_METHOD);
builder.append(RestConstants.ACCESS_CONTROL_REQUEST_METHOD).append(",");
}
if (!varyHeaders.contains(RestConstants.ACCESS_CONTROL_REQUEST_HEADERS)) {
response.addHeader(RestConstants.VARY, RestConstants.ACCESS_CONTROL_REQUEST_HEADERS);
builder.append(RestConstants.ACCESS_CONTROL_REQUEST_HEADERS).append(",");
}
if (builder.length() > 0) {
builder.deleteCharAt(builder.length() - 1);
response.addHeader(RestConstants.VARY, builder.toString());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

Expand Down Expand Up @@ -239,18 +238,10 @@ public HandlerMeta lookup(HttpRequest request, HttpResponse response) {
}

private String preprocessingCors(HttpRequest request, HttpResponse response) {
if (Objects.equals(request.method(), HttpMethods.OPTIONS.name())) {
if (CorsProcessor.isPreFlight(request)) {
String realMethod = request.header(RestConstants.ACCESS_CONTROL_REQUEST_METHOD);
request.setMethod(realMethod);
return realMethod;
} else {
throw new HttpResultPayloadException(HttpResult.builder()
.status(HttpStatus.FORBIDDEN)
.body(response.body())
.headers(response.headers())
.build());
}
if (CorsProcessor.isPreFlight(request)) {
String realMethod = request.header(RestConstants.ACCESS_CONTROL_REQUEST_METHOD);
request.setMethod(realMethod);
return realMethod;
}
return null;
}
Expand Down
Loading

0 comments on commit 791c905

Please sign in to comment.