]> source.dussan.org Git - jgit.git/commitdiff
Move first line parsing for v0/v1 pack negotiation out of UploadPack 90/129590/18
authorIvan Frade <ifrade@google.com>
Mon, 17 Sep 2018 18:48:48 +0000 (11:48 -0700)
committerIvan Frade <ifrade@google.com>
Fri, 12 Oct 2018 20:59:06 +0000 (13:59 -0700)
In protocol v0/v1 pack negotiation, the first want line contains the
options the client wants in effect. This parsing is done in UploadPack
but it doesn't have any interaction with that class.

Move the code to its own class and package, mark the current one
as deprecated (it is public API) and add unit tests.

Take the chance to move the parsing code from the constructor to a
factory method, making the class a simple container of results.

Change-Id: I1757f535dda78a4111a1c12c3a3b455a4b6f0c51
Signed-off-by: Ivan Frade <ifrade@google.com>
org.eclipse.jgit.http.server/META-INF/MANIFEST.MF
org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/GitSmartHttpTools.java
org.eclipse.jgit.test/META-INF/MANIFEST.MF
org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/transport/parser/FirstWantTest.java [new file with mode: 0644]
org.eclipse.jgit/META-INF/MANIFEST.MF
org.eclipse.jgit/src/org/eclipse/jgit/internal/transport/parser/FirstWant.java [new file with mode: 0644]
org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java

index f9a66b90a3b7de9afdb712d0752d34134fa723ad..c85e4964622374a51cde351c00bf78a34c967494 100644 (file)
@@ -21,6 +21,7 @@ Import-Package: javax.servlet;version="[2.5.0,3.2.0)",
  org.eclipse.jgit.errors;version="[5.2.0,5.3.0)",
  org.eclipse.jgit.internal.storage.dfs;version="[5.2.0,5.3.0)",
  org.eclipse.jgit.internal.storage.file;version="[5.2.0,5.3.0)",
+ org.eclipse.jgit.internal.transport.parser;version="[5.2.0,5.3.0)",
  org.eclipse.jgit.lib;version="[5.2.0,5.3.0)",
  org.eclipse.jgit.nls;version="[5.2.0,5.3.0)",
  org.eclipse.jgit.revwalk;version="[5.2.0,5.3.0)",
index b9ca12bf3d5b293a13eecb7913ae5b72b22c7d6a..ee4b32efb7304e05e37a3ae86e3224fcc3f2c435 100644 (file)
@@ -63,6 +63,7 @@ import java.util.List;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import org.eclipse.jgit.internal.transport.parser.FirstWant;
 import org.eclipse.jgit.lib.Constants;
 import org.eclipse.jgit.transport.PacketLineIn;
 import org.eclipse.jgit.transport.PacketLineOut;
