发送get请求时,为什么我会收到一半的消息?

时间:2019-09-05 11:37:16

标签: linux http get netty

我使用Netty框架构建了一个HTTP服务。我有一个get请求处理程序,它将发送一个index.html文档。但是现在我只能接收一半的消息。我不知道其中是否有问题我不知道的配置或其他问题。

Netty版本为4.1.29.Final

Java版本是1.8

Linux版本是centos 7

同一程序,当我将其部署在本地Windows系统上时,可以正常接收消息。
同一个程序,当我在本地Windows系统上部署它时,可以正常收到该消息。 我尝试搜索它,但没有任何想法。

下面是我启动该服务的代码。

EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
    ServerBootstrap b = new ServerBootstrap();
    b.option(ChannelOption.SO_BACKLOG, 1024);
    b.childOption(ChannelOption.TCP_NODELAY,true);
    b.childOption(ChannelOption.SO_KEEPALIVE,true);
    b.group(bossGroup, workerGroup)
            .channel(NioServerSocketChannel.class)
            .handler(new LoggingHandler(LogLevel.WARN))
            .childHandler(new ScmjHttpServerInitializer());

    Channel ch = b.bind(PORT).sync().channel();
    logger.info("netty http server listening on port "+ PORT);
    ch.closeFuture().sync();
} finally {
    bossGroup.shutdownGracefully();
    workerGroup.shutdownGracefully();
    ThreadPools.getInstance().destory();
}

下面是我的ChannelPipeline程序

public class ScmjHttpServerInitializer extends ChannelInitializer<SocketChannel> {
    @Override
    public void initChannel(SocketChannel ch) {
        ChannelPipeline p = ch.pipeline();
        p.addLast(new HttpServerCodec());
        p.addLast(new HttpObjectAggregator(1024*1024));
        p.addLast(new HttpServerExpectContinueHandler());
        p.addLast(new ScmjHttpServerHandler());
    }
}

下面是我的处理程序

@Sharable
public class ScmjHttpServerHandler extends SimpleChannelInboundHandler<HttpObject> {

    private static final Logger logger = LoggerFactory.getLogger(ScmjHttpServerHandler.class);

    private static byte[] IndexHtml = null;

    private static final String FAVICON_ICO = "/favicon.ico";
    private static final AsciiString CONTENT_TYPE = AsciiString.cached("Content-Type");
    private static final AsciiString CONTENT_LENGTH = AsciiString.cached("Content-Length");
    private static final AsciiString CONNECTION = AsciiString.cached("Connection");
    private static final AsciiString KEEP_ALIVE = AsciiString.cached("keep-alive");
    private static final AsciiString CHARSET = AsciiString.cached("charset");

    @Override
    public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
        if (msg instanceof HttpRequest){
            HttpRequest request = (HttpRequest) msg;
            HttpHeaders headers = request.headers();
            HttpMethod method = request.method();
            if (method.equals(HttpMethod.GET)){
                if(request.uri().startsWith("/FAVICON_ICO")){
                    ctx.close();
                    return;
                }
                if(IndexHtml == null){
                    try{
                        File file = new File(this.getClass().getResource("/").getPath() + "/index.html");
                        String contentType = new String(Files.readAllBytes(file.toPath()));
                        IndexHtml = contentType.getBytes();
                    }catch (Exception e){
                        String path = ScmjHttpServerHandler.class.getProtectionDomain().getCodeSource()
                                .getLocation().getFile();
                        path = java.net.URLDecoder.decode(path, "UTF-8");
                        File file = new File(path);
                        StringBuilder libPath = new StringBuilder(file.getParent());
                        libPath.append("/index.html");
                        File indexFile = new File(libPath.toString());
                        String contentType = new String(Files.readAllBytes(indexFile.toPath()));
                        IndexHtml = contentType.getBytes();
                    }
                }
                FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(IndexHtml));
                response.headers().set(CONTENT_TYPE, "text/html");
                response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes());
                ctx.writeAndFlush(response);
                ctx.close().sync();
            }else if (method.equals(HttpMethod.POST)){
                //....
            }else{
                //....
            }
        }else{
            //....
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ctx.close();
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) {
        ctx.flush();
    }

    private String getContentType(HttpHeaders headers){
        String typeStr = headers.get("Content-Type");
        if(typeStr == null)
            return "";
        String[] list = typeStr.split(";");
        return list == null ? "" : list[0];
    }
}

此索引文件是一个170 kb的html。

我的问题是centos 7系统,我不知道需要提供什么信息。如果需要单独提供,我可以添加内容,请告诉我如何操作

我想知道,当我从centos请求一个170kb的文件时。我刚刚收到了超过70kb的内容。

我需要配置什么才能改变这种情况?

0 个答案:

没有答案