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 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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.BufferedOutputStream;
  48. import java.io.IOException;
  49. import java.io.InputStream;
  50. import java.io.OutputStream;
  51. import java.net.ConnectException;
  52. import java.net.InetAddress;
  53. import java.net.InetSocketAddress;
  54. import java.net.Socket;
  55. import java.net.UnknownHostException;
  56. import org.eclipse.jgit.errors.TransportException;
  57. import org.eclipse.jgit.lib.Repository;
  58. /**
  59. * Transport through a git-daemon waiting for anonymous TCP connections.
  60. * <p>
  61. * This transport supports the <code>git://</code> protocol, usually run on
  62. * the IANA registered port 9418. It is a popular means for distributing open
  63. * source projects, as there are no authentication or authorization overheads.
  64. */
  65. class TransportGitAnon extends TcpTransport implements PackTransport {
  66. static final int GIT_PORT = Daemon.DEFAULT_PORT;
  67. static boolean canHandle(final URIish uri) {
  68. return "git".equals(uri.getScheme());
  69. }
  70. TransportGitAnon(final Repository local, final URIish uri) {
  71. super(local, uri);
  72. }
  73. @Override
  74. public FetchConnection openFetch() throws TransportException {
  75. return new TcpFetchConnection();
  76. }
  77. @Override
  78. public PushConnection openPush() throws TransportException {
  79. return new TcpPushConnection();
  80. }
  81. @Override
  82. public void close() {
  83. // Resources must be established per-connection.
  84. }
  85. Socket openConnection() throws TransportException {
  86. final int tms = getTimeout() > 0 ? getTimeout() * 1000 : 0;
  87. final int port = uri.getPort() > 0 ? uri.getPort() : GIT_PORT;
  88. final Socket s = new Socket();
  89. try {
  90. final InetAddress host = InetAddress.getByName(uri.getHost());
  91. s.bind(null);
  92. s.connect(new InetSocketAddress(host, port), tms);
  93. } catch (IOException c) {
  94. try {
  95. s.close();
  96. } catch (IOException closeErr) {
  97. // ignore a failure during close, we're already failing
  98. }
  99. if (c instanceof UnknownHostException)
  100. throw new TransportException(uri, "unknown host");
  101. if (c instanceof ConnectException)
  102. throw new TransportException(uri, c.getMessage());
  103. throw new TransportException(uri, c.getMessage(), c);
  104. }
  105. return s;
  106. }
  107. void service(final String name, final PacketLineOut pckOut)
  108. throws IOException {
  109. final StringBuilder cmd = new StringBuilder();
  110. cmd.append(name);
  111. cmd.append(' ');
  112. cmd.append(uri.getPath());
  113. cmd.append('\0');
  114. cmd.append("host=");
  115. cmd.append(uri.getHost());
  116. if (uri.getPort() > 0 && uri.getPort() != GIT_PORT) {
  117. cmd.append(":");
  118. cmd.append(uri.getPort());
  119. }
  120. cmd.append('\0');
  121. pckOut.writeString(cmd.toString());
  122. pckOut.flush();
  123. }
  124. class TcpFetchConnection extends BasePackFetchConnection {
  125. private Socket sock;
  126. TcpFetchConnection() throws TransportException {
  127. super(TransportGitAnon.this);
  128. sock = openConnection();
  129. try {
  130. InputStream sIn = sock.getInputStream();
  131. OutputStream sOut = sock.getOutputStream();
  132. sIn = new BufferedInputStream(sIn);
  133. sOut = new BufferedOutputStream(sOut);
  134. init(sIn, sOut);
  135. service("git-upload-pack", pckOut);
  136. } catch (IOException err) {
  137. close();
  138. throw new TransportException(uri,
  139. "remote hung up unexpectedly", err);
  140. }
  141. readAdvertisedRefs();
  142. }
  143. @Override
  144. public void close() {
  145. super.close();
  146. if (sock != null) {
  147. try {
  148. sock.close();
  149. } catch (IOException err) {
  150. // Ignore errors during close.
  151. } finally {
  152. sock = null;
  153. }
  154. }
  155. }
  156. }
  157. class TcpPushConnection extends BasePackPushConnection {
  158. private Socket sock;
  159. TcpPushConnection() throws TransportException {
  160. super(TransportGitAnon.this);
  161. sock = openConnection();
  162. try {
  163. InputStream sIn = sock.getInputStream();
  164. OutputStream sOut = sock.getOutputStream();
  165. sIn = new BufferedInputStream(sIn);
  166. sOut = new BufferedOutputStream(sOut);
  167. init(sIn, sOut);
  168. service("git-receive-pack", pckOut);
  169. } catch (IOException err) {
  170. close();
  171. throw new TransportException(uri,
  172. "remote hung up unexpectedly", err);
  173. }
  174. readAdvertisedRefs();
  175. }
  176. @Override
  177. public void close() {
  178. super.close();
  179. if (sock != null) {
  180. try {
  181. sock.close();
  182. } catch (IOException err) {
  183. // Ignore errors during close.
  184. } finally {
  185. sock = null;
  186. }
  187. }
  188. }
  189. }
  190. }