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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * Copyright (C) 2018, Google LLC.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.transport;
  44. import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_DEEPEN_RELATIVE;
  45. import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_FILTER;
  46. import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_AGENT;
  47. import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_INCLUDE_TAG;
  48. import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_NO_PROGRESS;
  49. import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_OFS_DELTA;
  50. import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_SERVER_OPTION;
  51. import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_SIDE_BAND_64K;
  52. import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_THIN_PACK;
  53. import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_WANT_REF;
  54. import java.io.IOException;
  55. import java.text.MessageFormat;
  56. import java.util.ArrayList;
  57. import java.util.List;
  58. import java.util.function.Consumer;
  59. import org.eclipse.jgit.errors.PackProtocolException;
  60. import org.eclipse.jgit.internal.JGitText;
  61. import org.eclipse.jgit.lib.ObjectId;
  62. /**
  63. * Parse the incoming git protocol lines from the wire and translate them into a
  64. * Request object.
  65. *
  66. * It requires a transferConfig object to know what the server supports (e.g.
  67. * ref-in-want and/or filters).
  68. */
  69. final class ProtocolV2Parser {
  70. private final TransferConfig transferConfig;
  71. ProtocolV2Parser(TransferConfig transferConfig) {
  72. this.transferConfig = transferConfig;
  73. }
  74. /*
  75. * Read lines until DELIM or END, calling the appropiate consumer.
  76. *
  77. * Returns the last read line (so caller can check if there is more to read
  78. * in the line).
  79. */
  80. private static String consumeCapabilities(PacketLineIn pckIn,
  81. Consumer<String> serverOptionConsumer,
  82. Consumer<String> agentConsumer) throws IOException {
  83. String serverOptionPrefix = OPTION_SERVER_OPTION + '=';
  84. String agentPrefix = OPTION_AGENT + '=';
  85. String line = pckIn.readString();
  86. while (line != PacketLineIn.DELIM && line != PacketLineIn.END) {
  87. if (line.startsWith(serverOptionPrefix)) {
  88. serverOptionConsumer
  89. .accept(line.substring(serverOptionPrefix.length()));
  90. } else if (line.startsWith(agentPrefix)) {
  91. agentConsumer.accept(line.substring(agentPrefix.length()));
  92. } else {
  93. // Unrecognized capability. Ignore it.
  94. }
  95. line = pckIn.readString();
  96. }
  97. return line;
  98. }
  99. /**
  100. * Parse the incoming fetch request arguments from the wire. The caller must
  101. * be sure that what is comings is a fetch request before coming here.
  102. *
  103. * @param pckIn
  104. * incoming lines
  105. * @return A FetchV2Request populated with information received from the
  106. * wire.
  107. * @throws PackProtocolException
  108. * incompatible options, wrong type of arguments or other issues
  109. * where the request breaks the protocol.
  110. * @throws IOException
  111. * an IO error prevented reading the incoming message.
  112. */
  113. FetchV2Request parseFetchRequest(PacketLineIn pckIn)
  114. throws PackProtocolException, IOException {
  115. FetchV2Request.Builder reqBuilder = FetchV2Request.builder();
  116. // Packs are always sent multiplexed and using full 64K
  117. // lengths.
  118. reqBuilder.addClientCapability(OPTION_SIDE_BAND_64K);
  119. String line = consumeCapabilities(pckIn,
  120. serverOption -> reqBuilder.addServerOption(serverOption),
  121. agent -> reqBuilder.setAgent(agent));
  122. if (line == PacketLineIn.END) {
  123. return reqBuilder.build();
  124. }
  125. if (line != PacketLineIn.DELIM) {
  126. throw new PackProtocolException(
  127. MessageFormat.format(JGitText.get().unexpectedPacketLine,
  128. line));
  129. }
  130. boolean filterReceived = false;
  131. while ((line = pckIn.readString()) != PacketLineIn.END) {
  132. if (line.startsWith("want ")) { //$NON-NLS-1$
  133. reqBuilder.addWantId(ObjectId.fromString(line.substring(5)));
  134. } else if (transferConfig.isAllowRefInWant()
  135. && line.startsWith(OPTION_WANT_REF + " ")) { //$NON-NLS-1$
  136. reqBuilder.addWantedRef(line.substring(OPTION_WANT_REF.length() + 1));
  137. } else if (line.startsWith("have ")) { //$NON-NLS-1$
  138. reqBuilder.addPeerHas(ObjectId.fromString(line.substring(5)));
  139. } else if (line.equals("done")) { //$NON-NLS-1$
  140. reqBuilder.setDoneReceived();
  141. } else if (line.equals(OPTION_THIN_PACK)) {
  142. reqBuilder.addClientCapability(OPTION_THIN_PACK);
  143. } else if (line.equals(OPTION_NO_PROGRESS)) {
  144. reqBuilder.addClientCapability(OPTION_NO_PROGRESS);
  145. } else if (line.equals(OPTION_INCLUDE_TAG)) {
  146. reqBuilder.addClientCapability(OPTION_INCLUDE_TAG);
  147. } else if (line.equals(OPTION_OFS_DELTA)) {
  148. reqBuilder.addClientCapability(OPTION_OFS_DELTA);
  149. } else if (line.startsWith("shallow ")) { //$NON-NLS-1$
  150. reqBuilder.addClientShallowCommit(
  151. ObjectId.fromString(line.substring(8)));
  152. } else if (line.startsWith("deepen ")) { //$NON-NLS-1$
  153. int parsedDepth = Integer.parseInt(line.substring(7));
  154. if (parsedDepth <= 0) {
  155. throw new PackProtocolException(
  156. MessageFormat.format(JGitText.get().invalidDepth,
  157. Integer.valueOf(parsedDepth)));
  158. }
  159. if (reqBuilder.getDeepenSince() != 0) {
  160. throw new PackProtocolException(
  161. JGitText.get().deepenSinceWithDeepen);
  162. }
  163. if (reqBuilder.hasDeepenNotRefs()) {
  164. throw new PackProtocolException(
  165. JGitText.get().deepenNotWithDeepen);
  166. }
  167. reqBuilder.setDepth(parsedDepth);
  168. } else if (line.startsWith("deepen-not ")) { //$NON-NLS-1$
  169. reqBuilder.addDeepenNotRef(line.substring(11));
  170. if (reqBuilder.getDepth() != 0) {
  171. throw new PackProtocolException(
  172. JGitText.get().deepenNotWithDeepen);
  173. }
  174. } else if (line.equals(OPTION_DEEPEN_RELATIVE)) {
  175. reqBuilder.addClientCapability(OPTION_DEEPEN_RELATIVE);
  176. } else if (line.startsWith("deepen-since ")) { //$NON-NLS-1$
  177. int ts = Integer.parseInt(line.substring(13));
  178. if (ts <= 0) {
  179. throw new PackProtocolException(MessageFormat
  180. .format(JGitText.get().invalidTimestamp, line));
  181. }
  182. if (reqBuilder.getDepth() != 0) {
  183. throw new PackProtocolException(
  184. JGitText.get().deepenSinceWithDeepen);
  185. }
  186. reqBuilder.setDeepenSince(ts);
  187. } else if (transferConfig.isAllowFilter()
  188. && line.startsWith(OPTION_FILTER + ' ')) {
  189. if (filterReceived) {
  190. throw new PackProtocolException(
  191. JGitText.get().tooManyFilters);
  192. }
  193. filterReceived = true;
  194. reqBuilder.setFilterSpec(FilterSpec.fromFilterLine(
  195. line.substring(OPTION_FILTER.length() + 1)));
  196. } else {
  197. throw new PackProtocolException(MessageFormat
  198. .format(JGitText.get().unexpectedPacketLine, line));
  199. }
  200. }
  201. return reqBuilder.build();
  202. }
  203. /**
  204. * Parse the incoming ls-refs request arguments from the wire. This is meant
  205. * for calling immediately after the caller has consumed a "command=ls-refs"
  206. * line indicating the beginning of a ls-refs request.
  207. *
  208. * The incoming PacketLineIn is consumed until an END line, but the caller
  209. * is responsible for closing it (if needed)
  210. *
  211. * @param pckIn
  212. * incoming lines. This method will read until an END line.
  213. * @return a LsRefsV2Request object with the data received in the wire.
  214. * @throws PackProtocolException
  215. * for inconsistencies in the protocol (e.g. unexpected lines)
  216. * @throws IOException
  217. * reporting problems reading the incoming messages from the
  218. * wire
  219. */
  220. LsRefsV2Request parseLsRefsRequest(PacketLineIn pckIn)
  221. throws PackProtocolException, IOException {
  222. LsRefsV2Request.Builder builder = LsRefsV2Request.builder();
  223. List<String> prefixes = new ArrayList<>();
  224. String line = consumeCapabilities(pckIn,
  225. serverOption -> builder.addServerOption(serverOption),
  226. agent -> builder.setAgent(agent));
  227. if (line == PacketLineIn.END) {
  228. return builder.build();
  229. }
  230. if (line != PacketLineIn.DELIM) {
  231. throw new PackProtocolException(MessageFormat
  232. .format(JGitText.get().unexpectedPacketLine, line));
  233. }
  234. while ((line = pckIn.readString()) != PacketLineIn.END) {
  235. if (line.equals("peel")) { //$NON-NLS-1$
  236. builder.setPeel(true);
  237. } else if (line.equals("symrefs")) { //$NON-NLS-1$
  238. builder.setSymrefs(true);
  239. } else if (line.startsWith("ref-prefix ")) { //$NON-NLS-1$
  240. prefixes.add(line.substring("ref-prefix ".length())); //$NON-NLS-1$
  241. } else {
  242. throw new PackProtocolException(MessageFormat
  243. .format(JGitText.get().unexpectedPacketLine, line));
  244. }
  245. }
  246. return builder.setRefPrefixes(prefixes).build();
  247. }
  248. }