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.transport;
11  
12  import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_FILTER;
13  
14  import java.io.EOFException;
15  import java.io.IOException;
16  import java.text.MessageFormat;
17  
18  import org.eclipse.jgit.errors.PackProtocolException;
19  import org.eclipse.jgit.internal.JGitText;
20  import org.eclipse.jgit.internal.transport.parser.FirstWant;
21  import org.eclipse.jgit.lib.ObjectId;
22  
23  /**
24   * Parser for git protocol versions 0 and 1.
25   *
26   * It reads the lines coming through the {@link PacketLineIn} and builds a
27   * {@link FetchV0Request} object.
28   *
29   * It requires a transferConfig object to know if the server supports filters.
30   */
31  final class ProtocolV0Parser {
32  
33  	private final TransferConfig transferConfig;
34  
35  	ProtocolV0Parser(TransferConfig transferConfig) {
36  		this.transferConfig = transferConfig;
37  	}
38  
39  	/**
40  	 * Parse an incoming protocol v1 upload request arguments from the wire.
41  	 *
42  	 * The incoming PacketLineIn is consumed until an END line, but the caller
43  	 * is responsible for closing it (if needed).
44  	 *
45  	 * @param pckIn
46  	 *            incoming lines. This method will read until an END line.
47  	 * @return a FetchV0Request with the data received in the wire.
48  	 * @throws PackProtocolException
49  	 * @throws IOException
50  	 */
51  	FetchV0Request recvWants(PacketLineIn pckIn)
52  			throws PackProtocolException, IOException {
53  		FetchV0Request.Builder reqBuilder = new FetchV0Request.Builder();
54  
55  		boolean isFirst = true;
56  		boolean filterReceived = false;
57  
58  		for (;;) {
59  			String line;
60  			try {
61  				line = pckIn.readString();
62  			} catch (EOFException eof) {
63  				if (isFirst) {
64  					break;
65  				}
66  				throw eof;
67  			}
68  
69  			if (PacketLineIn.isEnd(line)) {
70  				break;
71  			}
72  
73  			if (line.startsWith("deepen ")) { //$NON-NLS-1$
74  				int depth = Integer.parseInt(line.substring(7));
75  				if (depth <= 0) {
76  					throw new PackProtocolException(
77  							MessageFormat.format(JGitText.get().invalidDepth,
78  									Integer.valueOf(depth)));
79  				}
80  				reqBuilder.setDepth(depth);
81  				continue;
82  			}
83  
84  			if (line.startsWith("shallow ")) { //$NON-NLS-1$
85  				reqBuilder.addClientShallowCommit(
86  						ObjectId.fromString(line.substring(8)));
87  				continue;
88  			}
89  
90  			if (transferConfig.isAllowFilter()
91  					&& line.startsWith(OPTION_FILTER + " ")) { //$NON-NLS-1$
92  				String arg = line.substring(OPTION_FILTER.length() + 1);
93  
94  				if (filterReceived) {
95  					throw new PackProtocolException(
96  							JGitText.get().tooManyFilters);
97  				}
98  				filterReceived = true;
99  
100 				reqBuilder.setFilterSpec(FilterSpec.fromFilterLine(arg));
101 				continue;
102 			}
103 
104 			if (!line.startsWith("want ") || line.length() < 45) { //$NON-NLS-1$
105 				throw new PackProtocolException(MessageFormat
106 						.format(JGitText.get().expectedGot, "want", line)); //$NON-NLS-1$
107 			}
108 
109 			if (isFirst) {
110 				if (line.length() > 45) {
111 					FirstWant firstLine = FirstWant.fromLine(line);
112 					reqBuilder.addClientCapabilities(firstLine.getCapabilities());
113 					reqBuilder.setAgent(firstLine.getAgent());
114 					line = firstLine.getLine();
115 				}
116 			}
117 
118 			reqBuilder.addWantId(ObjectId.fromString(line.substring(5)));
119 			isFirst = false;
120 		}
121 
122 		return reqBuilder.build();
123 	}
124 
125 }