BasePackConnection.java

  1. /*
  2.  * Copyright (C) 2008-2010, Google Inc.
  3.  * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
  4.  * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  5.  * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  6.  * and other copyright owners as documented in the project's IP log.
  7.  *
  8.  * This program and the accompanying materials are made available
  9.  * under the terms of the Eclipse Distribution License v1.0 which
  10.  * accompanies this distribution, is reproduced below, and is
  11.  * available at http://www.eclipse.org/org/documents/edl-v10.php
  12.  *
  13.  * All rights reserved.
  14.  *
  15.  * Redistribution and use in source and binary forms, with or
  16.  * without modification, are permitted provided that the following
  17.  * conditions are met:
  18.  *
  19.  * - Redistributions of source code must retain the above copyright
  20.  *   notice, this list of conditions and the following disclaimer.
  21.  *
  22.  * - Redistributions in binary form must reproduce the above
  23.  *   copyright notice, this list of conditions and the following
  24.  *   disclaimer in the documentation and/or other materials provided
  25.  *   with the distribution.
  26.  *
  27.  * - Neither the name of the Eclipse Foundation, Inc. nor the
  28.  *   names of its contributors may be used to endorse or promote
  29.  *   products derived from this software without specific prior
  30.  *   written permission.
  31.  *
  32.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  33.  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  34.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  35.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  36.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  37.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  38.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  39.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  40.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  41.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  42.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  43.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  44.  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45.  */

  46. package org.eclipse.jgit.transport;

  47. import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_AGENT;

  48. import java.io.EOFException;
  49. import java.io.IOException;
  50. import java.io.InputStream;
  51. import java.io.OutputStream;
  52. import java.text.MessageFormat;
  53. import java.util.Arrays;
  54. import java.util.HashSet;
  55. import java.util.LinkedHashMap;
  56. import java.util.Set;

  57. import org.eclipse.jgit.errors.InvalidObjectIdException;
  58. import org.eclipse.jgit.errors.NoRemoteRepositoryException;
  59. import org.eclipse.jgit.errors.PackProtocolException;
  60. import org.eclipse.jgit.errors.RemoteRepositoryException;
  61. import org.eclipse.jgit.errors.TransportException;
  62. import org.eclipse.jgit.internal.JGitText;
  63. import org.eclipse.jgit.lib.ObjectId;
  64. import org.eclipse.jgit.lib.ObjectIdRef;
  65. import org.eclipse.jgit.lib.Ref;
  66. import org.eclipse.jgit.lib.Repository;
  67. import org.eclipse.jgit.util.io.InterruptTimer;
  68. import org.eclipse.jgit.util.io.TimeoutInputStream;
  69. import org.eclipse.jgit.util.io.TimeoutOutputStream;

  70. /**
  71.  * Base helper class for pack-based operations implementations. Provides partial
  72.  * implementation of pack-protocol - refs advertising and capabilities support,
  73.  * and some other helper methods.
  74.  *
  75.  * @see BasePackFetchConnection
  76.  * @see BasePackPushConnection
  77.  */
  78. abstract class BasePackConnection extends BaseConnection {

  79.     /** The repository this transport fetches into, or pushes out of. */
  80.     protected final Repository local;

  81.     /** Remote repository location. */
  82.     protected final URIish uri;

  83.     /** A transport connected to {@link #uri}. */
  84.     protected final Transport transport;

  85.     /** Low-level input stream, if a timeout was configured. */
  86.     protected TimeoutInputStream timeoutIn;

  87.     /** Low-level output stream, if a timeout was configured. */
  88.     protected TimeoutOutputStream timeoutOut;

  89.     /** Timer to manage {@link #timeoutIn} and {@link #timeoutOut}. */
  90.     private InterruptTimer myTimer;

  91.     /** Input stream reading from the remote. */
  92.     protected InputStream in;

  93.     /** Output stream sending to the remote. */
  94.     protected OutputStream out;

  95.     /** Packet line decoder around {@link #in}. */
  96.     protected PacketLineIn pckIn;

  97.     /** Packet line encoder around {@link #out}. */
  98.     protected PacketLineOut pckOut;

  99.     /** Send {@link PacketLineOut#end()} before closing {@link #out}? */
  100.     protected boolean outNeedsEnd;

  101.     /** True if this is a stateless RPC connection. */
  102.     protected boolean statelessRPC;

  103.     /** Capability tokens advertised by the remote side. */
  104.     private final Set<String> remoteCapablities = new HashSet<>();

  105.     /** Extra objects the remote has, but which aren't offered as refs. */
  106.     protected final Set<ObjectId> additionalHaves = new HashSet<>();

  107.     BasePackConnection(PackTransport packTransport) {
  108.         transport = (Transport) packTransport;
  109.         local = transport.local;
  110.         uri = transport.uri;
  111.     }

  112.     /**
  113.      * Configure this connection with the directional pipes.
  114.      *
  115.      * @param myIn
  116.      *            input stream to receive data from the peer. Caller must ensure
  117.      *            the input is buffered, otherwise read performance may suffer.
  118.      * @param myOut
  119.      *            output stream to transmit data to the peer. Caller must ensure
  120.      *            the output is buffered, otherwise write performance may
  121.      *            suffer.
  122.      */
  123.     protected final void init(InputStream myIn, OutputStream myOut) {
  124.         final int timeout = transport.getTimeout();
  125.         if (timeout > 0) {
  126.             final Thread caller = Thread.currentThread();
  127.             if (myTimer == null) {
  128.                 myTimer = new InterruptTimer(caller.getName() + "-Timer"); //$NON-NLS-1$
  129.             }
  130.             timeoutIn = new TimeoutInputStream(myIn, myTimer);
  131.             timeoutOut = new TimeoutOutputStream(myOut, myTimer);
  132.             timeoutIn.setTimeout(timeout * 1000);
  133.             timeoutOut.setTimeout(timeout * 1000);
  134.             myIn = timeoutIn;
  135.             myOut = timeoutOut;
  136.         }

  137.         in = myIn;
  138.         out = myOut;

  139.         pckIn = new PacketLineIn(in);
  140.         pckOut = new PacketLineOut(out);
  141.         outNeedsEnd = true;
  142.     }

  143.     /**
  144.      * Reads the advertised references through the initialized stream.
  145.      * <p>
  146.      * Subclass implementations may call this method only after setting up the
  147.      * input and output streams with {@link #init(InputStream, OutputStream)}.
  148.      * <p>
  149.      * If any errors occur, this connection is automatically closed by invoking
  150.      * {@link #close()} and the exception is wrapped (if necessary) and thrown
  151.      * as a {@link org.eclipse.jgit.errors.TransportException}.
  152.      *
  153.      * @throws org.eclipse.jgit.errors.TransportException
  154.      *             the reference list could not be scanned.
  155.      */
  156.     protected void readAdvertisedRefs() throws TransportException {
  157.         try {
  158.             readAdvertisedRefsImpl();
  159.         } catch (TransportException err) {
  160.             close();
  161.             throw err;
  162.         } catch (IOException | RuntimeException err) {
  163.             close();
  164.             throw new TransportException(err.getMessage(), err);
  165.         }
  166.     }

  167.     private void readAdvertisedRefsImpl() throws IOException {
  168.         final LinkedHashMap<String, Ref> avail = new LinkedHashMap<>();
  169.         for (;;) {
  170.             String line;

  171.             try {
  172.                 line = pckIn.readString();
  173.             } catch (EOFException eof) {
  174.                 if (avail.isEmpty())
  175.                     throw noRepository();
  176.                 throw eof;
  177.             }
  178.             if (PacketLineIn.isEnd(line))
  179.                 break;

  180.             if (line.startsWith("ERR ")) { //$NON-NLS-1$
  181.                 // This is a customized remote service error.
  182.                 // Users should be informed about it.
  183.                 throw new RemoteRepositoryException(uri, line.substring(4));
  184.             }

  185.             if (avail.isEmpty()) {
  186.                 final int nul = line.indexOf('\0');
  187.                 if (nul >= 0) {
  188.                     // The first line (if any) may contain "hidden"
  189.                     // capability values after a NUL byte.
  190.                     remoteCapablities.addAll(
  191.                             Arrays.asList(line.substring(nul + 1).split(" "))); //$NON-NLS-1$
  192.                     line = line.substring(0, nul);
  193.                 }
  194.             }

  195.             // Expecting to get a line in the form "sha1 refname"
  196.             if (line.length() < 41 || line.charAt(40) != ' ') {
  197.                 throw invalidRefAdvertisementLine(line);
  198.             }
  199.             String name = line.substring(41, line.length());
  200.             if (avail.isEmpty() && name.equals("capabilities^{}")) { //$NON-NLS-1$
  201.                 // special line from git-receive-pack to show
  202.                 // capabilities when there are no refs to advertise
  203.                 continue;
  204.             }

  205.             final ObjectId id;
  206.             try {
  207.                 id  = ObjectId.fromString(line.substring(0, 40));
  208.             } catch (InvalidObjectIdException e) {
  209.                 throw invalidRefAdvertisementLine(line);
  210.             }
  211.             if (name.equals(".have")) { //$NON-NLS-1$
  212.                 additionalHaves.add(id);
  213.             } else if (name.endsWith("^{}")) { //$NON-NLS-1$
  214.                 name = name.substring(0, name.length() - 3);
  215.                 final Ref prior = avail.get(name);
  216.                 if (prior == null)
  217.                     throw new PackProtocolException(uri, MessageFormat.format(
  218.                             JGitText.get().advertisementCameBefore, name, name));

  219.                 if (prior.getPeeledObjectId() != null)
  220.                     throw duplicateAdvertisement(name + "^{}"); //$NON-NLS-1$

  221.                 avail.put(name, new ObjectIdRef.PeeledTag(
  222.                         Ref.Storage.NETWORK, name, prior.getObjectId(), id));
  223.             } else {
  224.                 final Ref prior = avail.put(name, new ObjectIdRef.PeeledNonTag(
  225.                         Ref.Storage.NETWORK, name, id));
  226.                 if (prior != null)
  227.                     throw duplicateAdvertisement(name);
  228.             }
  229.         }
  230.         available(avail);
  231.     }

  232.     /**
  233.      * Create an exception to indicate problems finding a remote repository. The
  234.      * caller is expected to throw the returned exception.
  235.      *
  236.      * Subclasses may override this method to provide better diagnostics.
  237.      *
  238.      * @return a TransportException saying a repository cannot be found and
  239.      *         possibly why.
  240.      */
  241.     protected TransportException noRepository() {
  242.         return new NoRemoteRepositoryException(uri, JGitText.get().notFound);
  243.     }

  244.     /**
  245.      * Whether this option is supported
  246.      *
  247.      * @param option
  248.      *            option string
  249.      * @return whether this option is supported
  250.      */
  251.     protected boolean isCapableOf(String option) {
  252.         return remoteCapablities.contains(option);
  253.     }

  254.     /**
  255.      * Request capability
  256.      *
  257.      * @param b
  258.      *            buffer
  259.      * @param option
  260.      *            option we want
  261.      * @return {@code true} if the requested option is supported
  262.      */
  263.     protected boolean wantCapability(StringBuilder b, String option) {
  264.         if (!isCapableOf(option))
  265.             return false;
  266.         b.append(' ');
  267.         b.append(option);
  268.         return true;
  269.     }

  270.     /**
  271.      * Add user agent capability
  272.      *
  273.      * @param b
  274.      *            a {@link java.lang.StringBuilder} object.
  275.      */
  276.     protected void addUserAgentCapability(StringBuilder b) {
  277.         String a = UserAgent.get();
  278.         if (a != null && UserAgent.hasAgent(remoteCapablities)) {
  279.             b.append(' ').append(OPTION_AGENT).append('=').append(a);
  280.         }
  281.     }

  282.     /** {@inheritDoc} */
  283.     @Override
  284.     public String getPeerUserAgent() {
  285.         return UserAgent.getAgent(remoteCapablities, super.getPeerUserAgent());
  286.     }

  287.     private PackProtocolException duplicateAdvertisement(String name) {
  288.         return new PackProtocolException(uri, MessageFormat.format(JGitText.get().duplicateAdvertisementsOf, name));
  289.     }

  290.     private PackProtocolException invalidRefAdvertisementLine(String line) {
  291.         return new PackProtocolException(uri, MessageFormat.format(JGitText.get().invalidRefAdvertisementLine, line));
  292.     }

  293.     /** {@inheritDoc} */
  294.     @Override
  295.     public void close() {
  296.         if (out != null) {
  297.             try {
  298.                 if (outNeedsEnd) {
  299.                     outNeedsEnd = false;
  300.                     pckOut.end();
  301.                 }
  302.                 out.close();
  303.             } catch (IOException err) {
  304.                 // Ignore any close errors.
  305.             } finally {
  306.                 out = null;
  307.                 pckOut = null;
  308.             }
  309.         }

  310.         if (in != null) {
  311.             try {
  312.                 in.close();
  313.             } catch (IOException err) {
  314.                 // Ignore any close errors.
  315.             } finally {
  316.                 in = null;
  317.                 pckIn = null;
  318.             }
  319.         }

  320.         if (myTimer != null) {
  321.             try {
  322.                 myTimer.terminate();
  323.             } finally {
  324.                 myTimer = null;
  325.                 timeoutIn = null;
  326.                 timeoutOut = null;
  327.             }
  328.         }
  329.     }

  330.     /**
  331.      * Tell the peer we are disconnecting, if it cares to know.
  332.      */
  333.     protected void endOut() {
  334.         if (outNeedsEnd && out != null) {
  335.             try {
  336.                 outNeedsEnd = false;
  337.                 pckOut.end();
  338.             } catch (IOException e) {
  339.                 try {
  340.                     out.close();
  341.                 } catch (IOException err) {
  342.                     // Ignore any close errors.
  343.                 } finally {
  344.                     out = null;
  345.                     pckOut = null;
  346.                 }
  347.             }
  348.         }
  349.     }
  350. }