Remember how a Ref was read in from disk and created
[egit/zawir.git] / org.spearce.jgit / src / org / spearce / jgit / transport / TransportHttp.java
blobb18b8e36652c98aabf5bcc1f7ee6df7dfcea74b1
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.TreeMap;
55 import org.spearce.jgit.errors.NotSupportedException;
56 import org.spearce.jgit.errors.PackProtocolException;
57 import org.spearce.jgit.errors.TransportException;
58 import org.spearce.jgit.lib.ObjectId;
59 import org.spearce.jgit.lib.Ref;
60 import org.spearce.jgit.lib.Repository;
62 /**
63 * Transport over the non-Git aware HTTP and FTP protocol.
64 * <p>
65 * The HTTP transport does not require any specialized Git support on the remote
66 * (server side) repository. Object files are retrieved directly through
67 * standard HTTP GET requests, making it easy to serve a Git repository through
68 * a standard web host provider that does not offer specific support for Git.
70 * @see WalkFetchConnection
72 class TransportHttp extends WalkTransport {
73 static boolean canHandle(final URIish uri) {
74 if (!uri.isRemote())
75 return false;
76 final String s = uri.getScheme();
77 return "http".equals(s) || "https".equals(s) || "ftp".equals(s);
80 private final URL baseUrl;
82 private final URL objectsUrl;
84 private final ProxySelector proxySelector;
86 TransportHttp(final Repository local, final URIish uri)
87 throws NotSupportedException {
88 super(local, uri);
89 try {
90 String uriString = uri.toString();
91 if (!uriString.endsWith("/"))
92 uriString += "/";
93 baseUrl = new URL(uriString);
94 objectsUrl = new URL(baseUrl, "objects/");
95 } catch (MalformedURLException e) {
96 throw new NotSupportedException("Invalid URL " + uri, e);
98 proxySelector = ProxySelector.getDefault();
101 @Override
102 public FetchConnection openFetch() throws TransportException {
103 final HttpObjectDB c = new HttpObjectDB(objectsUrl);
104 final WalkFetchConnection r = new WalkFetchConnection(this, c);
105 c.readAdvertisedRefs(r);
106 return r;
109 Proxy proxyFor(final URL u) throws URISyntaxException {
110 return proxySelector.select(u.toURI()).get(0);
113 class HttpObjectDB extends WalkRemoteObjectDatabase {
114 private static final String INFO_REFS = "../info/refs";
116 private final URL objectsUrl;
118 HttpObjectDB(final URL b) {
119 objectsUrl = b;
122 @Override
123 Collection<WalkRemoteObjectDatabase> getAlternates() throws IOException {
124 try {
125 return readAlternates(INFO_HTTP_ALTERNATES);
126 } catch (FileNotFoundException err) {
127 // Fall through.
130 try {
131 return readAlternates(INFO_ALTERNATES);
132 } catch (FileNotFoundException err) {
133 // Fall through.
136 return null;
139 @Override
140 WalkRemoteObjectDatabase openAlternate(final String location)
141 throws IOException {
142 return new HttpObjectDB(new URL(objectsUrl, location));
145 @Override
146 Collection<String> getPackNames() throws IOException {
147 final Collection<String> packs = new ArrayList<String>();
148 try {
149 final BufferedReader br = openReader(INFO_PACKS);
150 try {
151 for (;;) {
152 final String s = br.readLine();
153 if (s == null || s.length() == 0)
154 break;
155 if (!s.startsWith("P pack-") || !s.endsWith(".pack"))
156 throw invalidAdvertisement(s);
157 packs.add(s.substring(2));
159 return packs;
160 } finally {
161 br.close();
163 } catch (FileNotFoundException err) {
164 return packs;
168 @Override
169 FileStream open(final String path) throws IOException {
170 final URL base = objectsUrl;
171 try {
172 final URL u = new URL(base, path);
173 final URLConnection c = u.openConnection(proxyFor(u));
174 final InputStream in = c.getInputStream();
175 final int len = c.getContentLength();
176 return new FileStream(in, len);
177 } catch (ConnectException ce) {
178 // The standard J2SE error message is not very useful.
180 if ("Connection timed out: connect".equals(ce.getMessage()))
181 throw new ConnectException("Connection timed out: " + base);
182 throw new ConnectException(ce.getMessage() + " " + base);
183 } catch (URISyntaxException e) {
184 final ConnectException err;
185 err = new ConnectException("Cannot determine proxy for " + base);
186 err.initCause(e);
187 throw err;
191 void readAdvertisedRefs(final WalkFetchConnection c)
192 throws TransportException {
193 try {
194 final BufferedReader br = openReader(INFO_REFS);
195 try {
196 readAdvertisedImpl(br, c);
197 } finally {
198 br.close();
200 } catch (IOException err) {
201 try {
202 throw new TransportException(new URL(objectsUrl, INFO_REFS)
203 + ": cannot read available refs", err);
204 } catch (MalformedURLException mue) {
205 throw new TransportException(objectsUrl + INFO_REFS
206 + ": cannot read available refs", err);
211 private void readAdvertisedImpl(final BufferedReader br,
212 final WalkFetchConnection connection) throws IOException,
213 PackProtocolException {
214 final TreeMap<String, Ref> avail = new TreeMap<String, Ref>();
215 for (;;) {
216 String line = br.readLine();
217 if (line == null)
218 break;
220 final int tab = line.indexOf('\t');
221 if (tab < 0)
222 throw invalidAdvertisement(line);
224 String name;
225 final ObjectId id;
227 name = line.substring(tab + 1);
228 id = ObjectId.fromString(line.substring(0, tab));
229 if (name.endsWith("^{}")) {
230 name = name.substring(0, name.length() - 3);
231 final Ref prior = avail.get(name);
232 if (prior == null)
233 throw outOfOrderAdvertisement(name);
235 if (prior.getPeeledObjectId() != null)
236 throw duplicateAdvertisement(name + "^{}");
238 avail.put(name, new Ref(Ref.Storage.NETWORK, name, prior
239 .getObjectId(), id));
240 } else {
241 final Ref prior = avail.put(name, new Ref(
242 Ref.Storage.NETWORK, name, id));
243 if (prior != null)
244 throw duplicateAdvertisement(name);
247 connection.available(avail);
250 private PackProtocolException outOfOrderAdvertisement(final String n) {
251 return new PackProtocolException("advertisement of " + n
252 + "^{} came before " + n);
255 private PackProtocolException invalidAdvertisement(final String n) {
256 return new PackProtocolException("invalid advertisement of " + n);
259 private PackProtocolException duplicateAdvertisement(final String n) {
260 return new PackProtocolException("duplicate advertisements of " + n);
263 @Override
264 void close() {
265 // We do not maintain persistent connections.