2 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
4 * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
8 * Redistribution and use in source and binary forms, with or
9 * without modification, are permitted provided that the following
12 * - Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * - Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials provided
18 * with the distribution.
20 * - Neither the name of the Git Development Community nor the
21 * names of its contributors may be used to endorse or promote
22 * products derived from this software without specific prior
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
26 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
27 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
30 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
35 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 package org
.spearce
.jgit
.transport
;
42 import java
.io
.IOException
;
43 import java
.net
.ConnectException
;
44 import java
.net
.InetAddress
;
45 import java
.net
.Socket
;
46 import java
.net
.UnknownHostException
;
48 import org
.spearce
.jgit
.errors
.TransportException
;
49 import org
.spearce
.jgit
.lib
.Repository
;
52 * Transport through a git-daemon waiting for anonymous TCP connections.
54 * This transport supports the <code>git://</code> protocol, usually run on
55 * the IANA registered port 9418. It is a popular means for distributing open
56 * source projects, as there are no authentication or authorization overheads.
58 class TransportGitAnon
extends PackTransport
{
59 /** IANA assigned port number for Git. */
60 static final int GIT_PORT
= 9418;
62 static boolean canHandle(final URIish uri
) {
63 return "git".equals(uri
.getScheme());
66 TransportGitAnon(final Repository local
, final URIish uri
) {
71 public FetchConnection
openFetch() throws TransportException
{
72 return new TcpFetchConnection();
76 public PushConnection
openPush() throws TransportException
{
77 return new TcpPushConnection();
80 Socket
openConnection() throws TransportException
{
81 final int port
= uri
.getPort() > 0 ? uri
.getPort() : GIT_PORT
;
83 return new Socket(InetAddress
.getByName(uri
.getHost()), port
);
84 } catch (IOException c
) {
85 final String us
= uri
.toString();
86 if (c
instanceof UnknownHostException
)
87 throw new TransportException(us
+ ": Unknown host");
88 if (c
instanceof ConnectException
)
89 throw new TransportException(us
+ ": " + c
.getMessage());
90 throw new TransportException(us
+ ": " + c
.getMessage(), c
);
94 void service(final String name
, final PacketLineOut pckOut
)
96 final StringBuilder cmd
= new StringBuilder();
99 cmd
.append(uri
.getPath());
102 cmd
.append(uri
.getHost());
104 pckOut
.writeString(cmd
.toString());
108 class TcpFetchConnection
extends BasePackFetchConnection
{
111 TcpFetchConnection() throws TransportException
{
112 super(TransportGitAnon
.this);
113 sock
= openConnection();
115 init(sock
.getInputStream(), sock
.getOutputStream());
116 service("git-upload-pack", pckOut
);
117 } catch (IOException err
) {
119 throw new TransportException(uri
.toString()
120 + ": remote hung up unexpectedly", err
);
122 readAdvertisedRefs();
126 public void close() {
132 } catch (IOException err
) {
133 // Ignore errors during close.
141 class TcpPushConnection
extends BasePackPushConnection
{
144 TcpPushConnection() throws TransportException
{
145 super(TransportGitAnon
.this);
146 sock
= openConnection();
148 init(sock
.getInputStream(), sock
.getOutputStream());
149 service("git-receive-pack", pckOut
);
150 } catch (IOException err
) {
152 throw new TransportException(uri
.toString()
153 + ": remote hung up unexpectedly", err
);
155 readAdvertisedRefs();
159 public void close() {
165 } catch (IOException err
) {
166 // Ignore errors during close.