BrowserAdapter:

-IDE now uses new HTTPDownloader download core


git-svn-id: svn://svn.jdownloader.org/jdownloader/trunk@49636 ebf7c1c2-ba36-0410-9fe8-c592906822b4

Former-commit-id: 2641a19321dd59d9a938d9c9775534196fe20e94
This commit is contained in:
jiaz 2024-08-22 09:27:07 +00:00
parent a76e647ca4
commit 95808566ca
2 changed files with 141 additions and 207 deletions

View File

@ -15,29 +15,41 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package jd.plugins;
import org.appwork.net.protocol.http.HTTPConstants;
import org.appwork.storage.config.JsonConfig;
import org.appwork.utils.net.HTTPHeader;
import org.jdownloader.settings.GeneralSettings;
import jd.controlling.downloadcontroller.SingleDownloadController;
import jd.http.Browser;
import jd.http.Request;
import jd.http.URLConnectionAdapter;
import jd.parser.html.Form;
import jd.plugins.download.DownloadInterface;
import jd.plugins.download.DownloadLinkDownloadable;
import jd.plugins.download.Downloadable;
import jd.plugins.download.raf.HTTPDownloader;
import jd.plugins.download.raf.OldRAFDownload;
import org.appwork.net.protocol.http.HTTPConstants;
import org.appwork.storage.config.JsonConfig;
import org.appwork.utils.DebugMode;
import org.appwork.utils.net.HTTPHeader;
import org.jdownloader.settings.GeneralSettings;
public class BrowserAdapter {
public static final int ERROR_REDIRECTED = -1;
@Deprecated
public static final int ERROR_REDIRECTED = -1;
private static final boolean NEW_CORE = DebugMode.TRUE_IN_IDE_ELSE_FALSE;
private static DownloadInterface getDownloadInterface(Downloadable downloadable, Request request, boolean resumeEnabled, int pluginConnections) throws Exception {
if (false) {
// was fine all the years, so better optimize handling in download system to better support edge cases with downloading
// content-encoding connections
request.getHeaders().put(new HTTPHeader(HTTPConstants.HEADER_REQUEST_ACCEPT_ENCODING, "identity", false));
final DownloadInterface dl;
if (NEW_CORE) {
dl = new HTTPDownloader(downloadable, request);
} else {
if (false) {
// was fine all the years, so better optimize handling in download system to better support edge cases with downloading
// content-encoding connections
request.getHeaders().put(new HTTPHeader(HTTPConstants.HEADER_REQUEST_ACCEPT_ENCODING, "identity", false));
}
dl = new jd.plugins.download.raf.OldRAFDownload(downloadable, request);
}
final jd.plugins.download.raf.OldRAFDownload dl = new jd.plugins.download.raf.OldRAFDownload(downloadable, request);
final int customizedConnections = downloadable.getChunks();
final int setConnections;
if (pluginConnections > 0) {
@ -73,8 +85,14 @@ public class BrowserAdapter {
setConnections = Math.min(customizedConnections, Math.abs(pluginConnections));
}
}
dl.setChunkNum(setConnections);
dl.setResume(resumeEnabled);
if (dl instanceof HTTPDownloader) {
((HTTPDownloader) dl).setChunkNum(setConnections);
((HTTPDownloader) dl).setMaxChunksNum(Math.abs(pluginConnections));
((HTTPDownloader) dl).setResume(resumeEnabled);
} else if (dl instanceof OldRAFDownload) {
((OldRAFDownload) dl).setChunkNum(setConnections);
((OldRAFDownload) dl).setResume(resumeEnabled);
}
return dl;
}
@ -89,201 +107,120 @@ public class BrowserAdapter {
return null;
}
public static DownloadInterface openDownload(Browser br, DownloadLink downloadLink, String link) throws Exception {
return openDownload(br, getDownloadable(downloadLink, br), br.createRequest(link), false, 1, false);
@Deprecated
public static DownloadInterface openDownload(final Browser br, DownloadLink downloadLink, String link) throws Exception {
return openDownload(br, downloadLink, br.createRequest(link), false, 1);
}
public static DownloadInterface openDownload(Browser br, DownloadLink downloadLink, String url, String postdata) throws Exception {
return openDownload(br, downloadLink, url, postdata, false, 1);
}
/**
*
* @param br
* @param downloadLink
* @param url
* @param postdata
* @param resume
* true|false, if chunks over 1 it must be true!
* @param chunks
* 0 = unlimited, chunks must start with negative sign otherwise it forces value to be used instead of up to value.
* @return
* @throws Exception
*/
public static DownloadInterface openDownload(Browser br, DownloadLink downloadLink, String url, String postdata, boolean resume, int chunks) throws Exception {
return openDownload(br, getDownloadable(downloadLink, br), br.createPostRequest(url, postdata), resume, chunks, false);
}
/**
*
* @param br
* @param downloadLink
* @param link
* @param resume
* true|false, if chunks over 1 it must be true!
* @param chunks
* 0 = unlimited, chunks must start with negative sign otherwise it forces value to be used instead of up to value.
* @return
* @throws Exception
*/
public static DownloadInterface openDownload(Browser br, DownloadLink downloadLink, String link, boolean resume, int chunks) throws Exception {
return openDownload(br, getDownloadable(downloadLink, br), br.createRequest(link), resume, chunks, false);
}
/**
*
* @param br
* @param downloadLink
* @param link
* @param resume
* true|false, if chunks over 1 it must be true!
* @param chunks
* 0 = unlimited, chunks must start with negative sign otherwise it forces value to be used instead of up to value.
* @param forceRedirectWait
* forces thread sleep between redirects (even if they are not the same, useful for (multi)hosters which wait via series
* redirects)
* @return
* @throws Exception
*/
public static DownloadInterface openDownload(Browser br, DownloadLink downloadLink, String link, boolean resume, int chunks, final boolean forceRedirectWait) throws Exception {
return openDownload(br, getDownloadable(downloadLink, br), br.createRequest(link), resume, chunks, forceRedirectWait);
}
/**
*
* @param br
* @param downloadLink
* @param form
* @param resume
* true|false, if chunks over 1 it must be true!
* @param chunks
* 0 = unlimited, chunks must start with negative sign otherwise it forces value to be used instead of up to value.
* @return
* @throws Exception
*/
public static DownloadInterface openDownload(Browser br, DownloadLink downloadLink, Form form, boolean resume, int chunks) throws Exception {
return openDownload(br, getDownloadable(downloadLink, br), br.createRequest(form), resume, chunks, false);
}
public static DownloadInterface openDownload(Browser br, DownloadLink downloadLink, Form form) throws Exception {
return openDownload(br, downloadLink, form, false, 1);
}
/**
*
* @param br
* @param downloadLink
* @param request
* @param resume
* @param chunks
* @return
* @throws Exception
*/
public static DownloadInterface openDownload(final Browser br, final DownloadLink downloadLink, final Request request, final boolean resume, final int chunks) throws Exception {
return openDownload(br, getDownloadable(downloadLink, br), request, resume, chunks, false);
}
/**
*
* @param br
* @param downloadable
* @param request
* @param resume
* true|false, if chunks over 1 it must be true!
* @param chunks
* 0 = unlimited, chunks must start with negative sign otherwise it forces value to be used instead of up to value.
* @return
* @throws Exception
*/
public static DownloadInterface openDownload(Browser br, Downloadable downloadable, Request request, boolean resume, int chunks) throws Exception {
return openDownload(br, downloadable, request, resume, chunks, false);
if (NEW_CORE) {
final String originalUrl = br.getURL();
int maxRedirects = 10;
final DownloadInterface dl = getDownloadInterface(downloadable, request, resume, chunks);
downloadable.setDownloadInterface(dl);
while (maxRedirects-- > 0) {
dl.setInitialRequest(request);
final URLConnectionAdapter connection = dl.connect(br);
if (connection.getRequest().getLocation() == null) {
return dl;
} else {
connection.disconnect();
request = br.createRedirectFollowingRequest(request);
if (originalUrl != null) {
request.getHeaders().put(HTTPConstants.HEADER_REQUEST_REFERER, originalUrl);
}
}
}
throw new PluginException(LinkStatus.ERROR_PLUGIN_DEFECT, "Redirectloop");
} else {
String originalUrl = br.getURL();
DownloadInterface dl = getDownloadInterface(downloadable, request, resume, chunks);
downloadable.setDownloadInterface(dl);
final PluginForHost plugin;
final DownloadLink downloadLink;
if (downloadable instanceof DownloadLinkDownloadable) {
plugin = ((DownloadLinkDownloadable) downloadable).getPlugin();
downloadLink = ((DownloadLinkDownloadable) downloadable).getDownloadLink();
} else {
plugin = null;
downloadLink = null;
}
try {
dl.connect(br);
} catch (PluginException handle) {
try {
dl.getConnection().disconnect();
} catch (Throwable ignore) {
}
if (handle.getValue() == ERROR_REDIRECTED) {
final int redirect_max = 10;
int redirect_count = 0;
String lastRedirectUrl = null;
while (redirect_count++ < redirect_max) {
request = br.createRedirectFollowingRequest(request);
final String redirectUrl = request.getUrl();
if (lastRedirectUrl != null && redirectUrl.equals(lastRedirectUrl)) {
// some providers don't like fast redirects, as they use this for preparing final file. lets add short wait
// based on
// retry count
if (plugin != null && downloadLink != null) {
plugin.sleep(redirect_count * 250l, downloadLink);
} else {
Thread.sleep(redirect_count * 250l);
}
}
if (originalUrl != null) {
request.getHeaders().put("Referer", originalUrl);
}
dl = getDownloadInterface(downloadable, request, resume, chunks);
downloadable.setDownloadInterface(dl);
try {
dl.connect(br);
break;
} catch (PluginException handle2) {
try {
dl.getConnection().disconnect();
} catch (Throwable ignore) {
}
if (handle2.getValue() == ERROR_REDIRECTED) {
lastRedirectUrl = redirectUrl;
continue;
} else {
throw handle2;
}
}
}
if (redirect_count++ >= redirect_max) {
throw new PluginException(LinkStatus.ERROR_FATAL, "Redirectloop");
}
} else {
throw handle;
}
} catch (Exception forward) {
try {
dl.getConnection().disconnect();
} catch (Throwable ignore) {
}
throw forward;
}
return dl;
}
}
/**
*
* @param br
* @param downloadable
* @param request
* @param resume
* true|false, if chunks over 1 it must be true!
* @param chunks
* 0 = unlimited, chunks must start with negative sign otherwise it forces value to be used instead of up to value.
* @param forceRedirectWait
* forces thread sleep between redirects (even if they are not the same, useful for (multi)hosters which wait via series
* redirects)
* @return
* @throws Exception
*/
public static DownloadInterface openDownload(Browser br, Downloadable downloadable, Request request, boolean resume, int chunks, boolean forceRedirectWait) throws Exception {
String originalUrl = br.getURL();
DownloadInterface dl = getDownloadInterface(downloadable, request, resume, chunks);
downloadable.setDownloadInterface(dl);
final PluginForHost plugin;
final DownloadLink downloadLink;
if (downloadable instanceof DownloadLinkDownloadable) {
plugin = ((DownloadLinkDownloadable) downloadable).getPlugin();
downloadLink = ((DownloadLinkDownloadable) downloadable).getDownloadLink();
} else {
plugin = null;
downloadLink = null;
}
try {
dl.connect(br);
} catch (PluginException handle) {
try {
dl.getConnection().disconnect();
} catch (Throwable ignore) {
}
if (handle.getValue() == ERROR_REDIRECTED) {
final int redirect_max = 10;
int redirect_count = 0;
String lastRedirectUrl = null;
while (redirect_count++ < redirect_max) {
request = br.createRedirectFollowingRequest(request);
final String redirectUrl = request.getUrl();
if (lastRedirectUrl != null && (forceRedirectWait || redirectUrl.equals(lastRedirectUrl))) {
// some providers don't like fast redirects, as they use this for preparing final file. lets add short wait based on
// retry count
if (plugin != null && downloadLink != null) {
plugin.sleep(redirect_count * 250l, downloadLink);
} else {
Thread.sleep(redirect_count * 250l);
}
}
if (originalUrl != null) {
request.getHeaders().put("Referer", originalUrl);
}
dl = getDownloadInterface(downloadable, request, resume, chunks);
downloadable.setDownloadInterface(dl);
try {
dl.connect(br);
break;
} catch (PluginException handle2) {
try {
dl.getConnection().disconnect();
} catch (Throwable ignore) {
}
if (handle2.getValue() == ERROR_REDIRECTED) {
lastRedirectUrl = redirectUrl;
continue;
} else {
throw handle2;
}
}
}
if (redirect_count++ >= redirect_max) {
throw new PluginException(LinkStatus.ERROR_FATAL, "Redirectloop");
}
} else {
throw handle;
}
} catch (Exception forward) {
try {
dl.getConnection().disconnect();
} catch (Throwable ignore) {
}
throw forward;
}
return dl;
public static DownloadInterface openDownload(final Browser br, final DownloadLink downloadLink, final Request request, final boolean resume, final int chunks) throws Exception {
return openDownload(br, getDownloadable(downloadLink, br), request, resume, chunks);
}
public static DownloadInterface openDownload(Browser br, DownloadLink downloadLink, String url, String postdata, boolean resume, int chunks) throws Exception {
return openDownload(br, downloadLink, br.createPostRequest(url, postdata), resume, chunks);
}
public static DownloadInterface openDownload(Browser br, DownloadLink downloadLink, String link, boolean resume, int chunks) throws Exception {
return openDownload(br, downloadLink, br.createRequest(link), resume, chunks);
}
public static DownloadInterface openDownload(Browser br, DownloadLink downloadLink, Form form, boolean resume, int chunks) throws Exception {
return openDownload(br, downloadLink, br.createRequest(form), resume, chunks);
}
}

View File

@ -32,7 +32,4 @@ public class RAFDownload extends OldRAFDownload {
super(BrowserAdapter.getDownloadable(downloadLink, null), request);
}
@Deprecated
public void addChunksDownloading(int i) {
}
}