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

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