Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

HttpSupport.java 8.6KB

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