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