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 	public static boolean isDelimiter(String s) {
263 		return s == DELIM;
264 	}
265 
266 	/**
267 	 * Get the delimiter marker.
268 	 * <p>
269 	 * Intended for use only in tests.
270 	 *
271 	 * @return The delimiter marker.
272 	 */
273 	static String delimiter() {
274 		return DELIM;
275 	}
276 
277 	/**
278 	 * Get the end marker.
279 	 * <p>
280 	 * Intended for use only in tests.
281 	 *
282 	 * @return The end marker.
283 	 */
284 	static String end() {
285 		return END;
286 	}
287 
288 	/**
289 	 * Check if a string is the packet end marker.
290 	 *
291 	 * @param s
292 	 *            the string to check
293 	 * @return true if the given string is {@link #END}, otherwise false.
294 	 * @since 5.4
295 	 */
296 	public static boolean isEnd(String s) {
297 		return s == END;
298 	}
299 
300 	void discardUntilEnd() throws IOException {
301 		for (;;) {
302 			int n = readLength();
303 			if (n == 0) {
304 				break;
305 			}
306 			IO.skipFully(in, n - 4);
307 		}
308 	}
309 
310 	int readLength() throws IOException {
311 		IO.readFully(in, lineBuffer, 0, 4);
312 		int len;
313 		try {
314 			len = RawParseUtils.parseHexInt16(lineBuffer, 0);
315 		} catch (ArrayIndexOutOfBoundsException err) {
316 			throw invalidHeader();
317 		}
318 
319 		if (len == 0) {
320 			return 0;
321 		} else if (len == 1) {
322 			return 1;
323 		} else if (len < 4) {
324 			throw invalidHeader();
325 		}
326 
327 		if (limit != 0) {
328 			int n = len - 4;
329 			if (limit < n) {
330 				limit = -1;
331 				try {
332 					IO.skipFully(in, n);
333 				} catch (IOException e) {
334 					// Ignore failure discarding packet over limit.
335 				}
336 				throw new InputOverLimitIOException();
337 			}
338 			// if set limit must not be 0 (means unlimited).
339 			limit = n < limit ? limit - n : -1;
340 		}
341 		return len;
342 	}
343 
344 	private IOException invalidHeader() {
345 		return new IOException(MessageFormat.format(JGitText.get().invalidPacketLineHeader,
346 				"" + (char) lineBuffer[0] + (char) lineBuffer[1] //$NON-NLS-1$
347 				+ (char) lineBuffer[2] + (char) lineBuffer[3]));
348 	}
349 
350 	/**
351 	 * IOException thrown by read when the configured input limit is exceeded.
352 	 *
353 	 * @since 4.7
354 	 */
355 	public static class InputOverLimitIOException extends IOException {
356 		private static final long serialVersionUID = 1L;
357 	}
358 
359 	/**
360 	 * Iterator over packet lines.
361 	 * <p>
362 	 * Calls {@link #readString()} on the {@link PacketLineIn} until
363 	 * {@link #END} is encountered.
364 	 *
365 	 * @since 5.4
366 	 *
367 	 */
368 	public static class PacketLineInIterator implements Iterable<String> {
369 		private PacketLineIn in;
370 
371 		private String current;
372 
373 		PacketLineInIterator(PacketLineIn in) throws IOException {
374 			this.in = in;
375 			current = in.readString();
376 		}
377 
378 		@Override
379 		public Iterator<String> iterator() {
380 			return new Iterator<String>() {
381 				@Override
382 				public boolean hasNext() {
383 					return !PacketLineIn.isEnd(current);
384 				}
385 
386 				@Override
387 				public String next() {
388 					String next = current;
389 					try {
390 						current = in.readString();
391 					} catch (IOException e) {
392 						throw new UncheckedIOException(e);
393 					}
394 					return next;
395 				}
396 			};
397 		}
398 
399 	}
400 }