ProtocolV0Parser.java

  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_FILTER;

  12. import java.io.EOFException;
  13. import java.io.IOException;
  14. import java.text.MessageFormat;

  15. import org.eclipse.jgit.errors.PackProtocolException;
  16. import org.eclipse.jgit.internal.JGitText;
  17. import org.eclipse.jgit.internal.transport.parser.FirstWant;
  18. import org.eclipse.jgit.lib.ObjectId;

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

  28.     private final TransferConfig transferConfig;

  29.     ProtocolV0Parser(TransferConfig transferConfig) {
  30.         this.transferConfig = transferConfig;
  31.     }

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

  47.         boolean isFirst = true;
  48.         boolean filterReceived = false;

  49.         for (;;) {
  50.             String line;
  51.             try {
  52.                 line = pckIn.readString();
  53.             } catch (EOFException eof) {
  54.                 if (isFirst) {
  55.                     break;
  56.                 }
  57.                 throw eof;
  58.             }

  59.             if (PacketLineIn.isEnd(line)) {
  60.                 break;
  61.             }

  62.             if (line.startsWith("deepen ")) { //$NON-NLS-1$
  63.                 int depth = Integer.parseInt(line.substring(7));
  64.                 if (depth <= 0) {
  65.                     throw new PackProtocolException(
  66.                             MessageFormat.format(JGitText.get().invalidDepth,
  67.                                     Integer.valueOf(depth)));
  68.                 }
  69.                 reqBuilder.setDepth(depth);
  70.                 continue;
  71.             }

  72.             if (line.startsWith("shallow ")) { //$NON-NLS-1$
  73.                 reqBuilder.addClientShallowCommit(
  74.                         ObjectId.fromString(line.substring(8)));
  75.                 continue;
  76.             }

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

  80.                 if (filterReceived) {
  81.                     throw new PackProtocolException(
  82.                             JGitText.get().tooManyFilters);
  83.                 }
  84.                 filterReceived = true;

  85.                 reqBuilder.setFilterSpec(FilterSpec.fromFilterLine(arg));
  86.                 continue;
  87.             }

  88.             if (!line.startsWith("want ") || line.length() < 45) { //$NON-NLS-1$
  89.                 throw new PackProtocolException(MessageFormat
  90.                         .format(JGitText.get().expectedGot, "want", line)); //$NON-NLS-1$
  91.             }

  92.             if (isFirst) {
  93.                 if (line.length() > 45) {
  94.                     FirstWant firstLine = FirstWant.fromLine(line);
  95.                     reqBuilder.addClientCapabilities(firstLine.getCapabilities());
  96.                     reqBuilder.setAgent(firstLine.getAgent());
  97.                     line = firstLine.getLine();
  98.                 }
  99.             }

  100.             reqBuilder.addWantId(ObjectId.fromString(line.substring(5)));
  101.             isFirst = false;
  102.         }

  103.         return reqBuilder.build();
  104.     }

  105. }