You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PacketLineIn.java 8.2KB

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