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 InputStream in;
91
92 private final byte[] lineBuffer;
93
94 /**
95 * Create a new packet line reader.
96 *
97 * @param i
98 * the input stream to consume.
99 */
100 public PacketLineIn(final InputStream i) {
101 in = i;
102 lineBuffer = new byte[SideBandOutputStream.SMALL_BUF];
103 }
104
105 AckNackResult readACK(final MutableObjectId returnedId) throws IOException {
106 final String line = readString();
107 if (line.length() == 0)
108 throw new PackProtocolException(JGitText.get().expectedACKNAKFoundEOF);
109 if ("NAK".equals(line)) //$NON-NLS-1$
110 return AckNackResult.NAK;
111 if (line.startsWith("ACK ")) { //$NON-NLS-1$
112 returnedId.fromString(line.substring(4, 44));
113 if (line.length() == 44)
114 return AckNackResult.ACK;
115
116 final String arg = line.substring(44);
117 if (arg.equals(" continue")) //$NON-NLS-1$
118 return AckNackResult.ACK_CONTINUE;
119 else if (arg.equals(" common")) //$NON-NLS-1$
120 return AckNackResult.ACK_COMMON;
121 else if (arg.equals(" ready")) //$NON-NLS-1$
122 return AckNackResult.ACK_READY;
123 }
124 if (line.startsWith("ERR ")) //$NON-NLS-1$
125 throw new PackProtocolException(line.substring(4));
126 throw new PackProtocolException(MessageFormat.format(JGitText.get().expectedACKNAKGot, line));
127 }
128
129 /**
130 * Read a single UTF-8 encoded string packet from the input stream.
131 * <p>
132 * If the string ends with an LF, it will be removed before returning the
133 * value to the caller. If this automatic trimming behavior is not desired,
134 * use {@link #readStringRaw()} instead.
135 *
136 * @return the string. {@link #END} if the string was the magic flush
137 * packet.
138 * @throws IOException
139 * the stream cannot be read.
140 */
141 public String readString() throws IOException {
142 int len = readLength();
143 if (len == 0) {
144 log.debug("git< 0000"); //$NON-NLS-1$
145 return END;
146 }
147
148 len -= 4; // length header (4 bytes)
149 if (len == 0) {
150 log.debug("git< "); //$NON-NLS-1$
151 return ""; //$NON-NLS-1$
152 }
153
154 byte[] raw;
155 if (len <= lineBuffer.length)
156 raw = lineBuffer;
157 else
158 raw = new byte[len];
159
160 IO.readFully(in, raw, 0, len);
161 if (raw[len - 1] == '\n')
162 len--;
163
164 String s = RawParseUtils.decode(Constants.CHARSET, raw, 0, len);
165 log.debug("git< " + s); //$NON-NLS-1$
166 return s;
167 }
168
169 /**
170 * Read a single UTF-8 encoded string packet from the input stream.
171 * <p>
172 * Unlike {@link #readString()} a trailing LF will be retained.
173 *
174 * @return the string. {@link #END} if the string was the magic flush
175 * packet.
176 * @throws IOException
177 * the stream cannot be read.
178 */
179 public String readStringRaw() throws IOException {
180 int len = readLength();
181 if (len == 0) {
182 log.debug("git< 0000"); //$NON-NLS-1$
183 return END;
184 }
185
186 len -= 4; // length header (4 bytes)
187
188 byte[] raw;
189 if (len <= lineBuffer.length)
190 raw = lineBuffer;
191 else
192 raw = new byte[len];
193
194 IO.readFully(in, raw, 0, len);
195
196 String s = RawParseUtils.decode(Constants.CHARSET, raw, 0, len);
197 log.debug("git< " + s); //$NON-NLS-1$
198 return s;
199 }
200
201 void discardUntilEnd() throws IOException {
202 for (;;) {
203 int n = readLength();
204 if (n == 0) {
205 break;
206 }
207 IO.skipFully(in, n - 4);
208 }
209 }
210
211 int readLength() throws IOException {
212 IO.readFully(in, lineBuffer, 0, 4);
213 try {
214 final int len = RawParseUtils.parseHexInt16(lineBuffer, 0);
215 if (len != 0 && len < 4)
216 throw new ArrayIndexOutOfBoundsException();
217 return len;
218 } catch (ArrayIndexOutOfBoundsException err) {
219 throw new IOException(MessageFormat.format(JGitText.get().invalidPacketLineHeader,
220 "" + (char) lineBuffer[0] + (char) lineBuffer[1] //$NON-NLS-1$
221 + (char) lineBuffer[2] + (char) lineBuffer[3]));
222 }
223 }
224 }