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 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 java.io.IOException;
  47. import java.io.InputStream;
  48. import java.text.MessageFormat;
  49. import org.eclipse.jgit.errors.PackProtocolException;
  50. import org.eclipse.jgit.internal.JGitText;
  51. import org.eclipse.jgit.lib.Constants;
  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. static enum AckNackResult {
  72. /** NAK */
  73. NAK,
  74. /** ACK */
  75. ACK,
  76. /** ACK + continue */
  77. ACK_CONTINUE,
  78. /** ACK + common */
  79. ACK_COMMON,
  80. /** ACK + ready */
  81. ACK_READY;
  82. }
  83. private final byte[] lineBuffer = new byte[SideBandOutputStream.SMALL_BUF];
  84. private final InputStream in;
  85. private long limit;
  86. /**
  87. * Create a new packet line reader.
  88. *
  89. * @param in
  90. * the input stream to consume.
  91. */
  92. public PacketLineIn(InputStream in) {
  93. this(in, 0);
  94. }
  95. /**
  96. * Create a new packet line reader.
  97. *
  98. * @param in
  99. * the input stream to consume.
  100. * @param limit
  101. * bytes to read from the input; unlimited if set to 0.
  102. * @since 4.7
  103. */
  104. public PacketLineIn(InputStream in, long limit) {
  105. this.in = in;
  106. this.limit = limit;
  107. }
  108. AckNackResult readACK(final MutableObjectId returnedId) throws IOException {
  109. final String line = readString();
  110. if (line.length() == 0)
  111. throw new PackProtocolException(JGitText.get().expectedACKNAKFoundEOF);
  112. if ("NAK".equals(line)) //$NON-NLS-1$
  113. return AckNackResult.NAK;
  114. if (line.startsWith("ACK ")) { //$NON-NLS-1$
  115. returnedId.fromString(line.substring(4, 44));
  116. if (line.length() == 44)
  117. return AckNackResult.ACK;
  118. final String arg = line.substring(44);
  119. if (arg.equals(" continue")) //$NON-NLS-1$
  120. return AckNackResult.ACK_CONTINUE;
  121. else if (arg.equals(" common")) //$NON-NLS-1$
  122. return AckNackResult.ACK_COMMON;
  123. else if (arg.equals(" ready")) //$NON-NLS-1$
  124. return AckNackResult.ACK_READY;
  125. }
  126. if (line.startsWith("ERR ")) //$NON-NLS-1$
  127. throw new PackProtocolException(line.substring(4));
  128. throw new PackProtocolException(MessageFormat.format(JGitText.get().expectedACKNAKGot, line));
  129. }
  130. /**
  131. * Read a single UTF-8 encoded string packet from the input stream.
  132. * <p>
  133. * If the string ends with an LF, it will be removed before returning the
  134. * value to the caller. If this automatic trimming behavior is not desired,
  135. * use {@link #readStringRaw()} instead.
  136. *
  137. * @return the string. {@link #END} if the string was the magic flush
  138. * packet.
  139. * @throws java.io.IOException
  140. * the stream cannot be read.
  141. */
  142. public String readString() throws IOException {
  143. int len = readLength();
  144. if (len == 0) {
  145. log.debug("git< 0000"); //$NON-NLS-1$
  146. return END;
  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. byte[] raw;
  154. if (len <= lineBuffer.length)
  155. raw = lineBuffer;
  156. else
  157. raw = new byte[len];
  158. IO.readFully(in, raw, 0, len);
  159. if (raw[len - 1] == '\n')
  160. len--;
  161. String s = RawParseUtils.decode(Constants.CHARSET, raw, 0, len);
  162. log.debug("git< " + s); //$NON-NLS-1$
  163. return s;
  164. }
  165. /**
  166. * Read a single UTF-8 encoded string packet from the input stream.
  167. * <p>
  168. * Unlike {@link #readString()} a trailing LF will be retained.
  169. *
  170. * @return the string. {@link #END} if the string was the magic flush
  171. * packet.
  172. * @throws java.io.IOException
  173. * the stream cannot be read.
  174. */
  175. public String readStringRaw() throws IOException {
  176. int len = readLength();
  177. if (len == 0) {
  178. log.debug("git< 0000"); //$NON-NLS-1$
  179. return END;
  180. }
  181. len -= 4; // length header (4 bytes)
  182. byte[] raw;
  183. if (len <= lineBuffer.length)
  184. raw = lineBuffer;
  185. else
  186. raw = new byte[len];
  187. IO.readFully(in, raw, 0, len);
  188. String s = RawParseUtils.decode(Constants.CHARSET, raw, 0, len);
  189. log.debug("git< " + s); //$NON-NLS-1$
  190. return s;
  191. }
  192. void discardUntilEnd() throws IOException {
  193. for (;;) {
  194. int n = readLength();
  195. if (n == 0) {
  196. break;
  197. }
  198. IO.skipFully(in, n - 4);
  199. }
  200. }
  201. int readLength() throws IOException {
  202. IO.readFully(in, lineBuffer, 0, 4);
  203. int len;
  204. try {
  205. len = RawParseUtils.parseHexInt16(lineBuffer, 0);
  206. } catch (ArrayIndexOutOfBoundsException err) {
  207. throw invalidHeader();
  208. }
  209. if (len == 0) {
  210. return 0;
  211. } else if (len < 4) {
  212. throw invalidHeader();
  213. }
  214. if (limit != 0) {
  215. int n = len - 4;
  216. if (limit < n) {
  217. limit = -1;
  218. try {
  219. IO.skipFully(in, n);
  220. } catch (IOException e) {
  221. // Ignore failure discarding packet over limit.
  222. }
  223. throw new InputOverLimitIOException();
  224. }
  225. // if set limit must not be 0 (means unlimited).
  226. limit = n < limit ? limit - n : -1;
  227. }
  228. return len;
  229. }
  230. private IOException invalidHeader() {
  231. return new IOException(MessageFormat.format(JGitText.get().invalidPacketLineHeader,
  232. "" + (char) lineBuffer[0] + (char) lineBuffer[1] //$NON-NLS-1$
  233. + (char) lineBuffer[2] + (char) lineBuffer[3]));
  234. }
  235. /**
  236. * IOException thrown by read when the configured input limit is exceeded.
  237. *
  238. * @since 4.7
  239. */
  240. public static class InputOverLimitIOException extends IOException {
  241. private static final long serialVersionUID = 1L;
  242. }
  243. }