Fixing CorsHandler response Content-Length
Motivation: https://github.com/netty/netty/issues/7253 Modifications: Adding `Content-Length: 0` to `CorsHandler.forbidden()` and `CorsHandler.handlePreflight()` Result: Contexts that are terminated by the CorsHandler will always include a Content-Length header
This commit is contained in:
parent
06da0ceb64
commit
ad548a6a0a
@ -22,6 +22,7 @@ import io.netty.channel.ChannelHandlerContext;
|
|||||||
import io.netty.channel.ChannelPromise;
|
import io.netty.channel.ChannelPromise;
|
||||||
import io.netty.handler.codec.http.DefaultFullHttpResponse;
|
import io.netty.handler.codec.http.DefaultFullHttpResponse;
|
||||||
import io.netty.handler.codec.http.HttpHeaderNames;
|
import io.netty.handler.codec.http.HttpHeaderNames;
|
||||||
|
import io.netty.handler.codec.http.HttpHeaderValues;
|
||||||
import io.netty.handler.codec.http.HttpHeaders;
|
import io.netty.handler.codec.http.HttpHeaders;
|
||||||
import io.netty.handler.codec.http.HttpRequest;
|
import io.netty.handler.codec.http.HttpRequest;
|
||||||
import io.netty.handler.codec.http.HttpResponse;
|
import io.netty.handler.codec.http.HttpResponse;
|
||||||
@ -81,6 +82,9 @@ public class CorsHandler extends ChannelDuplexHandler {
|
|||||||
setMaxAge(response);
|
setMaxAge(response);
|
||||||
setPreflightHeaders(response);
|
setPreflightHeaders(response);
|
||||||
}
|
}
|
||||||
|
if (!response.headers().contains(HttpHeaderNames.CONTENT_LENGTH)) {
|
||||||
|
response.headers().set(HttpHeaderNames.CONTENT_LENGTH, HttpHeaderValues.ZERO);
|
||||||
|
}
|
||||||
release(request);
|
release(request);
|
||||||
respond(ctx, request, response);
|
respond(ctx, request, response);
|
||||||
}
|
}
|
||||||
@ -205,8 +209,10 @@ public class CorsHandler extends ChannelDuplexHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static void forbidden(final ChannelHandlerContext ctx, final HttpRequest request) {
|
private static void forbidden(final ChannelHandlerContext ctx, final HttpRequest request) {
|
||||||
|
HttpResponse response = new DefaultFullHttpResponse(request.protocolVersion(), FORBIDDEN);
|
||||||
|
response.headers().set(HttpHeaderNames.CONTENT_LENGTH, HttpHeaderValues.ZERO);
|
||||||
release(request);
|
release(request);
|
||||||
respond(ctx, request, new DefaultFullHttpResponse(request.protocolVersion(), FORBIDDEN));
|
respond(ctx, request, response);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void respond(
|
private static void respond(
|
||||||
|
@ -161,6 +161,7 @@ public class CorsHandlerTest {
|
|||||||
final HttpResponse response = preflightRequest(config, "http://localhost:8888", "content-type, xheader1");
|
final HttpResponse response = preflightRequest(config, "http://localhost:8888", "content-type, xheader1");
|
||||||
assertThat(response.headers().get(of("CustomHeader")), equalTo("somevalue"));
|
assertThat(response.headers().get(of("CustomHeader")), equalTo("somevalue"));
|
||||||
assertThat(response.headers().get(VARY), equalTo(ORIGIN.toString()));
|
assertThat(response.headers().get(VARY), equalTo(ORIGIN.toString()));
|
||||||
|
assertThat(response.headers().get(CONTENT_LENGTH), is("0"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -276,6 +277,7 @@ public class CorsHandlerTest {
|
|||||||
final CorsConfig config = forOrigin("http://localhost:8080").shortCircuit().build();
|
final CorsConfig config = forOrigin("http://localhost:8080").shortCircuit().build();
|
||||||
final HttpResponse response = simpleRequest(config, "http://localhost:7777");
|
final HttpResponse response = simpleRequest(config, "http://localhost:7777");
|
||||||
assertThat(response.status(), is(FORBIDDEN));
|
assertThat(response.status(), is(FORBIDDEN));
|
||||||
|
assertThat(response.headers().get(CONTENT_LENGTH), is("0"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Loading…
Reference in New Issue
Block a user