3
3
import cc .arduino .net .CustomProxySelector ;
4
4
import org .apache .commons .codec .binary .Base64 ;
5
5
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 ;
8
8
import processing .app .BaseNoGui ;
9
9
import processing .app .PreferencesData ;
10
10
14
14
import java .net .Proxy ;
15
15
import java .net .URISyntaxException ;
16
16
import java .net .URL ;
17
+ import java .util .UUID ;
17
18
import java .util .function .Consumer ;
18
19
19
20
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 ;
21
24
private final URL requestURL ;
22
- private final String userAgent ;
23
- private int connectTimeout ;
25
+ private final String id ;
24
26
25
-
26
- public HttpConnectionManager (URL requestURL ) {
27
- this .requestURL = requestURL ;
27
+ static {
28
28
final String defaultUserAgent = String .format (
29
29
"ArduinoIDE/%s (%s; %s; %s; %s) Java/%s (%s)" ,
30
30
BaseNoGui .VERSION_NAME ,
@@ -35,15 +35,26 @@ public HttpConnectionManager(URL requestURL) {
35
35
System .getProperty ("java.version" ),
36
36
System .getProperty ("java.vendor" )
37
37
);
38
- this .userAgent = PreferencesData .get ("http.user_agent" , defaultUserAgent );
38
+ userAgent = PreferencesData .get ("http.user_agent" , defaultUserAgent );
39
+ int connectTimeoutFromConfig = 5000 ;
39
40
try {
40
- this . connectTimeout =
41
+ connectTimeoutFromConfig =
41
42
Integer .parseInt (
42
43
PreferencesData .get ("http.connection_timeout" , "5000" ));
43
44
} catch (NumberFormatException e ) {
44
45
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 ;
47
58
}
48
59
49
60
}
@@ -61,7 +72,6 @@ public HttpURLConnection makeConnection()
61
72
62
73
private HttpURLConnection makeConnection (URL requestURL , int movedTimes ,
63
74
Consumer <HttpURLConnection > beforeConnection ) throws IOException , URISyntaxException , ScriptException , NoSuchMethodException {
64
- log .info ("Prepare http request to " + requestURL );
65
75
if (movedTimes > 3 ) {
66
76
log .warn ("Too many redirect " + requestURL );
67
77
throw new IOException ("Too many redirect " + requestURL );
@@ -71,9 +81,15 @@ private HttpURLConnection makeConnection(URL requestURL, int movedTimes,
71
81
.getProxyFor (requestURL .toURI ());
72
82
log .debug ("Using proxy {}" , proxy );
73
83
84
+ final String requestId = UUID .randomUUID ().toString ()
85
+ .toUpperCase ().replace ("-" , "" ).substring (0 , 16 );
74
86
HttpURLConnection connection = (HttpURLConnection ) requestURL
75
87
.openConnection (proxy );
76
88
connection .setRequestProperty ("User-agent" , userAgent );
89
+ connection .setRequestProperty ("X-Request-ID" , requestId );
90
+ if (id != null ) {
91
+ connection .setRequestProperty ("X-ID" , id );
92
+ }
77
93
if (requestURL .getUserInfo () != null ) {
78
94
String auth = "Basic " + new String (
79
95
new Base64 ().encode (requestURL .getUserInfo ().getBytes ()));
@@ -86,10 +102,12 @@ private HttpURLConnection makeConnection(URL requestURL, int movedTimes,
86
102
beforeConnection .accept (connection );
87
103
88
104
// Connect
89
- log .info ("Connect to {} with method {}" , requestURL , connection .getRequestMethod ());
105
+ log .info ("Connect to {}, method={}, request id= {}" , requestURL , connection .getRequestMethod (), requestId );
90
106
91
107
connection .connect ();
92
108
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 ()));
93
111
94
112
if (resp == HttpURLConnection .HTTP_MOVED_PERM
95
113
|| resp == HttpURLConnection .HTTP_MOVED_TEMP ) {
@@ -99,13 +117,9 @@ private HttpURLConnection makeConnection(URL requestURL, int movedTimes,
99
117
100
118
return this .makeConnection (newUrl , movedTimes + 1 , beforeConnection );
101
119
}
102
- log .info ("The response code {}, headers {}" , resp , StringUtils .join (connection .getHeaderFields ()));
103
120
104
121
return connection ;
105
122
}
106
123
107
- public void setConnectTimeout (int connectTimeout ) {
108
- this .connectTimeout = connectTimeout ;
109
- }
110
124
}
111
125
0 commit comments