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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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_INCLUDE_TAG;
  47. import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_NO_PROGRESS;
  48. import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_OFS_DELTA;
  49. import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_SIDE_BAND_64K;
  50. import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_THIN_PACK;
  51. import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_WANT_REF;
  52. import java.io.IOException;
  53. import java.text.MessageFormat;
  54. import java.util.ArrayList;
  55. import java.util.List;
  56. import org.eclipse.jgit.errors.PackProtocolException;
  57. import org.eclipse.jgit.internal.JGitText;
  58. import org.eclipse.jgit.lib.ObjectId;
  59. import org.eclipse.jgit.lib.Ref;
  60. import org.eclipse.jgit.lib.RefDatabase;
  61. /**
  62. * Parse the incoming git protocol lines from the wire and translate them into a
  63. * Request object.
  64. *
  65. * It requires a transferConfig object to know what the server supports (e.g.
  66. * ref-in-want and/or filters).
  67. */
  68. final class ProtocolV2Parser {
  69. private final TransferConfig transferConfig;
  70. ProtocolV2Parser(TransferConfig transferConfig) {
  71. this.transferConfig = transferConfig;
  72. }
  73. /**
  74. * Parse the incoming fetch request arguments from the wire. The caller must
  75. * be sure that what is comings is a fetch request before coming here.
  76. *
  77. * This operation requires the reference database to validate incoming
  78. * references.
  79. *
  80. * @param pckIn
  81. * incoming lines
  82. * @param refdb
  83. * reference database (to validate that received references exist
  84. * and point to valid objects)
  85. * @return A FetchV2Request populated with information received from the
  86. * wire.
  87. * @throws PackProtocolException
  88. * incompatible options, wrong type of arguments or other issues
  89. * where the request breaks the protocol.
  90. * @throws IOException
  91. * an IO error prevented reading the incoming message or
  92. * accessing the ref database.
  93. */
  94. FetchV2Request parseFetchRequest(PacketLineIn pckIn, RefDatabase refdb)
  95. throws PackProtocolException, IOException {
  96. FetchV2Request.Builder reqBuilder = FetchV2Request.builder();
  97. // Packs are always sent multiplexed and using full 64K
  98. // lengths.
  99. reqBuilder.addOption(OPTION_SIDE_BAND_64K);
  100. String line;
  101. // Currently, we do not support any capabilities, so the next
  102. // line is DELIM.
  103. if ((line = pckIn.readString()) != PacketLineIn.DELIM) {
  104. throw new PackProtocolException(MessageFormat
  105. .format(JGitText.get().unexpectedPacketLine, line));
  106. }
  107. boolean filterReceived = false;
  108. while ((line = pckIn.readString()) != PacketLineIn.END) {
  109. if (line.startsWith("want ")) { //$NON-NLS-1$
  110. reqBuilder.addWantsIds(ObjectId.fromString(line.substring(5)));
  111. } else if (transferConfig.isAllowRefInWant()
  112. && line.startsWith(OPTION_WANT_REF + " ")) { //$NON-NLS-1$
  113. String refName = line.substring(OPTION_WANT_REF.length() + 1);
  114. // TODO(ifrade): This validation should be done after the
  115. // protocol parsing. It is not a protocol problem asking for an
  116. // unexisting ref and we wouldn't need the ref database here
  117. Ref ref = refdb.exactRef(refName);
  118. if (ref == null) {
  119. throw new PackProtocolException(MessageFormat
  120. .format(JGitText.get().invalidRefName, refName));
  121. }
  122. ObjectId oid = ref.getObjectId();
  123. if (oid == null) {
  124. throw new PackProtocolException(MessageFormat
  125. .format(JGitText.get().invalidRefName, refName));
  126. }
  127. reqBuilder.addWantedRef(refName, oid);
  128. reqBuilder.addWantsIds(oid);
  129. } else if (line.startsWith("have ")) { //$NON-NLS-1$
  130. reqBuilder.addPeerHas(ObjectId.fromString(line.substring(5)));
  131. } else if (line.equals("done")) { //$NON-NLS-1$
  132. reqBuilder.setDoneReceived();
  133. } else if (line.equals(OPTION_THIN_PACK)) {
  134. reqBuilder.addOption(OPTION_THIN_PACK);
  135. } else if (line.equals(OPTION_NO_PROGRESS)) {
  136. reqBuilder.addOption(OPTION_NO_PROGRESS);
  137. } else if (line.equals(OPTION_INCLUDE_TAG)) {
  138. reqBuilder.addOption(OPTION_INCLUDE_TAG);
  139. } else if (line.equals(OPTION_OFS_DELTA)) {
  140. reqBuilder.addOption(OPTION_OFS_DELTA);
  141. } else if (line.startsWith("shallow ")) { //$NON-NLS-1$
  142. reqBuilder.addClientShallowCommit(
  143. ObjectId.fromString(line.substring(8)));
  144. } else if (line.startsWith("deepen ")) { //$NON-NLS-1$
  145. int parsedDepth = Integer.parseInt(line.substring(7));
  146. if (parsedDepth <= 0) {
  147. throw new PackProtocolException(
  148. MessageFormat.format(JGitText.get().invalidDepth,
  149. Integer.valueOf(parsedDepth)));
  150. }
  151. if (reqBuilder.getDeepenSince() != 0) {
  152. throw new PackProtocolException(
  153. JGitText.get().deepenSinceWithDeepen);
  154. }
  155. if (reqBuilder.hasDeepenNotRefs()) {
  156. throw new PackProtocolException(
  157. JGitText.get().deepenNotWithDeepen);
  158. }
  159. reqBuilder.setDepth(parsedDepth);
  160. } else if (line.startsWith("deepen-not ")) { //$NON-NLS-1$
  161. reqBuilder.addDeepenNotRef(line.substring(11));
  162. if (reqBuilder.getDepth() != 0) {
  163. throw new PackProtocolException(
  164. JGitText.get().deepenNotWithDeepen);
  165. }
  166. } else if (line.equals(OPTION_DEEPEN_RELATIVE)) {
  167. reqBuilder.addOption(OPTION_DEEPEN_RELATIVE);
  168. } else if (line.startsWith("deepen-since ")) { //$NON-NLS-1$
  169. int ts = Integer.parseInt(line.substring(13));
  170. if (ts <= 0) {
  171. throw new PackProtocolException(MessageFormat
  172. .format(JGitText.get().invalidTimestamp, line));
  173. }
  174. if (reqBuilder.getDepth() != 0) {
  175. throw new PackProtocolException(
  176. JGitText.get().deepenSinceWithDeepen);
  177. }
  178. reqBuilder.setDeepenSince(ts);
  179. } else if (transferConfig.isAllowFilter()
  180. && line.startsWith(OPTION_FILTER + ' ')) {
  181. if (filterReceived) {
  182. throw new PackProtocolException(
  183. JGitText.get().tooManyFilters);
  184. }
  185. filterReceived = true;
  186. reqBuilder.setFilterBlobLimit(filterLine(
  187. line.substring(OPTION_FILTER.length() + 1)));
  188. } else {
  189. throw new PackProtocolException(MessageFormat
  190. .format(JGitText.get().unexpectedPacketLine, line));
  191. }
  192. }
  193. return reqBuilder.build();
  194. }
  195. /**
  196. * Parse the incoming ls-refs request arguments from the wire. This is meant
  197. * for calling immediately after the caller has consumed a "command=ls-refs"
  198. * line indicating the beginning of a ls-refs request.
  199. *
  200. * The incoming PacketLineIn is consumed until an END line, but the caller
  201. * is responsible for closing it (if needed)
  202. *
  203. * @param pckIn
  204. * incoming lines. This method will read until an END line.
  205. * @return a LsRefsV2Request object with the data received in the wire.
  206. * @throws PackProtocolException
  207. * for inconsistencies in the protocol (e.g. unexpected lines)
  208. * @throws IOException
  209. * reporting problems reading the incoming messages from the
  210. * wire
  211. */
  212. LsRefsV2Request parseLsRefsRequest(PacketLineIn pckIn)
  213. throws PackProtocolException, IOException {
  214. LsRefsV2Request.Builder builder = LsRefsV2Request.builder();
  215. List<String> prefixes = new ArrayList<>();
  216. String line = pckIn.readString();
  217. // Currently, we do not support any capabilities, so the next
  218. // line is DELIM if there are arguments or END if not.
  219. if (line == PacketLineIn.DELIM) {
  220. while ((line = pckIn.readString()) != PacketLineIn.END) {
  221. if (line.equals("peel")) { //$NON-NLS-1$
  222. builder.setPeel(true);
  223. } else if (line.equals("symrefs")) { //$NON-NLS-1$
  224. builder.setSymrefs(true);
  225. } else if (line.startsWith("ref-prefix ")) { //$NON-NLS-1$
  226. prefixes.add(line.substring("ref-prefix ".length())); //$NON-NLS-1$
  227. } else {
  228. throw new PackProtocolException(MessageFormat
  229. .format(JGitText.get().unexpectedPacketLine, line));
  230. }
  231. }
  232. } else if (line != PacketLineIn.END) {
  233. throw new PackProtocolException(MessageFormat
  234. .format(JGitText.get().unexpectedPacketLine, line));
  235. }
  236. return builder.setRefPrefixes(prefixes).build();
  237. }
  238. /*
  239. * Process the content of "filter" line from the protocol. It has a shape
  240. * like "blob:none" or "blob:limit=N", with limit a positive number.
  241. *
  242. * @param blobLine
  243. * the content of the "filter" line in the protocol
  244. * @return N, the limit, defaulting to 0 if "none"
  245. * @throws PackProtocolException
  246. * invalid filter because due to unrecognized format or
  247. * negative/non-numeric filter.
  248. */
  249. static long filterLine(String blobLine) throws PackProtocolException {
  250. long blobLimit = -1;
  251. if (blobLine.equals("blob:none")) { //$NON-NLS-1$
  252. blobLimit = 0;
  253. } else if (blobLine.startsWith("blob:limit=")) { //$NON-NLS-1$
  254. try {
  255. blobLimit = Long
  256. .parseLong(blobLine.substring("blob:limit=".length())); //$NON-NLS-1$
  257. } catch (NumberFormatException e) {
  258. throw new PackProtocolException(MessageFormat
  259. .format(JGitText.get().invalidFilter, blobLine));
  260. }
  261. }
  262. /*
  263. * We must have (1) either "blob:none" or "blob:limit=" set (because we
  264. * only support blob size limits for now), and (2) if the latter, then
  265. * it must be nonnegative. Throw if (1) or (2) is not met.
  266. */
  267. if (blobLimit < 0) {
  268. throw new PackProtocolException(
  269. MessageFormat.format(JGitText.get().invalidFilter, blobLine));
  270. }
  271. return blobLimit;
  272. }
  273. }