From d9e2b50953a07e1aaf37678e095d3de17def6373 Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Wed, 19 Feb 2020 19:46:50 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20channelRead0=20=E5=A4=84=E7=90=86?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E7=A7=BB=E5=8A=A8=E5=88=B0=E4=B8=9A=E5=8A=A1?= =?UTF-8?q?=E7=BA=BF=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: MiaoWoo --- packages/plugins/src/MiaoConsole.ts | 53 ++++++++++++++++------------- 1 file changed, 29 insertions(+), 24 deletions(-) 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) + } + } + })) } } })