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 public static final String ENCODING_GZIP = "gzip";
146
147
148 public static final String TEXT_PLAIN = "text/plain";
149
150
151 public static final String HDR_AUTHORIZATION = "Authorization";
152
153
154 public static final String HDR_WWW_AUTHENTICATE = "WWW-Authenticate";
155
156
157
158
159
160
161
162
163
164 public static void encode(final StringBuilder urlstr, final String key) {
165 if (key == null || key.length() == 0)
166 return;
167 try {
168 urlstr.append(URLEncoder.encode(key, "UTF-8"));
169 } catch (UnsupportedEncodingException e) {
170 throw new RuntimeException(JGitText.get().couldNotURLEncodeToUTF8, e);
171 }
172 }
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188 public static int response(final HttpConnection c) throws IOException {
189 try {
190 return c.getResponseCode();
191 } catch (ConnectException ce) {
192 final URL url = c.getURL();
193 final String host = (url == null) ? "<null>" : url.getHost();
194
195
196 if ("Connection timed out: connect".equals(ce.getMessage()))
197 throw new ConnectException(MessageFormat.format(JGitText.get().connectionTimeOut, host));
198 throw new ConnectException(ce.getMessage() + " " + host);
199 }
200 }
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215 public static int response(final java.net.HttpURLConnection c)
216 throws IOException {
217 try {
218 return c.getResponseCode();
219 } catch (ConnectException ce) {
220 final URL url = c.getURL();
221 final String host = (url == null) ? "<null>" : url.getHost();
222
223
224 if ("Connection timed out: connect".equals(ce.getMessage()))
225 throw new ConnectException(MessageFormat.format(
226 JGitText.get().connectionTimeOut, host));
227 throw new ConnectException(ce.getMessage() + " " + host);
228 }
229 }
230
231
232
233
234
235
236
237
238
239
240
241
242
243 public static Proxy proxyFor(final ProxySelector proxySelector, final URL u)
244 throws ConnectException {
245 try {
246 return proxySelector.select(u.toURI()).get(0);
247 } catch (URISyntaxException e) {
248 final ConnectException err;
249 err = new ConnectException(MessageFormat.format(JGitText.get().cannotDetermineProxyFor, u));
250 err.initCause(e);
251 throw err;
252 }
253 }
254
255
256
257
258
259
260
261
262 public static void disableSslVerify(HttpConnection conn)
263 throws IOException {
264 final TrustManager[] trustAllCerts = new TrustManager[] {
265 new DummyX509TrustManager() };
266 try {
267 conn.configure(null, trustAllCerts, null);
268 conn.setHostnameVerifier(new DummyHostnameVerifier());
269 } catch (KeyManagementException e) {
270 throw new IOException(e.getMessage());
271 } catch (NoSuchAlgorithmException e) {
272 throw new IOException(e.getMessage());
273 }
274 }
275
276 private static class DummyX509TrustManager implements X509TrustManager {
277 public X509Certificate[] getAcceptedIssuers() {
278 return null;
279 }
280
281 public void checkClientTrusted(X509Certificate[] certs,
282 String authType) {
283
284 }
285
286 public void checkServerTrusted(X509Certificate[] certs,
287 String authType) {
288
289 }
290 }
291
292 private static class DummyHostnameVerifier implements HostnameVerifier {
293 public boolean verify(String hostname, SSLSession session) {
294
295 return true;
296 }
297 }
298
299 private HttpSupport() {
300
301 }
302 }