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<>();
121 
122 	/** Extra objects the remote has, but which aren't offered as refs. */
123 	protected final Set<ObjectId> additionalHaves = new HashSet<>();
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 			if (myTimer == null) {
147 				myTimer = new InterruptTimer(caller.getName() + "-Timer"); //$NON-NLS-1$
148 			}
149 			timeoutIn = new TimeoutInputStream(myIn, myTimer);
150 			timeoutOut = new TimeoutOutputStream(myOut, myTimer);
151 			timeoutIn.setTimeout(timeout * 1000);
152 			timeoutOut.setTimeout(timeout * 1000);
153 			myIn = timeoutIn;
154 			myOut = timeoutOut;
155 		}
156 
157 		in = myIn;
158 		out = myOut;
159 
160 		pckIn = new PacketLineIn(in);
161 		pckOut = new PacketLineOut(out);
162 		outNeedsEnd = true;
163 	}
164 
165 	/**
166 	 * Reads the advertised references through the initialized stream.
167 	 * <p>
168 	 * Subclass implementations may call this method only after setting up the
169 	 * input and output streams with {@link #init(InputStream, OutputStream)}.
170 	 * <p>
171 	 * If any errors occur, this connection is automatically closed by invoking
172 	 * {@link #close()} and the exception is wrapped (if necessary) and thrown
173 	 * as a {@link TransportException}.
174 	 *
175 	 * @throws TransportException
176 	 *             the reference list could not be scanned.
177 	 */
178 	protected void readAdvertisedRefs() throws TransportException {
179 		try {
180 			readAdvertisedRefsImpl();
181 		} catch (TransportException err) {
182 			close();
183 			throw err;
184 		} catch (IOException err) {
185 			close();
186 			throw new TransportException(err.getMessage(), err);
187 		} catch (RuntimeException err) {
188 			close();
189 			throw new TransportException(err.getMessage(), err);
190 		}
191 	}
192 
193 	private void readAdvertisedRefsImpl() throws IOException {
194 		final LinkedHashMap<String, Ref> avail = new LinkedHashMap<>();
195 		for (;;) {
196 			String line;
197 
198 			try {
199 				line = pckIn.readString();
200 			} catch (EOFException eof) {
201 				if (avail.isEmpty())
202 					throw noRepository();
203 				throw eof;
204 			}
205 			if (line == PacketLineIn.END)
206 				break;
207 
208 			if (line.startsWith("ERR ")) { //$NON-NLS-1$
209 				// This is a customized remote service error.
210 				// Users should be informed about it.
211 				throw new RemoteRepositoryException(uri, line.substring(4));
212 			}
213 
214 			if (avail.isEmpty()) {
215 				final int nul = line.indexOf('\0');
216 				if (nul >= 0) {
217 					// The first line (if any) may contain "hidden"
218 					// capability values after a NUL byte.
219 					for (String c : line.substring(nul + 1).split(" ")) //$NON-NLS-1$
220 						remoteCapablities.add(c);
221 					line = line.substring(0, nul);
222 				}
223 			}
224 
225 			String name = line.substring(41, line.length());
226 			if (avail.isEmpty() && name.equals("capabilities^{}")) { //$NON-NLS-1$
227 				// special line from git-receive-pack to show
228 				// capabilities when there are no refs to advertise
229 				continue;
230 			}
231 
232 			final ObjectId id = ObjectId.fromString(line.substring(0, 40));
233 			if (name.equals(".have")) { //$NON-NLS-1$
234 				additionalHaves.add(id);
235 			} else if (name.endsWith("^{}")) { //$NON-NLS-1$
236 				name = name.substring(0, name.length() - 3);
237 				final Ref prior = avail.get(name);
238 				if (prior == null)
239 					throw new PackProtocolException(uri, MessageFormat.format(
240 							JGitText.get().advertisementCameBefore, name, name));
241 
242 				if (prior.getPeeledObjectId() != null)
243 					throw duplicateAdvertisement(name + "^{}"); //$NON-NLS-1$
244 
245 				avail.put(name, new ObjectIdRef.PeeledTag(
246 						Ref.Storage.NETWORK, name, prior.getObjectId(), id));
247 			} else {
248 				final Ref prior = avail.put(name, new ObjectIdRef.PeeledNonTag(
249 						Ref.Storage.NETWORK, name, id));
250 				if (prior != null)
251 					throw duplicateAdvertisement(name);
252 			}
253 		}
254 		available(avail);
255 	}
256 
257 	/**
258 	 * Create an exception to indicate problems finding a remote repository. The
259 	 * caller is expected to throw the returned exception.
260 	 *
261 	 * Subclasses may override this method to provide better diagnostics.
262 	 *
263 	 * @return a TransportException saying a repository cannot be found and
264 	 *         possibly why.
265 	 */
266 	protected TransportException noRepository() {
267 		return new NoRemoteRepositoryException(uri, JGitText.get().notFound);
268 	}
269 
270 	protected boolean isCapableOf(final String option) {
271 		return remoteCapablities.contains(option);
272 	}
273 
274 	protected boolean wantCapability(final StringBuilder b, final String option) {
275 		if (!isCapableOf(option))
276 			return false;
277 		b.append(' ');
278 		b.append(option);
279 		return true;
280 	}
281 
282 	protected void addUserAgentCapability(StringBuilder b) {
283 		String a = UserAgent.get();
284 		if (a != null && UserAgent.hasAgent(remoteCapablities)) {
285 			b.append(' ').append(OPTION_AGENT).append('=').append(a);
286 		}
287 	}
288 
289 	@Override
290 	public String getPeerUserAgent() {
291 		return UserAgent.getAgent(remoteCapablities, super.getPeerUserAgent());
292 	}
293 
294 	private PackProtocolException duplicateAdvertisement(final String name) {
295 		return new PackProtocolException(uri, MessageFormat.format(JGitText.get().duplicateAdvertisementsOf, name));
296 	}
297 
298 	@Override
299 	public void close() {
300 		if (out != null) {
301 			try {
302 				if (outNeedsEnd) {
303 					outNeedsEnd = false;
304 					pckOut.end();
305 				}
306 				out.close();
307 			} catch (IOException err) {
308 				// Ignore any close errors.
309 			} finally {
310 				out = null;
311 				pckOut = null;
312 			}
313 		}
314 
315 		if (in != null) {
316 			try {
317 				in.close();
318 			} catch (IOException err) {
319 				// Ignore any close errors.
320 			} finally {
321 				in = null;
322 				pckIn = null;
323 			}
324 		}
325 
326 		if (myTimer != null) {
327 			try {
328 				myTimer.terminate();
329 			} finally {
330 				myTimer = null;
331 				timeoutIn = null;
332 				timeoutOut = null;
333 			}
334 		}
335 	}
336 
337 	/** Tell the peer we are disconnecting, if it cares to know. */
338 	protected void endOut() {
339 		if (outNeedsEnd && out != null) {
340 			try {
341 				outNeedsEnd = false;
342 				pckOut.end();
343 			} catch (IOException e) {
344 				try {
345 					out.close();
346 				} catch (IOException err) {
347 					// Ignore any close errors.
348 				} finally {
349 					out = null;
350 					pckOut = null;
351 				}
352 			}
353 		}
354 	}
355 }