1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 package org.eclipse.jgit.transport;
47
48 import java.io.BufferedInputStream;
49 import java.io.BufferedOutputStream;
50 import java.io.IOException;
51 import java.io.InputStream;
52 import java.io.OutputStream;
53 import java.net.ConnectException;
54 import java.net.InetAddress;
55 import java.net.InetSocketAddress;
56 import java.net.Socket;
57 import java.net.UnknownHostException;
58 import java.util.Collections;
59 import java.util.EnumSet;
60 import java.util.Set;
61
62 import org.eclipse.jgit.errors.NotSupportedException;
63 import org.eclipse.jgit.errors.TransportException;
64 import org.eclipse.jgit.internal.JGitText;
65 import org.eclipse.jgit.lib.Repository;
66
67
68
69
70
71
72
73
74 class TransportGitAnon extends TcpTransport implements PackTransport {
75 static final int GIT_PORT = Daemon.DEFAULT_PORT;
76
77 static final TransportProtocol PROTO_GIT = new TransportProtocol() {
78 @Override
79 public String getName() {
80 return JGitText.get().transportProtoGitAnon;
81 }
82
83 @Override
84 public Set<String> getSchemes() {
85 return Collections.singleton("git");
86 }
87
88 @Override
89 public Set<URIishField> getRequiredFields() {
90 return Collections.unmodifiableSet(EnumSet.of(URIishField.HOST,
91 URIishField.PATH));
92 }
93
94 @Override
95 public Set<URIishField> getOptionalFields() {
96 return Collections.unmodifiableSet(EnumSet.of(URIishField.PORT));
97 }
98
99 @Override
100 public int getDefaultPort() {
101 return GIT_PORT;
102 }
103
104 @Override
105 public Transport open(URIish uri, Repository local, String remoteName)
106 throws NotSupportedException {
107 return new TransportGitAnon(local, uri);
108 }
109
110 @Override
111 public Transport open(URIish uri) throws NotSupportedException, TransportException {
112 return new TransportGitAnon(uri);
113 }
114 };
115
116 TransportGitAnon(final Repository local, final URIish uri) {
117 super(local, uri);
118 }
119
120 TransportGitAnon(final URIish uri) {
121 super(uri);
122 }
123
124
125 @Override
126 public FetchConnection openFetch() throws TransportException {
127 return new TcpFetchConnection();
128 }
129
130
131 @Override
132 public PushConnection openPush() throws TransportException {
133 return new TcpPushConnection();
134 }
135
136
137 @Override
138 public void close() {
139
140 }
141
142 Socket openConnection() throws TransportException {
143 final int tms = getTimeout() > 0 ? getTimeout() * 1000 : 0;
144 final int port = uri.getPort() > 0 ? uri.getPort() : GIT_PORT;
145 final Socket s = new Socket();
146 try {
147 final InetAddress host = InetAddress.getByName(uri.getHost());
148 s.bind(null);
149 s.connect(new InetSocketAddress(host, port), tms);
150 } catch (IOException c) {
151 try {
152 s.close();
153 } catch (IOException closeErr) {
154
155 }
156 if (c instanceof UnknownHostException)
157 throw new TransportException(uri, JGitText.get().unknownHost);
158 if (c instanceof ConnectException)
159 throw new TransportException(uri, c.getMessage());
160 throw new TransportException(uri, c.getMessage(), c);
161 }
162 return s;
163 }
164
165 void service(final String name, final PacketLineOut pckOut)
166 throws IOException {
167 final StringBuilder cmd = new StringBuilder();
168 cmd.append(name);
169 cmd.append(' ');
170 cmd.append(uri.getPath());
171 cmd.append('\0');
172 cmd.append("host=");
173 cmd.append(uri.getHost());
174 if (uri.getPort() > 0 && uri.getPort() != GIT_PORT) {
175 cmd.append(":");
176 cmd.append(uri.getPort());
177 }
178 cmd.append('\0');
179 pckOut.writeString(cmd.toString());
180 pckOut.flush();
181 }
182
183 class TcpFetchConnection extends BasePackFetchConnection {
184 private Socket sock;
185
186 TcpFetchConnection() throws TransportException {
187 super(TransportGitAnon.this);
188 sock = openConnection();
189 try {
190 InputStream sIn = sock.getInputStream();
191 OutputStream sOut = sock.getOutputStream();
192
193 sIn = new BufferedInputStream(sIn);
194 sOut = new BufferedOutputStream(sOut);
195
196 init(sIn, sOut);
197 service("git-upload-pack", pckOut);
198 } catch (IOException err) {
199 close();
200 throw new TransportException(uri,
201 JGitText.get().remoteHungUpUnexpectedly, err);
202 }
203 readAdvertisedRefs();
204 }
205
206 @Override
207 public void close() {
208 super.close();
209
210 if (sock != null) {
211 try {
212 sock.close();
213 } catch (IOException err) {
214
215 } finally {
216 sock = null;
217 }
218 }
219 }
220 }
221
222 class TcpPushConnection extends BasePackPushConnection {
223 private Socket sock;
224
225 TcpPushConnection() throws TransportException {
226 super(TransportGitAnon.this);
227 sock = openConnection();
228 try {
229 InputStream sIn = sock.getInputStream();
230 OutputStream sOut = sock.getOutputStream();
231
232 sIn = new BufferedInputStream(sIn);
233 sOut = new BufferedOutputStream(sOut);
234
235 init(sIn, sOut);
236 service("git-receive-pack", pckOut);
237 } catch (IOException err) {
238 close();
239 throw new TransportException(uri,
240 JGitText.get().remoteHungUpUnexpectedly, err);
241 }
242 readAdvertisedRefs();
243 }
244
245 @Override
246 public void close() {
247 super.close();
248
249 if (sock != null) {
250 try {
251 sock.close();
252 } catch (IOException err) {
253
254 } finally {
255 sock = null;
256 }
257 }
258 }
259 }
260 }