try{
SESecurityManager.setThreadPasswordHandler( this );
// System.out.println( "Connecting to " + url + ": " + Thread.currentThread().getId());
HttpURLConnection connection;
int response;
while( true ){
URL target = redirected_url==null?original_url:redirected_url;
connection = (HttpURLConnection)target.openConnection();
connection.setRequestProperty( "Connection", "Keep-Alive" );
connection.setRequestProperty( "User-Agent", user_agent );
for (int i=0;i<prop_names.length;i++){
connection.setRequestProperty( prop_names[i], prop_values[i] );
}
int time_remaining = listener.getPermittedTime();
if ( time_remaining > 0 ){
Java15Utils.setConnectTimeout( connection, time_remaining );
}
connection.connect();
time_remaining = listener.getPermittedTime();
if ( time_remaining < 0 ){
throw( new IOException( "Timeout during connect" ));
}
Java15Utils.setReadTimeout( connection, time_remaining );
connected = true;
response = connection.getResponseCode();
if ( response == HttpURLConnection.HTTP_ACCEPTED ||
response == HttpURLConnection.HTTP_OK ||
response == HttpURLConnection.HTTP_PARTIAL ){
if ( redirected_url != null ){
consec_redirect_fails = 0;
}
break;
}
if ( redirected_url == null ){
break;
}
// try again with original URL
consec_redirect_fails++;
redirected_url = null;
}
URL final_url = connection.getURL();
if ( consec_redirect_fails < 10 && !original_url.toExternalForm().equals( final_url.toExternalForm())){
redirected_url = final_url;
}
last_response = response;
last_response_retry_after_secs = -1;
if ( response == 503 ){
// webseed support for temp unavail - read the retry_after
long retry_after_date = new Long(connection.getHeaderFieldDate("Retry-After", -1L)).longValue();
if ( retry_after_date <= -1 ){
last_response_retry_after_secs = connection.getHeaderFieldInt("Retry-After", -1);
}else{
last_response_retry_after_secs = (int)((retry_after_date - System.currentTimeMillis())/1000);
if ( last_response_retry_after_secs < 0 ){
last_response_retry_after_secs = -1;
}
}
}
is = connection.getInputStream();
if ( response == HttpURLConnection.HTTP_ACCEPTED ||
response == HttpURLConnection.HTTP_OK ||
response == HttpURLConnection.HTTP_PARTIAL ){
int pos = 0;
byte[] buffer = null;
int buffer_pos = 0;
int buffer_len = 0;
while( pos < length ){
if ( buffer == null ){
buffer = listener.getBuffer();
buffer_pos = listener.getBufferPosition();
buffer_len = listener.getBufferLength();
}
listener.setBufferPosition( buffer_pos );
int to_read = buffer_len - buffer_pos;
int permitted = listener.getPermittedBytes();
if ( permitted < to_read ){
to_read = permitted;
}
int len = is.read( buffer, buffer_pos, to_read );
if ( len < 0 ){
break;
}
listener.reportBytesRead( len );
pos += len;
buffer_pos += len;
if ( buffer_pos == buffer_len ){
listener.done();
buffer = null;
buffer_pos = 0;
}
}
if ( pos != length ){
String log_str;
if ( buffer == null ){
log_str = "No buffer assigned";
}else{
log_str = new String( buffer, 0, length );
if ( log_str.length() > 64 ){
log_str = log_str.substring( 0, 64 );
}
}
outcome = "Connection failed: data too short - " + length + "/" + pos + " [" + log_str + "]";
throw( new ExternalSeedException( outcome ));
}
outcome = "read " + pos + " bytes";
// System.out.println( "download length: " + pos );
}else{
outcome = "Connection failed: " + connection.getResponseMessage();
ExternalSeedException error = new ExternalSeedException( outcome );
error.setPermanentFailure( true );