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 public String getName() {
79 return JGitText.get().transportProtoGitAnon;
80 }
81
82 public Set<String> getSchemes() {
83 return Collections.singleton("git");
84 }
85
86 public Set<URIishField> getRequiredFields() {
87 return Collections.unmodifiableSet(EnumSet.of(URIishField.HOST,
88 URIishField.PATH));
89 }
90
91 public Set<URIishField> getOptionalFields() {
92 return Collections.unmodifiableSet(EnumSet.of(URIishField.PORT));
93 }
94
95 public int getDefaultPort() {
96 return GIT_PORT;
97 }
98
99 public Transport open(URIish uri, Repository local, String remoteName)
100 throws NotSupportedException {
101 return new TransportGitAnon(local, uri);
102 }
103
104 @Override
105 public Transport open(URIish uri) throws NotSupportedException, TransportException {
106 return new TransportGitAnon(uri);
107 }
108 };
109
110 TransportGitAnon(final Repository local, final URIish uri) {
111 super(local, uri);
112 }
113
114 TransportGitAnon(final URIish uri) {
115 super(uri);
116 }
117
118 @Override
119 public FetchConnection openFetch() throws TransportException {
120 return new TcpFetchConnection();
121 }
122
123 @Override
124 public PushConnection openPush() throws TransportException {
125 return new TcpPushConnection();
126 }
127
128 @Override
129 public void close() {
130
131 }
132
133 Socket openConnection() throws TransportException {
134 final int tms = getTimeout() > 0 ? getTimeout() * 1000 : 0;
135 final int port = uri.getPort() > 0 ? uri.getPort() : GIT_PORT;
136 final Socket s = new Socket();
137 try {
138 final InetAddress host = InetAddress.getByName(uri.getHost());
139 s.bind(null);
140 s.connect(new InetSocketAddress(host, port), tms);
141 } catch (IOException c) {
142 try {
143 s.close();
144 } catch (IOException closeErr) {
145
146 }
147 if (c instanceof UnknownHostException)
148 throw new TransportException(uri, JGitText.get().unknownHost);
149 if (c instanceof ConnectException)
150 throw new TransportException(uri, c.getMessage());
151 throw new TransportException(uri, c.getMessage(), c);
152 }
153 return s;
154 }
155
156 void service(final String name, final PacketLineOut pckOut)
157 throws IOException {
158 final StringBuilder cmd = new StringBuilder();
159 cmd.append(name);
160 cmd.append(' ');
161 cmd.append(uri.getPath());
162 cmd.append('\0');
163 cmd.append("host=");
164 cmd.append(uri.getHost());
165 if (uri.getPort() > 0 && uri.getPort() != GIT_PORT) {
166 cmd.append(":");
167 cmd.append(uri.getPort());
168 }
169 cmd.append('\0');
170 pckOut.writeString(cmd.toString());
171 pckOut.flush();
172 }
173
174 class TcpFetchConnection extends BasePackFetchConnection {
175 private Socket sock;
176
177 TcpFetchConnection() throws TransportException {
178 super(TransportGitAnon.this);
179 sock = openConnection();
180 try {
181 InputStream sIn = sock.getInputStream();
182 OutputStream sOut = sock.getOutputStream();
183
184 sIn = new BufferedInputStream(sIn);
185 sOut = new BufferedOutputStream(sOut);
186
187 init(sIn, sOut);
188 service("git-upload-pack", pckOut);
189 } catch (IOException err) {
190 close();
191 throw new TransportException(uri,
192 JGitText.get().remoteHungUpUnexpectedly, err);
193 }
194 readAdvertisedRefs();
195 }
196
197 @Override
198 public void close() {
199 super.close();
200
201 if (sock != null) {
202 try {
203 sock.close();
204 } catch (IOException err) {
205
206 } finally {
207 sock = null;
208 }
209 }
210 }
211 }
212
213 class TcpPushConnection extends BasePackPushConnection {
214 private Socket sock;
215
216 TcpPushConnection() throws TransportException {
217 super(TransportGitAnon.this);
218 sock = openConnection();
219 try {
220 InputStream sIn = sock.getInputStream();
221 OutputStream sOut = sock.getOutputStream();
222
223 sIn = new BufferedInputStream(sIn);
224 sOut = new BufferedOutputStream(sOut);
225
226 init(sIn, sOut);
227 service("git-receive-pack", pckOut);
228 } catch (IOException err) {
229 close();
230 throw new TransportException(uri,
231 JGitText.get().remoteHungUpUnexpectedly, err);
232 }
233 readAdvertisedRefs();
234 }
235
236 @Override
237 public void close() {
238 super.close();
239
240 if (sock != null) {
241 try {
242 sock.close();
243 } catch (IOException err) {
244
245 } finally {
246 sock = null;
247 }
248 }
249 }
250 }
251 }