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