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.

ProtocolV2Parser.java 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright (C) 2018, Google LLC. and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.transport;
  11. import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_DEEPEN_RELATIVE;
  12. import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_FILTER;
  13. import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_AGENT;
  14. import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_INCLUDE_TAG;
  15. import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_NO_PROGRESS;
  16. import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_OFS_DELTA;
  17. import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_SERVER_OPTION;
  18. import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_SIDEBAND_ALL;
  19. import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_SIDE_BAND_64K;
  20. import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_THIN_PACK;
  21. import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_WANT_REF;
  22. import java.io.IOException;
  23. import java.text.MessageFormat;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import java.util.function.Consumer;
  27. import org.eclipse.jgit.errors.PackProtocolException;
  28. import org.eclipse.jgit.internal.JGitText;
  29. import org.eclipse.jgit.lib.ObjectId;
  30. /**
  31. * Parse the incoming git protocol lines from the wire and translate them into a
  32. * Request object.
  33. *
  34. * It requires a transferConfig object to know what the server supports (e.g.
  35. * ref-in-want and/or filters).
  36. */
  37. final class ProtocolV2Parser {
  38. private final TransferConfig transferConfig;
  39. ProtocolV2Parser(TransferConfig transferConfig) {
  40. this.transferConfig = transferConfig;
  41. }
  42. /*
  43. * Read lines until DELIM or END, calling the appropiate consumer.
  44. *
  45. * Returns the last read line (so caller can check if there is more to read
  46. * in the line).
  47. */
  48. private static String consumeCapabilities(PacketLineIn pckIn,
  49. Consumer<String> serverOptionConsumer,
  50. Consumer<String> agentConsumer) throws IOException {
  51. String serverOptionPrefix = OPTION_SERVER_OPTION + '=';
  52. String agentPrefix = OPTION_AGENT + '=';
  53. String line = pckIn.readString();
  54. while (!PacketLineIn.isDelimiter(line) && !PacketLineIn.isEnd(line)) {
  55. if (line.startsWith(serverOptionPrefix)) {
  56. serverOptionConsumer
  57. .accept(line.substring(serverOptionPrefix.length()));
  58. } else if (line.startsWith(agentPrefix)) {
  59. agentConsumer.accept(line.substring(agentPrefix.length()));
  60. } else {
  61. // Unrecognized capability. Ignore it.
  62. }
  63. line = pckIn.readString();
  64. }
  65. return line;
  66. }
  67. /**
  68. * Parse the incoming fetch request arguments from the wire. The caller must
  69. * be sure that what is comings is a fetch request before coming here.
  70. *
  71. * @param pckIn
  72. * incoming lines
  73. * @return A FetchV2Request populated with information received from the
  74. * wire.
  75. * @throws PackProtocolException
  76. * incompatible options, wrong type of arguments or other issues
  77. * where the request breaks the protocol.
  78. * @throws IOException
  79. * an IO error prevented reading the incoming message.
  80. */
  81. FetchV2Request parseFetchRequest(PacketLineIn pckIn)
  82. throws PackProtocolException, IOException {
  83. FetchV2Request.Builder reqBuilder = FetchV2Request.builder();
  84. // Packs are always sent multiplexed and using full 64K
  85. // lengths.
  86. reqBuilder.addClientCapability(OPTION_SIDE_BAND_64K);
  87. String line = consumeCapabilities(pckIn,
  88. serverOption -> reqBuilder.addServerOption(serverOption),
  89. agent -> reqBuilder.setAgent(agent));
  90. if (PacketLineIn.isEnd(line)) {
  91. return reqBuilder.build();
  92. }
  93. if (!PacketLineIn.isDelimiter(line)) {
  94. throw new PackProtocolException(
  95. MessageFormat.format(JGitText.get().unexpectedPacketLine,
  96. line));
  97. }
  98. boolean filterReceived = false;
  99. for (String line2 : pckIn.readStrings()) {
  100. if (line2.startsWith("want ")) { //$NON-NLS-1$
  101. reqBuilder.addWantId(ObjectId.fromString(line2.substring(5)));
  102. } else if (transferConfig.isAllowRefInWant()
  103. && line2.startsWith(OPTION_WANT_REF + " ")) { //$NON-NLS-1$
  104. reqBuilder.addWantedRef(
  105. line2.substring(OPTION_WANT_REF.length() + 1));
  106. } else if (line2.startsWith("have ")) { //$NON-NLS-1$
  107. reqBuilder.addPeerHas(ObjectId.fromString(line2.substring(5)));
  108. } else if (line2.equals("done")) { //$NON-NLS-1$
  109. reqBuilder.setDoneReceived();
  110. } else if (line2.equals(OPTION_THIN_PACK)) {
  111. reqBuilder.addClientCapability(OPTION_THIN_PACK);
  112. } else if (line2.equals(OPTION_NO_PROGRESS)) {
  113. reqBuilder.addClientCapability(OPTION_NO_PROGRESS);
  114. } else if (line2.equals(OPTION_INCLUDE_TAG)) {
  115. reqBuilder.addClientCapability(OPTION_INCLUDE_TAG);
  116. } else if (line2.equals(OPTION_OFS_DELTA)) {
  117. reqBuilder.addClientCapability(OPTION_OFS_DELTA);
  118. } else if (line2.startsWith("shallow ")) { //$NON-NLS-1$
  119. reqBuilder.addClientShallowCommit(
  120. ObjectId.fromString(line2.substring(8)));
  121. } else if (line2.startsWith("deepen ")) { //$NON-NLS-1$
  122. int parsedDepth = Integer.parseInt(line2.substring(7));
  123. if (parsedDepth <= 0) {
  124. throw new PackProtocolException(
  125. MessageFormat.format(JGitText.get().invalidDepth,
  126. Integer.valueOf(parsedDepth)));
  127. }
  128. if (reqBuilder.getDeepenSince() != 0) {
  129. throw new PackProtocolException(
  130. JGitText.get().deepenSinceWithDeepen);
  131. }
  132. if (reqBuilder.hasDeepenNotRefs()) {
  133. throw new PackProtocolException(
  134. JGitText.get().deepenNotWithDeepen);
  135. }
  136. reqBuilder.setDepth(parsedDepth);
  137. } else if (line2.startsWith("deepen-not ")) { //$NON-NLS-1$
  138. reqBuilder.addDeepenNotRef(line2.substring(11));
  139. if (reqBuilder.getDepth() != 0) {
  140. throw new PackProtocolException(
  141. JGitText.get().deepenNotWithDeepen);
  142. }
  143. } else if (line2.equals(OPTION_DEEPEN_RELATIVE)) {
  144. reqBuilder.addClientCapability(OPTION_DEEPEN_RELATIVE);
  145. } else if (line2.startsWith("deepen-since ")) { //$NON-NLS-1$
  146. int ts = Integer.parseInt(line2.substring(13));
  147. if (ts <= 0) {
  148. throw new PackProtocolException(MessageFormat
  149. .format(JGitText.get().invalidTimestamp, line2));
  150. }
  151. if (reqBuilder.getDepth() != 0) {
  152. throw new PackProtocolException(
  153. JGitText.get().deepenSinceWithDeepen);
  154. }
  155. reqBuilder.setDeepenSince(ts);
  156. } else if (transferConfig.isAllowFilter()
  157. && line2.startsWith(OPTION_FILTER + ' ')) {
  158. if (filterReceived) {
  159. throw new PackProtocolException(
  160. JGitText.get().tooManyFilters);
  161. }
  162. filterReceived = true;
  163. reqBuilder.setFilterSpec(FilterSpec.fromFilterLine(
  164. line2.substring(OPTION_FILTER.length() + 1)));
  165. } else if (transferConfig.isAllowSidebandAll()
  166. && line2.equals(OPTION_SIDEBAND_ALL)) {
  167. reqBuilder.setSidebandAll(true);
  168. } else if (line2.startsWith("packfile-uris ")) { //$NON-NLS-1$
  169. for (String s : line2.substring(14).split(",")) { //$NON-NLS-1$
  170. reqBuilder.addPackfileUriProtocol(s);
  171. }
  172. } else {
  173. throw new PackProtocolException(MessageFormat
  174. .format(JGitText.get().unexpectedPacketLine, line2));
  175. }
  176. }
  177. return reqBuilder.build();
  178. }
  179. /**
  180. * Parse the incoming ls-refs request arguments from the wire. This is meant
  181. * for calling immediately after the caller has consumed a "command=ls-refs"
  182. * line indicating the beginning of a ls-refs request.
  183. *
  184. * The incoming PacketLineIn is consumed until an END line, but the caller
  185. * is responsible for closing it (if needed)
  186. *
  187. * @param pckIn
  188. * incoming lines. This method will read until an END line.
  189. * @return a LsRefsV2Request object with the data received in the wire.
  190. * @throws PackProtocolException
  191. * for inconsistencies in the protocol (e.g. unexpected lines)
  192. * @throws IOException
  193. * reporting problems reading the incoming messages from the
  194. * wire
  195. */
  196. LsRefsV2Request parseLsRefsRequest(PacketLineIn pckIn)
  197. throws PackProtocolException, IOException {
  198. LsRefsV2Request.Builder builder = LsRefsV2Request.builder();
  199. List<String> prefixes = new ArrayList<>();
  200. String line = consumeCapabilities(pckIn,
  201. serverOption -> builder.addServerOption(serverOption),
  202. agent -> builder.setAgent(agent));
  203. if (PacketLineIn.isEnd(line)) {
  204. return builder.build();
  205. }
  206. if (!PacketLineIn.isDelimiter(line)) {
  207. throw new PackProtocolException(MessageFormat
  208. .format(JGitText.get().unexpectedPacketLine, line));
  209. }
  210. for (String line2 : pckIn.readStrings()) {
  211. if (line2.equals("peel")) { //$NON-NLS-1$
  212. builder.setPeel(true);
  213. } else if (line2.equals("symrefs")) { //$NON-NLS-1$
  214. builder.setSymrefs(true);
  215. } else if (line2.startsWith("ref-prefix ")) { //$NON-NLS-1$
  216. prefixes.add(line2.substring("ref-prefix ".length())); //$NON-NLS-1$
  217. } else {
  218. throw new PackProtocolException(MessageFormat
  219. .format(JGitText.get().unexpectedPacketLine, line2));
  220. }
  221. }
  222. return builder.setRefPrefixes(prefixes).build();
  223. }
  224. }