ProtocolV0Parser.java

  1. /*
  2.  * Copyright (C) 2018, 2022 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_FILTER;
  12. import static org.eclipse.jgit.transport.GitProtocolConstants.PACKET_DEEPEN;
  13. import static org.eclipse.jgit.transport.GitProtocolConstants.PACKET_DEEPEN_NOT;
  14. import static org.eclipse.jgit.transport.GitProtocolConstants.PACKET_DEEPEN_SINCE;
  15. import static org.eclipse.jgit.transport.GitProtocolConstants.PACKET_SHALLOW;
  16. import static org.eclipse.jgit.transport.GitProtocolConstants.PACKET_WANT;

  17. import java.io.EOFException;
  18. import java.io.IOException;
  19. import java.text.MessageFormat;

  20. import org.eclipse.jgit.errors.PackProtocolException;
  21. import org.eclipse.jgit.internal.JGitText;
  22. import org.eclipse.jgit.internal.transport.parser.FirstWant;
  23. import org.eclipse.jgit.lib.ObjectId;

  24. /**
  25.  * Parser for git protocol versions 0 and 1.
  26.  *
  27.  * It reads the lines coming through the {@link PacketLineIn} and builds a
  28.  * {@link FetchV0Request} object.
  29.  *
  30.  * It requires a transferConfig object to know if the server supports filters.
  31.  */
  32. final class ProtocolV0Parser {

  33.     private final TransferConfig transferConfig;

  34.     ProtocolV0Parser(TransferConfig transferConfig) {
  35.         this.transferConfig = transferConfig;
  36.     }

  37.     /**
  38.      * Parse an incoming protocol v1 upload request arguments from the wire.
  39.      *
  40.      * The incoming PacketLineIn is consumed until an END line, but the caller
  41.      * is responsible for closing it (if needed).
  42.      *
  43.      * @param pckIn
  44.      *            incoming lines. This method will read until an END line.
  45.      * @return a FetchV0Request with the data received in the wire.
  46.      * @throws PackProtocolException
  47.      * @throws IOException
  48.      */
  49.     FetchV0Request recvWants(PacketLineIn pckIn)
  50.             throws PackProtocolException, IOException {
  51.         FetchV0Request.Builder reqBuilder = new FetchV0Request.Builder();

  52.         boolean isFirst = true;
  53.         boolean filterReceived = false;

  54.         for (;;) {
  55.             String line;
  56.             try {
  57.                 line = pckIn.readString();
  58.             } catch (EOFException eof) {
  59.                 if (isFirst) {
  60.                     break;
  61.                 }
  62.                 throw eof;
  63.             }

  64.             if (PacketLineIn.isEnd(line)) {
  65.                 break;
  66.             }

  67.             if (line.startsWith(PACKET_DEEPEN)) {
  68.                 int depth = Integer
  69.                         .parseInt(line.substring(PACKET_DEEPEN.length()));
  70.                 if (depth <= 0) {
  71.                     throw new PackProtocolException(
  72.                             MessageFormat.format(JGitText.get().invalidDepth,
  73.                                     Integer.valueOf(depth)));
  74.                 }
  75.                 if (reqBuilder.getDeepenSince() != 0) {
  76.                     throw new PackProtocolException(
  77.                             JGitText.get().deepenSinceWithDeepen);
  78.                 }
  79.                 if (reqBuilder.hasDeepenNots()) {
  80.                     throw new PackProtocolException(
  81.                             JGitText.get().deepenNotWithDeepen);
  82.                 }
  83.                 reqBuilder.setDepth(depth);
  84.                 continue;
  85.             }

  86.             if (line.startsWith(PACKET_DEEPEN_NOT)) {
  87.                 reqBuilder.addDeepenNot(
  88.                         line.substring(PACKET_DEEPEN_NOT.length()));
  89.                 if (reqBuilder.getDepth() != 0) {
  90.                     throw new PackProtocolException(
  91.                             JGitText.get().deepenNotWithDeepen);
  92.                 }
  93.                 continue;
  94.             }

  95.             if (line.startsWith(PACKET_DEEPEN_SINCE)) {
  96.                 // TODO: timestamps should be long
  97.                 int ts = Integer
  98.                         .parseInt(line.substring(PACKET_DEEPEN_SINCE.length()));
  99.                 if (ts <= 0) {
  100.                     throw new PackProtocolException(MessageFormat
  101.                             .format(JGitText.get().invalidTimestamp, line));
  102.                 }
  103.                 if (reqBuilder.getDepth() != 0) {
  104.                     throw new PackProtocolException(
  105.                             JGitText.get().deepenSinceWithDeepen);
  106.                 }
  107.                 reqBuilder.setDeepenSince(ts);
  108.                 continue;
  109.             }

  110.             if (line.startsWith(PACKET_SHALLOW)) {
  111.                 reqBuilder.addClientShallowCommit(
  112.                         ObjectId.fromString(
  113.                                 line.substring(PACKET_SHALLOW.length())));
  114.                 continue;
  115.             }

  116.             if (transferConfig.isAllowFilter()
  117.                     && line.startsWith(OPTION_FILTER + " ")) { //$NON-NLS-1$
  118.                 String arg = line.substring(OPTION_FILTER.length() + 1);

  119.                 if (filterReceived) {
  120.                     throw new PackProtocolException(
  121.                             JGitText.get().tooManyFilters);
  122.                 }
  123.                 filterReceived = true;

  124.                 reqBuilder.setFilterSpec(FilterSpec.fromFilterLine(arg));
  125.                 continue;
  126.             }

  127.             if (!line.startsWith(PACKET_WANT) || line.length() < 45) {
  128.                 throw new PackProtocolException(MessageFormat
  129.                         .format(JGitText.get().expectedGot, "want", line)); //$NON-NLS-1$
  130.             }

  131.             if (isFirst) {
  132.                 if (line.length() > 45) {
  133.                     FirstWant firstLine = FirstWant.fromLine(line);
  134.                     reqBuilder.addClientCapabilities(firstLine.getCapabilities());
  135.                     reqBuilder.setAgent(firstLine.getAgent());
  136.                     reqBuilder.setClientSID(firstLine.getClientSID());
  137.                     line = firstLine.getLine();
  138.                 }
  139.             }

  140.             reqBuilder.addWantId(
  141.                     ObjectId.fromString(line.substring(PACKET_WANT.length())));
  142.             isFirst = false;
  143.         }

  144.         return reqBuilder.build();
  145.     }

  146. }