You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HttpSupport.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * Copyright (C) 2010, Google Inc.
  3. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.util;
  45. import java.io.IOException;
  46. import java.io.UnsupportedEncodingException;
  47. import java.net.ConnectException;
  48. import java.net.Proxy;
  49. import java.net.ProxySelector;
  50. import java.net.URISyntaxException;
  51. import java.net.URL;
  52. import java.net.URLEncoder;
  53. import java.security.KeyManagementException;
  54. import java.security.NoSuchAlgorithmException;
  55. import java.security.cert.X509Certificate;
  56. import java.text.MessageFormat;
  57. import javax.net.ssl.HostnameVerifier;
  58. import javax.net.ssl.SSLSession;
  59. import javax.net.ssl.TrustManager;
  60. import javax.net.ssl.X509TrustManager;
  61. import org.eclipse.jgit.internal.JGitText;
  62. import org.eclipse.jgit.transport.http.HttpConnection;
  63. /** Extra utilities to support usage of HTTP. */
  64. public class HttpSupport {
  65. /** The {@code GET} HTTP method. */
  66. public static final String METHOD_GET = "GET"; //$NON-NLS-1$
  67. /** The {@code HEAD} HTTP method.
  68. * @since 4.3 */
  69. public static final String METHOD_HEAD = "HEAD"; //$NON-NLS-1$
  70. /** The {@code POST} HTTP method.
  71. * @since 4.3 */
  72. public static final String METHOD_PUT = "PUT"; //$NON-NLS-1$
  73. /** The {@code POST} HTTP method. */
  74. public static final String METHOD_POST = "POST"; //$NON-NLS-1$
  75. /** The {@code Cache-Control} header. */
  76. public static final String HDR_CACHE_CONTROL = "Cache-Control"; //$NON-NLS-1$
  77. /** The {@code Pragma} header. */
  78. public static final String HDR_PRAGMA = "Pragma"; //$NON-NLS-1$
  79. /** The {@code User-Agent} header. */
  80. public static final String HDR_USER_AGENT = "User-Agent"; //$NON-NLS-1$
  81. /**
  82. * The {@code Server} header.
  83. * @since 4.0
  84. */
  85. public static final String HDR_SERVER = "Server"; //$NON-NLS-1$
  86. /** The {@code Date} header. */
  87. public static final String HDR_DATE = "Date"; //$NON-NLS-1$
  88. /** The {@code Expires} header. */
  89. public static final String HDR_EXPIRES = "Expires"; //$NON-NLS-1$
  90. /** The {@code ETag} header. */
  91. public static final String HDR_ETAG = "ETag"; //$NON-NLS-1$
  92. /** The {@code If-None-Match} header. */
  93. public static final String HDR_IF_NONE_MATCH = "If-None-Match"; //$NON-NLS-1$
  94. /** The {@code Last-Modified} header. */
  95. public static final String HDR_LAST_MODIFIED = "Last-Modified"; //$NON-NLS-1$
  96. /** The {@code If-Modified-Since} header. */
  97. public static final String HDR_IF_MODIFIED_SINCE = "If-Modified-Since"; //$NON-NLS-1$
  98. /** The {@code Accept} header. */
  99. public static final String HDR_ACCEPT = "Accept"; //$NON-NLS-1$
  100. /** The {@code Content-Type} header. */
  101. public static final String HDR_CONTENT_TYPE = "Content-Type"; //$NON-NLS-1$
  102. /** The {@code Content-Length} header. */
  103. public static final String HDR_CONTENT_LENGTH = "Content-Length"; //$NON-NLS-1$
  104. /** The {@code Content-Encoding} header. */
  105. public static final String HDR_CONTENT_ENCODING = "Content-Encoding"; //$NON-NLS-1$
  106. /** The {@code Content-Range} header. */
  107. public static final String HDR_CONTENT_RANGE = "Content-Range"; //$NON-NLS-1$
  108. /** The {@code Accept-Ranges} header. */
  109. public static final String HDR_ACCEPT_RANGES = "Accept-Ranges"; //$NON-NLS-1$
  110. /** The {@code If-Range} header. */
  111. public static final String HDR_IF_RANGE = "If-Range"; //$NON-NLS-1$
  112. /** The {@code Range} header. */
  113. public static final String HDR_RANGE = "Range"; //$NON-NLS-1$
  114. /** The {@code Accept-Encoding} header. */
  115. public static final String HDR_ACCEPT_ENCODING = "Accept-Encoding"; //$NON-NLS-1$
  116. /** The {@code gzip} encoding value for {@link #HDR_ACCEPT_ENCODING}. */
  117. public static final String ENCODING_GZIP = "gzip"; //$NON-NLS-1$
  118. /** The {@code x-gzip} encoding value for {@link #HDR_ACCEPT_ENCODING}. */
  119. public static final String ENCODING_X_GZIP = "x-gzip"; //$NON-NLS-1$
  120. /** The standard {@code text/plain} MIME type. */
  121. public static final String TEXT_PLAIN = "text/plain"; //$NON-NLS-1$
  122. /** The {@code Authorization} header. */
  123. public static final String HDR_AUTHORIZATION = "Authorization"; //$NON-NLS-1$
  124. /** The {@code WWW-Authenticate} header. */
  125. public static final String HDR_WWW_AUTHENTICATE = "WWW-Authenticate"; //$NON-NLS-1$
  126. /**
  127. * URL encode a value string into an output buffer.
  128. *
  129. * @param urlstr
  130. * the output buffer.
  131. * @param key
  132. * value which must be encoded to protected special characters.
  133. */
  134. public static void encode(final StringBuilder urlstr, final String key) {
  135. if (key == null || key.length() == 0)
  136. return;
  137. try {
  138. urlstr.append(URLEncoder.encode(key, "UTF-8")); //$NON-NLS-1$
  139. } catch (UnsupportedEncodingException e) {
  140. throw new RuntimeException(JGitText.get().couldNotURLEncodeToUTF8, e);
  141. }
  142. }
  143. /**
  144. * Get the HTTP response code from the request.
  145. * <p>
  146. * Roughly the same as <code>c.getResponseCode()</code> but the
  147. * ConnectException is translated to be more understandable.
  148. *
  149. * @param c
  150. * connection the code should be obtained from.
  151. * @return r HTTP status code, usually 200 to indicate success. See
  152. * {@link HttpConnection} for other defined constants.
  153. * @throws IOException
  154. * communications error prevented obtaining the response code.
  155. * @since 3.3
  156. */
  157. public static int response(final HttpConnection c) throws IOException {
  158. try {
  159. return c.getResponseCode();
  160. } catch (ConnectException ce) {
  161. final URL url = c.getURL();
  162. final String host = (url == null) ? "<null>" : url.getHost(); //$NON-NLS-1$
  163. // The standard J2SE error message is not very useful.
  164. //
  165. if ("Connection timed out: connect".equals(ce.getMessage())) //$NON-NLS-1$
  166. throw new ConnectException(MessageFormat.format(JGitText.get().connectionTimeOut, host));
  167. throw new ConnectException(ce.getMessage() + " " + host); //$NON-NLS-1$
  168. }
  169. }
  170. /**
  171. * Get the HTTP response code from the request.
  172. * <p>
  173. * Roughly the same as <code>c.getResponseCode()</code> but the
  174. * ConnectException is translated to be more understandable.
  175. *
  176. * @param c
  177. * connection the code should be obtained from.
  178. * @return r HTTP status code, usually 200 to indicate success. See
  179. * {@link HttpConnection} for other defined constants.
  180. * @throws IOException
  181. * communications error prevented obtaining the response code.
  182. */
  183. public static int response(final java.net.HttpURLConnection c)
  184. throws IOException {
  185. try {
  186. return c.getResponseCode();
  187. } catch (ConnectException ce) {
  188. final URL url = c.getURL();
  189. final String host = (url == null) ? "<null>" : url.getHost(); //$NON-NLS-1$
  190. // The standard J2SE error message is not very useful.
  191. //
  192. if ("Connection timed out: connect".equals(ce.getMessage())) //$NON-NLS-1$
  193. throw new ConnectException(MessageFormat.format(
  194. JGitText.get().connectionTimeOut, host));
  195. throw new ConnectException(ce.getMessage() + " " + host); //$NON-NLS-1$
  196. }
  197. }
  198. /**
  199. * Determine the proxy server (if any) needed to obtain a URL.
  200. *
  201. * @param proxySelector
  202. * proxy support for the caller.
  203. * @param u
  204. * location of the server caller wants to talk to.
  205. * @return proxy to communicate with the supplied URL.
  206. * @throws ConnectException
  207. * the proxy could not be computed as the supplied URL could not
  208. * be read. This failure should never occur.
  209. */
  210. public static Proxy proxyFor(final ProxySelector proxySelector, final URL u)
  211. throws ConnectException {
  212. try {
  213. return proxySelector.select(u.toURI()).get(0);
  214. } catch (URISyntaxException e) {
  215. final ConnectException err;
  216. err = new ConnectException(MessageFormat.format(JGitText.get().cannotDetermineProxyFor, u));
  217. err.initCause(e);
  218. throw err;
  219. }
  220. }
  221. /**
  222. * Disable SSL and hostname verification for given HTTP connection
  223. *
  224. * @param conn
  225. * @throws IOException
  226. * @since 4.3
  227. */
  228. public static void disableSslVerify(HttpConnection conn)
  229. throws IOException {
  230. final TrustManager[] trustAllCerts = new TrustManager[] {
  231. new DummyX509TrustManager() };
  232. try {
  233. conn.configure(null, trustAllCerts, null);
  234. conn.setHostnameVerifier(new DummyHostnameVerifier());
  235. } catch (KeyManagementException e) {
  236. throw new IOException(e.getMessage());
  237. } catch (NoSuchAlgorithmException e) {
  238. throw new IOException(e.getMessage());
  239. }
  240. }
  241. private static class DummyX509TrustManager implements X509TrustManager {
  242. public X509Certificate[] getAcceptedIssuers() {
  243. return null;
  244. }
  245. public void checkClientTrusted(X509Certificate[] certs,
  246. String authType) {
  247. // no check
  248. }
  249. public void checkServerTrusted(X509Certificate[] certs,
  250. String authType) {
  251. // no check
  252. }
  253. }
  254. private static class DummyHostnameVerifier implements HostnameVerifier {
  255. public boolean verify(String hostname, SSLSession session) {
  256. // always accept
  257. return true;
  258. }
  259. }
  260. private HttpSupport() {
  261. // Utility class only.
  262. }
  263. }