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 java.io.IOException;
49  import java.io.InputStream;
50  import java.text.MessageFormat;
51  
52  import org.eclipse.jgit.errors.PackProtocolException;
53  import org.eclipse.jgit.internal.JGitText;
54  import org.eclipse.jgit.lib.Constants;
55  import org.eclipse.jgit.lib.MutableObjectId;
56  import org.eclipse.jgit.util.IO;
57  import org.eclipse.jgit.util.RawParseUtils;
58  import org.slf4j.Logger;
59  import org.slf4j.LoggerFactory;
60  
61  /**
62   * Read Git style pkt-line formatting from an input stream.
63   * <p>
64   * This class is not thread safe and may issue multiple reads to the underlying
65   * stream for each method call made.
66   * <p>
67   * This class performs no buffering on its own. This makes it suitable to
68   * interleave reads performed by this class with reads performed directly
69   * against the underlying InputStream.
70   */
71  public class PacketLineIn {
72  	private static final Logger log = LoggerFactory.getLogger(PacketLineIn.class);
73  
74  	/** Magic return from {@link #readString()} when a flush packet is found. */
75  	public static final String END = new StringBuilder(0).toString(); 	/* must not string pool */
76  
77  	static enum AckNackResult {
78  		/** NAK */
79  		NAK,
80  		/** ACK */
81  		ACK,
82  		/** ACK + continue */
83  		ACK_CONTINUE,
84  		/** ACK + common */
85  		ACK_COMMON,
86  		/** ACK + ready */
87  		ACK_READY;
88  	}
89  
90  	private final byte[] lineBuffer = new byte[SideBandOutputStream.SMALL_BUF];
91  	private final InputStream in;
92  	private long limit;
93  
94  	/**
95  	 * Create a new packet line reader.
96  	 *
97  	 * @param in
98  	 *            the input stream to consume.
99  	 */
100 	public PacketLineIn(InputStream in) {
101 		this(in, 0);
102 	}
103 
104 	/**
105 	 * Create a new packet line reader.
106 	 *
107 	 * @param in
108 	 *            the input stream to consume.
109 	 * @param limit
110 	 *            bytes to read from the input; unlimited if set to 0.
111 	 * @since 4.7
112 	 */
113 	public PacketLineIn(InputStream in, long limit) {
114 		this.in = in;
115 		this.limit = limit;
116 	}
117 
118 	AckNackResult readACK(final MutableObjectId returnedId) throws IOException {
119 		final String line = readString();
120 		if (line.length() == 0)
121 			throw new PackProtocolException(JGitText.get().expectedACKNAKFoundEOF);
122 		if ("NAK".equals(line)) //$NON-NLS-1$
123 			return AckNackResult.NAK;
124 		if (line.startsWith("ACK ")) { //$NON-NLS-1$
125 			returnedId.fromString(line.substring(4, 44));
126 			if (line.length() == 44)
127 				return AckNackResult.ACK;
128 
129 			final String arg = line.substring(44);
130 			if (arg.equals(" continue")) //$NON-NLS-1$
131 				return AckNackResult.ACK_CONTINUE;
132 			else if (arg.equals(" common")) //$NON-NLS-1$
133 				return AckNackResult.ACK_COMMON;
134 			else if (arg.equals(" ready")) //$NON-NLS-1$
135 				return AckNackResult.ACK_READY;
136 		}
137 		if (line.startsWith("ERR ")) //$NON-NLS-1$
138 			throw new PackProtocolException(line.substring(4));
139 		throw new PackProtocolException(MessageFormat.format(JGitText.get().expectedACKNAKGot, line));
140 	}
141 
142 	/**
143 	 * Read a single UTF-8 encoded string packet from the input stream.
144 	 * <p>
145 	 * If the string ends with an LF, it will be removed before returning the
146 	 * value to the caller. If this automatic trimming behavior is not desired,
147 	 * use {@link #readStringRaw()} instead.
148 	 *
149 	 * @return the string. {@link #END} if the string was the magic flush
150 	 *         packet.
151 	 * @throws IOException
152 	 *             the stream cannot be read.
153 	 */
154 	public String readString() throws IOException {
155 		int len = readLength();
156 		if (len == 0) {
157 			log.debug("git< 0000"); //$NON-NLS-1$
158 			return END;
159 		}
160 
161 		len -= 4; // length header (4 bytes)
162 		if (len == 0) {
163 			log.debug("git< "); //$NON-NLS-1$
164 			return ""; //$NON-NLS-1$
165 		}
166 
167 		byte[] raw;
168 		if (len <= lineBuffer.length)
169 			raw = lineBuffer;
170 		else
171 			raw = new byte[len];
172 
173 		IO.readFully(in, raw, 0, len);
174 		if (raw[len - 1] == '\n')
175 			len--;
176 
177 		String s = RawParseUtils.decode(Constants.CHARSET, raw, 0, len);
178 		log.debug("git< " + s); //$NON-NLS-1$
179 		return s;
180 	}
181 
182 	/**
183 	 * Read a single UTF-8 encoded string packet from the input stream.
184 	 * <p>
185 	 * Unlike {@link #readString()} a trailing LF will be retained.
186 	 *
187 	 * @return the string. {@link #END} if the string was the magic flush
188 	 *         packet.
189 	 * @throws IOException
190 	 *             the stream cannot be read.
191 	 */
192 	public String readStringRaw() throws IOException {
193 		int len = readLength();
194 		if (len == 0) {
195 			log.debug("git< 0000"); //$NON-NLS-1$
196 			return END;
197 		}
198 
199 		len -= 4; // length header (4 bytes)
200 
201 		byte[] raw;
202 		if (len <= lineBuffer.length)
203 			raw = lineBuffer;
204 		else
205 			raw = new byte[len];
206 
207 		IO.readFully(in, raw, 0, len);
208 
209 		String s = RawParseUtils.decode(Constants.CHARSET, raw, 0, len);
210 		log.debug("git< " + s); //$NON-NLS-1$
211 		return s;
212 	}
213 
214 	void discardUntilEnd() throws IOException {
215 		for (;;) {
216 			int n = readLength();
217 			if (n == 0) {
218 				break;
219 			}
220 			IO.skipFully(in, n - 4);
221 		}
222 	}
223 
224 	int readLength() throws IOException {
225 		IO.readFully(in, lineBuffer, 0, 4);
226 		int len;
227 		try {
228 			len = RawParseUtils.parseHexInt16(lineBuffer, 0);
229 		} catch (ArrayIndexOutOfBoundsException err) {
230 			throw invalidHeader();
231 		}
232 
233 		if (len == 0) {
234 			return 0;
235 		} else if (len < 4) {
236 			throw invalidHeader();
237 		}
238 
239 		if (limit != 0) {
240 			int n = len - 4;
241 			if (limit < n) {
242 				limit = -1;
243 				try {
244 					IO.skipFully(in, n);
245 				} catch (IOException e) {
246 					// Ignore failure discarding packet over limit.
247 				}
248 				throw new InputOverLimitIOException();
249 			}
250 			// if set limit must not be 0 (means unlimited).
251 			limit = n < limit ? limit - n : -1;
252 		}
253 		return len;
254 	}
255 
256 	private IOException invalidHeader() {
257 		return new IOException(MessageFormat.format(JGitText.get().invalidPacketLineHeader,
258 				"" + (char) lineBuffer[0] + (char) lineBuffer[1] //$NON-NLS-1$
259 				+ (char) lineBuffer[2] + (char) lineBuffer[3]));
260 	}
261 
262 	/**
263 	 * IOException thrown by read when the configured input limit is exceeded.
264 	 *
265 	 * @since 4.7
266 	 */
267 	public static class InputOverLimitIOException extends IOException {
268 		private static final long serialVersionUID = 1L;
269 	}
270 }