You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TransportGitAnon.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
  3. * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  4. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  5. * and other copyright owners as documented in the project's IP log.
  6. *
  7. * This program and the accompanying materials are made available
  8. * under the terms of the Eclipse Distribution License v1.0 which
  9. * accompanies this distribution, is reproduced below, and is
  10. * available at http://www.eclipse.org/org/documents/edl-v10.php
  11. *
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or
  15. * without modification, are permitted provided that the following
  16. * conditions are met:
  17. *
  18. * - Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * - Neither the name of the Eclipse Foundation, Inc. nor the
  27. * names of its contributors may be used to endorse or promote
  28. * products derived from this software without specific prior
  29. * written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  32. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  33. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  34. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  36. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  37. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  38. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  39. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  40. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  41. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  42. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  43. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. */
  45. package org.eclipse.jgit.transport;
  46. import java.io.BufferedInputStream;
  47. import java.io.IOException;
  48. import java.io.InputStream;
  49. import java.io.OutputStream;
  50. import java.net.ConnectException;
  51. import java.net.InetAddress;
  52. import java.net.InetSocketAddress;
  53. import java.net.Socket;
  54. import java.net.UnknownHostException;
  55. import java.util.Collections;
  56. import java.util.EnumSet;
  57. import java.util.Set;
  58. import org.eclipse.jgit.errors.NotSupportedException;
  59. import org.eclipse.jgit.errors.TransportException;
  60. import org.eclipse.jgit.internal.JGitText;
  61. import org.eclipse.jgit.lib.Repository;
  62. import org.eclipse.jgit.util.io.SafeBufferedOutputStream;
  63. /**
  64. * Transport through a git-daemon waiting for anonymous TCP connections.
  65. * <p>
  66. * This transport supports the <code>git://</code> protocol, usually run on
  67. * the IANA registered port 9418. It is a popular means for distributing open
  68. * source projects, as there are no authentication or authorization overheads.
  69. */
  70. class TransportGitAnon extends TcpTransport implements PackTransport {
  71. static final int GIT_PORT = Daemon.DEFAULT_PORT;
  72. static final TransportProtocol PROTO_GIT = new TransportProtocol() {
  73. public String getName() {
  74. return JGitText.get().transportProtoGitAnon;
  75. }
  76. public Set<String> getSchemes() {
  77. return Collections.singleton("git"); //$NON-NLS-1$
  78. }
  79. public Set<URIishField> getRequiredFields() {
  80. return Collections.unmodifiableSet(EnumSet.of(URIishField.HOST,
  81. URIishField.PATH));
  82. }
  83. public Set<URIishField> getOptionalFields() {
  84. return Collections.unmodifiableSet(EnumSet.of(URIishField.PORT));
  85. }
  86. public int getDefaultPort() {
  87. return GIT_PORT;
  88. }
  89. public Transport open(URIish uri, Repository local, String remoteName)
  90. throws NotSupportedException {
  91. return new TransportGitAnon(local, uri);
  92. }
  93. @Override
  94. public Transport open(URIish uri) throws NotSupportedException, TransportException {
  95. return new TransportGitAnon(uri);
  96. }
  97. };
  98. TransportGitAnon(final Repository local, final URIish uri) {
  99. super(local, uri);
  100. }
  101. TransportGitAnon(final URIish uri) {
  102. super(uri);
  103. }
  104. @Override
  105. public FetchConnection openFetch() throws TransportException {
  106. return new TcpFetchConnection();
  107. }
  108. @Override
  109. public PushConnection openPush() throws TransportException {
  110. return new TcpPushConnection();
  111. }
  112. @Override
  113. public void close() {
  114. // Resources must be established per-connection.
  115. }
  116. Socket openConnection() throws TransportException {
  117. final int tms = getTimeout() > 0 ? getTimeout() * 1000 : 0;
  118. final int port = uri.getPort() > 0 ? uri.getPort() : GIT_PORT;
  119. final Socket s = new Socket();
  120. try {
  121. final InetAddress host = InetAddress.getByName(uri.getHost());
  122. s.bind(null);
  123. s.connect(new InetSocketAddress(host, port), tms);
  124. } catch (IOException c) {
  125. try {
  126. s.close();
  127. } catch (IOException closeErr) {
  128. // ignore a failure during close, we're already failing
  129. }
  130. if (c instanceof UnknownHostException)
  131. throw new TransportException(uri, JGitText.get().unknownHost);
  132. if (c instanceof ConnectException)
  133. throw new TransportException(uri, c.getMessage());
  134. throw new TransportException(uri, c.getMessage(), c);
  135. }
  136. return s;
  137. }
  138. void service(final String name, final PacketLineOut pckOut)
  139. throws IOException {
  140. final StringBuilder cmd = new StringBuilder();
  141. cmd.append(name);
  142. cmd.append(' ');
  143. cmd.append(uri.getPath());
  144. cmd.append('\0');
  145. cmd.append("host="); //$NON-NLS-1$
  146. cmd.append(uri.getHost());
  147. if (uri.getPort() > 0 && uri.getPort() != GIT_PORT) {
  148. cmd.append(":"); //$NON-NLS-1$
  149. cmd.append(uri.getPort());
  150. }
  151. cmd.append('\0');
  152. pckOut.writeString(cmd.toString());
  153. pckOut.flush();
  154. }
  155. class TcpFetchConnection extends BasePackFetchConnection {
  156. private Socket sock;
  157. TcpFetchConnection() throws TransportException {
  158. super(TransportGitAnon.this);
  159. sock = openConnection();
  160. try {
  161. InputStream sIn = sock.getInputStream();
  162. OutputStream sOut = sock.getOutputStream();
  163. sIn = new BufferedInputStream(sIn);
  164. sOut = new SafeBufferedOutputStream(sOut);
  165. init(sIn, sOut);
  166. service("git-upload-pack", pckOut); //$NON-NLS-1$
  167. } catch (IOException err) {
  168. close();
  169. throw new TransportException(uri,
  170. JGitText.get().remoteHungUpUnexpectedly, err);
  171. }
  172. readAdvertisedRefs();
  173. }
  174. @Override
  175. public void close() {
  176. super.close();
  177. if (sock != null) {
  178. try {
  179. sock.close();
  180. } catch (IOException err) {
  181. // Ignore errors during close.
  182. } finally {
  183. sock = null;
  184. }
  185. }
  186. }
  187. }
  188. class TcpPushConnection extends BasePackPushConnection {
  189. private Socket sock;
  190. TcpPushConnection() throws TransportException {
  191. super(TransportGitAnon.this);
  192. sock = openConnection();
  193. try {
  194. InputStream sIn = sock.getInputStream();
  195. OutputStream sOut = sock.getOutputStream();
  196. sIn = new BufferedInputStream(sIn);
  197. sOut = new SafeBufferedOutputStream(sOut);
  198. init(sIn, sOut);
  199. service("git-receive-pack", pckOut); //$NON-NLS-1$
  200. } catch (IOException err) {
  201. close();
  202. throw new TransportException(uri,
  203. JGitText.get().remoteHungUpUnexpectedly, err);
  204. }
  205. readAdvertisedRefs();
  206. }
  207. @Override
  208. public void close() {
  209. super.close();
  210. if (sock != null) {
  211. try {
  212. sock.close();
  213. } catch (IOException err) {
  214. // Ignore errors during close.
  215. } finally {
  216. sock = null;
  217. }
  218. }
  219. }
  220. }
  221. }