2017-09-26 7 views
0

netty의 CorsHandler을 (를) 연결을 끊으려면 어떻게합니까? 원점은 통과하지만 원점이 허용되지 않으면 닫히지 않습니다. CorsHandler 인스턴스와 함께이 서버를 설치했습니다.netty와의 연결을 끊는 방법 CorsHandler

import io.netty.bootstrap.ServerBootstrap; 
import io.netty.channel.*; 
import io.netty.channel.nio.NioEventLoopGroup; 
import io.netty.channel.socket.nio.NioServerSocketChannel; 
import io.netty.handler.codec.http.HttpMethod; 
import io.netty.handler.codec.http.HttpObjectAggregator; 
import io.netty.handler.codec.http.HttpServerCodec; 
import io.netty.handler.codec.http.cors.CorsConfigBuilder; 
import io.netty.handler.codec.http.cors.CorsHandler; 
import io.netty.handler.logging.LogLevel; 
import io.netty.handler.logging.LoggingHandler; 

/** 
* Runs netty with a CORS handler on 8080 
*/ 
public class NettyCorsApp { 
    private static final int PORT = 8080; 

    public static void main(String[] args) throws Exception { 
     EventLoopGroup eventLoopGroup = new NioEventLoopGroup(); 
     try { 
      ServerBootstrap bootstrap = new ServerBootstrap() 
        .group(eventLoopGroup) 
        .handler(new LoggingHandler(LogLevel.INFO)) 
        .childHandler(new ChannelInitializer<Channel>() { 
         @Override 
         protected void initChannel(Channel ch) throws Exception { 
          ChannelPipeline pipeline = ch.pipeline(); 
          pipeline.addLast(new HttpServerCodec()); 
          pipeline.addLast(new HttpObjectAggregator(1024 * 1024)); // 1MB 
          pipeline.addLast(new CorsHandler(
            CorsConfigBuilder.forOrigin("http://example.com") 
              .allowedRequestMethods(HttpMethod.POST) 
              .build()) 
          ); 
         } 
        }) 
        .channel(NioServerSocketChannel.class); 
      Channel channel = bootstrap.bind(PORT).sync().channel(); 
      channel.closeFuture().sync(); 
     } finally { 
      eventLoopGroup.shutdownGracefully(); 
     } 
    } 
} 

CORS 검사를 통과 한 원본을 요청하면 CorsHandler는 예상대로 연결을 닫습니다.

$ curl -sv -X OPTIONS -H 'Origin: http://example.com' -H 'Access-Control-Request-Method: POST' http://localhost:8080 
* Rebuilt URL to: http://localhost:8080/ 
* Trying ::1... 
* TCP_NODELAY set 
* Connected to localhost (::1) port 8080 (#0) 
> OPTIONS/HTTP/1.1 
> Host: localhost:8080 
> User-Agent: curl/7.54.0 
> Accept: */* 
> Origin: http://example.com 
> Access-Control-Request-Method: POST 
> 
< HTTP/1.1 200 OK 
< access-control-allow-origin: http://example.com 
< vary: origin 
< access-control-allow-methods: POST 
< access-control-allow-headers: 
< access-control-max-age: 0 
< date: "Tue, 26 Sep 2017 20:03:53 GMT" 
< content-length: 0 
< 
* Connection #0 to host localhost left intact 

CORS 검사를 통과하지 못한 원본에서 요청하면 연결을 닫지 않습니다.

$ curl -sv -X OPTIONS -H 'Origin: http://invalid.com' -H 'Access-Control-Request-Method: POST' http://localhost:8080 
* Rebuilt URL to: http://localhost:8080/ 
* Trying ::1... 
* TCP_NODELAY set 
* Connected to localhost (::1) port 8080 (#0) 
> OPTIONS/HTTP/1.1 
> Host: localhost:8080 
> User-Agent: curl/7.54.0 
> Accept: */* 
> Origin: http://invalid.com 
> Access-Control-Request-Method: POST 
> 
< HTTP/1.1 200 OK 
* no chunk, no close, no size. Assume close to signal end 
< 

netty에 버그가있을 수 있습니다. 제출하면 버그가 될 수 있습니다.

+0

나는 그물코 4.1.6 - 최종 및 4.1.16-최종, 같은 결과를 테스트했다. – mgbelisle

+0

Stack Overflow는 버그보고 사이트가 아니므로 github의 netty issues 섹션에서 버그를보고해야합니다. https://github.com/netty/netty/issues 또한 상단 예제에서 netty는 연결을 닫지 않습니다 (예 : 거기에 ano 연결 헤더가 있습니다.) – Ferrybig

+0

당신 말이 맞아요, 지적 해 주셔서 고맙습니다. 나는 연결 유지와 vs 1.1 및 vs 1.1의 비교를 잘 이해하지 못했지만 지금은 당신이 말하는 것을보고 있습니다. 나는 여기에 제출 한 netty의 버그를 추적했다 : https://github.com/netty/netty/pull/7261 내가 처음으로 말했던 것처럼 그것이 버그인지 알지 못했다. 게시하다. 내가 버그라고 알고 있다면 확실히 제출하지 않았을 것입니다. – mgbelisle

답변

1

@Ferrybig이 지적한 바에 따르면 CorsHandler은 요청에 Connection: close 헤더가 없기 때문에 의도적으로 연결을 닫지 않습니다. Http 1.0은 tcp 연결이 기본적으로 닫혀 있어야하고 http 1.1 (defafdults를 말리는)가 기본적으로 열려 있어야한다고 가정합니다.

잘못된 원인으로 컬이 종료되지 않은 이유는 CorsHandler이 응답에 Content-Length 헤더를 포함하지 않았기 때문입니다. 이는 버그입니다.

https://github.com/netty/netty/pull/7261