ProtocolV0Parser.java

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

  45. import java.io.EOFException;
  46. import java.io.IOException;
  47. import java.text.MessageFormat;

  48. import org.eclipse.jgit.errors.PackProtocolException;
  49. import org.eclipse.jgit.internal.JGitText;
  50. import org.eclipse.jgit.internal.transport.parser.FirstWant;
  51. import org.eclipse.jgit.lib.ObjectId;

  52. /**
  53.  * Parser for git protocol versions 0 and 1.
  54.  *
  55.  * It reads the lines coming through the {@link PacketLineIn} and builds a
  56.  * {@link FetchV0Request} object.
  57.  *
  58.  * It requires a transferConfig object to know if the server supports filters.
  59.  */
  60. final class ProtocolV0Parser {

  61.     private final TransferConfig transferConfig;

  62.     ProtocolV0Parser(TransferConfig transferConfig) {
  63.         this.transferConfig = transferConfig;
  64.     }

  65.     /**
  66.      * Parse an incoming protocol v1 upload request arguments from the wire.
  67.      *
  68.      * The incoming PacketLineIn is consumed until an END line, but the caller
  69.      * is responsible for closing it (if needed).
  70.      *
  71.      * @param pckIn
  72.      *            incoming lines. This method will read until an END line.
  73.      * @return a FetchV0Request with the data received in the wire.
  74.      * @throws PackProtocolException
  75.      * @throws IOException
  76.      */
  77.     FetchV0Request recvWants(PacketLineIn pckIn)
  78.             throws PackProtocolException, IOException {
  79.         FetchV0Request.Builder reqBuilder = new FetchV0Request.Builder();

  80.         boolean isFirst = true;
  81.         boolean filterReceived = false;

  82.         for (;;) {
  83.             String line;
  84.             try {
  85.                 line = pckIn.readString();
  86.             } catch (EOFException eof) {
  87.                 if (isFirst) {
  88.                     break;
  89.                 }
  90.                 throw eof;
  91.             }

  92.             if (line == PacketLineIn.END) {
  93.                 break;
  94.             }

  95.             if (line.startsWith("deepen ")) { //$NON-NLS-1$
  96.                 int depth = Integer.parseInt(line.substring(7));
  97.                 if (depth <= 0) {
  98.                     throw new PackProtocolException(
  99.                             MessageFormat.format(JGitText.get().invalidDepth,
  100.                                     Integer.valueOf(depth)));
  101.                 }
  102.                 reqBuilder.setDepth(depth);
  103.                 continue;
  104.             }

  105.             if (line.startsWith("shallow ")) { //$NON-NLS-1$
  106.                 reqBuilder.addClientShallowCommit(
  107.                         ObjectId.fromString(line.substring(8)));
  108.                 continue;
  109.             }

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

  113.                 if (filterReceived) {
  114.                     throw new PackProtocolException(
  115.                             JGitText.get().tooManyFilters);
  116.                 }
  117.                 filterReceived = true;

  118.                 reqBuilder.setFilterBlobLimit(ProtocolV2Parser.filterLine(arg));
  119.                 continue;
  120.             }

  121.             if (!line.startsWith("want ") || line.length() < 45) { //$NON-NLS-1$
  122.                 throw new PackProtocolException(MessageFormat
  123.                         .format(JGitText.get().expectedGot, "want", line)); //$NON-NLS-1$
  124.             }

  125.             if (isFirst) {
  126.                 if (line.length() > 45) {
  127.                     FirstWant firstLine = FirstWant.fromLine(line);
  128.                     reqBuilder.addClientCapabilities(firstLine.getCapabilities());
  129.                     reqBuilder.setAgent(firstLine.getAgent());
  130.                     line = firstLine.getLine();
  131.                 }
  132.             }

  133.             reqBuilder.addWantId(ObjectId.fromString(line.substring(5)));
  134.             isFirst = false;
  135.         }

  136.         return reqBuilder.build();
  137.     }

  138. }