Skip to content
This repository has been archived by the owner on Jul 30, 2024. It is now read-only.

Commit

Permalink
修复https下载bug issue#7
Browse files Browse the repository at this point in the history
  • Loading branch information
monkeyWie committed Dec 16, 2017
1 parent 30e5a39 commit 88d9aed
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 33 deletions.
4 changes: 2 additions & 2 deletions src/main/java/lee/study/down/HttpDownServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -87,7 +84,7 @@ public Map<String, String> 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", "服务器异常!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/lee/study/down/hanndle/HttpDownInitializer.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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);
Expand Down Expand Up @@ -69,22 +69,23 @@ public void afterResponse(Channel clientChannel, Channel proxyChannel, HttpRespo
downFlag = true;
}
}
HttpRequestInfo httpRequestInfo = (HttpRequestInfo) pipeline.getHttpRequest();
if (downFlag) { //如果是下载
proxyChannel.close();//关闭嗅探下载连接
System.out.println("=====================下载===========================");
System.out.println(pipeline.getHttpRequest().toString());
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);
}
}
12 changes: 11 additions & 1 deletion src/main/java/lee/study/down/model/HttpRequestInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
23 changes: 10 additions & 13 deletions src/main/java/lee/study/down/util/HttpDownUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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类
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 88d9aed

Please sign in to comment.