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