View Javadoc
1   /*
2    * Copyright (C) 2008-2010, Google Inc.
3    * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
4    * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
5    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
6    * and other copyright owners as documented in the project's IP log.
7    *
8    * This program and the accompanying materials are made available
9    * under the terms of the Eclipse Distribution License v1.0 which
10   * accompanies this distribution, is reproduced below, and is
11   * available at http://www.eclipse.org/org/documents/edl-v10.php
12   *
13   * All rights reserved.
14   *
15   * Redistribution and use in source and binary forms, with or
16   * without modification, are permitted provided that the following
17   * conditions are met:
18   *
19   * - Redistributions of source code must retain the above copyright
20   *   notice, this list of conditions and the following disclaimer.
21   *
22   * - Redistributions in binary form must reproduce the above
23   *   copyright notice, this list of conditions and the following
24   *   disclaimer in the documentation and/or other materials provided
25   *   with the distribution.
26   *
27   * - Neither the name of the Eclipse Foundation, Inc. nor the
28   *   names of its contributors may be used to endorse or promote
29   *   products derived from this software without specific prior
30   *   written permission.
31   *
32   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
33   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
34   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
37   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
39   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
40   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
41   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
42   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
43   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
44   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45   */
46  
47  package org.eclipse.jgit.transport;
48  
49  import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_AGENT;
50  
51  import java.io.EOFException;
52  import java.io.IOException;
53  import java.io.InputStream;
54  import java.io.OutputStream;
55  import java.text.MessageFormat;
56  import java.util.HashSet;
57  import java.util.LinkedHashMap;
58  import java.util.Set;
59  
60  import org.eclipse.jgit.errors.NoRemoteRepositoryException;
61  import org.eclipse.jgit.errors.PackProtocolException;
62  import org.eclipse.jgit.errors.RemoteRepositoryException;
63  import org.eclipse.jgit.errors.TransportException;
64  import org.eclipse.jgit.internal.JGitText;
65  import org.eclipse.jgit.lib.ObjectId;
66  import org.eclipse.jgit.lib.ObjectIdRef;
67  import org.eclipse.jgit.lib.Ref;
68  import org.eclipse.jgit.lib.Repository;
69  import org.eclipse.jgit.util.io.InterruptTimer;
70  import org.eclipse.jgit.util.io.TimeoutInputStream;
71  import org.eclipse.jgit.util.io.TimeoutOutputStream;
72  
73  /**
74   * Base helper class for pack-based operations implementations. Provides partial
75   * implementation of pack-protocol - refs advertising and capabilities support,
76   * and some other helper methods.
77   *
78   * @see BasePackFetchConnection
79   * @see BasePackPushConnection
80   */
81  abstract class BasePackConnection extends BaseConnection {
82  
83  	/** The repository this transport fetches into, or pushes out of. */
84  	protected final Repository local;
85  
86  	/** Remote repository location. */
87  	protected final URIish uri;
88  
89  	/** A transport connected to {@link #uri}. */
90  	protected final Transport transport;
91  
92  	/** Low-level input stream, if a timeout was configured. */
93  	protected TimeoutInputStream timeoutIn;
94  
95  	/** Low-level output stream, if a timeout was configured. */
96  	protected TimeoutOutputStream timeoutOut;
97  
98  	/** Timer to manage {@link #timeoutIn} and {@link #timeoutOut}. */
99  	private InterruptTimer myTimer;
100 
101 	/** Input stream reading from the remote. */
102 	protected InputStream in;
103 
104 	/** Output stream sending to the remote. */
105 	protected OutputStream out;
106 
107 	/** Packet line decoder around {@link #in}. */
108 	protected PacketLineIn pckIn;
109 
110 	/** Packet line encoder around {@link #out}. */
111 	protected PacketLineOut pckOut;
112 
113 	/** Send {@link PacketLineOut#end()} before closing {@link #out}? */
114 	protected boolean outNeedsEnd;
115 
116 	/** True if this is a stateless RPC connection. */
117 	protected boolean statelessRPC;
118 
119 	/** Capability tokens advertised by the remote side. */
120 	private final Set<String> remoteCapablities = new HashSet<String>();
121 
122 	/** Extra objects the remote has, but which aren't offered as refs. */
123 	protected final Set<ObjectId> additionalHaves = new HashSet<ObjectId>();
124 
125 	BasePackConnection(final PackTransport packTransport) {
126 		transport = (Transport) packTransport;
127 		local = transport.local;
128 		uri = transport.uri;
129 	}
130 
131 	/**
132 	 * Configure this connection with the directional pipes.
133 	 *
134 	 * @param myIn
135 	 *            input stream to receive data from the peer. Caller must ensure
136 	 *            the input is buffered, otherwise read performance may suffer.
137 	 * @param myOut
138 	 *            output stream to transmit data to the peer. Caller must ensure
139 	 *            the output is buffered, otherwise write performance may
140 	 *            suffer.
141 	 */
142 	protected final void init(InputStream myIn, OutputStream myOut) {
143 		final int timeout = transport.getTimeout();
144 		if (timeout > 0) {
145 			final Thread caller = Thread.currentThread();
146 			myTimer = new InterruptTimer(caller.getName() + "-Timer"); //$NON-NLS-1$
147 			timeoutIn = new TimeoutInputStream(myIn, myTimer);
148 			timeoutOut = new TimeoutOutputStream(myOut, myTimer);
149 			timeoutIn.setTimeout(timeout * 1000);
150 			timeoutOut.setTimeout(timeout * 1000);
151 			myIn = timeoutIn;
152 			myOut = timeoutOut;
153 		}
154 
155 		in = myIn;
156 		out = myOut;
157 
158 		pckIn = new PacketLineIn(in);
159 		pckOut = new PacketLineOut(out);
160 		outNeedsEnd = true;
161 	}
162 
163 	/**
164 	 * Reads the advertised references through the initialized stream.
165 	 * <p>
166 	 * Subclass implementations may call this method only after setting up the
167 	 * input and output streams with {@link #init(InputStream, OutputStream)}.
168 	 * <p>
169 	 * If any errors occur, this connection is automatically closed by invoking
170 	 * {@link #close()} and the exception is wrapped (if necessary) and thrown
171 	 * as a {@link TransportException}.
172 	 *
173 	 * @throws TransportException
174 	 *             the reference list could not be scanned.
175 	 */
176 	protected void readAdvertisedRefs() throws TransportException {
177 		try {
178 			readAdvertisedRefsImpl();
179 		} catch (TransportException err) {
180 			close();
181 			throw err;
182 		} catch (IOException err) {
183 			close();
184 			throw new TransportException(err.getMessage(), err);
185 		} catch (RuntimeException err) {
186 			close();
187 			throw new TransportException(err.getMessage(), err);
188 		}
189 	}
190 
191 	private void readAdvertisedRefsImpl() throws IOException {
192 		final LinkedHashMap<String, Ref> avail = new LinkedHashMap<String, Ref>();
193 		for (;;) {
194 			String line;
195 
196 			try {
197 				line = pckIn.readString();
198 			} catch (EOFException eof) {
199 				if (avail.isEmpty())
200 					throw noRepository();
201 				throw eof;
202 			}
203 			if (line == PacketLineIn.END)
204 				break;
205 
206 			if (line.startsWith("ERR ")) { //$NON-NLS-1$
207 				// This is a customized remote service error.
208 				// Users should be informed about it.
209 				throw new RemoteRepositoryException(uri, line.substring(4));
210 			}
211 
212 			if (avail.isEmpty()) {
213 				final int nul = line.indexOf('\0');
214 				if (nul >= 0) {
215 					// The first line (if any) may contain "hidden"
216 					// capability values after a NUL byte.
217 					for (String c : line.substring(nul + 1).split(" ")) //$NON-NLS-1$
218 						remoteCapablities.add(c);
219 					line = line.substring(0, nul);
220 				}
221 			}
222 
223 			String name = line.substring(41, line.length());
224 			if (avail.isEmpty() && name.equals("capabilities^{}")) { //$NON-NLS-1$
225 				// special line from git-receive-pack to show
226 				// capabilities when there are no refs to advertise
227 				continue;
228 			}
229 
230 			final ObjectId id = ObjectId.fromString(line.substring(0, 40));
231 			if (name.equals(".have")) { //$NON-NLS-1$
232 				additionalHaves.add(id);
233 			} else if (name.endsWith("^{}")) { //$NON-NLS-1$
234 				name = name.substring(0, name.length() - 3);
235 				final Ref prior = avail.get(name);
236 				if (prior == null)
237 					throw new PackProtocolException(uri, MessageFormat.format(
238 							JGitText.get().advertisementCameBefore, name, name));
239 
240 				if (prior.getPeeledObjectId() != null)
241 					throw duplicateAdvertisement(name + "^{}"); //$NON-NLS-1$
242 
243 				avail.put(name, new ObjectIdRef.PeeledTag(
244 						Ref.Storage.NETWORK, name, prior.getObjectId(), id));
245 			} else {
246 				final Ref prior = avail.put(name, new ObjectIdRef.PeeledNonTag(
247 						Ref.Storage.NETWORK, name, id));
248 				if (prior != null)
249 					throw duplicateAdvertisement(name);
250 			}
251 		}
252 		available(avail);
253 	}
254 
255 	/**
256 	 * Create an exception to indicate problems finding a remote repository. The
257 	 * caller is expected to throw the returned exception.
258 	 *
259 	 * Subclasses may override this method to provide better diagnostics.
260 	 *
261 	 * @return a TransportException saying a repository cannot be found and
262 	 *         possibly why.
263 	 */
264 	protected TransportException noRepository() {
265 		return new NoRemoteRepositoryException(uri, JGitText.get().notFound);
266 	}
267 
268 	protected boolean isCapableOf(final String option) {
269 		return remoteCapablities.contains(option);
270 	}
271 
272 	protected boolean wantCapability(final StringBuilder b, final String option) {
273 		if (!isCapableOf(option))
274 			return false;
275 		b.append(' ');
276 		b.append(option);
277 		return true;
278 	}
279 
280 	protected void addUserAgentCapability(StringBuilder b) {
281 		String a = UserAgent.get();
282 		if (a != null && UserAgent.hasAgent(remoteCapablities)) {
283 			b.append(' ').append(OPTION_AGENT).append('=').append(a);
284 		}
285 	}
286 
287 	@Override
288 	public String getPeerUserAgent() {
289 		return UserAgent.getAgent(remoteCapablities, super.getPeerUserAgent());
290 	}
291 
292 	private PackProtocolException duplicateAdvertisement(final String name) {
293 		return new PackProtocolException(uri, MessageFormat.format(JGitText.get().duplicateAdvertisementsOf, name));
294 	}
295 
296 	@Override
297 	public void close() {
298 		if (out != null) {
299 			try {
300 				if (outNeedsEnd) {
301 					outNeedsEnd = false;
302 					pckOut.end();
303 				}
304 				out.close();
305 			} catch (IOException err) {
306 				// Ignore any close errors.
307 			} finally {
308 				out = null;
309 				pckOut = null;
310 			}
311 		}
312 
313 		if (in != null) {
314 			try {
315 				in.close();
316 			} catch (IOException err) {
317 				// Ignore any close errors.
318 			} finally {
319 				in = null;
320 				pckIn = null;
321 			}
322 		}
323 
324 		if (myTimer != null) {
325 			try {
326 				myTimer.terminate();
327 			} finally {
328 				myTimer = null;
329 				timeoutIn = null;
330 				timeoutOut = null;
331 			}
332 		}
333 	}
334 
335 	/** Tell the peer we are disconnecting, if it cares to know. */
336 	protected void endOut() {
337 		if (outNeedsEnd && out != null) {
338 			try {
339 				outNeedsEnd = false;
340 				pckOut.end();
341 			} catch (IOException e) {
342 				try {
343 					out.close();
344 				} catch (IOException err) {
345 					// Ignore any close errors.
346 				} finally {
347 					out = null;
348 					pckOut = null;
349 				}
350 			}
351 		}
352 	}
353 }