From 88d9aed1c104b1cf70a221a235e1a5eae2d1e2d2 Mon Sep 17 00:00:00 2001 From: lee Date: Sat, 16 Dec 2017 17:46:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dhttps=E4=B8=8B=E8=BD=BDbug=20?= =?UTF-8?q?issue#7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/lee/study/down/HttpDownServer.java | 4 ++-- .../down/controller/HttpDownController.java | 7 ++---- ...back.java => DefaultHttpDownCallback.java} | 2 +- .../down/hanndle/HttpDownInitializer.java | 5 ---- .../intercept/HttpDownSniffIntercept.java | 13 ++++++----- .../lee/study/down/model/HttpRequestInfo.java | 12 +++++++++- .../lee/study/down/util/HttpDownUtil.java | 23 ++++++++----------- 7 files changed, 33 insertions(+), 33 deletions(-) rename src/main/java/lee/study/down/dispatch/{HttpDownStartCallback.java => DefaultHttpDownCallback.java} (97%) diff --git a/src/main/java/lee/study/down/HttpDownServer.java b/src/main/java/lee/study/down/HttpDownServer.java index c0bbf971..7c0c8410 100644 --- a/src/main/java/lee/study/down/HttpDownServer.java +++ b/src/main/java/lee/study/down/HttpDownServer.java @@ -14,7 +14,7 @@ import java.util.concurrent.ConcurrentHashMap; import lee.study.down.dispatch.HttpDownErrorCheckTask; import lee.study.down.dispatch.HttpDownProgressEventTask; -import lee.study.down.dispatch.HttpDownStartCallback; +import lee.study.down.dispatch.DefaultHttpDownCallback; import lee.study.down.intercept.BdyBatchDownIntercept; import lee.study.down.intercept.BdyIntercept; import lee.study.down.intercept.HttpDownIntercept; @@ -98,7 +98,7 @@ public static void start(int port) { //下载中的还原之前的状态 httpDownInfo = (HttpDownInfo) ByteUtil.deserialize(taskInfoFile.getPath()); TaskInfo taskInfo = httpDownInfo.getTaskInfo(); - taskInfo.setCallback(new HttpDownStartCallback()); + taskInfo.setCallback(new DefaultHttpDownCallback()); //全部标记为失败,等待重新下载 taskInfo.getChunkInfoList().forEach(chunk -> { if (chunk.getStatus() == 1) { diff --git a/src/main/java/lee/study/down/controller/HttpDownController.java b/src/main/java/lee/study/down/controller/HttpDownController.java index fdbe2146..0736053c 100644 --- a/src/main/java/lee/study/down/controller/HttpDownController.java +++ b/src/main/java/lee/study/down/controller/HttpDownController.java @@ -1,15 +1,12 @@ package lee.study.down.controller; import java.io.File; -import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.UUID; import lee.study.down.HttpDownServer; -import lee.study.down.dispatch.HttpDownCallback; -import lee.study.down.dispatch.HttpDownStartCallback; +import lee.study.down.dispatch.DefaultHttpDownCallback; import lee.study.down.form.DownForm; import lee.study.down.model.ChunkInfo; import lee.study.down.model.HttpDownInfo; @@ -87,7 +84,7 @@ public Map startTask(@RequestBody DownForm downForm) { chunkInfoList.add(chunkInfo); } taskInfo.setChunkInfoList(chunkInfoList); - HttpDownUtil.taskDown(httpDownModel, new HttpDownStartCallback()); + HttpDownUtil.taskDown(httpDownModel, new DefaultHttpDownCallback()); } catch (Exception e) { e.printStackTrace(); map.put("msg", "服务器异常!"); diff --git a/src/main/java/lee/study/down/dispatch/HttpDownStartCallback.java b/src/main/java/lee/study/down/dispatch/DefaultHttpDownCallback.java similarity index 97% rename from src/main/java/lee/study/down/dispatch/HttpDownStartCallback.java rename to src/main/java/lee/study/down/dispatch/DefaultHttpDownCallback.java index a403d12f..9eb661d9 100644 --- a/src/main/java/lee/study/down/dispatch/HttpDownStartCallback.java +++ b/src/main/java/lee/study/down/dispatch/DefaultHttpDownCallback.java @@ -12,7 +12,7 @@ import lee.study.down.util.ByteUtil; import lee.study.down.util.HttpDownUtil; -public class HttpDownStartCallback implements HttpDownCallback { +public class DefaultHttpDownCallback implements HttpDownCallback { @Override public void start(TaskInfo taskInfo) { diff --git a/src/main/java/lee/study/down/hanndle/HttpDownInitializer.java b/src/main/java/lee/study/down/hanndle/HttpDownInitializer.java index 16115f43..5a18da6a 100644 --- a/src/main/java/lee/study/down/hanndle/HttpDownInitializer.java +++ b/src/main/java/lee/study/down/hanndle/HttpDownInitializer.java @@ -1,7 +1,6 @@ package lee.study.down.hanndle; import io.netty.buffer.ByteBuf; -import io.netty.buffer.Unpooled; import io.netty.channel.Channel; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; @@ -11,18 +10,14 @@ import io.netty.handler.codec.http.LastHttpContent; import io.netty.util.ReferenceCountUtil; import java.io.File; -import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; -import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.file.Files; -import java.nio.file.Path; import java.nio.file.Paths; import lee.study.down.dispatch.HttpDownCallback; import lee.study.down.model.ChunkInfo; import lee.study.down.model.TaskInfo; -import lee.study.down.util.HttpDownUtil; import lee.study.proxyee.server.HttpProxyServer; public class HttpDownInitializer extends ChannelInitializer { diff --git a/src/main/java/lee/study/down/intercept/HttpDownSniffIntercept.java b/src/main/java/lee/study/down/intercept/HttpDownSniffIntercept.java index 1f2afb6a..94a66757 100644 --- a/src/main/java/lee/study/down/intercept/HttpDownSniffIntercept.java +++ b/src/main/java/lee/study/down/intercept/HttpDownSniffIntercept.java @@ -11,9 +11,9 @@ import io.netty.handler.codec.http.HttpResponse; import io.netty.handler.codec.http.LastHttpContent; import io.netty.util.ReferenceCountUtil; +import lee.study.down.model.HttpRequestInfo; import lee.study.proxyee.intercept.HttpProxyIntercept; import lee.study.proxyee.intercept.HttpProxyInterceptPipeline; -import lee.study.proxyee.model.HttpRequestInfo; public class HttpDownSniffIntercept extends HttpProxyIntercept { @@ -22,10 +22,10 @@ public class HttpDownSniffIntercept extends HttpProxyIntercept { @Override public void beforeRequest(Channel clientChannel, HttpRequest httpRequest, HttpProxyInterceptPipeline pipeline) throws Exception { + pipeline.setHttpRequest(HttpRequestInfo.adapter(httpRequest)); String contentLength = httpRequest.headers().get(HttpHeaderNames.CONTENT_LENGTH); //缓存request content if (contentLength != null) { - pipeline.setHttpRequest(HttpRequestInfo.adapter(httpRequest)); content = PooledByteBufAllocator.DEFAULT.buffer(); } pipeline.beforeRequest(clientChannel, httpRequest); @@ -69,6 +69,7 @@ public void afterResponse(Channel clientChannel, Channel proxyChannel, HttpRespo downFlag = true; } } + HttpRequestInfo httpRequestInfo = (HttpRequestInfo) pipeline.getHttpRequest(); if (downFlag) { //如果是下载 proxyChannel.close();//关闭嗅探下载连接 System.out.println("=====================下载==========================="); @@ -76,15 +77,15 @@ public void afterResponse(Channel clientChannel, Channel proxyChannel, HttpRespo System.out.println("------------------------------------------------"); System.out.println(httpResponse.toString()); System.out.println("================================================"); + //原始的请求协议 + httpRequestInfo.setRequestProto(pipeline.getRequestProto()); pipeline.afterResponse(clientChannel, proxyChannel, httpResponse); - } else if(pipeline.getHttpRequest() instanceof HttpRequestInfo){ - HttpRequestInfo httpRequestInfo = (HttpRequestInfo) pipeline.getHttpRequest(); + } else { if (httpRequestInfo.content() != null) { httpRequestInfo.setContent(null); } } } - pipeline.getDefault() - .afterResponse(clientChannel, proxyChannel, httpResponse, pipeline); + pipeline.getDefault().afterResponse(clientChannel, proxyChannel, httpResponse, pipeline); } } diff --git a/src/main/java/lee/study/down/model/HttpRequestInfo.java b/src/main/java/lee/study/down/model/HttpRequestInfo.java index c6f200f0..0e0af7dd 100644 --- a/src/main/java/lee/study/down/model/HttpRequestInfo.java +++ b/src/main/java/lee/study/down/model/HttpRequestInfo.java @@ -8,11 +8,12 @@ import io.netty.handler.codec.http.HttpVersion; import java.io.Serializable; import java.util.Map.Entry; -import lee.study.proxyee.model.HttpHeadsInfo; +import lee.study.proxyee.util.ProtoUtil.RequestProto; public class HttpRequestInfo implements HttpRequest, Serializable { private static final long serialVersionUID = -4521453515739581677L; + private RequestProto requestProto; private HttpVer version; private String method; private String uri; @@ -37,6 +38,15 @@ public byte[] content() { return content; } + public RequestProto requestProto() { + return requestProto; + } + + public HttpRequest setRequestProto(RequestProto requestProto) { + this.requestProto = requestProto; + return this; + } + @Override public HttpMethod method() { return new HttpMethod(method); diff --git a/src/main/java/lee/study/down/util/HttpDownUtil.java b/src/main/java/lee/study/down/util/HttpDownUtil.java index 22547af2..8f373429 100644 --- a/src/main/java/lee/study/down/util/HttpDownUtil.java +++ b/src/main/java/lee/study/down/util/HttpDownUtil.java @@ -34,8 +34,8 @@ import lee.study.down.hanndle.HttpDownInitializer; import lee.study.down.model.ChunkInfo; import lee.study.down.model.HttpDownInfo; +import lee.study.down.model.HttpRequestInfo; import lee.study.down.model.TaskInfo; -import lee.study.proxyee.model.HttpRequestInfo; import lee.study.proxyee.server.HttpProxyServer; import lee.study.proxyee.util.ProtoUtil; import lee.study.proxyee.util.ProtoUtil.RequestProto; @@ -88,8 +88,6 @@ public static boolean checkHead(HttpRequest httpRequest, CharSequence headName, public static void startDownTask(TaskInfo taskInfo, HttpRequest httpRequest, HttpResponse httpResponse, Channel clientChannel) { HttpHeaders httpHeaders = httpResponse.headers(); - /*HttpDownInfo httpDownInfo = new HttpDownInfo(taskInfo, - HttpRequestInfo.adapter(httpRequest));*/ HttpDownInfo httpDownInfo = new HttpDownInfo(taskInfo, httpRequest); HttpDownServer.DOWN_CONTENT.put(taskInfo.getId(), httpDownInfo); httpHeaders.clear(); @@ -120,7 +118,8 @@ public static TaskInfo getTaskInfo(HttpRequest httpRequest, HttpHeaders resHeade if (resHeaders.contains(HttpHeaderNames.CONTENT_LENGTH)) { CountDownLatch cdl = new CountDownLatch(1); try { - final ProtoUtil.RequestProto requestProto = ProtoUtil.getRequestProto(httpRequest); + HttpRequestInfo requestInfo = (HttpRequestInfo) httpRequest; + RequestProto requestProto = requestInfo.requestProto(); Bootstrap bootstrap = new Bootstrap(); bootstrap.group(loopGroup) // 注册线程池 .channel(NioSocketChannel.class) // 使用NioSocketChannel来作为连接用的channel类 @@ -153,7 +152,6 @@ public void channelRead(ChannelHandlerContext ctx0, Object msg0) }); ChannelFuture cf = bootstrap.connect(requestProto.getHost(), requestProto.getPort()).sync(); //请求下载一个字节测试是否支持断点下载 - HttpRequestInfo requestInfo = (HttpRequestInfo) httpRequest; httpRequest.headers().set(HttpHeaderNames.RANGE, "bytes=0-0"); cf.channel().writeAndFlush(httpRequest); if (requestInfo.content() != null) { @@ -218,7 +216,6 @@ public static void taskDown(HttpDownInfo httpDownInfo, HttpDownCallback callback throws Exception { TaskInfo taskInfo = httpDownInfo.getTaskInfo(); taskInfo.setCallback(callback); - RequestProto requestProto = ProtoUtil.getRequestProto(httpDownInfo.getRequest()); File file = new File(taskInfo.getFilePath() + File.separator + taskInfo.getFileName()); if (file.exists()) { file.delete(); @@ -238,29 +235,31 @@ public static void taskDown(HttpDownInfo httpDownInfo, HttpDownCallback callback //避免分段下载速度比总的下载速度大太多的问题 chunkInfo.setStatus(1); chunkInfo.setStartTime(taskInfo.getStartTime()); - chunkDown(httpDownInfo, chunkInfo, requestProto); + chunkDown(httpDownInfo, chunkInfo); } } catch (Exception e) { throw e; } } - public static void chunkDown(HttpDownInfo httpDownInfo, ChunkInfo chunkInfo, - RequestProto requestProto) + public static void chunkDown(HttpDownInfo httpDownInfo, ChunkInfo chunkInfo) throws Exception { TaskInfo taskInfo = httpDownInfo.getTaskInfo(); HttpDownCallback callback = taskInfo.getCallback(); + HttpRequestInfo requestInfo = (HttpRequestInfo) httpDownInfo.getRequest(); + RequestProto requestProto = requestInfo.requestProto(); ChannelFuture cf = HttpDownServer.DOWN_BOOT .handler( new HttpDownInitializer(requestProto.getSsl(), taskInfo, chunkInfo, callback)) .connect(requestProto.getHost(), requestProto.getPort()); cf.addListener((ChannelFutureListener) future -> { if (future.isSuccess()) { - HttpRequestInfo requestInfo = (HttpRequestInfo) httpDownInfo.getRequest(); if (httpDownInfo.getTaskInfo().isSupportRange()) { requestInfo.headers() .set(HttpHeaderNames.RANGE, "bytes=" + chunkInfo.getNowStartPosition() + "-" + chunkInfo.getEndPosition()); + } else { + requestInfo.headers().remove(HttpHeaderNames.RANGE); } future.channel().writeAndFlush(httpDownInfo.getRequest()); if (requestInfo.content() != null) { @@ -288,9 +287,7 @@ public static void retryDown(TaskInfo taskInfo, ChunkInfo chunkInfo) chunkInfo.setNowStartPosition(chunkInfo.getOriStartPosition() + chunkInfo.getDownSize()); } HttpDownInfo httpDownInfo = HttpDownServer.DOWN_CONTENT.get(taskInfo.getId()); - RequestProto requestProto = ProtoUtil - .getRequestProto(httpDownInfo.getRequest()); - chunkDown(httpDownInfo, chunkInfo, requestProto); + chunkDown(httpDownInfo, chunkInfo); } public static void safeClose(Channel channel, FileChannel fileChannel) {