Support push over the sftp:// dumb transport
[egit/zawir.git] / org.spearce.jgit / src / org / spearce / jgit / transport / TransportHttp.java
blob465595041ddd060880e9fdca849d40ef49c6be56
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.transport;
40 import java.io.BufferedReader;
41 import java.io.FileNotFoundException;
42 import java.io.IOException;
43 import java.io.InputStream;
44 import java.net.ConnectException;
45 import java.net.MalformedURLException;
46 import java.net.Proxy;
47 import java.net.ProxySelector;
48 import java.net.URISyntaxException;
49 import java.net.URL;
50 import java.net.URLConnection;
51 import java.util.ArrayList;
52 import java.util.Collection;
53 import java.util.Map;
54 import java.util.TreeMap;
56 import org.spearce.jgit.errors.NotSupportedException;
57 import org.spearce.jgit.errors.PackProtocolException;
58 import org.spearce.jgit.errors.TransportException;
59 import org.spearce.jgit.lib.ObjectId;
60 import org.spearce.jgit.lib.Ref;
61 import org.spearce.jgit.lib.Repository;
63 /**
64 * Transport over the non-Git aware HTTP and FTP protocol.
65 * <p>
66 * The HTTP transport does not require any specialized Git support on the remote
67 * (server side) repository. Object files are retrieved directly through
68 * standard HTTP GET requests, making it easy to serve a Git repository through
69 * a standard web host provider that does not offer specific support for Git.
71 * @see WalkFetchConnection
73 class TransportHttp extends WalkTransport {
74 static boolean canHandle(final URIish uri) {
75 if (!uri.isRemote())
76 return false;
77 final String s = uri.getScheme();
78 return "http".equals(s) || "https".equals(s) || "ftp".equals(s);
81 private final URL baseUrl;
83 private final URL objectsUrl;
85 private final ProxySelector proxySelector;
87 TransportHttp(final Repository local, final URIish uri)
88 throws NotSupportedException {
89 super(local, uri);
90 try {
91 String uriString = uri.toString();
92 if (!uriString.endsWith("/"))
93 uriString += "/";
94 baseUrl = new URL(uriString);
95 objectsUrl = new URL(baseUrl, "objects/");
96 } catch (MalformedURLException e) {
97 throw new NotSupportedException("Invalid URL " + uri, e);
99 proxySelector = ProxySelector.getDefault();
102 @Override
103 public FetchConnection openFetch() throws TransportException {
104 final HttpObjectDB c = new HttpObjectDB(objectsUrl);
105 final WalkFetchConnection r = new WalkFetchConnection(this, c);
106 r.available(c.readAdvertisedRefs());
107 return r;
110 Proxy proxyFor(final URL u) throws URISyntaxException {
111 return proxySelector.select(u.toURI()).get(0);
114 class HttpObjectDB extends WalkRemoteObjectDatabase {
115 private final URL objectsUrl;
117 HttpObjectDB(final URL b) {
118 objectsUrl = b;
121 @Override
122 Collection<WalkRemoteObjectDatabase> getAlternates() throws IOException {
123 try {
124 return readAlternates(INFO_HTTP_ALTERNATES);
125 } catch (FileNotFoundException err) {
126 // Fall through.
129 try {
130 return readAlternates(INFO_ALTERNATES);
131 } catch (FileNotFoundException err) {
132 // Fall through.
135 return null;
138 @Override
139 WalkRemoteObjectDatabase openAlternate(final String location)
140 throws IOException {
141 return new HttpObjectDB(new URL(objectsUrl, location));
144 @Override
145 Collection<String> getPackNames() throws IOException {
146 final Collection<String> packs = new ArrayList<String>();
147 try {
148 final BufferedReader br = openReader(INFO_PACKS);
149 try {
150 for (;;) {
151 final String s = br.readLine();
152 if (s == null || s.length() == 0)
153 break;
154 if (!s.startsWith("P pack-") || !s.endsWith(".pack"))
155 throw invalidAdvertisement(s);
156 packs.add(s.substring(2));
158 return packs;
159 } finally {
160 br.close();
162 } catch (FileNotFoundException err) {
163 return packs;
167 @Override
168 FileStream open(final String path) throws IOException {
169 final URL base = objectsUrl;
170 try {
171 final URL u = new URL(base, path);
172 final URLConnection c = u.openConnection(proxyFor(u));
173 final InputStream in = c.getInputStream();
174 final int len = c.getContentLength();
175 return new FileStream(in, len);
176 } catch (ConnectException ce) {
177 // The standard J2SE error message is not very useful.
179 if ("Connection timed out: connect".equals(ce.getMessage()))
180 throw new ConnectException("Connection timed out: " + base);
181 throw new ConnectException(ce.getMessage() + " " + base);
182 } catch (URISyntaxException e) {
183 final ConnectException err;
184 err = new ConnectException("Cannot determine proxy for " + base);
185 err.initCause(e);
186 throw err;
190 Map<String, Ref> readAdvertisedRefs() throws TransportException {
191 try {
192 final BufferedReader br = openReader(INFO_REFS);
193 try {
194 return readAdvertisedImpl(br);
195 } finally {
196 br.close();
198 } catch (IOException err) {
199 try {
200 throw new TransportException(new URL(objectsUrl, INFO_REFS)
201 + ": cannot read available refs", err);
202 } catch (MalformedURLException mue) {
203 throw new TransportException(objectsUrl + INFO_REFS
204 + ": cannot read available refs", err);
209 private Map<String, Ref> readAdvertisedImpl(final BufferedReader br)
210 throws IOException, PackProtocolException {
211 final TreeMap<String, Ref> avail = new TreeMap<String, Ref>();
212 for (;;) {
213 String line = br.readLine();
214 if (line == null)
215 break;
217 final int tab = line.indexOf('\t');
218 if (tab < 0)
219 throw invalidAdvertisement(line);
221 String name;
222 final ObjectId id;
224 name = line.substring(tab + 1);
225 id = ObjectId.fromString(line.substring(0, tab));
226 if (name.endsWith("^{}")) {
227 name = name.substring(0, name.length() - 3);
228 final Ref prior = avail.get(name);
229 if (prior == null)
230 throw outOfOrderAdvertisement(name);
232 if (prior.getPeeledObjectId() != null)
233 throw duplicateAdvertisement(name + "^{}");
235 avail.put(name, new Ref(Ref.Storage.NETWORK, name, prior
236 .getObjectId(), id));
237 } else {
238 final Ref prior = avail.put(name, new Ref(
239 Ref.Storage.NETWORK, name, id));
240 if (prior != null)
241 throw duplicateAdvertisement(name);
244 return avail;
247 private PackProtocolException outOfOrderAdvertisement(final String n) {
248 return new PackProtocolException("advertisement of " + n
249 + "^{} came before " + n);
252 private PackProtocolException invalidAdvertisement(final String n) {
253 return new PackProtocolException("invalid advertisement of " + n);
256 private PackProtocolException duplicateAdvertisement(final String n) {
257 return new PackProtocolException("duplicate advertisements of " + n);
260 @Override
261 void close() {
262 // We do not maintain persistent connections.