2 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
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
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
;
50 import java
.net
.URLConnection
;
51 import java
.util
.ArrayList
;
52 import java
.util
.Collection
;
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
;
64 * Transport over the non-Git aware HTTP and FTP protocol.
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
) {
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
{
91 String uriString
= uri
.toString();
92 if (!uriString
.endsWith("/"))
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();
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());
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
) {
122 Collection
<WalkRemoteObjectDatabase
> getAlternates() throws IOException
{
124 return readAlternates(INFO_HTTP_ALTERNATES
);
125 } catch (FileNotFoundException err
) {
130 return readAlternates(INFO_ALTERNATES
);
131 } catch (FileNotFoundException err
) {
139 WalkRemoteObjectDatabase
openAlternate(final String location
)
141 return new HttpObjectDB(new URL(objectsUrl
, location
));
145 Collection
<String
> getPackNames() throws IOException
{
146 final Collection
<String
> packs
= new ArrayList
<String
>();
148 final BufferedReader br
= openReader(INFO_PACKS
);
151 final String s
= br
.readLine();
152 if (s
== null || s
.length() == 0)
154 if (!s
.startsWith("P pack-") || !s
.endsWith(".pack"))
155 throw invalidAdvertisement(s
);
156 packs
.add(s
.substring(2));
162 } catch (FileNotFoundException err
) {
168 FileStream
open(final String path
) throws IOException
{
169 final URL base
= objectsUrl
;
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
);
190 Map
<String
, Ref
> readAdvertisedRefs() throws TransportException
{
192 final BufferedReader br
= openReader(INFO_REFS
);
194 return readAdvertisedImpl(br
);
198 } catch (IOException err
) {
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
>();
213 String line
= br
.readLine();
217 final int tab
= line
.indexOf('\t');
219 throw invalidAdvertisement(line
);
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
);
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
));
238 final Ref prior
= avail
.put(name
, new Ref(
239 Ref
.Storage
.NETWORK
, name
, id
));
241 throw duplicateAdvertisement(name
);
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
);
262 // We do not maintain persistent connections.