1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-12-01 12:24:14 +01:00

Set correct user-agent when performing HTTP requests

This commit is contained in:
Cristian Maglie 2016-03-08 11:11:52 +01:00
parent 60ba8661d8
commit 1f3c8b9f6c

View File

@ -32,6 +32,8 @@ package cc.arduino.utils.network;
import cc.arduino.net.CustomProxySelector; import cc.arduino.net.CustomProxySelector;
import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Base64;
import org.apache.commons.compress.utils.IOUtils; import org.apache.commons.compress.utils.IOUtils;
import processing.app.BaseNoGui;
import processing.app.PreferencesData; import processing.app.PreferencesData;
import java.io.File; import java.io.File;
@ -66,12 +68,15 @@ public class FileDownloader extends Observable {
private final File outputFile; private final File outputFile;
private InputStream stream = null; private InputStream stream = null;
private Exception error; private Exception error;
private String userAgent;
public FileDownloader(URL url, File file) { public FileDownloader(URL url, File file) {
downloadUrl = url; downloadUrl = url;
outputFile = file; outputFile = file;
downloaded = 0; downloaded = 0;
initialSize = 0; initialSize = 0;
userAgent = "ArduinoIDE/" + BaseNoGui.VERSION_NAME + " Java/"
+ System.getProperty("java.version");
} }
public long getInitialSize() { public long getInitialSize() {
@ -151,7 +156,7 @@ public class FileDownloader extends Observable {
} }
HttpURLConnection connection = (HttpURLConnection) downloadUrl.openConnection(proxy); HttpURLConnection connection = (HttpURLConnection) downloadUrl.openConnection(proxy);
connection.setRequestProperty("User-agent", userAgent);
if (downloadUrl.getUserInfo() != null) { if (downloadUrl.getUserInfo() != null) {
String auth = "Basic " + new String(new Base64().encode(downloadUrl.getUserInfo().getBytes())); String auth = "Basic " + new String(new Base64().encode(downloadUrl.getUserInfo().getBytes()));
connection.setRequestProperty("Authorization", auth); connection.setRequestProperty("Authorization", auth);
@ -172,6 +177,7 @@ public class FileDownloader extends Observable {
// open the new connnection again // open the new connnection again
connection = (HttpURLConnection) newUrl.openConnection(proxy); connection = (HttpURLConnection) newUrl.openConnection(proxy);
connection.setRequestProperty("User-agent", userAgent);
if (downloadUrl.getUserInfo() != null) { if (downloadUrl.getUserInfo() != null) {
String auth = "Basic " + new String(new Base64().encode(downloadUrl.getUserInfo().getBytes())); String auth = "Basic " + new String(new Base64().encode(downloadUrl.getUserInfo().getBytes()));
connection.setRequestProperty("Authorization", auth); connection.setRequestProperty("Authorization", auth);