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 — Discard the following HttpContent for preflight request (#15941) · netty/netty@28e8017

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
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@
2222
import io.netty.channel.ChannelHandlerContext;
2323
import io.netty.channel.ChannelPromise;
2424
import io.netty.handler.codec.http.DefaultFullHttpResponse;
25+
import io.netty.handler.codec.http.HttpContent;
2526
import io.netty.handler.codec.http.HttpHeaderNames;
2627
import io.netty.handler.codec.http.HttpHeaderValues;
2728
import io.netty.handler.codec.http.HttpHeaders;
2829
import io.netty.handler.codec.http.DefaultHttpHeadersFactory;
2930
import io.netty.handler.codec.http.HttpRequest;
3031
import io.netty.handler.codec.http.HttpResponse;
3132
import io.netty.handler.codec.http.HttpUtil;
33+
import io.netty.util.ReferenceCountUtil;
3234
import io.netty.util.internal.logging.InternalLogger;
3335
import io.netty.util.internal.logging.InternalLoggerFactory;
3436

@@ -58,6 +60,7 @@ public class CorsHandler extends ChannelDuplexHandler {
5860
private HttpRequest request;
5961
private final List<CorsConfig> configList;
6062
private final boolean isShortCircuit;
63+
private boolean consumeContent;
6164

6265
/**
6366
* Creates a new instance with a single {@link CorsConfig}.
@@ -87,13 +90,28 @@ public void channelRead(final ChannelHandlerContext ctx, final Object msg) throw
8790
config = getForOrigin(origin);
8891
if (isPreflightRequest(request)) {
8992
handlePreflight(ctx, request);
93+
// Enable consumeContent so that all following HttpContent
94+
// for this request will be released and not propagated downstream.
95+
consumeContent = true;
9096
return;
9197
}
9298
if (isShortCircuit && !(origin == null || config != null)) {
9399
forbidden(ctx, request);
100+
consumeContent = true;
94101
return;
95102
}
103+
104+
// This request is forwarded, stop discarding
105+
consumeContent = false;
106+
ctx.fireChannelRead(msg);
107+
return;
108+
}
109+
110+
if (consumeContent && (msg instanceof HttpContent)) {
111+
ReferenceCountUtil.release(msg);
112+
return;
96113
}
114+
97115
ctx.fireChannelRead(msg);
98116
}
99117

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
+153Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,17 @@
2121
import io.netty.channel.embedded.EmbeddedChannel;
2222
import io.netty.handler.codec.http.DefaultFullHttpRequest;
2323
import io.netty.handler.codec.http.DefaultFullHttpResponse;
24+
import io.netty.handler.codec.http.DefaultLastHttpContent;
2425
import io.netty.handler.codec.http.FullHttpRequest;
2526
import io.netty.handler.codec.http.DefaultHttpHeadersFactory;
27+
import io.netty.handler.codec.http.DefaultHttpContent;
28+
import io.netty.handler.codec.http.HttpContent;
29+
import io.netty.handler.codec.http.LastHttpContent;
2630
import io.netty.handler.codec.http.HttpMethod;
2731
import io.netty.handler.codec.http.HttpResponse;
2832
import io.netty.handler.codec.http.HttpUtil;
2933
import io.netty.util.AsciiString;
34+
import io.netty.util.CharsetUtil;
3035
import io.netty.util.ReferenceCountUtil;
3136
import org.junit.jupiter.api.Test;
3237

@@ -512,6 +517,154 @@ public void simpleRequestDoNotAllowPrivateNetwork() {
512517
assertTrue(ReferenceCountUtil.release(response));
513518
}
514519

520+
@Test
521+
public void preflightEmptyLastDiscarded() {
522+
CorsConfig config = forOrigin("http://allowed").build();
523+
EmbeddedChannel ch = new EmbeddedChannel(new CorsHandler(config));
524+
525+
FullHttpRequest preflight = new DefaultFullHttpRequest(HTTP_1_1, OPTIONS, "/test");
526+
preflight.headers().set(ORIGIN, "http://allowed");
527+
preflight.headers().set(ACCESS_CONTROL_REQUEST_METHOD, "GET");
528+
529+
assertFalse(ch.writeInbound(preflight));
530+
531+
Object outbound = ch.readOutbound();
532+
assertNotNull(outbound); // preflight response
533+
534+
LastHttpContent lastHttpContent = LastHttpContent.EMPTY_LAST_CONTENT;
535+
assertFalse(ch.writeInbound(lastHttpContent));
536+
537+
// Nothing should have been forwarded
538+
assertNull(ch.readInbound());
539+
540+
assertFalse(ch.finish());
541+
}
542+
543+
@Test
544+
public void preflightSecondEmptyLastForwardedAfterFirstDiscard() {
545+
CorsConfig config = forOrigin("http://allowed").build();
546+
EmbeddedChannel ch = new EmbeddedChannel(new CorsHandler(config));
547+
548+
FullHttpRequest preflight = new DefaultFullHttpRequest(HTTP_1_1, OPTIONS, "/test");
549+
preflight.headers().set(ORIGIN, "http://allowed");
550+
preflight.headers().set(ACCESS_CONTROL_REQUEST_METHOD, "GET");
551+
552+
assertFalse(ch.writeInbound(preflight));
553+
ReferenceCountUtil.release(ch.readOutbound());
554+
555+
LastHttpContent first = LastHttpContent.EMPTY_LAST_CONTENT;
556+
LastHttpContent second = LastHttpContent.EMPTY_LAST_CONTENT;
557+
558+
assertFalse(ch.writeInbound(first));
559+
560+
assertFalse(ch.writeInbound(second));
561+
562+
assertNull(ch.readInbound());
563+
assertFalse(ch.finish());
564+
}
565+
566+
@Test
567+
public void preflightSecondNonEmptyLastDiscarded() {
568+
CorsConfig config = forOrigin("http://allowed").build();
569+
EmbeddedChannel ch = new EmbeddedChannel(new CorsHandler(config));
570+
571+
FullHttpRequest preflight = new DefaultFullHttpRequest(HTTP_1_1, OPTIONS, "/test");
572+
preflight.headers().set(ORIGIN, "http://allowed");
573+
preflight.headers().set(ACCESS_CONTROL_REQUEST_METHOD, "GET");
574+
575+
assertFalse(ch.writeInbound(preflight));
576+
ReferenceCountUtil.release(ch.readOutbound());
577+
578+
LastHttpContent first = LastHttpContent.EMPTY_LAST_CONTENT;
579+
LastHttpContent second = new DefaultLastHttpContent(
580+
Unpooled.copiedBuffer("test message", CharsetUtil.UTF_8));
581+
582+
assertFalse(ch.writeInbound(first));
583+
assertFalse(ch.writeInbound(second));
584+
assertNull(ch.readInbound());
585+
assertFalse(ch.finish());
586+
}
587+
588+
@Test
589+
public void preflightNonEmptyLastForwarded() {
590+
CorsConfig config = forOrigin("http://allowed").build();
591+
EmbeddedChannel ch = new EmbeddedChannel(new CorsHandler(config));
592+
593+
FullHttpRequest preflight = new DefaultFullHttpRequest(HTTP_1_1, OPTIONS, "/x");
594+
preflight.headers().set(ORIGIN, "http://allowed");
595+
preflight.headers().set(ACCESS_CONTROL_REQUEST_METHOD, "GET");
596+
597+
assertFalse(ch.writeInbound(preflight));
598+
Object outbound = ch.releaseOutbound();
599+
assertNotNull(outbound);
600+
601+
LastHttpContent nonEmpty = new DefaultLastHttpContent(Unpooled.copiedBuffer("x", CharsetUtil.UTF_8));
602+
assertFalse(ch.writeInbound(nonEmpty));
603+
604+
Object inbound = ch.readInbound();
605+
assertNull(inbound);
606+
607+
assertFalse(ch.finish());
608+
}
609+
610+
@Test
611+
public void testNormalRequestForwarded() {
612+
CorsConfig config = forOrigin("http://allowed").build();
613+
EmbeddedChannel ch = new EmbeddedChannel(new CorsHandler(config));
614+
615+
FullHttpRequest req = new DefaultFullHttpRequest(HTTP_1_1, GET, "/test");
616+
req.headers().set(ORIGIN, "http://allowed");
617+
618+
assertTrue(ch.writeInbound(req));
619+
620+
LastHttpContent last = LastHttpContent.EMPTY_LAST_CONTENT;
621+
assertTrue(ch.writeInbound(last));
622+
623+
Object firstInbound = ch.readInbound();
624+
Object secondInbound = ch.readInbound();
625+
626+
assertNotNull(firstInbound);
627+
assertNotNull(secondInbound);
628+
629+
assertNull(ch.readInbound());
630+
assertFalse(ch.finish());
631+
}
632+
633+
@Test
634+
public void preflightEmptyLastDiscardedThenNewRequestForwarded() {
635+
CorsConfig config = forOrigin("http://allowed").build();
636+
EmbeddedChannel ch = new EmbeddedChannel(new CorsHandler(config));
637+
638+
// Preflight request
639+
FullHttpRequest preflight = new DefaultFullHttpRequest(HTTP_1_1, OPTIONS, "/pre");
640+
preflight.headers().set(ORIGIN, "http://allowed");
641+
preflight.headers().set(ACCESS_CONTROL_REQUEST_METHOD, "GET");
642+
assertFalse(ch.writeInbound(preflight));
643+
Object preflightResp = ch.readOutbound();
644+
assertNotNull(preflightResp);
645+
ReferenceCountUtil.release(preflightResp);
646+
647+
// Empty last content should be discarded
648+
assertFalse(ch.writeInbound(LastHttpContent.EMPTY_LAST_CONTENT));
649+
assertNull(ch.readInbound());
650+
651+
// New request should be forwarded
652+
FullHttpRequest req = new DefaultFullHttpRequest(HTTP_1_1, GET, "/next");
653+
req.headers().set(ORIGIN, "http://allowed");
654+
assertTrue(ch.writeInbound(req));
655+
656+
Object firstInbound = ch.readInbound();
657+
assertNotNull(firstInbound);
658+
659+
HttpContent content = new DefaultHttpContent(Unpooled.copiedBuffer("test message", CharsetUtil.UTF_8));
660+
assertTrue(ch.writeInbound(content));
661+
Object secondInbound = ch.readInbound();
662+
assertNotNull(secondInbound);
663+
664+
assertNull(ch.readInbound());
665+
assertFalse(ch.finish());
666+
}
667+
515668
private static HttpResponse simpleRequest(final CorsConfig config, final String origin) {
516669
return simpleRequest(config, origin, null);
517670
}

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.