View Javadoc
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.internal.transport.parser;
11  
12  import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_AGENT;
13  
14  import java.util.Collections;
15  import java.util.HashSet;
16  import java.util.Set;
17  
18  import org.eclipse.jgit.annotations.Nullable;
19  import org.eclipse.jgit.errors.PackProtocolException;
20  import org.eclipse.jgit.internal.JGitText;
21  
22  /**
23   * In the pack negotiation phase (protocol v0/v1), the client sends a list of
24   * wants. The first "want" line is special, as it (can) have a list of
25   * capabilities appended.
26   *
27   * E.g. "want oid cap1 cap2 cap3"
28   *
29   * Do not confuse this line with the first one in the reference advertisement,
30   * which is sent by the server, looks like
31   * "b8f7c471373b8583ced0025cfad8c9916c484b76 HEAD\0 cap1 cap2 cap3" and is
32   * parsed by the BasePackConnection.readAdvertisedRefs method.
33   *
34   * This class parses the input want line and holds the results: the actual want
35   * line and the capabilities.
36   *
37   */
38  public class FirstWant {
39  	private final String line;
40  
41  	private final Set<String> capabilities;
42  
43  	@Nullable
44  	private final String agent;
45  
46  	private static final String AGENT_PREFIX = OPTION_AGENT + '=';
47  
48  	/**
49  	 * Parse the first want line in the protocol v0/v1 pack negotiation.
50  	 *
51  	 * @param line
52  	 *            line from the client.
53  	 * @return an instance of FirstWant
54  	 * @throws PackProtocolException
55  	 *             if the line doesn't follow the protocol format.
56  	 */
57  	public static FirstWant fromLine(String line) throws PackProtocolException {
58  		String wantLine;
59  		Set<String> capabilities;
60  		String agent = null;
61  
62  		if (line.length() > 45) {
63  			String opt = line.substring(45);
64  			if (!opt.startsWith(" ")) { //$NON-NLS-1$
65  				throw new PackProtocolException(JGitText.get().wantNoSpaceWithCapabilities);
66  			}
67  			opt = opt.substring(1);
68  
69  			HashSet<String> opts = new HashSet<>();
70  			for (String clientCapability : opt.split(" ")) { //$NON-NLS-1$
71  				if (clientCapability.startsWith(AGENT_PREFIX)) {
72  					agent = clientCapability.substring(AGENT_PREFIX.length());
73  				} else {
74  					opts.add(clientCapability);
75  				}
76  			}
77  			wantLine = line.substring(0, 45);
78  			capabilities = Collections.unmodifiableSet(opts);
79  		} else {
80  			wantLine = line;
81  			capabilities = Collections.emptySet();
82  		}
83  
84  		return new FirstWant(wantLine, capabilities, agent);
85  	}
86  
87  	private FirstWant(String line, Set<String> capabilities,
88  			@Nullable String agent) {
89  		this.line = line;
90  		this.capabilities = capabilities;
91  		this.agent = agent;
92  	}
93  
94  	/** @return non-capabilities part of the line. */
95  	public String getLine() {
96  		return line;
97  	}
98  
99  	/**
100 	 * @return capabilities parsed from the line as an immutable set (excluding
101 	 *         agent).
102 	 */
103 	public Set<String> getCapabilities() {
104 		return capabilities;
105 	}
106 
107 	/** @return client user agent parsed from the line. */
108 	@Nullable
109 	public String getAgent() {
110 		return agent;
111 	}
112 }