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

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