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

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