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.2KB

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