Correctly use a long for the offsets within a generated pack
[egit/charleso.git] / org.spearce.jgit / src / org / spearce / jgit / util / HttpSupport.java
blob33dfcee4c2ed96b35b6494cbd447e140ce1092a9
1 /*
2 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
21 * written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package org.spearce.jgit.util;
40 import java.io.IOException;
41 import java.io.UnsupportedEncodingException;
42 import java.net.ConnectException;
43 import java.net.HttpURLConnection;
44 import java.net.MalformedURLException;
45 import java.net.Proxy;
46 import java.net.ProxySelector;
47 import java.net.URISyntaxException;
48 import java.net.URL;
49 import java.net.URLEncoder;
51 import org.spearce.jgit.awtui.AwtAuthenticator;
53 /** Extra utilities to support usage of HTTP. */
54 public class HttpSupport {
55 /**
56 * Configure the JRE's standard HTTP based on <code>http_proxy</code>.
57 * <p>
58 * The popular libcurl library honors the <code>http_proxy</code>
59 * environment variable as a means of specifying an HTTP proxy for requests
60 * made behind a firewall. This is not natively recognized by the JRE, so
61 * this method can be used by command line utilities to configure the JRE
62 * before the first request is sent.
64 * @throws MalformedURLException
65 * the value in <code>http_proxy</code> is unsupportable.
67 public static void configureHttpProxy() throws MalformedURLException {
68 final String s = System.getenv("http_proxy");
69 if (s == null || s.equals(""))
70 return;
72 final URL u = new URL(s);
73 if (!"http".equals(u.getProtocol()))
74 throw new MalformedURLException("Invalid http_proxy: " + s
75 + ": Only http supported.");
77 final String proxyHost = u.getHost();
78 final int proxyPort = u.getPort();
80 System.setProperty("http.proxyHost", proxyHost);
81 if (proxyPort > 0)
82 System.setProperty("http.proxyPort", String.valueOf(proxyPort));
84 final String userpass = u.getUserInfo();
85 if (userpass != null && userpass.contains(":")) {
86 final int c = userpass.indexOf(':');
87 final String user = userpass.substring(0, c);
88 final String pass = userpass.substring(c + 1);
89 AwtAuthenticator.add(new AwtAuthenticator.CachedAuthentication(
90 proxyHost, proxyPort, user, pass));
94 /**
95 * URL encode a value string into an output buffer.
97 * @param urlstr
98 * the output buffer.
99 * @param key
100 * value which must be encoded to protected special characters.
102 public static void encode(final StringBuilder urlstr, final String key) {
103 if (key == null || key.length() == 0)
104 return;
105 try {
106 urlstr.append(URLEncoder.encode(key, "UTF-8"));
107 } catch (UnsupportedEncodingException e) {
108 throw new RuntimeException("Could not URL encode to UTF-8", e);
113 * Get the HTTP response code from the request.
114 * <p>
115 * Roughly the same as <code>c.getResponseCode()</code> but the
116 * ConnectException is translated to be more understandable.
118 * @param c
119 * connection the code should be obtained from.
120 * @return r HTTP status code, usually 200 to indicate success. See
121 * {@link HttpURLConnection} for other defined constants.
122 * @throws IOException
123 * communications error prevented obtaining the response code.
125 public static int response(final HttpURLConnection c) throws IOException {
126 try {
127 return c.getResponseCode();
128 } catch (ConnectException ce) {
129 final String host = c.getURL().getHost();
130 // The standard J2SE error message is not very useful.
132 if ("Connection timed out: connect".equals(ce.getMessage()))
133 throw new ConnectException("Connection time out: " + host);
134 throw new ConnectException(ce.getMessage() + " " + host);
139 * Determine the proxy server (if any) needed to obtain a URL.
141 * @param proxySelector
142 * proxy support for the caller.
143 * @param u
144 * location of the server caller wants to talk to.
145 * @return proxy to communicate with the supplied URL.
146 * @throws ConnectException
147 * the proxy could not be computed as the supplied URL could not
148 * be read. This failure should never occur.
150 public static Proxy proxyFor(final ProxySelector proxySelector, final URL u)
151 throws ConnectException {
152 try {
153 return proxySelector.select(u.toURI()).get(0);
154 } catch (URISyntaxException e) {
155 final ConnectException err;
156 err = new ConnectException("Cannot determine proxy for " + u);
157 err.initCause(e);
158 throw err;
162 private HttpSupport() {
163 // Utility class only.