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