mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-16 11:21:18 +03:00
FileDownloader: made it play nice with 301 status code
This commit is contained in:
@ -28,6 +28,8 @@
|
|||||||
*/
|
*/
|
||||||
package cc.arduino.utils.network;
|
package cc.arduino.utils.network;
|
||||||
|
|
||||||
|
import org.apache.commons.codec.binary.Base64;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
@ -37,8 +39,6 @@ import java.net.SocketTimeoutException;
|
|||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.Observable;
|
import java.util.Observable;
|
||||||
|
|
||||||
import org.apache.commons.codec.binary.Base64;
|
|
||||||
|
|
||||||
public class FileDownloader extends Observable {
|
public class FileDownloader extends Observable {
|
||||||
|
|
||||||
public enum Status {
|
public enum Status {
|
||||||
@ -48,7 +48,7 @@ public class FileDownloader extends Observable {
|
|||||||
COMPLETE, //
|
COMPLETE, //
|
||||||
CANCELLED, //
|
CANCELLED, //
|
||||||
ERROR, //
|
ERROR, //
|
||||||
};
|
}
|
||||||
|
|
||||||
private Status status;
|
private Status status;
|
||||||
private long initialSize;
|
private long initialSize;
|
||||||
@ -120,13 +120,10 @@ public class FileDownloader extends Observable {
|
|||||||
|
|
||||||
setStatus(Status.CONNECTING);
|
setStatus(Status.CONNECTING);
|
||||||
|
|
||||||
HttpURLConnection connection = (HttpURLConnection) downloadUrl
|
HttpURLConnection connection = (HttpURLConnection) downloadUrl.openConnection();
|
||||||
.openConnection();
|
|
||||||
|
|
||||||
if (downloadUrl.getUserInfo() != null) {
|
if (downloadUrl.getUserInfo() != null) {
|
||||||
String auth = "Basic "
|
String auth = "Basic " + new String(new Base64().encode(downloadUrl.getUserInfo().getBytes()));
|
||||||
+ new String(new Base64().encode(downloadUrl
|
|
||||||
.getUserInfo().getBytes()));
|
|
||||||
connection.setRequestProperty("Authorization", auth);
|
connection.setRequestProperty("Authorization", auth);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -137,9 +134,27 @@ public class FileDownloader extends Observable {
|
|||||||
// Connect
|
// Connect
|
||||||
connection.connect();
|
connection.connect();
|
||||||
int resp = connection.getResponseCode();
|
int resp = connection.getResponseCode();
|
||||||
if (resp < 200 || resp >= 300)
|
|
||||||
throw new IOException(
|
if (resp == HttpURLConnection.HTTP_MOVED_PERM || resp == HttpURLConnection.HTTP_MOVED_TEMP) {
|
||||||
"Recevied invalid http status code from server: " + resp);
|
String newUrl = connection.getHeaderField("Location");
|
||||||
|
|
||||||
|
// open the new connnection again
|
||||||
|
connection = (HttpURLConnection) new URL(newUrl).openConnection();
|
||||||
|
if (downloadUrl.getUserInfo() != null) {
|
||||||
|
String auth = "Basic " + new String(new Base64().encode(downloadUrl.getUserInfo().getBytes()));
|
||||||
|
connection.setRequestProperty("Authorization", auth);
|
||||||
|
}
|
||||||
|
|
||||||
|
connection.setRequestProperty("Range", "bytes=" + initialSize + "-");
|
||||||
|
connection.setConnectTimeout(5000);
|
||||||
|
|
||||||
|
connection.connect();
|
||||||
|
resp = connection.getResponseCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (resp < 200 || resp >= 300) {
|
||||||
|
throw new IOException("Recevied invalid http status code from server: " + resp);
|
||||||
|
}
|
||||||
|
|
||||||
// Check for valid content length.
|
// Check for valid content length.
|
||||||
long len = connection.getContentLength();
|
long len = connection.getContentLength();
|
||||||
|
Reference in New Issue
Block a user