1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-01-20 09:52:13 +01:00

Refactoring HttpConnectionManger and request-id

This commit is contained in:
Mattia Bertorello 2019-07-03 15:27:50 +02:00
parent 5157688590
commit 00818af181
No known key found for this signature in database
GPG Key ID: CE1FB2BE91770F24

View File

@ -3,8 +3,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.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.apache.logging.log4j.LogManager;
import org.slf4j.LoggerFactory; import org.apache.logging.log4j.Logger;
import processing.app.BaseNoGui; import processing.app.BaseNoGui;
import processing.app.PreferencesData; import processing.app.PreferencesData;
@ -14,17 +14,17 @@ import java.net.HttpURLConnection;
import java.net.Proxy; import java.net.Proxy;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.util.UUID;
import java.util.function.Consumer; import java.util.function.Consumer;
public class HttpConnectionManager { public class HttpConnectionManager {
private static Logger log = LoggerFactory.getLogger(HttpConnectionManager.class); private static Logger log = LogManager.getLogger(HttpConnectionManager.class);
private static final String userAgent;
private static final int connectTimeout;
private final URL requestURL; private final URL requestURL;
private final String userAgent; private final String id;
private int connectTimeout;
static {
public HttpConnectionManager(URL requestURL) {
this.requestURL = requestURL;
final String defaultUserAgent = String.format( final String defaultUserAgent = String.format(
"ArduinoIDE/%s (%s; %s; %s; %s) Java/%s (%s)", "ArduinoIDE/%s (%s; %s; %s; %s) Java/%s (%s)",
BaseNoGui.VERSION_NAME, BaseNoGui.VERSION_NAME,
@ -35,15 +35,26 @@ public class HttpConnectionManager {
System.getProperty("java.version"), System.getProperty("java.version"),
System.getProperty("java.vendor") System.getProperty("java.vendor")
); );
this.userAgent = PreferencesData.get("http.user_agent", defaultUserAgent); userAgent = PreferencesData.get("http.user_agent", defaultUserAgent);
int connectTimeoutFromConfig = 5000;
try { try {
this.connectTimeout = connectTimeoutFromConfig =
Integer.parseInt( Integer.parseInt(
PreferencesData.get("http.connection_timeout", "5000")); PreferencesData.get("http.connection_timeout", "5000"));
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
log.warn( log.warn(
"Cannot parse the http.connection_timeout configuration switch to default 5000 milliseconds", e.getCause()); "Cannot parse the http.connection_timeout configuration switch to default {} milliseconds", connectTimeoutFromConfig, e.getCause());
this.connectTimeout = 5000; }
connectTimeout = connectTimeoutFromConfig;
}
public HttpConnectionManager(URL requestURL) {
this.requestURL = requestURL;
if (requestURL.getHost().endsWith("arduino.cc")) {
final String idString = PreferencesData.get("update.id", "0");
id = Long.toString(Long.parseLong(idString));
} else {
id = null;
} }
} }
@ -61,7 +72,6 @@ public class HttpConnectionManager {
private HttpURLConnection makeConnection(URL requestURL, int movedTimes, private HttpURLConnection makeConnection(URL requestURL, int movedTimes,
Consumer<HttpURLConnection> beforeConnection) throws IOException, URISyntaxException, ScriptException, NoSuchMethodException { Consumer<HttpURLConnection> beforeConnection) throws IOException, URISyntaxException, ScriptException, NoSuchMethodException {
log.info("Prepare http request to " + requestURL);
if (movedTimes > 3) { if (movedTimes > 3) {
log.warn("Too many redirect " + requestURL); log.warn("Too many redirect " + requestURL);
throw new IOException("Too many redirect " + requestURL); throw new IOException("Too many redirect " + requestURL);
@ -71,9 +81,15 @@ public class HttpConnectionManager {
.getProxyFor(requestURL.toURI()); .getProxyFor(requestURL.toURI());
log.debug("Using proxy {}", proxy); log.debug("Using proxy {}", proxy);
final String requestId = UUID.randomUUID().toString()
.toUpperCase().replace("-", "").substring(0, 16);
HttpURLConnection connection = (HttpURLConnection) requestURL HttpURLConnection connection = (HttpURLConnection) requestURL
.openConnection(proxy); .openConnection(proxy);
connection.setRequestProperty("User-agent", userAgent); connection.setRequestProperty("User-agent", userAgent);
connection.setRequestProperty("X-Request-ID", requestId);
if (id != null) {
connection.setRequestProperty("X-ID", id);
}
if (requestURL.getUserInfo() != null) { if (requestURL.getUserInfo() != null) {
String auth = "Basic " + new String( String auth = "Basic " + new String(
new Base64().encode(requestURL.getUserInfo().getBytes())); new Base64().encode(requestURL.getUserInfo().getBytes()));
@ -86,10 +102,12 @@ public class HttpConnectionManager {
beforeConnection.accept(connection); beforeConnection.accept(connection);
// Connect // Connect
log.info("Connect to {} with method {}", requestURL, connection.getRequestMethod()); log.info("Connect to {}, method={}, request id={}", requestURL, connection.getRequestMethod(),requestId);
connection.connect(); connection.connect();
int resp = connection.getResponseCode(); int resp = connection.getResponseCode();
log.info("Request complete URL=\"{}\", method={}, response code={}, request id={}, headers={}",
requestURL, connection.getRequestMethod(), resp, requestId, StringUtils.join(connection.getHeaderFields()));
if (resp == HttpURLConnection.HTTP_MOVED_PERM if (resp == HttpURLConnection.HTTP_MOVED_PERM
|| resp == HttpURLConnection.HTTP_MOVED_TEMP) { || resp == HttpURLConnection.HTTP_MOVED_TEMP) {
@ -99,13 +117,9 @@ public class HttpConnectionManager {
return this.makeConnection(newUrl, movedTimes + 1, beforeConnection); return this.makeConnection(newUrl, movedTimes + 1, beforeConnection);
} }
log.info("The response code {}, headers {}", resp, StringUtils.join(connection.getHeaderFields()));
return connection; return connection;
} }
public void setConnectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
}
} }