1
2
3
4
5
6
7
8
9
10
11
12 package org.eclipse.jgit.util;
13
14 import static java.nio.charset.StandardCharsets.UTF_8;
15
16 import java.io.IOException;
17 import java.io.UnsupportedEncodingException;
18 import java.net.ConnectException;
19 import java.net.Proxy;
20 import java.net.ProxySelector;
21 import java.net.URI;
22 import java.net.URISyntaxException;
23 import java.net.URL;
24 import java.net.URLEncoder;
25 import java.security.KeyManagementException;
26 import java.security.NoSuchAlgorithmException;
27 import java.security.cert.X509Certificate;
28 import java.text.MessageFormat;
29 import java.util.Arrays;
30 import java.util.Collections;
31 import java.util.LinkedHashSet;
32 import java.util.Set;
33
34 import javax.net.ssl.HostnameVerifier;
35 import javax.net.ssl.SSLSession;
36 import javax.net.ssl.SSLSocket;
37 import javax.net.ssl.TrustManager;
38 import javax.net.ssl.X509TrustManager;
39
40 import org.eclipse.jgit.internal.JGitText;
41 import org.eclipse.jgit.transport.http.HttpConnection;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45
46
47
48 public class HttpSupport {
49 private final static Logger LOG = LoggerFactory
50 .getLogger(HttpSupport.class);
51
52
53 public static final String METHOD_GET = "GET";
54
55
56
57 public static final String METHOD_HEAD = "HEAD";
58
59
60
61 public static final String METHOD_PUT = "PUT";
62
63
64 public static final String METHOD_POST = "POST";
65
66
67 public static final String HDR_CACHE_CONTROL = "Cache-Control";
68
69
70 public static final String HDR_PRAGMA = "Pragma";
71
72
73 public static final String HDR_USER_AGENT = "User-Agent";
74
75
76
77
78
79 public static final String HDR_SERVER = "Server";
80
81
82 public static final String HDR_DATE = "Date";
83
84
85 public static final String HDR_EXPIRES = "Expires";
86
87
88 public static final String HDR_ETAG = "ETag";
89
90
91 public static final String HDR_IF_NONE_MATCH = "If-None-Match";
92
93
94 public static final String HDR_LAST_MODIFIED = "Last-Modified";
95
96
97 public static final String HDR_IF_MODIFIED_SINCE = "If-Modified-Since";
98
99
100 public static final String HDR_ACCEPT = "Accept";
101
102
103 public static final String HDR_CONTENT_TYPE = "Content-Type";
104
105
106 public static final String HDR_CONTENT_LENGTH = "Content-Length";
107
108
109 public static final String HDR_CONTENT_ENCODING = "Content-Encoding";
110
111
112 public static final String HDR_CONTENT_RANGE = "Content-Range";
113
114
115 public static final String HDR_ACCEPT_RANGES = "Accept-Ranges";
116
117
118 public static final String HDR_IF_RANGE = "If-Range";
119
120
121 public static final String HDR_RANGE = "Range";
122
123
124 public static final String HDR_ACCEPT_ENCODING = "Accept-Encoding";
125
126
127
128
129
130 public static final String HDR_LOCATION = "Location";
131
132
133 public static final String ENCODING_GZIP = "gzip";
134
135
136
137
138
139 public static final String ENCODING_X_GZIP = "x-gzip";
140
141
142 public static final String TEXT_PLAIN = "text/plain";
143
144
145 public static final String HDR_AUTHORIZATION = "Authorization";
146
147
148 public static final String HDR_WWW_AUTHENTICATE = "WWW-Authenticate";
149
150
151
152
153
154
155 public static final String HDR_COOKIE = "Cookie";
156
157
158
159
160
161
162 public static final String HDR_SET_COOKIE = "Set-Cookie";
163
164
165
166
167
168
169 public static final String HDR_SET_COOKIE2 = "Set-Cookie2";
170
171 private static Set<String> configuredHttpsProtocols;
172
173
174
175
176
177
178
179
180
181 public static void encode(StringBuilder urlstr, String key) {
182 if (key == null || key.length() == 0)
183 return;
184 try {
185 urlstr.append(URLEncoder.encode(key, UTF_8.name()));
186 } catch (UnsupportedEncodingException e) {
187 throw new RuntimeException(JGitText.get().couldNotURLEncodeToUTF8, e);
188 }
189 }
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206 public static int response(HttpConnection c) throws IOException {
207 try {
208 return c.getResponseCode();
209 } catch (ConnectException ce) {
210 final URL url = c.getURL();
211 final String host = (url == null) ? "<null>" : url.getHost();
212
213
214 if ("Connection timed out: connect".equals(ce.getMessage()))
215 throw new ConnectException(MessageFormat.format(JGitText.get().connectionTimeOut, host));
216 throw new ConnectException(ce.getMessage() + " " + host);
217 }
218 }
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234 public static int response(java.net.HttpURLConnection c)
235 throws IOException {
236 try {
237 return c.getResponseCode();
238 } catch (ConnectException ce) {
239 final URL url = c.getURL();
240 final String host = (url == null) ? "<null>" : url.getHost();
241
242
243 if ("Connection timed out: connect".equals(ce.getMessage()))
244 throw new ConnectException(MessageFormat.format(
245 JGitText.get().connectionTimeOut, host));
246 throw new ConnectException(ce.getMessage() + " " + host);
247 }
248 }
249
250
251
252
253
254
255
256
257
258
259
260
261
262 public static String responseHeader(final HttpConnection c,
263 final String headerName) throws IOException {
264 return c.getHeaderField(headerName);
265 }
266
267
268
269
270
271
272
273
274
275
276
277
278
279 public static Proxy proxyFor(ProxySelector proxySelector, URL u)
280 throws ConnectException {
281 try {
282 URI uri = new URI(u.getProtocol(), null, u.getHost(), u.getPort(),
283 null, null, null);
284 return proxySelector.select(uri).get(0);
285 } catch (URISyntaxException e) {
286 final ConnectException err;
287 err = new ConnectException(MessageFormat.format(JGitText.get().cannotDetermineProxyFor, u));
288 err.initCause(e);
289 throw err;
290 }
291 }
292
293
294
295
296
297
298
299
300
301
302 public static void disableSslVerify(HttpConnection conn)
303 throws IOException {
304 final TrustManager[] trustAllCerts = new TrustManager[] {
305 new DummyX509TrustManager() };
306 try {
307 conn.configure(null, trustAllCerts, null);
308 conn.setHostnameVerifier(new DummyHostnameVerifier());
309 } catch (KeyManagementException | NoSuchAlgorithmException e) {
310 throw new IOException(e.getMessage());
311 }
312 }
313
314 private static class DummyX509TrustManager implements X509TrustManager {
315 @Override
316 public X509Certificate[] getAcceptedIssuers() {
317 return null;
318 }
319
320 @Override
321 public void checkClientTrusted(X509Certificate[] certs,
322 String authType) {
323
324 }
325
326 @Override
327 public void checkServerTrusted(X509Certificate[] certs,
328 String authType) {
329
330 }
331 }
332
333 private static class DummyHostnameVerifier implements HostnameVerifier {
334 @Override
335 public boolean verify(String hostname, SSLSession session) {
336
337 return true;
338 }
339 }
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384 public static void configureTLS(SSLSocket socket) {
385
386 Set<String> enabled = new LinkedHashSet<>(
387 Arrays.asList(socket.getEnabledProtocols()));
388 for (String s : socket.getSupportedProtocols()) {
389 if (s.startsWith("TLS")) {
390 enabled.add(s);
391 }
392 }
393
394 Set<String> configured = getConfiguredProtocols();
395 if (!configured.isEmpty()) {
396 enabled.retainAll(configured);
397 }
398 if (!enabled.isEmpty()) {
399 socket.setEnabledProtocols(enabled.toArray(new String[0]));
400 }
401 }
402
403 private static Set<String> getConfiguredProtocols() {
404 Set<String> result = configuredHttpsProtocols;
405 if (result == null) {
406 String configured = getProperty("https.protocols");
407 if (StringUtils.isEmptyOrNull(configured)) {
408 result = Collections.emptySet();
409 } else {
410 result = new LinkedHashSet<>(
411 Arrays.asList(configured.split("\\s*,\\s*")));
412 }
413 configuredHttpsProtocols = result;
414 }
415 return result;
416 }
417
418 private static String getProperty(String property) {
419 try {
420 return SystemReader.getInstance().getProperty(property);
421 } catch (SecurityException e) {
422 LOG.warn(JGitText.get().failedReadHttpsProtocols, e);
423 return null;
424 }
425 }
426
427 private HttpSupport() {
428
429 }
430 }