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 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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.HttpURLConnection;
  49. import java.net.Proxy;
  50. import java.net.ProxySelector;
  51. import java.net.URISyntaxException;
  52. import java.net.URL;
  53. import java.net.URLEncoder;
  54. import java.text.MessageFormat;
  55. import org.eclipse.jgit.JGitText;
  56. /** Extra utilities to support usage of HTTP. */
  57. public class HttpSupport {
  58. /** The {@code GET} HTTP method. */
  59. public static final String METHOD_GET = "GET";
  60. /** The {@code POST} HTTP method. */
  61. public static final String METHOD_POST = "POST";
  62. /** The {@code Cache-Control} header. */
  63. public static final String HDR_CACHE_CONTROL = "Cache-Control";
  64. /** The {@code Pragma} header. */
  65. public static final String HDR_PRAGMA = "Pragma";
  66. /** The {@code User-Agent} header. */
  67. public static final String HDR_USER_AGENT = "User-Agent";
  68. /** The {@code Date} header. */
  69. public static final String HDR_DATE = "Date";
  70. /** The {@code Expires} header. */
  71. public static final String HDR_EXPIRES = "Expires";
  72. /** The {@code ETag} header. */
  73. public static final String HDR_ETAG = "ETag";
  74. /** The {@code If-None-Match} header. */
  75. public static final String HDR_IF_NONE_MATCH = "If-None-Match";
  76. /** The {@code Last-Modified} header. */
  77. public static final String HDR_LAST_MODIFIED = "Last-Modified";
  78. /** The {@code If-Modified-Since} header. */
  79. public static final String HDR_IF_MODIFIED_SINCE = "If-Modified-Since";
  80. /** The {@code Accept} header. */
  81. public static final String HDR_ACCEPT = "Accept";
  82. /** The {@code Content-Type} header. */
  83. public static final String HDR_CONTENT_TYPE = "Content-Type";
  84. /** The {@code Content-Length} header. */
  85. public static final String HDR_CONTENT_LENGTH = "Content-Length";
  86. /** The {@code Content-Encoding} header. */
  87. public static final String HDR_CONTENT_ENCODING = "Content-Encoding";
  88. /** The {@code Content-Range} header. */
  89. public static final String HDR_CONTENT_RANGE = "Content-Range";
  90. /** The {@code Accept-Ranges} header. */
  91. public static final String HDR_ACCEPT_RANGES = "Accept-Ranges";
  92. /** The {@code If-Range} header. */
  93. public static final String HDR_IF_RANGE = "If-Range";
  94. /** The {@code Range} header. */
  95. public static final String HDR_RANGE = "Range";
  96. /** The {@code Accept-Encoding} header. */
  97. public static final String HDR_ACCEPT_ENCODING = "Accept-Encoding";
  98. /** The {@code gzip} encoding value for {@link #HDR_ACCEPT_ENCODING}. */
  99. public static final String ENCODING_GZIP = "gzip";
  100. /** The standard {@code text/plain} MIME type. */
  101. public static final String TEXT_PLAIN = "text/plain";
  102. /** The {@code Authorization} header. */
  103. public static final String HDR_AUTHORIZATION = "Authorization";
  104. /** The {@code WWW-Authenticate} header. */
  105. public static final String HDR_WWW_AUTHENTICATE = "WWW-Authenticate";
  106. /**
  107. * URL encode a value string into an output buffer.
  108. *
  109. * @param urlstr
  110. * the output buffer.
  111. * @param key
  112. * value which must be encoded to protected special characters.
  113. */
  114. public static void encode(final StringBuilder urlstr, final String key) {
  115. if (key == null || key.length() == 0)
  116. return;
  117. try {
  118. urlstr.append(URLEncoder.encode(key, "UTF-8"));
  119. } catch (UnsupportedEncodingException e) {
  120. throw new RuntimeException(JGitText.get().couldNotURLEncodeToUTF8, e);
  121. }
  122. }
  123. /**
  124. * Get the HTTP response code from the request.
  125. * <p>
  126. * Roughly the same as <code>c.getResponseCode()</code> but the
  127. * ConnectException is translated to be more understandable.
  128. *
  129. * @param c
  130. * connection the code should be obtained from.
  131. * @return r HTTP status code, usually 200 to indicate success. See
  132. * {@link HttpURLConnection} for other defined constants.
  133. * @throws IOException
  134. * communications error prevented obtaining the response code.
  135. */
  136. public static int response(final HttpURLConnection c) throws IOException {
  137. try {
  138. return c.getResponseCode();
  139. } catch (ConnectException ce) {
  140. final String host = c.getURL().getHost();
  141. // The standard J2SE error message is not very useful.
  142. //
  143. if ("Connection timed out: connect".equals(ce.getMessage()))
  144. throw new ConnectException(MessageFormat.format(JGitText.get().connectionTimeOut, host));
  145. throw new ConnectException(ce.getMessage() + " " + host);
  146. }
  147. }
  148. /**
  149. * Determine the proxy server (if any) needed to obtain a URL.
  150. *
  151. * @param proxySelector
  152. * proxy support for the caller.
  153. * @param u
  154. * location of the server caller wants to talk to.
  155. * @return proxy to communicate with the supplied URL.
  156. * @throws ConnectException
  157. * the proxy could not be computed as the supplied URL could not
  158. * be read. This failure should never occur.
  159. */
  160. public static Proxy proxyFor(final ProxySelector proxySelector, final URL u)
  161. throws ConnectException {
  162. try {
  163. return proxySelector.select(u.toURI()).get(0);
  164. } catch (URISyntaxException e) {
  165. final ConnectException err;
  166. err = new ConnectException(MessageFormat.format(JGitText.get().cannotDetermineProxyFor, u));
  167. err.initCause(e);
  168. throw err;
  169. }
  170. }
  171. private HttpSupport() {
  172. // Utility class only.
  173. }
  174. }