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.transport;
44  
45  import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_DEEPEN_RELATIVE;
46  import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_FILTER;
47  import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_INCLUDE_TAG;
48  import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_NO_PROGRESS;
49  import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_OFS_DELTA;
50  import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_SIDE_BAND_64K;
51  import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_THIN_PACK;
52  import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_WANT_REF;
53  
54  import java.io.IOException;
55  import java.text.MessageFormat;
56  
57  import org.eclipse.jgit.errors.PackProtocolException;
58  import org.eclipse.jgit.internal.JGitText;
59  import org.eclipse.jgit.lib.ObjectId;
60  
61  /**
62   * Parse the incoming git protocol lines from the wire and translate them into a
63   * Request object.
64   *
65   * It requires a transferConfig object to know what the server supports (e.g.
66   * ref-in-want and/or filters).
67   */
68  final class ProtocolV2Parser {
69  
70  	private final TransferConfig transferConfig;
71  
72  	ProtocolV2Parser(TransferConfig transferConfig) {
73  		this.transferConfig = transferConfig;
74  	}
75  
76  	/**
77  	 * Parse the incoming fetch request arguments from the wire. The caller must
78  	 * be sure that what is comings is a fetch request before coming here.
79  	 *
80  	 * @param pckIn
81  	 *            incoming lines
82  	 * @return A FetchV2Request populated with information received from the
83  	 *         wire.
84  	 * @throws PackProtocolException
85  	 *             incompatible options, wrong type of arguments or other issues
86  	 *             where the request breaks the protocol.
87  	 * @throws IOException
88  	 *             an IO error prevented reading the incoming message.
89  	 */
90  	FetchV2Request parseFetchRequest(PacketLineIn pckIn)
91  			throws PackProtocolException, IOException {
92  		FetchV2Request.Builder reqBuilder = FetchV2Request.builder();
93  
94  		// Packs are always sent multiplexed and using full 64K
95  		// lengths.
96  		reqBuilder.addOption(OPTION_SIDE_BAND_64K);
97  
98  		String line;
99  
100 		// Currently, we do not support any capabilities, so the next
101 		// line is DELIM.
102 		if ((line = pckIn.readString()) != PacketLineIn.DELIM) {
103 			throw new PackProtocolException(MessageFormat
104 					.format(JGitText.get().unexpectedPacketLine, line));
105 		}
106 
107 		boolean filterReceived = false;
108 		while ((line = pckIn.readString()) != PacketLineIn.END) {
109 			if (line.startsWith("want ")) { //$NON-NLS-1$
110 				reqBuilder.addWantsId(ObjectId.fromString(line.substring(5)));
111 			} else if (transferConfig.isAllowRefInWant()
112 					&& line.startsWith(OPTION_WANT_REF + " ")) { //$NON-NLS-1$
113 				reqBuilder.addWantedRef(line.substring(OPTION_WANT_REF.length() + 1));
114 			} else if (line.startsWith("have ")) { //$NON-NLS-1$
115 				reqBuilder.addPeerHas(ObjectId.fromString(line.substring(5)));
116 			} else if (line.equals("done")) { //$NON-NLS-1$
117 				reqBuilder.setDoneReceived();
118 			} else if (line.equals(OPTION_THIN_PACK)) {
119 				reqBuilder.addOption(OPTION_THIN_PACK);
120 			} else if (line.equals(OPTION_NO_PROGRESS)) {
121 				reqBuilder.addOption(OPTION_NO_PROGRESS);
122 			} else if (line.equals(OPTION_INCLUDE_TAG)) {
123 				reqBuilder.addOption(OPTION_INCLUDE_TAG);
124 			} else if (line.equals(OPTION_OFS_DELTA)) {
125 				reqBuilder.addOption(OPTION_OFS_DELTA);
126 			} else if (line.startsWith("shallow ")) { //$NON-NLS-1$
127 				reqBuilder.addClientShallowCommit(
128 						ObjectId.fromString(line.substring(8)));
129 			} else if (line.startsWith("deepen ")) { //$NON-NLS-1$
130 				int parsedDepth = Integer.parseInt(line.substring(7));
131 				if (parsedDepth <= 0) {
132 					throw new PackProtocolException(
133 							MessageFormat.format(JGitText.get().invalidDepth,
134 									Integer.valueOf(parsedDepth)));
135 				}
136 				if (reqBuilder.getDeepenSince() != 0) {
137 					throw new PackProtocolException(
138 							JGitText.get().deepenSinceWithDeepen);
139 				}
140 				if (reqBuilder.hasDeepenNotRefs()) {
141 					throw new PackProtocolException(
142 							JGitText.get().deepenNotWithDeepen);
143 				}
144 				reqBuilder.setDepth(parsedDepth);
145 			} else if (line.startsWith("deepen-not ")) { //$NON-NLS-1$
146 				reqBuilder.addDeepenNotRef(line.substring(11));
147 				if (reqBuilder.getDepth() != 0) {
148 					throw new PackProtocolException(
149 							JGitText.get().deepenNotWithDeepen);
150 				}
151 			} else if (line.equals(OPTION_DEEPEN_RELATIVE)) {
152 				reqBuilder.addOption(OPTION_DEEPEN_RELATIVE);
153 			} else if (line.startsWith("deepen-since ")) { //$NON-NLS-1$
154 				int ts = Integer.parseInt(line.substring(13));
155 				if (ts <= 0) {
156 					throw new PackProtocolException(MessageFormat
157 							.format(JGitText.get().invalidTimestamp, line));
158 				}
159 				if (reqBuilder.getDepth() != 0) {
160 					throw new PackProtocolException(
161 							JGitText.get().deepenSinceWithDeepen);
162 				}
163 				reqBuilder.setDeepenSince(ts);
164 			} else if (transferConfig.isAllowFilter()
165 					&& line.startsWith(OPTION_FILTER + ' ')) {
166 				if (filterReceived) {
167 					throw new PackProtocolException(
168 							JGitText.get().tooManyFilters);
169 				}
170 				filterReceived = true;
171 				reqBuilder.setFilterBlobLimit(filterLine(
172 						line.substring(OPTION_FILTER.length() + 1)));
173 			} else {
174 				throw new PackProtocolException(MessageFormat
175 						.format(JGitText.get().unexpectedPacketLine, line));
176 			}
177 		}
178 
179 		return reqBuilder.build();
180 	}
181 
182 	/**
183 	 * Process the content of "filter" line from the protocol. It has a shape
184 	 * like "blob:none" or "blob:limit=N", with limit a positive number.
185 	 *
186 	 * @param blobLine
187 	 *            the content of the "filter" line in the protocol
188 	 * @return N, the limit, defaulting to 0 if "none"
189 	 * @throws PackProtocolException
190 	 *             invalid filter because due to unrecognized format or
191 	 *             negative/non-numeric filter.
192 	 */
193 	static long filterLine(String blobLine) throws PackProtocolException {
194 		long blobLimit = -1;
195 
196 		if (blobLine.equals("blob:none")) { //$NON-NLS-1$
197 			blobLimit = 0;
198 		} else if (blobLine.startsWith("blob:limit=")) { //$NON-NLS-1$
199 			try {
200 				blobLimit = Long
201 						.parseLong(blobLine.substring("blob:limit=".length())); //$NON-NLS-1$
202 			} catch (NumberFormatException e) {
203 				throw new PackProtocolException(MessageFormat
204 						.format(JGitText.get().invalidFilter, blobLine));
205 			}
206 		}
207 		/*
208 		 * We must have (1) either "blob:none" or "blob:limit=" set (because we
209 		 * only support blob size limits for now), and (2) if the latter, then
210 		 * it must be nonnegative. Throw if (1) or (2) is not met.
211 		 */
212 		if (blobLimit < 0) {
213 			throw new PackProtocolException(
214 					MessageFormat.format(JGitText.get().invalidFilter, blobLine));
215 		}
216 
217 		return blobLimit;
218 	}
219 
220 }