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 org.eclipse.jgit.lib.Constants.CHARACTER_ENCODING;
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
178
179
180 public static void encode(StringBuilder urlstr, String key) {
181 if (key == null || key.length() == 0)
182 return;
183 try {
184 urlstr.append(URLEncoder.encode(key, CHARACTER_ENCODING));
185 } catch (UnsupportedEncodingException e) {
186 throw new RuntimeException(JGitText.get().couldNotURLEncodeToUTF8, e);
187 }
188 }
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205 public static int response(HttpConnection c) throws IOException {
206 try {
207 return c.getResponseCode();
208 } catch (ConnectException ce) {
209 final URL url = c.getURL();
210 final String host = (url == null) ? "<null>" : url.getHost();
211
212
213 if ("Connection timed out: connect".equals(ce.getMessage()))
214 throw new ConnectException(MessageFormat.format(JGitText.get().connectionTimeOut, host));
215 throw new ConnectException(ce.getMessage() + " " + host);
216 }
217 }
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233 public static int response(java.net.HttpURLConnection c)
234 throws IOException {
235 try {
236 return c.getResponseCode();
237 } catch (ConnectException ce) {
238 final URL url = c.getURL();
239 final String host = (url == null) ? "<null>" : url.getHost();
240
241
242 if ("Connection timed out: connect".equals(ce.getMessage()))
243 throw new ConnectException(MessageFormat.format(
244 JGitText.get().connectionTimeOut, host));
245 throw new ConnectException(ce.getMessage() + " " + host);
246 }
247 }
248
249
250
251
252
253
254
255
256
257
258
259
260
261 public static String responseHeader(final HttpConnection c,
262 final String headerName) throws IOException {
263 return c.getHeaderField(headerName);
264 }
265
266
267
268
269
270
271
272
273
274
275
276
277
278 public static Proxy proxyFor(ProxySelector proxySelector, URL u)
279 throws ConnectException {
280 try {
281 return proxySelector.select(u.toURI()).get(0);
282 } catch (URISyntaxException e) {
283 final ConnectException err;
284 err = new ConnectException(MessageFormat.format(JGitText.get().cannotDetermineProxyFor, u));
285 err.initCause(e);
286 throw err;
287 }
288 }
289
290
291
292
293
294
295
296
297
298
299 public static void disableSslVerify(HttpConnection conn)
300 throws IOException {
301 final TrustManager[] trustAllCerts = new TrustManager[] {
302 new DummyX509TrustManager() };
303 try {
304 conn.configure(null, trustAllCerts, null);
305 conn.setHostnameVerifier(new DummyHostnameVerifier());
306 } catch (KeyManagementException e) {
307 throw new IOException(e.getMessage());
308 } catch (NoSuchAlgorithmException e) {
309 throw new IOException(e.getMessage());
310 }
311 }
312
313 private static class DummyX509TrustManager implements X509TrustManager {
314 @Override
315 public X509Certificate[] getAcceptedIssuers() {
316 return null;
317 }
318
319 @Override
320 public void checkClientTrusted(X509Certificate[] certs,
321 String authType) {
322
323 }
324
325 @Override
326 public void checkServerTrusted(X509Certificate[] certs,
327 String authType) {
328
329 }
330 }
331
332 private static class DummyHostnameVerifier implements HostnameVerifier {
333 @Override
334 public boolean verify(String hostname, SSLSession session) {
335
336 return true;
337 }
338 }
339
340 private HttpSupport() {
341
342 }
343 }