use of lee.study.down.model.HttpRequestInfo in project proxyee-down by monkeyWie.
the class AbstractHttpDownBootstrap method startChunkDown.
protected void startChunkDown(ChunkInfo chunkInfo, int updateStatus) throws Exception {
HttpRequestInfo requestInfo = (HttpRequestInfo) httpDownInfo.getRequest();
RequestProto requestProto = requestInfo.requestProto();
LOGGER.debug("开始下载:" + chunkInfo);
Bootstrap bootstrap = new Bootstrap().channel(NioSocketChannel.class).group(clientLoopGroup).handler(new HttpDownInitializer(requestProto.getSsl(), this, chunkInfo));
if (httpDownInfo.getProxyConfig() != null) {
// 代理服务器解析DNS和连接
bootstrap.resolver(NoopAddressResolverGroup.INSTANCE);
}
if (callback != null) {
callback.onChunkConnecting(httpDownInfo, chunkInfo);
}
ChannelFuture cf = bootstrap.connect(requestProto.getHost(), requestProto.getPort());
chunkInfo.setStatus(updateStatus);
// 重置最后下载时间
chunkInfo.setLastDownTime(System.currentTimeMillis());
cf.addListener((ChannelFutureListener) future -> {
if (future.isSuccess()) {
synchronized (chunkInfo) {
setChannel(chunkInfo, future.channel());
}
synchronized (requestInfo) {
LOGGER.debug("下载连接成功:channelId[" + future.channel().id() + "]\t" + chunkInfo);
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) {
HttpContent content = new DefaultLastHttpContent();
content.content().writeBytes(requestInfo.content());
future.channel().writeAndFlush(content);
}
} else {
LOGGER.debug("下载连接失败:" + chunkInfo);
chunkInfo.setStatus(HttpDownStatus.FAIL);
future.channel().close();
}
});
}
use of lee.study.down.model.HttpRequestInfo in project proxyee-down by monkeyWie.
the class HttpDownUtil method getTaskInfo.
/**
* 检测是否支持断点下载
*/
public static TaskInfo getTaskInfo(HttpRequest httpRequest, HttpHeaders resHeaders, ProxyConfig proxyConfig, SslContext clientSslCtx, NioEventLoopGroup loopGroup) throws Exception {
HttpResponse httpResponse = null;
if (resHeaders == null) {
httpResponse = getResponse(httpRequest, proxyConfig, clientSslCtx, loopGroup);
// 处理重定向
if ((httpResponse.status().code() + "").indexOf("30") == 0) {
// TODO 302重定向乱码 https://link.gimhoy.com/googledrive/aHR0cHM6Ly9kcml2ZS5nb29nbGUuY29tL29wZW4/aWQ9MThlVmNKeEhwaE40RUpGTUowSk10bWNXOVhCcWJhVE1k.jpg
String redirectUrl = httpResponse.headers().get(HttpHeaderNames.LOCATION);
HttpRequestInfo requestInfo = (HttpRequestInfo) httpRequest;
requestInfo.headers().remove("Host");
requestInfo.setUri(redirectUrl);
RequestProto requestProto = ProtoUtil.getRequestProto(requestInfo);
requestInfo.headers().set("Host", requestProto.getHost());
requestInfo.setRequestProto(requestProto);
return getTaskInfo(httpRequest, null, proxyConfig, clientSslCtx, loopGroup);
}
resHeaders = httpResponse.headers();
}
TaskInfo taskInfo = new TaskInfo().setId(UUID.randomUUID().toString()).setFileName(getDownFileName(httpRequest, resHeaders)).setTotalSize(getDownFileSize(resHeaders));
// chunked编码不支持断点下载
if (resHeaders.contains(HttpHeaderNames.CONTENT_LENGTH)) {
if (httpResponse == null) {
httpResponse = getResponse(httpRequest, proxyConfig, clientSslCtx, loopGroup);
}
// 206表示支持断点下载
if (httpResponse.status().equals(HttpResponseStatus.PARTIAL_CONTENT)) {
taskInfo.setSupportRange(true);
}
}
return taskInfo;
}
use of lee.study.down.model.HttpRequestInfo in project proxyee-down by monkeyWie.
the class HttpDownSniffIntercept method beforeRequest.
@Override
public void beforeRequest(Channel clientChannel, HttpContent httpContent, HttpProxyInterceptPipeline pipeline) throws Exception {
if (content != null) {
ByteBuf temp = httpContent.content().slice();
content.writeBytes(temp);
if (httpContent instanceof LastHttpContent) {
try {
byte[] contentBts = new byte[content.readableBytes()];
content.readBytes(contentBts);
((HttpRequestInfo) pipeline.getHttpRequest()).setContent(contentBts);
} finally {
ReferenceCountUtil.release(content);
}
}
}
pipeline.beforeRequest(clientChannel, httpContent);
}
use of lee.study.down.model.HttpRequestInfo in project proxyee-down by monkeyWie.
the class DownContent method setUrl.
public static List<TaskInfoForm> setUrl(List<TaskInfo> taskInfoList) {
List<TaskInfoForm> ret = new ArrayList<>();
for (TaskInfo taskInfo : taskInfoList) {
TaskInfoForm taskInfoForm = new TaskInfoForm();
BeanUtils.copyProperties(taskInfo, taskInfoForm);
HttpRequestInfo httpRequest = (HttpRequestInfo) ContentManager.DOWN.getDownInfo(taskInfo.getId()).getRequest();
String uri = httpRequest.uri();
String host = httpRequest.requestProto().getHost();
String url = (uri.indexOf("/") == 0 ? host : "") + uri;
if (url.indexOf("http://") != 0 && url.indexOf("https://") != 0) {
url = (httpRequest.requestProto().getSsl() ? "https://" : "http://") + url;
}
taskInfoForm.setUrl(url);
ret.add(taskInfoForm);
}
return ret;
}
use of lee.study.down.model.HttpRequestInfo in project proxyee-down by monkeyWie.
the class HttpDownController method commonBuildTask.
public static ResultInfo commonBuildTask(BuildTaskForm form) throws Exception {
ResultInfo resultInfo = new ResultInfo();
Map<String, String> heads = new LinkedHashMap<>();
if (form.getHeads() != null) {
for (Map<String, String> head : form.getHeads()) {
String key = head.get("key");
String value = head.get("value");
if (!StringUtils.isEmpty(head.get("key")) && !StringUtils.isEmpty(head.get("value"))) {
heads.put(key, value);
}
}
}
try {
HttpRequestInfo requestInfo = HttpDownUtil.buildGetRequest(form.getUrl(), heads, form.getBody());
TaskInfo taskInfo = HttpDownUtil.getTaskInfo(requestInfo, null, ContentManager.CONFIG.get().getSecProxyConfig(), HttpDownConstant.clientSslContext, HttpDownConstant.clientLoopGroup);
HttpDownInfo httpDownInfo = new HttpDownInfo(taskInfo, requestInfo, ContentManager.CONFIG.get().getSecProxyConfig());
ContentManager.DOWN.putBoot(httpDownInfo);
resultInfo.setData(taskInfo.getId());
} catch (MalformedURLException e) {
resultInfo.setStatus(ResultStatus.BAD.getCode()).setMsg("链接格式不正确");
} catch (TimeoutException e) {
resultInfo.setStatus(ResultStatus.BAD.getCode()).setMsg("连接超时,请重试");
} catch (Exception e) {
throw new RuntimeException("buildTask error:" + form.toString(), e);
}
return resultInfo;
}
Aggregations