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 @Override
125 public FetchConnection openFetch() throws TransportException {
126 return new TcpFetchConnection();
127 }
128
129 @Override
130 public PushConnection openPush() throws TransportException {
131 return new TcpPushConnection();
132 }
133
134 @Override
135 public void close() {
136
137 }
138
139 Socket openConnection() throws TransportException {
140 final int tms = getTimeout() > 0 ? getTimeout() * 1000 : 0;
141 final int port = uri.getPort() > 0 ? uri.getPort() : GIT_PORT;
142 final Socket s = new Socket();
143 try {
144 final InetAddress host = InetAddress.getByName(uri.getHost());
145 s.bind(null);
146 s.connect(new InetSocketAddress(host, port), tms);
147 } catch (IOException c) {
148 try {
149 s.close();
150 } catch (IOException closeErr) {
151
152 }
153 if (c instanceof UnknownHostException)
154 throw new TransportException(uri, JGitText.get().unknownHost);
155 if (c instanceof ConnectException)
156 throw new TransportException(uri, c.getMessage());
157 throw new TransportException(uri, c.getMessage(), c);
158 }
159 return s;
160 }
161
162 void service(final String name, final PacketLineOut pckOut)
163 throws IOException {
164 final StringBuilder cmd = new StringBuilder();
165 cmd.append(name);
166 cmd.append(' ');
167 cmd.append(uri.getPath());
168 cmd.append('\0');
169 cmd.append("host=");
170 cmd.append(uri.getHost());
171 if (uri.getPort() > 0 && uri.getPort() != GIT_PORT) {
172 cmd.append(":");
173 cmd.append(uri.getPort());
174 }
175 cmd.append('\0');
176 pckOut.writeString(cmd.toString());
177 pckOut.flush();
178 }
179
180 class TcpFetchConnection extends BasePackFetchConnection {
181 private Socket sock;
182
183 TcpFetchConnection() throws TransportException {
184 super(TransportGitAnon.this);
185 sock = openConnection();
186 try {
187 InputStream sIn = sock.getInputStream();
188 OutputStream sOut = sock.getOutputStream();
189
190 sIn = new BufferedInputStream(sIn);
191 sOut = new BufferedOutputStream(sOut);
192
193 init(sIn, sOut);
194 service("git-upload-pack", pckOut);
195 } catch (IOException err) {
196 close();
197 throw new TransportException(uri,
198 JGitText.get().remoteHungUpUnexpectedly, err);
199 }
200 readAdvertisedRefs();
201 }
202
203 @Override
204 public void close() {
205 super.close();
206
207 if (sock != null) {
208 try {
209 sock.close();
210 } catch (IOException err) {
211
212 } finally {
213 sock = null;
214 }
215 }
216 }
217 }
218
219 class TcpPushConnection extends BasePackPushConnection {
220 private Socket sock;
221
222 TcpPushConnection() throws TransportException {
223 super(TransportGitAnon.this);
224 sock = openConnection();
225 try {
226 InputStream sIn = sock.getInputStream();
227 OutputStream sOut = sock.getOutputStream();
228
229 sIn = new BufferedInputStream(sIn);
230 sOut = new BufferedOutputStream(sOut);
231
232 init(sIn, sOut);
233 service("git-receive-pack", pckOut);
234 } catch (IOException err) {
235 close();
236 throw new TransportException(uri,
237 JGitText.get().remoteHungUpUnexpectedly, err);
238 }
239 readAdvertisedRefs();
240 }
241
242 @Override
243 public void close() {
244 super.close();
245
246 if (sock != null) {
247 try {
248 sock.close();
249 } catch (IOException err) {
250
251 } finally {
252 sock = null;
253 }
254 }
255 }
256 }
257 }