📄 resourcedownloaderurlimpl.java
字号:
}
public InputStream
download()
throws ResourceDownloaderException
{
// System.out.println("ResourceDownloader:download - " + getName());
try{
reportActivity(this, getLogIndent() + "Downloading: " + original_url );
try{
this_mon.enter();
if ( download_initiated ){
throw( new ResourceDownloaderException("Download already initiated"));
}
download_initiated = true;
}finally{
this_mon.exit();
}
try{
URL url = new URL( original_url.toString().replaceAll( " ", "%20" ));
// some authentications screw up without an explicit port number here
String protocol = url.getProtocol().toLowerCase();
if ( url.getPort() == -1 && !protocol.equals( "magnet" )){
int target_port;
if ( protocol.equals( "http" )){
target_port = 80;
}else{
target_port = 443;
}
try{
String str = original_url.toString().replaceAll( " ", "%20" );
int pos = str.indexOf( "://" );
pos = str.indexOf( "/", pos+4 );
// might not have a trailing "/"
if ( pos == -1 ){
url = new URL( str + ":" + target_port + "/" );
}else{
url = new URL( str.substring(0,pos) + ":" + target_port + str.substring(pos));
}
}catch( Throwable e ){
Debug.printStackTrace( e );
}
}
url = AddressUtils.adjustURL( url );
try{
if ( auth_supplied ){
SESecurityManager.addPasswordHandler( url, this );
}
for (int i=0;i<2;i++){
try{
HttpURLConnection con;
if ( url.getProtocol().equalsIgnoreCase("https")){
// see ConfigurationChecker for SSL client defaults
HttpsURLConnection ssl_con = (HttpsURLConnection)url.openConnection();
// allow for certs that contain IP addresses rather than dns names
ssl_con.setHostnameVerifier(
new HostnameVerifier()
{
public boolean
verify(
String host,
SSLSession session )
{
return( true );
}
});
con = ssl_con;
}else{
con = (HttpURLConnection) url.openConnection();
}
con.setRequestProperty("User-Agent", Constants.AZUREUS_NAME + " " + Constants.AZUREUS_VERSION);
con.setRequestProperty( "Connection", "close" );
con.addRequestProperty( "Accept-Encoding", "gzip" );
if (postData != null) {
con.setDoOutput(true);
con.setRequestMethod("POST");
OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
wr.write(postData);
wr.flush();
}
con.connect();
int response = con.getResponseCode();
if ((response != HttpURLConnection.HTTP_ACCEPTED) && (response != HttpURLConnection.HTTP_OK)) {
throw( new ResourceDownloaderException("Error on connect for '" + url.toString() + "': " + Integer.toString(response) + " " + con.getResponseMessage()));
}
boolean gzip = false;
try{
this_mon.enter();
input_stream = con.getInputStream();
String encoding = con.getHeaderField( "content-encoding");
gzip = encoding != null && encoding.equalsIgnoreCase("gzip");
if ( gzip ){
input_stream = new GZIPInputStream( input_stream );
}
}finally{
this_mon.exit();
}
ByteArrayOutputStream baos;
try{
byte[] buf = new byte[BUFFER_SIZE];
int total_read = 0;
// unfortunately not all servers set content length
/* From Apache's mod_deflate doc:
* http://httpd.apache.org/docs/2.0/mod/mod_deflate.html
Note on Content-Length
If you evaluate the request body yourself, don't trust the
Content-Length header! The Content-Length header reflects
the length of the incoming data from the client and not the
byte count of the decompressed data stream.
*/
int size = gzip ? -1 : con.getContentLength();
baos = size>0?new ByteArrayOutputStream(size):new ByteArrayOutputStream();
while( !cancel_download ){
int read = input_stream.read(buf);
if ( read > 0 ){
baos.write(buf, 0, read);
total_read += read;
if ( size > 0){
informPercentDone(( 100 * total_read ) / size );
}
}else{
break;
}
}
// if we've got a size, make sure we've read all of it
if ( size > 0 && total_read != size ){
if ( total_read > size ){
// this has been seen with UPnP linksys - more data is read than
// the content-length has us believe is coming (1 byte in fact...)
Debug.out( "Inconsistent stream length for '" + original_url + "': expected = " + size + ", actual = " + total_read );
}else{
throw( new IOException( "Premature end of stream" ));
}
}
}finally{
input_stream.close();
}
InputStream res = new ByteArrayInputStream( baos.toByteArray());
if ( informComplete( res )){
return( res );
}
throw( new ResourceDownloaderException("Contents downloaded but rejected: '" + original_url + "'" ));
}catch( SSLException e ){
if ( i == 0 ){
if ( SESecurityManager.installServerCertificates( url ) != null ){
// certificate has been installed
continue; // retry with new certificate
}
}
throw( e );
}
}
throw( new ResourceDownloaderException("Should never get here" ));
}finally{
if ( auth_supplied ){
SESecurityManager.removePasswordHandler( url, this );
}
}
}catch (java.net.MalformedURLException e){
throw( new ResourceDownloaderException("Exception while parsing URL '" + original_url + "':" + e.getMessage(), e));
}catch (java.net.UnknownHostException e){
throw( new ResourceDownloaderException("Exception while initializing download of '" + original_url + "': Unknown Host '" + e.getMessage() + "'", e));
}catch (java.io.IOException e ){
throw( new ResourceDownloaderException("I/O Exception while downloading '" + original_url + "':" + e.toString(), e ));
}
}catch( Throwable e ){
ResourceDownloaderException rde;
if ( e instanceof ResourceDownloaderException ){
rde = (ResourceDownloaderException)e;
}else{
rde = new ResourceDownloaderException( "Unexpected error", e );
}
informFailed(rde);
throw( rde );
}
}
public void
cancel()
{
setCancelled();
cancel_download = true;
try{
this_mon.enter();
if ( input_stream != null ){
try{
input_stream.close();
}catch( Throwable e ){
}
}
}finally{
this_mon.exit();
}
informFailed( new ResourceDownloaderException( "Download cancelled" ));
}
public PasswordAuthentication
getAuthentication(
String realm,
URL tracker )
{
if ( user_name == null || password == null ){
String user_info = tracker.getUserInfo();
if ( user_info == null ){
return( null );
}
String user_bit = user_info;
String pw_bit = "";
int pos = user_info.indexOf(':');
if ( pos != -1 ){
user_bit = user_info.substring(0,pos);
pw_bit = user_info.substring(pos+1);
}
return( new PasswordAuthentication( user_bit, pw_bit.toCharArray()));
}
return( new PasswordAuthentication( user_name, password.toCharArray()));
}
public void
setAuthenticationOutcome(
String realm,
URL tracker,
boolean success )
{
}
public void
clearPasswords()
{
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -