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.Arrays;
57  import java.util.HashSet;
58  import java.util.LinkedHashMap;
59  import java.util.Set;
60  
61  import org.eclipse.jgit.errors.InvalidObjectIdException;
62  import org.eclipse.jgit.errors.NoRemoteRepositoryException;
63  import org.eclipse.jgit.errors.PackProtocolException;
64  import org.eclipse.jgit.errors.RemoteRepositoryException;
65  import org.eclipse.jgit.errors.TransportException;
66  import org.eclipse.jgit.internal.JGitText;
67  import org.eclipse.jgit.lib.ObjectId;
68  import org.eclipse.jgit.lib.ObjectIdRef;
69  import org.eclipse.jgit.lib.Ref;
70  import org.eclipse.jgit.lib.Repository;
71  import org.eclipse.jgit.util.io.InterruptTimer;
72  import org.eclipse.jgit.util.io.TimeoutInputStream;
73  import org.eclipse.jgit.util.io.TimeoutOutputStream;
74  
75  /**
76   * Base helper class for pack-based operations implementations. Provides partial
77   * implementation of pack-protocol - refs advertising and capabilities support,
78   * and some other helper methods.
79   *
80   * @see BasePackFetchConnection
81   * @see BasePackPushConnection
82   */
83  abstract class BasePackConnection extends BaseConnection {
84  
85  	/** The repository this transport fetches into, or pushes out of. */
86  	protected final Repository local;
87  
88  	/** Remote repository location. */
89  	protected final URIish uri;
90  
91  	/** A transport connected to {@link #uri}. */
92  	protected final Transport transport;
93  
94  	/** Low-level input stream, if a timeout was configured. */
95  	protected TimeoutInputStream timeoutIn;
96  
97  	/** Low-level output stream, if a timeout was configured. */
98  	protected TimeoutOutputStream timeoutOut;
99  
100 	/** Timer to manage {@link #timeoutIn} and {@link #timeoutOut}. */
101 	private InterruptTimer myTimer;
102 
103 	/** Input stream reading from the remote. */
104 	protected InputStream in;
105 
106 	/** Output stream sending to the remote. */
107 	protected OutputStream out;
108 
109 	/** Packet line decoder around {@link #in}. */
110 	protected PacketLineIn pckIn;
111 
112 	/** Packet line encoder around {@link #out}. */
113 	protected PacketLineOut pckOut;
114 
115 	/** Send {@link PacketLineOut#end()} before closing {@link #out}? */
116 	protected boolean outNeedsEnd;
117 
118 	/** True if this is a stateless RPC connection. */
119 	protected boolean statelessRPC;
120 
121 	/** Capability tokens advertised by the remote side. */
122 	private final Set<String> remoteCapablities = new HashSet<>();
123 
124 	/** Extra objects the remote has, but which aren't offered as refs. */
125 	protected final Set<ObjectId> additionalHaves = new HashSet<>();
126 
127 	BasePackConnection(PackTransport packTransport) {
128 		transport = (Transport) packTransport;
129 		local = transport.local;
130 		uri = transport.uri;
131 	}
132 
133 	/**
134 	 * Configure this connection with the directional pipes.
135 	 *
136 	 * @param myIn
137 	 *            input stream to receive data from the peer. Caller must ensure
138 	 *            the input is buffered, otherwise read performance may suffer.
139 	 * @param myOut
140 	 *            output stream to transmit data to the peer. Caller must ensure
141 	 *            the output is buffered, otherwise write performance may
142 	 *            suffer.
143 	 */
144 	protected final void init(InputStream myIn, OutputStream myOut) {
145 		final int timeout = transport.getTimeout();
146 		if (timeout > 0) {
147 			final Thread caller = Thread.currentThread();
148 			if (myTimer == null) {
149 				myTimer = new InterruptTimer(caller.getName() + "-Timer"); //$NON-NLS-1$
150 			}
151 			timeoutIn = new TimeoutInputStream(myIn, myTimer);
152 			timeoutOut = new TimeoutOutputStream(myOut, myTimer);
153 			timeoutIn.setTimeout(timeout * 1000);
154 			timeoutOut.setTimeout(timeout * 1000);
155 			myIn = timeoutIn;
156 			myOut = timeoutOut;
157 		}
158 
159 		in = myIn;
160 		out = myOut;
161 
162 		pckIn = new PacketLineIn(in);
163 		pckOut = new PacketLineOut(out);
164 		outNeedsEnd = true;
165 	}
166 
167 	/**
168 	 * Reads the advertised references through the initialized stream.
169 	 * <p>
170 	 * Subclass implementations may call this method only after setting up the
171 	 * input and output streams with {@link #init(InputStream, OutputStream)}.
172 	 * <p>
173 	 * If any errors occur, this connection is automatically closed by invoking
174 	 * {@link #close()} and the exception is wrapped (if necessary) and thrown
175 	 * as a {@link org.eclipse.jgit.errors.TransportException}.
176 	 *
177 	 * @throws org.eclipse.jgit.errors.TransportException
178 	 *             the reference list could not be scanned.
179 	 */
180 	protected void readAdvertisedRefs() throws TransportException {
181 		try {
182 			readAdvertisedRefsImpl();
183 		} catch (TransportException err) {
184 			close();
185 			throw err;
186 		} catch (IOException | RuntimeException err) {
187 			close();
188 			throw new TransportException(err.getMessage(), err);
189 		}
190 	}
191 
192 	private void readAdvertisedRefsImpl() throws IOException {
193 		final LinkedHashMap<String, Ref> avail = new LinkedHashMap<>();
194 		for (;;) {
195 			String line;
196 
197 			try {
198 				line = pckIn.readString();
199 			} catch (EOFException eof) {
200 				if (avail.isEmpty())
201 					throw noRepository();
202 				throw eof;
203 			}
204 			if (PacketLineIn.isEnd(line))
205 				break;
206 
207 			if (line.startsWith("ERR ")) { //$NON-NLS-1$
208 				// This is a customized remote service error.
209 				// Users should be informed about it.
210 				throw new RemoteRepositoryException(uri, line.substring(4));
211 			}
212 
213 			if (avail.isEmpty()) {
214 				final int nul = line.indexOf('\0');
215 				if (nul >= 0) {
216 					// The first line (if any) may contain "hidden"
217 					// capability values after a NUL byte.
218 					remoteCapablities.addAll(
219 							Arrays.asList(line.substring(nul + 1).split(" "))); //$NON-NLS-1$
220 					line = line.substring(0, nul);
221 				}
222 			}
223 
224 			// Expecting to get a line in the form "sha1 refname"
225 			if (line.length() < 41 || line.charAt(40) != ' ') {
226 				throw invalidRefAdvertisementLine(line);
227 			}
228 			String name = line.substring(41, line.length());
229 			if (avail.isEmpty() && name.equals("capabilities^{}")) { //$NON-NLS-1$
230 				// special line from git-receive-pack to show
231 				// capabilities when there are no refs to advertise
232 				continue;
233 			}
234 
235 			final ObjectId id;
236 			try {
237 				id  = ObjectId.fromString(line.substring(0, 40));
238 			} catch (InvalidObjectIdException e) {
239 				throw invalidRefAdvertisementLine(line);
240 			}
241 			if (name.equals(".have")) { //$NON-NLS-1$
242 				additionalHaves.add(id);
243 			} else if (name.endsWith("^{}")) { //$NON-NLS-1$
244 				name = name.substring(0, name.length() - 3);
245 				final Ref prior = avail.get(name);
246 				if (prior == null)
247 					throw new PackProtocolException(uri, MessageFormat.format(
248 							JGitText.get().advertisementCameBefore, name, name));
249 
250 				if (prior.getPeeledObjectId() != null)
251 					throw duplicateAdvertisement(name + "^{}"); //$NON-NLS-1$
252 
253 				avail.put(name, new ObjectIdRef.PeeledTag(
254 						Ref.Storage.NETWORK, name, prior.getObjectId(), id));
255 			} else {
256 				final Ref prior = avail.put(name, new ObjectIdRef.PeeledNonTag(
257 						Ref.Storage.NETWORK, name, id));
258 				if (prior != null)
259 					throw duplicateAdvertisement(name);
260 			}
261 		}
262 		available(avail);
263 	}
264 
265 	/**
266 	 * Create an exception to indicate problems finding a remote repository. The
267 	 * caller is expected to throw the returned exception.
268 	 *
269 	 * Subclasses may override this method to provide better diagnostics.
270 	 *
271 	 * @return a TransportException saying a repository cannot be found and
272 	 *         possibly why.
273 	 */
274 	protected TransportException noRepository() {
275 		return new NoRemoteRepositoryException(uri, JGitText.get().notFound);
276 	}
277 
278 	/**
279 	 * Whether this option is supported
280 	 *
281 	 * @param option
282 	 *            option string
283 	 * @return whether this option is supported
284 	 */
285 	protected boolean isCapableOf(String option) {
286 		return remoteCapablities.contains(option);
287 	}
288 
289 	/**
290 	 * Request capability
291 	 *
292 	 * @param b
293 	 *            buffer
294 	 * @param option
295 	 *            option we want
296 	 * @return {@code true} if the requested option is supported
297 	 */
298 	protected boolean wantCapability(StringBuilder b, String option) {
299 		if (!isCapableOf(option))
300 			return false;
301 		b.append(' ');
302 		b.append(option);
303 		return true;
304 	}
305 
306 	/**
307 	 * Add user agent capability
308 	 *
309 	 * @param b
310 	 *            a {@link java.lang.StringBuilder} object.
311 	 */
312 	protected void addUserAgentCapability(StringBuilder b) {
313 		String a = UserAgent.get();
314 		if (a != null && UserAgent.hasAgent(remoteCapablities)) {
315 			b.append(' ').append(OPTION_AGENT).append('=').append(a);
316 		}
317 	}
318 
319 	/** {@inheritDoc} */
320 	@Override
321 	public String getPeerUserAgent() {
322 		return UserAgent.getAgent(remoteCapablities, super.getPeerUserAgent());
323 	}
324 
325 	private PackProtocolException duplicateAdvertisement(String name) {
326 		return new PackProtocolException(uri, MessageFormat.format(JGitText.get().duplicateAdvertisementsOf, name));
327 	}
328 
329 	private PackProtocolException invalidRefAdvertisementLine(String line) {
330 		return new PackProtocolException(uri, MessageFormat.format(JGitText.get().invalidRefAdvertisementLine, line));
331 	}
332 
333 	/** {@inheritDoc} */
334 	@Override
335 	public void close() {
336 		if (out != null) {
337 			try {
338 				if (outNeedsEnd) {
339 					outNeedsEnd = false;
340 					pckOut.end();
341 				}
342 				out.close();
343 			} catch (IOException err) {
344 				// Ignore any close errors.
345 			} finally {
346 				out = null;
347 				pckOut = null;
348 			}
349 		}
350 
351 		if (in != null) {
352 			try {
353 				in.close();
354 			} catch (IOException err) {
355 				// Ignore any close errors.
356 			} finally {
357 				in = null;
358 				pckIn = null;
359 			}
360 		}
361 
362 		if (myTimer != null) {
363 			try {
364 				myTimer.terminate();
365 			} finally {
366 				myTimer = null;
367 				timeoutIn = null;
368 				timeoutOut = null;
369 			}
370 		}
371 	}
372 
373 	/**
374 	 * Tell the peer we are disconnecting, if it cares to know.
375 	 */
376 	protected void endOut() {
377 		if (outNeedsEnd && out != null) {
378 			try {
379 				outNeedsEnd = false;
380 				pckOut.end();
381 			} catch (IOException e) {
382 				try {
383 					out.close();
384 				} catch (IOException err) {
385 					// Ignore any close errors.
386 				} finally {
387 					out = null;
388 					pckOut = null;
389 				}
390 			}
391 		}
392 	}
393 }