Skip to content

Commit 00818af

Browse files
author
Mattia Bertorello
committed
Refactoring HttpConnectionManger and request-id
1 parent 5157688 commit 00818af

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

Diff for: arduino-core/src/cc/arduino/utils/network/HttpConnectionManager.java

+32-18
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import cc.arduino.net.CustomProxySelector;
44
import org.apache.commons.codec.binary.Base64;
55
import org.apache.commons.lang3.StringUtils;
6-
import org.slf4j.Logger;
7-
import org.slf4j.LoggerFactory;
6+
import org.apache.logging.log4j.LogManager;
7+
import org.apache.logging.log4j.Logger;
88
import processing.app.BaseNoGui;
99
import processing.app.PreferencesData;
1010

@@ -14,17 +14,17 @@
1414
import java.net.Proxy;
1515
import java.net.URISyntaxException;
1616
import java.net.URL;
17+
import java.util.UUID;
1718
import java.util.function.Consumer;
1819

1920
public class HttpConnectionManager {
20-
private static Logger log = LoggerFactory.getLogger(HttpConnectionManager.class);
21+
private static Logger log = LogManager.getLogger(HttpConnectionManager.class);
22+
private static final String userAgent;
23+
private static final int connectTimeout;
2124
private final URL requestURL;
22-
private final String userAgent;
23-
private int connectTimeout;
25+
private final String id;
2426

25-
26-
public HttpConnectionManager(URL requestURL) {
27-
this.requestURL = requestURL;
27+
static {
2828
final String defaultUserAgent = String.format(
2929
"ArduinoIDE/%s (%s; %s; %s; %s) Java/%s (%s)",
3030
BaseNoGui.VERSION_NAME,
@@ -35,15 +35,26 @@ public HttpConnectionManager(URL requestURL) {
3535
System.getProperty("java.version"),
3636
System.getProperty("java.vendor")
3737
);
38-
this.userAgent = PreferencesData.get("http.user_agent", defaultUserAgent);
38+
userAgent = PreferencesData.get("http.user_agent", defaultUserAgent);
39+
int connectTimeoutFromConfig = 5000;
3940
try {
40-
this.connectTimeout =
41+
connectTimeoutFromConfig =
4142
Integer.parseInt(
4243
PreferencesData.get("http.connection_timeout", "5000"));
4344
} catch (NumberFormatException e) {
4445
log.warn(
45-
"Cannot parse the http.connection_timeout configuration switch to default 5000 milliseconds", e.getCause());
46-
this.connectTimeout = 5000;
46+
"Cannot parse the http.connection_timeout configuration switch to default {} milliseconds", connectTimeoutFromConfig, e.getCause());
47+
}
48+
connectTimeout = connectTimeoutFromConfig;
49+
}
50+
51+
public HttpConnectionManager(URL requestURL) {
52+
this.requestURL = requestURL;
53+
if (requestURL.getHost().endsWith("arduino.cc")) {
54+
final String idString = PreferencesData.get("update.id", "0");
55+
id = Long.toString(Long.parseLong(idString));
56+
} else {
57+
id = null;
4758
}
4859

4960
}
@@ -61,7 +72,6 @@ public HttpURLConnection makeConnection()
6172

6273
private HttpURLConnection makeConnection(URL requestURL, int movedTimes,
6374
Consumer<HttpURLConnection> beforeConnection) throws IOException, URISyntaxException, ScriptException, NoSuchMethodException {
64-
log.info("Prepare http request to " + requestURL);
6575
if (movedTimes > 3) {
6676
log.warn("Too many redirect " + requestURL);
6777
throw new IOException("Too many redirect " + requestURL);
@@ -71,9 +81,15 @@ private HttpURLConnection makeConnection(URL requestURL, int movedTimes,
7181
.getProxyFor(requestURL.toURI());
7282
log.debug("Using proxy {}", proxy);
7383

84+
final String requestId = UUID.randomUUID().toString()
85+
.toUpperCase().replace("-", "").substring(0, 16);
7486
HttpURLConnection connection = (HttpURLConnection) requestURL
7587
.openConnection(proxy);
7688
connection.setRequestProperty("User-agent", userAgent);
89+
connection.setRequestProperty("X-Request-ID", requestId);
90+
if (id != null) {
91+
connection.setRequestProperty("X-ID", id);
92+
}
7793
if (requestURL.getUserInfo() != null) {
7894
String auth = "Basic " + new String(
7995
new Base64().encode(requestURL.getUserInfo().getBytes()));
@@ -86,10 +102,12 @@ private HttpURLConnection makeConnection(URL requestURL, int movedTimes,
86102
beforeConnection.accept(connection);
87103

88104
// Connect
89-
log.info("Connect to {} with method {}", requestURL, connection.getRequestMethod());
105+
log.info("Connect to {}, method={}, request id={}", requestURL, connection.getRequestMethod(),requestId);
90106

91107
connection.connect();
92108
int resp = connection.getResponseCode();
109+
log.info("Request complete URL=\"{}\", method={}, response code={}, request id={}, headers={}",
110+
requestURL, connection.getRequestMethod(), resp, requestId, StringUtils.join(connection.getHeaderFields()));
93111

94112
if (resp == HttpURLConnection.HTTP_MOVED_PERM
95113
|| resp == HttpURLConnection.HTTP_MOVED_TEMP) {
@@ -99,13 +117,9 @@ private HttpURLConnection makeConnection(URL requestURL, int movedTimes,
99117

100118
return this.makeConnection(newUrl, movedTimes + 1, beforeConnection);
101119
}
102-
log.info("The response code {}, headers {}", resp, StringUtils.join(connection.getHeaderFields()));
103120

104121
return connection;
105122
}
106123

107-
public void setConnectTimeout(int connectTimeout) {
108-
this.connectTimeout = connectTimeout;
109-
}
110124
}
111125

0 commit comments

Comments
 (0)