@@ -246,9 +247,9 @@ public class GitSmartHttpTools {
                        // not have an UploadPack, or it might not have read any of the request.
                        // So, cheat and read the first line.
                        String line = new PacketLineIn(req.getInputStream()).readString();
-                       UploadPack.FirstLine parsed = new UploadPack.FirstLine(line);
-                       return (parsed.getOptions().contains(OPTION_SIDE_BAND)
-                                       || parsed.getOptions().contains(OPTION_SIDE_BAND_64K));
+                       FirstWant parsed = FirstWant.fromLine(line);
+                       return (parsed.getCapabilities().contains(OPTION_SIDE_BAND)
+                                       || parsed.getCapabilities().contains(OPTION_SIDE_BAND_64K));
                } catch (IOException e) {
                        // Probably the connection is closed and a subsequent write will fail, but
                        // try it just in case.
index 2f828a4f39ecc59bc73acb4c8dc326a7bd97e46e..6df9e39094cd784610139290815ec7e5a96e51c8 100644 (file)
@@ -32,6 +32,7 @@ Import-Package: com.googlecode.javaewah;version="[1.1.6,2.0.0)",
  org.eclipse.jgit.internal.storage.pack;version="[5.2.0,5.3.0)",
  org.eclipse.jgit.internal.storage.reftable;version="[5.2.0,5.3.0)",
  org.eclipse.jgit.internal.storage.reftree;version="[5.2.0,5.3.0)",
+ org.eclipse.jgit.internal.transport.parser;version="[5.2.0,5.3.0)",
  org.eclipse.jgit.junit;version="[5.2.0,5.3.0)",
  org.eclipse.jgit.lfs;version="[5.2.0,5.3.0)",
  org.eclipse.jgit.lib;version="[5.2.0,5.3.0)",
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/transport/parser/FirstWantTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/transport/parser/FirstWantTest.java
new file mode 100644 (file)
index 0000000..572ec8b
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ * Copyright (C) 2018, Google LLC.
+ * and other copyright owners as documented in the project's IP log.
+ *
+ * This program and the accompanying materials are made available
+ * under the terms of the Eclipse Distribution License v1.0 which
+ * accompanies this distribution, is reproduced below, and is
+ * available at http://www.eclipse.org/org/documents/edl-v10.php
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above
+ *   copyright notice, this list of conditions and the following
+ *   disclaimer in the documentation and/or other materials provided
+ *   with the distribution.
+ *
+ * - Neither the name of the Eclipse Foundation, Inc. nor the
+ *   names of its contributors may be used to endorse or promote
+ *   products derived from this software without specific prior
+ *   written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package org.eclipse.jgit.internal.transport.parser;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.junit.Test;
+
+public class FirstWantTest {
+
+       @Test
+       public void testFirstWantWithOptions() {
+               String line = "want b9d4d1eb2f93058814480eae9e1b67550f46ee38 "
+                               + "no-progress include-tag ofs-delta agent=JGit/unknown";
+
+               FirstWant r = FirstWant.fromLine(line);
+               assertEquals("want b9d4d1eb2f93058814480eae9e1b67550f46ee38",
+                               r.getLine());
+               Set<String> capabilities = r.getCapabilities();
+               Set<String> expectedCapabilities = new HashSet<>(
+                               Arrays.asList("no-progress", "include-tag", "ofs-delta",
+                                               "agent=JGit/unknown"));
+               assertEquals(expectedCapabilities, capabilities);
+       }
+
+       @Test
+       public void testFirstWantWithoutOptions() {
+               String line = "want b9d4d1eb2f93058814480eae9e1b67550f46ee38";
+
+               FirstWant r = FirstWant.fromLine(line);
+               assertEquals("want b9d4d1eb2f93058814480eae9e1b67550f46ee38",
+                               r.getLine());
+               assertTrue(r.getCapabilities().isEmpty());
+       }
+}
index 34bcdb95f2130ecaaceb560748defa94659df2b5..2b9ed0845b21ab64a145cd230193a00a8841b12f 100644 (file)
@@ -80,6 +80,7 @@ Export-Package: org.eclipse.jgit.annotations;version="5.2.0",
  org.eclipse.jgit.internal.storage.reftable;version="5.2.0";
   x-friends:="org.eclipse.jgit.http.test,org.eclipse.jgit.junit,org.eclipse.jgit.test,org.eclipse.jgit.pgm",
  org.eclipse.jgit.internal.storage.reftree;version="5.2.0";x-friends:="org.eclipse.jgit.junit,org.eclipse.jgit.test,org.eclipse.jgit.pgm",
+ org.eclipse.jgit.internal.transport.parser;version="5.2.0",
  org.eclipse.jgit.lib;version="5.2.0";
   uses:="org.eclipse.jgit.revwalk,
    org.eclipse.jgit.treewalk.filter,
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/transport/parser/FirstWant.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/transport/parser/FirstWant.java
new file mode 100644 (file)
index 0000000..ea0fbed
--- /dev/null
@@ -0,0 +1,115 @@
+/*
+ * Copyright (C) 2018, Google LLC.
+ * and other copyright owners as documented in the project's IP log.
+ *
+ * This program and the accompanying materials are made available
+ * under the terms of the Eclipse Distribution License v1.0 which
+ * accompanies this distribution, is reproduced below, and is
+ * available at http://www.eclipse.org/org/documents/edl-v10.php
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above
+ *   copyright notice, this list of conditions and the following
+ *   disclaimer in the documentation and/or other materials provided
+ *   with the distribution.
+ *
+ * - Neither the name of the Eclipse Foundation, Inc. nor the
+ *   names of its contributors may be used to endorse or promote
+ *   products derived from this software without specific prior
+ *   written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package org.eclipse.jgit.internal.transport.parser;
+
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * In the pack negotiation phase (protocol v0/v1), the client sends a list of
+ * wants. The first "want" line is special, as it (can) have a list of
+ * capabilities appended.
+ *
+ * E.g. "want oid cap1 cap2 cap3"
+ *
+ * Do not confuse this line with the first one in the reference advertisement,
+ * which is sent by the server, looks like
+ * "b8f7c471373b8583ced0025cfad8c9916c484b76 HEAD\0 cap1 cap2 cap3" and is
+ * parsed by the BasePackConnection.readAdvertisedRefs method.
+ *
+ * This class parses the input want line and holds the results: the actual want
+ * line and the capabilities.
+ *
+ * @since 5.2
+ */
+public class FirstWant {
+       private final String line;
+
+       private final Set<String> capabilities;
+
+       /**
+        * Parse the first want line in the protocol v0/v1 pack negotiation.
+        *
+        * @param line
+        *            line from the client.
+        * @return an instance of FirstWant
+        */
+       public static FirstWant fromLine(String line) {
+               String wantLine;
+               Set<String> capabilities;
+
+               if (line.length() > 45) {
+                       final HashSet<String> opts = new HashSet<>();
+                       String opt = line.substring(45);
+                       if (opt.startsWith(" ")) { //$NON-NLS-1$
+                               opt = opt.substring(1);
+                       }
+                       for (String c : opt.split(" ")) { //$NON-NLS-1$
+                               opts.add(c);
+                       }
+                       wantLine = line.substring(0, 45);
+                       capabilities = Collections.unmodifiableSet(opts);
+               } else {
+                       wantLine = line;
+                       capabilities = Collections.emptySet();
+               }
+
+               return new FirstWant(wantLine, capabilities);
+       }
+
+       private FirstWant(String line, Set<String> capabilities) {
+               this.line = line;
+               this.capabilities = capabilities;
+       }
+
+       /** @return non-capabilities part of the line. */
+       public String getLine() {
+               return line;
+       }
+
+       /** @return capabilities parsed from the line as an immutable set. */
+       public Set<String> getCapabilities() {
+               return capabilities;
+       }
+}
index 213bfe504ba3ddb4afcb78011120042a6ffe84ec..8d2158d5605da060106277bd31b8c3105b005bf6 100644 (file)
@@ -86,6 +86,7 @@ import org.eclipse.jgit.errors.MissingObjectException;
 import org.eclipse.jgit.errors.PackProtocolException;
 import org.eclipse.jgit.internal.JGitText;
 import org.eclipse.jgit.internal.storage.pack.PackWriter;
+import org.eclipse.jgit.internal.transport.parser.FirstWant;
 import org.eclipse.jgit.lib.BitmapIndex;
 import org.eclipse.jgit.lib.BitmapIndex.BitmapBuilder;
 import org.eclipse.jgit.lib.Constants;
@@ -177,41 +178,32 @@ public class UploadPack {
                                throws PackProtocolException, IOException;
        }
 
-       /** Data in the first line of a request, the line itself plus options. */
+       /**
+        * Data in the first line of a want-list, the line itself plus options.
+        *
+        * @deprecated Use {@link FirstWant} instead
+        */
+       @Deprecated
        public static class FirstLine {
-               private final String line;
-               private final Set<String> options;
+
+               private final FirstWant firstWant;
 
                /**
-                * Parse the first line of a receive-pack request.
-                *
                 * @param line
                 *            line from the client.
                 */
                public FirstLine(String line) {
-                       if (line.length() > 45) {
-                               final HashSet<String> opts = new HashSet<>();
-                               String opt = line.substring(45);
-                               if (opt.startsWith(" ")) //$NON-NLS-1$
-                                       opt = opt.substring(1);
-                               for (String c : opt.split(" ")) //$NON-NLS-1$
-                                       opts.add(c);
-                               this.line = line.substring(0, 45);
-                               this.options = Collections.unmodifiableSet(opts);
-                       } else {
-                               this.line = line;
-                               this.options = Collections.emptySet();
-                       }
+                       firstWant = FirstWant.fromLine(line);
                }
 
                /** @return non-capabilities part of the line. */
                public String getLine() {
-                       return line;
+                       return firstWant.getLine();
                }
 
-               /** @return options parsed from the line. */
-               public Set<String> getOptions() {
-                       return options;
+               /** @return capabilities parsed from the line. */
+               public Set<String> getCapabilities() {
+                       return firstWant.getCapabilities();
                }
        }
 
@@ -1391,8 +1383,8 @@ public class UploadPack {
 
                        if (isFirst) {
                                if (line.length() > 45) {
-                                       FirstLine firstLine = new FirstLine(line);
-                                       options = firstLine.getOptions();
+                                       FirstWant firstLine = FirstWant.fromLine(line);
+                                       options = firstLine.getCapabilities();
                                        line = firstLine.getLine();
                                } else
                                        options = Collections.emptySet();