View Javadoc
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.internal.transport.parser;
44  
45  import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_AGENT;
46  
47  import java.util.Collections;
48  import java.util.HashSet;
49  import java.util.Set;
50  
51  import org.eclipse.jgit.annotations.Nullable;
52  import org.eclipse.jgit.errors.PackProtocolException;
53  import org.eclipse.jgit.internal.JGitText;
54  
55  /**
56   * In the pack negotiation phase (protocol v0/v1), the client sends a list of
57   * wants. The first "want" line is special, as it (can) have a list of
58   * capabilities appended.
59   *
60   * E.g. "want oid cap1 cap2 cap3"
61   *
62   * Do not confuse this line with the first one in the reference advertisement,
63   * which is sent by the server, looks like
64   * "b8f7c471373b8583ced0025cfad8c9916c484b76 HEAD\0 cap1 cap2 cap3" and is
65   * parsed by the BasePackConnection.readAdvertisedRefs method.
66   *
67   * This class parses the input want line and holds the results: the actual want
68   * line and the capabilities.
69   *
70   */
71  public class FirstWant {
72  	private final String line;
73  
74  	private final Set<String> capabilities;
75  
76  	@Nullable
77  	private final String agent;
78  
79  	private static final String AGENT_PREFIX = OPTION_AGENT + '=';
80  
81  	/**
82  	 * Parse the first want line in the protocol v0/v1 pack negotiation.
83  	 *
84  	 * @param line
85  	 *            line from the client.
86  	 * @return an instance of FirstWant
87  	 * @throws PackProtocolException
88  	 *             if the line doesn't follow the protocol format.
89  	 */
90  	public static FirstWant fromLine(String line) throws PackProtocolException {
91  		String wantLine;
92  		Set<String> capabilities;
93  		String agent = null;
94  
95  		if (line.length() > 45) {
96  			String opt = line.substring(45);
97  			if (!opt.startsWith(" ")) { //$NON-NLS-1$
98  				throw new PackProtocolException(JGitText.get().wantNoSpaceWithCapabilities);
99  			}
100 			opt = opt.substring(1);
101 
102 			HashSet<String> opts = new HashSet<>();
103 			for (String clientCapability : opt.split(" ")) { //$NON-NLS-1$
104 				if (clientCapability.startsWith(AGENT_PREFIX)) {
105 					agent = clientCapability.substring(AGENT_PREFIX.length());
106 				} else {
107 					opts.add(clientCapability);
108 				}
109 			}
110 			wantLine = line.substring(0, 45);
111 			capabilities = Collections.unmodifiableSet(opts);
112 		} else {
113 			wantLine = line;
114 			capabilities = Collections.emptySet();
115 		}
116 
117 		return new FirstWant(wantLine, capabilities, agent);
118 	}
119 
120 	private FirstWant(String line, Set<String> capabilities,
121 			@Nullable String agent) {
122 		this.line = line;
123 		this.capabilities = capabilities;
124 		this.agent = agent;
125 	}
126 
127 	/** @return non-capabilities part of the line. */
128 	public String getLine() {
129 		return line;
130 	}
131 
132 	/**
133 	 * @return capabilities parsed from the line as an immutable set (excluding
134 	 *         agent).
135 	 */
136 	public Set<String> getCapabilities() {
137 		return capabilities;
138 	}
139 
140 	/** @return client user agent parsed from the line. */
141 	@Nullable
142 	public String getAgent() {
143 		return agent;
144 	}
145 }