]> source.dussan.org Git - jgit.git/commitdiff
FirstWant: Parse client session-id if received. 65/196565/7
authorJosh Brown <sjoshbrown@google.com>
Tue, 1 Nov 2022 18:16:44 +0000 (18:16 +0000)
committerJosh Brown <sjoshbrown@google.com>
Wed, 2 Nov 2022 20:12:03 +0000 (20:12 +0000)
In protocol V0 the client capabilities are appended to the first line.
Parsing session-id is currently only supported during a ReceivePack
operation. This change will parse the client session-id capability if
it has been sent by the client.

If the server sends the session-id capability to the client. The client
may respond with a session ID of its own. FirstWant.fromLine will now
parse the ID and make it available via the getClientSID method.

This change does not add support to send the session-id capability from
the server. The change is necessary to support session-id in UploadPack.

Change-Id: Id3fe44fdf9a72984ee3de9cf40cc4e71d434df4a
Signed-off-by: Josh Brown <sjoshbrown@google.com>
org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/transport/parser/FirstWantTest.java
org.eclipse.jgit/src/org/eclipse/jgit/internal/transport/parser/FirstWant.java

index 1e8ce8ef11ccec5d475886b137f22f983cef9c56..230f6a4081f182d764473f1e1d05f26bd201c3a1 100644 (file)
@@ -27,7 +27,8 @@ public class FirstWantTest {
        @Test
        public void testFirstWantWithOptions() throws PackProtocolException {
                String line = "want b9d4d1eb2f93058814480eae9e1b67550f46ee38 "
-                               + "no-progress include-tag ofs-delta agent=JGit/unknown";
+                               + "no-progress include-tag ofs-delta agent=JGit/unknown "
+                               + "session-id=the.client.sid";
 
                FirstWant r = FirstWant.fromLine(line);
                assertEquals("want b9d4d1eb2f93058814480eae9e1b67550f46ee38",
@@ -37,6 +38,7 @@ public class FirstWantTest {
                                Arrays.asList("no-progress", "include-tag", "ofs-delta"));
                assertEquals(expectedCapabilities, capabilities);
                assertEquals("JGit/unknown", r.getAgent());
+               assertEquals("the.client.sid", r.getClientSID());
        }
 
        @Test
@@ -94,4 +96,12 @@ public class FirstWantTest {
                assertEquals(r.getCapabilities().size(), 0);
                assertEquals("pack.age/Version", r.getAgent());
        }
+
+       @Test
+       public void testFirstWantValidSessionID() throws PackProtocolException {
+               FirstWant r = FirstWant
+                               .fromLine(makeFirstWantLine("session-id=client.session.id"));
+               assertEquals(r.getCapabilities().size(), 0);
+               assertEquals("client.session.id", r.getClientSID());
+       }
 }
index 8138f0645287f3e5e3371376bf1f3ff6cfbb6852..30d629665ff48c3c05023e2737f1faa5116f83e9 100644 (file)
@@ -10,6 +10,7 @@
 package org.eclipse.jgit.internal.transport.parser;
 
 import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_AGENT;
+import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_SESSION_ID;
 
 import java.util.Collections;
 import java.util.HashSet;
@@ -43,8 +44,13 @@ public class FirstWant {
        @Nullable
        private final String agent;
 
+       @Nullable
+       private final String clientSID;
+
        private static final String AGENT_PREFIX = OPTION_AGENT + '=';
 
+       private static final String SESSION_ID_PREFIX = OPTION_SESSION_ID + '=';
+
        /**
         * Parse the first want line in the protocol v0/v1 pack negotiation.
         *
@@ -58,6 +64,7 @@ public class FirstWant {
                String wantLine;
                Set<String> capabilities;
                String agent = null;
+               String clientSID = null;
 
                if (line.length() > 45) {
                        String opt = line.substring(45);
@@ -70,6 +77,9 @@ public class FirstWant {
                        for (String clientCapability : opt.split(" ")) { //$NON-NLS-1$
                                if (clientCapability.startsWith(AGENT_PREFIX)) {
                                        agent = clientCapability.substring(AGENT_PREFIX.length());
+                               } else if (clientCapability.startsWith(SESSION_ID_PREFIX)) {
+                                       clientSID = clientCapability
+                                                       .substring(SESSION_ID_PREFIX.length());
                                } else {
                                        opts.add(clientCapability);
                                }
@@ -81,14 +91,15 @@ public class FirstWant {
                        capabilities = Collections.emptySet();
                }
 
-               return new FirstWant(wantLine, capabilities, agent);
+               return new FirstWant(wantLine, capabilities, agent, clientSID);
        }
 
        private FirstWant(String line, Set<String> capabilities,
-                       @Nullable String agent) {
+                       @Nullable String agent, @Nullable String clientSID) {
                this.line = line;
                this.capabilities = capabilities;
                this.agent = agent;
+               this.clientSID = clientSID;
        }
 
        /** @return non-capabilities part of the line. */
@@ -98,7 +109,7 @@ public class FirstWant {
 
        /**
         * @return capabilities parsed from the line as an immutable set (excluding
-        *         agent).
+        *         agent and session-id).
         */
        public Set<String> getCapabilities() {
                return capabilities;
@@ -109,4 +120,10 @@ public class FirstWant {
        public String getAgent() {
                return agent;
        }
+
+       /** @return client session-id parsed from the line. */
+       @Nullable
+       public String getClientSID() {
+               return clientSID;
+       }
 }