GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — CORS: Don't override vary header if it already exists · netty/netty@1a896eb

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-http/src
    • main/java/io/netty/handler/codec/http/cors
      • CorsHandler.java
    • test/java/io/netty/handler/codec/http/cors
      • CorsHandlerTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec-http/src/main/java/io/netty/handler/codec/http/cors/CorsHandler.java‎

Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/cors/CorsHandler.java
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,9 @@ private void echoRequestOrigin(final HttpResponse response) {
193193
}
194194

195195
private static void setVaryHeader(final HttpResponse response) {
196-
response.headers().set(HttpHeaderNames.VARY, HttpHeaderNames.ORIGIN);
196+
if (!response.headers().containsValue(HttpHeaderNames.VARY, HttpHeaderNames.ORIGIN, true)) {
197+
response.headers().add(HttpHeaderNames.VARY, HttpHeaderNames.ORIGIN);
198+
}
197199
}
198200

199201
private static void setAnyOrigin(final HttpResponse response) {
Collapse file

‎codec-http/src/test/java/io/netty/handler/codec/http/cors/CorsHandlerTest.java‎

Copy file name to clipboardExpand all lines: codec-http/src/test/java/io/netty/handler/codec/http/cors/CorsHandlerTest.java
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import io.netty.handler.codec.http.FullHttpRequest;
2626
import io.netty.handler.codec.http.DefaultHttpHeadersFactory;
2727
import io.netty.handler.codec.http.DefaultHttpContent;
28+
import io.netty.handler.codec.http.FullHttpResponse;
2829
import io.netty.handler.codec.http.HttpContent;
2930
import io.netty.handler.codec.http.LastHttpContent;
3031
import io.netty.handler.codec.http.HttpMethod;
@@ -665,6 +666,32 @@ public void preflightEmptyLastDiscardedThenNewRequestForwarded() {
665666
assertFalse(ch.finish());
666667
}
667668

669+
@Test
670+
public void varyHeaderIsAppended() {
671+
CorsConfig config = forAnyOrigin().allowCredentials().build();
672+
EmbeddedChannel channel = new EmbeddedChannel(new CorsHandler(config),
673+
new SimpleChannelInboundHandler<Object>() {
674+
@Override
675+
protected void channelRead0(ChannelHandlerContext ctx, Object msg) {
676+
HttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.buffer(0));
677+
response.headers().set(VARY, ACCEPT_ENCODING);
678+
ctx.writeAndFlush(response);
679+
}
680+
});
681+
FullHttpRequest request = createHttpRequest(GET);
682+
request.headers().set(ORIGIN, "http://localhost:7777");
683+
assertFalse(channel.writeInbound(request));
684+
685+
FullHttpResponse response = channel.readOutbound();
686+
687+
List<String> varyHeaders = response.headers().getAll(VARY);
688+
assertThat(varyHeaders).contains(ACCEPT_ENCODING.toString());
689+
assertThat(varyHeaders).contains(ORIGIN.toString());
690+
691+
response.release();
692+
assertFalse(channel.finish());
693+
}
694+
668695
private static HttpResponse simpleRequest(final CorsConfig config, final String origin) {
669696
return simpleRequest(config, origin, null);
670697
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.