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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. };
  94. TransportGitAnon(final Repository local, final URIish uri) {
  95. super(local, uri);
  96. }
  97. @Override
  98. public FetchConnection openFetch() throws TransportException {
  99. return new TcpFetchConnection();
  100. }
  101. @Override
  102. public PushConnection openPush() throws TransportException {
  103. return new TcpPushConnection();
  104. }
  105. @Override
  106. public void close() {
  107. // Resources must be established per-connection.
  108. }
  109. Socket openConnection() throws TransportException {
  110. final int tms = getTimeout() > 0 ? getTimeout() * 1000 : 0;
  111. final int port = uri.getPort() > 0 ? uri.getPort() : GIT_PORT;
  112. final Socket s = new Socket();
  113. try {
  114. final InetAddress host = InetAddress.getByName(uri.getHost());
  115. s.bind(null);
  116. s.connect(new InetSocketAddress(host, port), tms);
  117. } catch (IOException c) {
  118. try {
  119. s.close();
  120. } catch (IOException closeErr) {
  121. // ignore a failure during close, we're already failing
  122. }
  123. if (c instanceof UnknownHostException)
  124. throw new TransportException(uri, JGitText.get().unknownHost);
  125. if (c instanceof ConnectException)
  126. throw new TransportException(uri, c.getMessage());
  127. throw new TransportException(uri, c.getMessage(), c);
  128. }
  129. return s;
  130. }
  131. void service(final String name, final PacketLineOut pckOut)
  132. throws IOException {
  133. final StringBuilder cmd = new StringBuilder();
  134. cmd.append(name);
  135. cmd.append(' ');
  136. cmd.append(uri.getPath());
  137. cmd.append('\0');
  138. cmd.append("host=");
  139. cmd.append(uri.getHost());
  140. if (uri.getPort() > 0 && uri.getPort() != GIT_PORT) {
  141. cmd.append(":");
  142. cmd.append(uri.getPort());
  143. }
  144. cmd.append('\0');
  145. pckOut.writeString(cmd.toString());
  146. pckOut.flush();
  147. }
  148. class TcpFetchConnection extends BasePackFetchConnection {
  149. private Socket sock;
  150. TcpFetchConnection() throws TransportException {
  151. super(TransportGitAnon.this);
  152. sock = openConnection();
  153. try {
  154. InputStream sIn = sock.getInputStream();
  155. OutputStream sOut = sock.getOutputStream();
  156. sIn = new BufferedInputStream(sIn);
  157. sOut = new SafeBufferedOutputStream(sOut);
  158. init(sIn, sOut);
  159. service("git-upload-pack", pckOut);
  160. } catch (IOException err) {
  161. close();
  162. throw new TransportException(uri,
  163. JGitText.get().remoteHungUpUnexpectedly, err);
  164. }
  165. readAdvertisedRefs();
  166. }
  167. @Override
  168. public void close() {
  169. super.close();
  170. if (sock != null) {
  171. try {
  172. sock.close();
  173. } catch (IOException err) {
  174. // Ignore errors during close.
  175. } finally {
  176. sock = null;
  177. }
  178. }
  179. }
  180. }
  181. class TcpPushConnection extends BasePackPushConnection {
  182. private Socket sock;
  183. TcpPushConnection() throws TransportException {
  184. super(TransportGitAnon.this);
  185. sock = openConnection();
  186. try {
  187. InputStream sIn = sock.getInputStream();
  188. OutputStream sOut = sock.getOutputStream();
  189. sIn = new BufferedInputStream(sIn);
  190. sOut = new SafeBufferedOutputStream(sOut);
  191. init(sIn, sOut);
  192. service("git-receive-pack", pckOut);
  193. } catch (IOException err) {
  194. close();
  195. throw new TransportException(uri,
  196. JGitText.get().remoteHungUpUnexpectedly, err);
  197. }
  198. readAdvertisedRefs();
  199. }
  200. @Override
  201. public void close() {
  202. super.close();
  203. if (sock != null) {
  204. try {
  205. sock.close();
  206. } catch (IOException err) {
  207. // Ignore errors during close.
  208. } finally {
  209. sock = null;
  210. }
  211. }
  212. }
  213. }
  214. }