diff --git a/packages/plugins/src/MiaoConsole.ts b/packages/plugins/src/MiaoConsole.ts index 4d744a46..e615e159 100644 --- a/packages/plugins/src/MiaoConsole.ts +++ b/packages/plugins/src/MiaoConsole.ts @@ -159,6 +159,7 @@ const WebSocketServerProtocolHandler = Java.type('io.netty.handler.codec.http.we const SimpleChannelInboundHandler = Java.type('io.netty.channel.SimpleChannelInboundHandler') const FullHttpRequestMatcher = TypeParameterMatcher.get(base.getClass('io.netty.handler.codec.http.FullHttpRequest')) const File = Java.type('java.io.File') +const Runnable = Java.type('java.lang.Runnable') const RandomAccessFile = Java.type('java.io.RandomAccessFile') const DefaultFileRegion = Java.type('io.netty.channel.DefaultFileRegion') const ChannelFutureListener = Java.type('io.netty.channel.ChannelFutureListener') @@ -170,30 +171,34 @@ const HttpRequestHandler = Java.extend(SimpleChannelInboundHandler, { if ('/ws' == request.getUri()) { ctx.fireChannelRead(request.retain()) } else { - if (HttpHeaders.is100ContinueExpected(request)) { - ctx.writeAndFlush(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.CONTINUE)) - } - let filename = request.getUri().split('?')[0].substr(1) - let file = new File('/home/project/TSWorkSpace/ms/packages/plugins/public', filename || 'index.html') - if (!file.exists() || !file.isFile()) { - ctx.write(new DefaultHttpResponse(request.getProtocolVersion(), HttpResponseStatus.NOT_FOUND)) - ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT).addListener(ChannelFutureListener.CLOSE) - return - } - let response = new DefaultHttpResponse(request.getProtocolVersion(), HttpResponseStatus.OK) - response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/html charset=UTF-8") - let raf = new RandomAccessFile(file, 'r') - let keepAlive = HttpHeaders.isKeepAlive(request) - if (keepAlive) { - response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, file.length()) - response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE) - } - ctx.write(response) - ctx.write(new DefaultFileRegion(raf.getChannel(), 0, raf.length())) - let future = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT) - if (!keepAlive) { - future.addListener(ChannelFutureListener.CLOSE) - } + ctx.executor().execute(new Runnable({ + run: () => { + if (HttpHeaders.is100ContinueExpected(request)) { + ctx.writeAndFlush(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.CONTINUE)) + } + let filename = request.getUri().split('?')[0].substr(1) + let file = new File('/home/project/WebWorkSpace/MiaoConsole', filename || 'index.html') + if (!file.exists() || !file.isFile()) { + ctx.write(new DefaultHttpResponse(request.getProtocolVersion(), HttpResponseStatus.NOT_FOUND)) + ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT).addListener(ChannelFutureListener.CLOSE) + return + } + let response = new DefaultHttpResponse(request.getProtocolVersion(), HttpResponseStatus.OK) + response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/html charset=UTF-8") + let raf = new RandomAccessFile(file, 'r') + let keepAlive = HttpHeaders.isKeepAlive(request) + if (keepAlive) { + response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, file.length()) + response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE) + } + ctx.write(response) + ctx.write(new DefaultFileRegion(raf.getChannel(), 0, raf.length())) + let future = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT) + if (!keepAlive) { + future.addListener(ChannelFutureListener.CLOSE) + } + } + })) } } })