View Javadoc
1   /*
2    * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
3    * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
4    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
5    * and other copyright owners as documented in the project's IP log.
6    *
7    * This program and the accompanying materials are made available
8    * under the terms of the Eclipse Distribution License v1.0 which
9    * accompanies this distribution, is reproduced below, and is
10   * available at http://www.eclipse.org/org/documents/edl-v10.php
11   *
12   * All rights reserved.
13   *
14   * Redistribution and use in source and binary forms, with or
15   * without modification, are permitted provided that the following
16   * conditions are met:
17   *
18   * - Redistributions of source code must retain the above copyright
19   *   notice, this list of conditions and the following disclaimer.
20   *
21   * - Redistributions in binary form must reproduce the above
22   *   copyright notice, this list of conditions and the following
23   *   disclaimer in the documentation and/or other materials provided
24   *   with the distribution.
25   *
26   * - Neither the name of the Eclipse Foundation, Inc. nor the
27   *   names of its contributors may be used to endorse or promote
28   *   products derived from this software without specific prior
29   *   written permission.
30   *
31   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
32   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
33   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
34   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
36   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
38   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
39   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
40   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
43   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44   */
45  
46  package org.eclipse.jgit.transport;
47  
48  import java.io.BufferedInputStream;
49  import java.io.BufferedOutputStream;
50  import java.io.IOException;
51  import java.io.InputStream;
52  import java.io.OutputStream;
53  import java.net.ConnectException;
54  import java.net.InetAddress;
55  import java.net.InetSocketAddress;
56  import java.net.Socket;
57  import java.net.UnknownHostException;
58  import java.util.Collections;
59  import java.util.EnumSet;
60  import java.util.Set;
61  
62  import org.eclipse.jgit.errors.NotSupportedException;
63  import org.eclipse.jgit.errors.TransportException;
64  import org.eclipse.jgit.internal.JGitText;
65  import org.eclipse.jgit.lib.Repository;
66  
67  /**
68   * Transport through a git-daemon waiting for anonymous TCP connections.
69   * <p>
70   * This transport supports the <code>git://</code> protocol, usually run on
71   * the IANA registered port 9418. It is a popular means for distributing open
72   * source projects, as there are no authentication or authorization overheads.
73   */
74  class TransportGitAnon extends TcpTransport implements PackTransport {
75  	static final int GIT_PORT = Daemon.DEFAULT_PORT;
76  
77  	static final TransportProtocol PROTO_GIT = new TransportProtocol() {
78  		@Override
79  		public String getName() {
80  			return JGitText.get().transportProtoGitAnon;
81  		}
82  
83  		@Override
84  		public Set<String> getSchemes() {
85  			return Collections.singleton("git"); //$NON-NLS-1$
86  		}
87  
88  		@Override
89  		public Set<URIishField> getRequiredFields() {
90  			return Collections.unmodifiableSet(EnumSet.of(URIishField.HOST,
91  					URIishField.PATH));
92  		}
93  
94  		@Override
95  		public Set<URIishField> getOptionalFields() {
96  			return Collections.unmodifiableSet(EnumSet.of(URIishField.PORT));
97  		}
98  
99  		@Override
100 		public int getDefaultPort() {
101 			return GIT_PORT;
102 		}
103 
104 		@Override
105 		public Transport open(URIish uri, Repository local, String remoteName)
106 				throws NotSupportedException {
107 			return new TransportGitAnon(local, uri);
108 		}
109 
110 		@Override
111 		public Transport open(URIish uri) throws NotSupportedException, TransportException {
112 			return new TransportGitAnon(uri);
113 		}
114 	};
115 
116 	TransportGitAnon(Repository local, URIish uri) {
117 		super(local, uri);
118 	}
119 
120 	TransportGitAnon(URIish uri) {
121 		super(uri);
122 	}
123 
124 	/** {@inheritDoc} */
125 	@Override
126 	public FetchConnection openFetch() throws TransportException {
127 		return new TcpFetchConnection();
128 	}
129 
130 	/** {@inheritDoc} */
131 	@Override
132 	public PushConnection openPush() throws TransportException {
133 		return new TcpPushConnection();
134 	}
135 
136 	/** {@inheritDoc} */
137 	@Override
138 	public void close() {
139 		// Resources must be established per-connection.
140 	}
141 
142 	Socket openConnection() throws TransportException {
143 		final int tms = getTimeout() > 0 ? getTimeout() * 1000 : 0;
144 		final int port = uri.getPort() > 0 ? uri.getPort() : GIT_PORT;
145 		@SuppressWarnings("resource") // Closed by the caller
146 		final Socket s = new Socket();
147 		try {
148 			final InetAddress host = InetAddress.getByName(uri.getHost());
149 			s.bind(null);
150 			s.connect(new InetSocketAddress(host, port), tms);
151 		} catch (IOException c) {
152 			try {
153 				s.close();
154 			} catch (IOException closeErr) {
155 				// ignore a failure during close, we're already failing
156 			}
157 			if (c instanceof UnknownHostException)
158 				throw new TransportException(uri, JGitText.get().unknownHost);
159 			if (c instanceof ConnectException)
160 				throw new TransportException(uri, c.getMessage());
161 			throw new TransportException(uri, c.getMessage(), c);
162 		}
163 		return s;
164 	}
165 
166 	void service(String name, PacketLineOut pckOut)
167 			throws IOException {
168 		final StringBuilder cmd = new StringBuilder();
169 		cmd.append(name);
170 		cmd.append(' ');
171 		cmd.append(uri.getPath());
172 		cmd.append('\0');
173 		cmd.append("host="); //$NON-NLS-1$
174 		cmd.append(uri.getHost());
175 		if (uri.getPort() > 0 && uri.getPort() != GIT_PORT) {
176 			cmd.append(":"); //$NON-NLS-1$
177 			cmd.append(uri.getPort());
178 		}
179 		cmd.append('\0');
180 		pckOut.writeString(cmd.toString());
181 		pckOut.flush();
182 	}
183 
184 	class TcpFetchConnection extends BasePackFetchConnection {
185 		private Socket sock;
186 
187 		TcpFetchConnection() throws TransportException {
188 			super(TransportGitAnon.this);
189 			sock = openConnection();
190 			try {
191 				InputStream sIn = sock.getInputStream();
192 				OutputStream sOut = sock.getOutputStream();
193 
194 				sIn = new BufferedInputStream(sIn);
195 				sOut = new BufferedOutputStream(sOut);
196 
197 				init(sIn, sOut);
198 				service("git-upload-pack", pckOut); //$NON-NLS-1$
199 			} catch (IOException err) {
200 				close();
201 				throw new TransportException(uri,
202 						JGitText.get().remoteHungUpUnexpectedly, err);
203 			}
204 			readAdvertisedRefs();
205 		}
206 
207 		@Override
208 		public void close() {
209 			super.close();
210 
211 			if (sock != null) {
212 				try {
213 					sock.close();
214 				} catch (IOException err) {
215 					// Ignore errors during close.
216 				} finally {
217 					sock = null;
218 				}
219 			}
220 		}
221 	}
222 
223 	class TcpPushConnection extends BasePackPushConnection {
224 		private Socket sock;
225 
226 		TcpPushConnection() throws TransportException {
227 			super(TransportGitAnon.this);
228 			sock = openConnection();
229 			try {
230 				InputStream sIn = sock.getInputStream();
231 				OutputStream sOut = sock.getOutputStream();
232 
233 				sIn = new BufferedInputStream(sIn);
234 				sOut = new BufferedOutputStream(sOut);
235 
236 				init(sIn, sOut);
237 				service("git-receive-pack", pckOut); //$NON-NLS-1$
238 			} catch (IOException err) {
239 				close();
240 				throw new TransportException(uri,
241 						JGitText.get().remoteHungUpUnexpectedly, err);
242 			}
243 			readAdvertisedRefs();
244 		}
245 
246 		@Override
247 		public void close() {
248 			super.close();
249 
250 			if (sock != null) {
251 				try {
252 					sock.close();
253 				} catch (IOException err) {
254 					// Ignore errors during close.
255 				} finally {
256 					sock = null;
257 				}
258 			}
259 		}
260 	}
261 }