View Javadoc
1   /*
2    * Copyright (C) 2008-2010, Google Inc.
3    * Copyright (C) 2008-2009, 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 static java.nio.charset.StandardCharsets.UTF_8;
49  
50  import java.io.IOException;
51  import java.io.InputStream;
52  import java.io.UncheckedIOException;
53  import java.text.MessageFormat;
54  import java.util.Iterator;
55  
56  import org.eclipse.jgit.errors.PackProtocolException;
57  import org.eclipse.jgit.internal.JGitText;
58  import org.eclipse.jgit.lib.MutableObjectId;
59  import org.eclipse.jgit.util.IO;
60  import org.eclipse.jgit.util.RawParseUtils;
61  import org.slf4j.Logger;
62  import org.slf4j.LoggerFactory;
63  
64  /**
65   * Read Git style pkt-line formatting from an input stream.
66   * <p>
67   * This class is not thread safe and may issue multiple reads to the underlying
68   * stream for each method call made.
69   * <p>
70   * This class performs no buffering on its own. This makes it suitable to
71   * interleave reads performed by this class with reads performed directly
72   * against the underlying InputStream.
73   */
74  public class PacketLineIn {
75  	private static final Logger log = LoggerFactory.getLogger(PacketLineIn.class);
76  
77  	/**
78  	 * Magic return from {@link #readString()} when a flush packet is found.
79  	 *
80  	 * @deprecated Callers should use {@link #isEnd(String)} to check if a
81  	 *             string is the end marker, or
82  	 *             {@link PacketLineIn#readStrings()} to iterate over all
83  	 *             strings in the input stream until the marker is reached.
84  	 */
85  	@Deprecated
86  	public static final String END = new StringBuilder(0).toString(); 	/* must not string pool */
87  
88  	/**
89  	 * Magic return from {@link #readString()} when a delim packet is found.
90  	 *
91  	 * @since 5.0
92  	 * @deprecated Callers should use {@link #isDelimiter(String)} to check if a
93  	 *             string is the delimiter.
94  	 */
95  	@Deprecated
96  	public static final String DELIM = new StringBuilder(0).toString(); 	/* must not string pool */
97  
98  	static enum AckNackResult {
99  		/** NAK */
100 		NAK,
101 		/** ACK */
102 		ACK,
103 		/** ACK + continue */
104 		ACK_CONTINUE,
105 		/** ACK + common */
106 		ACK_COMMON,
107 		/** ACK + ready */
108 		ACK_READY;
109 	}
110 
111 	private final byte[] lineBuffer = new byte[SideBandOutputStream.SMALL_BUF];
112 	private final InputStream in;
113 	private long limit;
114 
115 	/**
116 	 * Create a new packet line reader.
117 	 *
118 	 * @param in
119 	 *            the input stream to consume.
120 	 */
121 	public PacketLineIn(InputStream in) {
122 		this(in, 0);
123 	}
124 
125 	/**
126 	 * Create a new packet line reader.
127 	 *
128 	 * @param in
129 	 *            the input stream to consume.
130 	 * @param limit
131 	 *            bytes to read from the input; unlimited if set to 0.
132 	 * @since 4.7
133 	 */
134 	public PacketLineIn(InputStream in, long limit) {
135 		this.in = in;
136 		this.limit = limit;
137 	}
138 
139 	AckNackResult readACK(MutableObjectId returnedId) throws IOException {
140 		final String line = readString();
141 		if (line.length() == 0)
142 			throw new PackProtocolException(JGitText.get().expectedACKNAKFoundEOF);
143 		if ("NAK".equals(line)) //$NON-NLS-1$
144 			return AckNackResult.NAK;
145 		if (line.startsWith("ACK ")) { //$NON-NLS-1$
146 			returnedId.fromString(line.substring(4, 44));
147 			if (line.length() == 44)
148 				return AckNackResult.ACK;
149 
150 			final String arg = line.substring(44);
151 			if (arg.equals(" continue")) //$NON-NLS-1$
152 				return AckNackResult.ACK_CONTINUE;
153 			else if (arg.equals(" common")) //$NON-NLS-1$
154 				return AckNackResult.ACK_COMMON;
155 			else if (arg.equals(" ready")) //$NON-NLS-1$
156 				return AckNackResult.ACK_READY;
157 		}
158 		if (line.startsWith("ERR ")) //$NON-NLS-1$
159 			throw new PackProtocolException(line.substring(4));
160 		throw new PackProtocolException(MessageFormat.format(JGitText.get().expectedACKNAKGot, line));
161 	}
162 
163 	/**
164 	 * Read a single UTF-8 encoded string packet from the input stream.
165 	 * <p>
166 	 * If the string ends with an LF, it will be removed before returning the
167 	 * value to the caller. If this automatic trimming behavior is not desired,
168 	 * use {@link #readStringRaw()} instead.
169 	 *
170 	 * @return the string. {@link #END} if the string was the magic flush
171 	 *         packet, {@link #DELIM} if the string was the magic DELIM
172 	 *         packet.
173 	 * @throws java.io.IOException
174 	 *             the stream cannot be read.
175 	 */
176 	public String readString() throws IOException {
177 		int len = readLength();
178 		if (len == 0) {
179 			log.debug("git< 0000"); //$NON-NLS-1$
180 			return END;
181 		}
182 		if (len == 1) {
183 			log.debug("git< 0001"); //$NON-NLS-1$
184 			return DELIM;
185 		}
186 
187 		len -= 4; // length header (4 bytes)
188 		if (len == 0) {
189 			log.debug("git< "); //$NON-NLS-1$
190 			return ""; //$NON-NLS-1$
191 		}
192 
193 		byte[] raw;
194 		if (len <= lineBuffer.length)
195 			raw = lineBuffer;
196 		else
197 			raw = new byte[len];
198 
199 		IO.readFully(in, raw, 0, len);
200 		if (raw[len - 1] == '\n')
201 			len--;
202 
203 		String s = RawParseUtils.decode(UTF_8, raw, 0, len);
204 		log.debug("git< " + s); //$NON-NLS-1$
205 		return s;
206 	}
207 
208 	/**
209 	 * Get an iterator to read strings from the input stream.
210 	 *
211 	 * @return an iterator that calls {@link #readString()} until {@link #END}
212 	 *         is encountered.
213 	 *
214 	 * @throws IOException
215 	 *             on failure to read the initial packet line.
216 	 * @since 5.4
217 	 */
218 	public PacketLineInIterator readStrings() throws IOException {
219 		return new PacketLineInIterator(this);
220 	}
221 
222 	/**
223 	 * Read a single UTF-8 encoded string packet from the input stream.
224 	 * <p>
225 	 * Unlike {@link #readString()} a trailing LF will be retained.
226 	 *
227 	 * @return the string. {@link #END} if the string was the magic flush
228 	 *         packet.
229 	 * @throws java.io.IOException
230 	 *             the stream cannot be read.
231 	 */
232 	public String readStringRaw() throws IOException {
233 		int len = readLength();
234 		if (len == 0) {
235 			log.debug("git< 0000"); //$NON-NLS-1$
236 			return END;
237 		}
238 
239 		len -= 4; // length header (4 bytes)
240 
241 		byte[] raw;
242 		if (len <= lineBuffer.length)
243 			raw = lineBuffer;
244 		else
245 			raw = new byte[len];
246 
247 		IO.readFully(in, raw, 0, len);
248 
249 		String s = RawParseUtils.decode(UTF_8, raw, 0, len);
250 		log.debug("git< " + s); //$NON-NLS-1$
251 		return s;
252 	}
253 
254 	/**
255 	 * Check if a string is the delimiter marker.
256 	 *
257 	 * @param s
258 	 *            the string to check
259 	 * @return true if the given string is {@link #DELIM}, otherwise false.
260 	 * @since 5.4
261 	 */
262 	@SuppressWarnings({ "ReferenceEquality", "StringEquality" })
263 	public static boolean isDelimiter(String s) {
264 		return s == DELIM;
265 	}
266 
267 	/**
268 	 * Get the delimiter marker.
269 	 * <p>
270 	 * Intended for use only in tests.
271 	 *
272 	 * @return The delimiter marker.
273 	 */
274 	static String delimiter() {
275 		return DELIM;
276 	}
277 
278 	/**
279 	 * Get the end marker.
280 	 * <p>
281 	 * Intended for use only in tests.
282 	 *
283 	 * @return The end marker.
284 	 */
285 	static String end() {
286 		return END;
287 	}
288 
289 	/**
290 	 * Check if a string is the packet end marker.
291 	 *
292 	 * @param s
293 	 *            the string to check
294 	 * @return true if the given string is {@link #END}, otherwise false.
295 	 * @since 5.4
296 	 */
297 	@SuppressWarnings({ "ReferenceEquality", "StringEquality" })
298 	public static boolean isEnd(String s) {
299 		return s == END;
300 	}
301 
302 	void discardUntilEnd() throws IOException {
303 		for (;;) {
304 			int n = readLength();
305 			if (n == 0) {
306 				break;
307 			}
308 			IO.skipFully(in, n - 4);
309 		}
310 	}
311 
312 	int readLength() throws IOException {
313 		IO.readFully(in, lineBuffer, 0, 4);
314 		int len;
315 		try {
316 			len = RawParseUtils.parseHexInt16(lineBuffer, 0);
317 		} catch (ArrayIndexOutOfBoundsException err) {
318 			throw invalidHeader();
319 		}
320 
321 		if (len == 0) {
322 			return 0;
323 		} else if (len == 1) {
324 			return 1;
325 		} else if (len < 4) {
326 			throw invalidHeader();
327 		}
328 
329 		if (limit != 0) {
330 			int n = len - 4;
331 			if (limit < n) {
332 				limit = -1;
333 				try {
334 					IO.skipFully(in, n);
335 				} catch (IOException e) {
336 					// Ignore failure discarding packet over limit.
337 				}
338 				throw new InputOverLimitIOException();
339 			}
340 			// if set limit must not be 0 (means unlimited).
341 			limit = n < limit ? limit - n : -1;
342 		}
343 		return len;
344 	}
345 
346 	private IOException invalidHeader() {
347 		return new IOException(MessageFormat.format(JGitText.get().invalidPacketLineHeader,
348 				"" + (char) lineBuffer[0] + (char) lineBuffer[1] //$NON-NLS-1$
349 				+ (char) lineBuffer[2] + (char) lineBuffer[3]));
350 	}
351 
352 	/**
353 	 * IOException thrown by read when the configured input limit is exceeded.
354 	 *
355 	 * @since 4.7
356 	 */
357 	public static class InputOverLimitIOException extends IOException {
358 		private static final long serialVersionUID = 1L;
359 	}
360 
361 	/**
362 	 * Iterator over packet lines.
363 	 * <p>
364 	 * Calls {@link #readString()} on the {@link PacketLineIn} until
365 	 * {@link #END} is encountered.
366 	 *
367 	 * @since 5.4
368 	 *
369 	 */
370 	public static class PacketLineInIterator implements Iterable<String> {
371 		private PacketLineIn in;
372 
373 		private String current;
374 
375 		PacketLineInIterator(PacketLineIn in) throws IOException {
376 			this.in = in;
377 			current = in.readString();
378 		}
379 
380 		@Override
381 		public Iterator<String> iterator() {
382 			return new Iterator<String>() {
383 				@Override
384 				public boolean hasNext() {
385 					return !PacketLineIn.isEnd(current);
386 				}
387 
388 				@Override
389 				public String next() {
390 					String next = current;
391 					try {
392 						current = in.readString();
393 					} catch (IOException e) {
394 						throw new UncheckedIOException(e);
395 					}
396 					return next;
397 				}
398 			};
399 		}
400 
401 	}
402 }