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