Exemple #1
0
        /// <exception cref="System.IO.IOException"></exception>
        private void ReadSmartHeaders(InputStream @in, string service)
        {
            // A smart reply will have a '#' after the first 4 bytes, but
            // a dumb reply cannot contain a '#' until after byte 41. Do a
            // quick check to make sure its a smart reply before we parse
            // as a pkt-line stream.
            //
            byte[] magic = new byte[5];
            IOUtil.ReadFully(@in, magic, 0, magic.Length);
            if (magic[4] != '#')
            {
                throw new TransportException(uri, MessageFormat.Format(JGitText.Get().expectedPktLineWithService
                                                                       , RawParseUtils.Decode(magic)));
            }
            PacketLineIn pckIn = new PacketLineIn(new UnionInputStream(new ByteArrayInputStream
                                                                           (magic), @in));
            string exp = "# service=" + service;
            //$NON-NLS-1$
            string act = pckIn.ReadString();

            if (!exp.Equals(act))
            {
                throw new TransportException(uri, MessageFormat.Format(JGitText.Get().expectedGot
                                                                       , exp, act));
            }
            while (pckIn.ReadString() != PacketLineIn.END)
            {
            }
        }
Exemple #2
0
        /// <exception cref="System.IO.IOException"></exception>
        /// <exception cref="NGit.Transport.Resolver.ServiceNotEnabledException"></exception>
        /// <exception cref="NGit.Transport.Resolver.ServiceNotAuthorizedException"></exception>
        internal virtual void Execute(Socket sock)
        {
            rawIn  = new BufferedInputStream(sock.GetInputStream());
            rawOut = new SafeBufferedOutputStream(sock.GetOutputStream());
            if (0 < daemon.GetTimeout())
            {
                sock.ReceiveTimeout = daemon.GetTimeout() * 1000;
            }
            string cmd = new PacketLineIn(rawIn).ReadStringRaw();
            int    nul = cmd.IndexOf('\0');

            if (nul >= 0)
            {
                // Newer clients hide a "host" header behind this byte.
                // Currently we don't use it for anything, so we ignore
                // this portion of the command.
                //
                cmd = Sharpen.Runtime.Substring(cmd, 0, nul);
            }
            DaemonService srv = GetDaemon().MatchService(cmd);

            if (srv == null)
            {
                return;
            }
            sock.ReceiveTimeout = 0;
            srv.Execute(this, cmd);
        }
Exemple #3
0
 internal SideBandInputStream(InputStream @in, ProgressMonitor progress, TextWriter
                              messageStream)
 {
     rawIn       = @in;
     pckIn       = new PacketLineIn(rawIn);
     monitor     = progress;
     messages    = messageStream;
     currentTask = string.Empty;
 }
        public virtual void TestUsingHiddenDeltaBaseFails()
        {
            byte[] delta = new byte[] { unchecked ((int)(0x1)), unchecked ((int)(0x1)), unchecked (
                                            (int)(0x1)), (byte)('c') };
            TestRepository <Repository> s = new TestRepository <Repository>(src);
            RevCommit N = s.Commit().Parent(B).Add("q", s.Blob(BinaryDelta.Apply(dst.Open(b).
                                                                                 GetCachedBytes(), delta))).Create();

            TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(1024);
            PackHeader(pack, 3);
            Copy(pack, src.Open(N));
            Copy(pack, src.Open(s.ParseBody(N).Tree));
            pack.Write((Constants.OBJ_REF_DELTA) << 4 | 4);
            b.CopyRawTo(pack);
            Deflate(pack, delta);
            Digest(pack);
            TemporaryBuffer.Heap inBuf     = new TemporaryBuffer.Heap(1024);
            PacketLineOut        inPckLine = new PacketLineOut(inBuf);

            inPckLine.WriteString(ObjectId.ZeroId.Name + ' ' + N.Name + ' ' + "refs/heads/s"
                                  + '\0' + BasePackPushConnection.CAPABILITY_REPORT_STATUS);
            inPckLine.End();
            pack.WriteTo(inBuf, PM);
            TemporaryBuffer.Heap outBuf = new TemporaryBuffer.Heap(1024);
            ReceivePack          rp     = new ReceivePack(dst);

            rp.SetCheckReceivedObjects(true);
            rp.SetCheckReferencedObjectsAreReachable(true);
            rp.SetAdvertiseRefsHook(new ReceivePackAdvertiseRefsHookTest.HidePrivateHook());
            try
            {
                Receive(rp, inBuf, outBuf);
                NUnit.Framework.Assert.Fail("Expected UnpackException");
            }
            catch (UnpackException failed)
            {
                Exception err = failed.InnerException;
                NUnit.Framework.Assert.IsTrue(err is MissingObjectException);
                MissingObjectException moe = (MissingObjectException)err;
                NUnit.Framework.Assert.AreEqual(b, moe.GetObjectId());
            }
            PacketLineIn r      = AsPacketLineIn(outBuf);
            string       master = r.ReadString();
            int          nul    = master.IndexOf('\0');

            NUnit.Framework.Assert.IsTrue(nul > 0, "has capability list");
            NUnit.Framework.Assert.AreEqual(B.Name + ' ' + R_MASTER, Sharpen.Runtime.Substring
                                                (master, 0, nul));
            NUnit.Framework.Assert.AreSame(PacketLineIn.END, r.ReadString());
            NUnit.Framework.Assert.AreEqual("unpack error Missing blob " + b.Name, r.ReadString
                                                ());
            NUnit.Framework.Assert.AreEqual("ng refs/heads/s n/a (unpacker error)", r.ReadString
                                                ());
            NUnit.Framework.Assert.AreSame(PacketLineIn.END, r.ReadString());
        }
        public virtual void TestUsingUnknownBlobFails()
        {
            // Try to use the 'n' blob that is not on the server.
            //
            TestRepository <Repository> s = new TestRepository <Repository>(src);
            RevBlob   n = s.Blob("n");
            RevCommit N = s.Commit().Parent(B).Add("q", n).Create();

            // But don't include it in the pack.
            //
            TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(1024);
            PackHeader(pack, 2);
            Copy(pack, src.Open(N));
            Copy(pack, src.Open(s.ParseBody(N).Tree));
            Digest(pack);
            TemporaryBuffer.Heap inBuf     = new TemporaryBuffer.Heap(1024);
            PacketLineOut        inPckLine = new PacketLineOut(inBuf);

            inPckLine.WriteString(ObjectId.ZeroId.Name + ' ' + N.Name + ' ' + "refs/heads/s"
                                  + '\0' + BasePackPushConnection.CAPABILITY_REPORT_STATUS);
            inPckLine.End();
            pack.WriteTo(inBuf, PM);
            TemporaryBuffer.Heap outBuf = new TemporaryBuffer.Heap(1024);
            ReceivePack          rp     = new ReceivePack(dst);

            rp.SetCheckReceivedObjects(true);
            rp.SetCheckReferencedObjectsAreReachable(true);
            rp.SetAdvertiseRefsHook(new ReceivePackAdvertiseRefsHookTest.HidePrivateHook());
            try
            {
                Receive(rp, inBuf, outBuf);
                NUnit.Framework.Assert.Fail("Expected UnpackException");
            }
            catch (UnpackException failed)
            {
                Exception err = failed.InnerException;
                NUnit.Framework.Assert.IsTrue(err is MissingObjectException);
                MissingObjectException moe = (MissingObjectException)err;
                NUnit.Framework.Assert.AreEqual(n, moe.GetObjectId());
            }
            PacketLineIn r      = AsPacketLineIn(outBuf);
            string       master = r.ReadString();
            int          nul    = master.IndexOf('\0');

            NUnit.Framework.Assert.IsTrue(nul > 0, "has capability list");
            NUnit.Framework.Assert.AreEqual(B.Name + ' ' + R_MASTER, Sharpen.Runtime.Substring
                                                (master, 0, nul));
            NUnit.Framework.Assert.AreSame(PacketLineIn.END, r.ReadString());
            NUnit.Framework.Assert.AreEqual("unpack error Missing blob " + n.Name, r.ReadString
                                                ());
            NUnit.Framework.Assert.AreEqual("ng refs/heads/s n/a (unpacker error)", r.ReadString
                                                ());
            NUnit.Framework.Assert.AreSame(PacketLineIn.END, r.ReadString());
        }
Exemple #6
0
 public override void Close()
 {
     if (@out != null)
     {
         try
         {
             if (outNeedsEnd)
             {
                 outNeedsEnd = false;
                 pckOut.End();
             }
             @out.Close();
         }
         catch (IOException)
         {
         }
         finally
         {
             // Ignore any close errors.
             @out   = null;
             pckOut = null;
         }
     }
     if (@in != null)
     {
         try
         {
             @in.Close();
         }
         catch (IOException)
         {
         }
         finally
         {
             // Ignore any close errors.
             @in   = null;
             pckIn = null;
         }
     }
     if (myTimer != null)
     {
         try
         {
             myTimer.Terminate();
         }
         finally
         {
             myTimer    = null;
             timeoutIn  = null;
             timeoutOut = null;
         }
     }
 }
        public virtual void TestCreateBranchAtHiddenCommitFails()
        {
            TemporaryBuffer.Heap pack = new TemporaryBuffer.Heap(64);
            PackHeader(pack, 0);
            Digest(pack);
            TemporaryBuffer.Heap inBuf     = new TemporaryBuffer.Heap(256);
            PacketLineOut        inPckLine = new PacketLineOut(inBuf);

            inPckLine.WriteString(ObjectId.ZeroId.Name + ' ' + P.Name + ' ' + "refs/heads/s"
                                  + '\0' + BasePackPushConnection.CAPABILITY_REPORT_STATUS);
            inPckLine.End();
            pack.WriteTo(inBuf, PM);
            TemporaryBuffer.Heap outBuf = new TemporaryBuffer.Heap(1024);
            ReceivePack          rp     = new ReceivePack(dst);

            rp.SetCheckReceivedObjects(true);
            rp.SetCheckReferencedObjectsAreReachable(true);
            rp.SetAdvertiseRefsHook(new ReceivePackAdvertiseRefsHookTest.HidePrivateHook());
            try
            {
                Receive(rp, inBuf, outBuf);
                NUnit.Framework.Assert.Fail("Expected UnpackException");
            }
            catch (UnpackException failed)
            {
                Exception err = failed.InnerException;
                NUnit.Framework.Assert.IsTrue(err is MissingObjectException);
                MissingObjectException moe = (MissingObjectException)err;
                NUnit.Framework.Assert.AreEqual(P, moe.GetObjectId());
            }
            PacketLineIn r      = AsPacketLineIn(outBuf);
            string       master = r.ReadString();
            int          nul    = master.IndexOf('\0');

            NUnit.Framework.Assert.IsTrue(nul > 0, "has capability list");
            NUnit.Framework.Assert.AreEqual(B.Name + ' ' + R_MASTER, Sharpen.Runtime.Substring
                                                (master, 0, nul));
            NUnit.Framework.Assert.AreSame(PacketLineIn.END, r.ReadString());
            NUnit.Framework.Assert.AreEqual("unpack error Missing commit " + P.Name, r.ReadString
                                                ());
            NUnit.Framework.Assert.AreEqual("ng refs/heads/s n/a (unpacker error)", r.ReadString
                                                ());
            NUnit.Framework.Assert.AreSame(PacketLineIn.END, r.ReadString());
        }
Exemple #8
0
        private string EnableCapabilities(ProgressMonitor monitor)
        {
            StringBuilder line = new StringBuilder();

            capableReport     = WantCapability(line, CAPABILITY_REPORT_STATUS);
            capableDeleteRefs = WantCapability(line, CAPABILITY_DELETE_REFS);
            capableOfsDelta   = WantCapability(line, CAPABILITY_OFS_DELTA);
            capableSideBand   = WantCapability(line, CAPABILITY_SIDE_BAND_64K);
            if (capableSideBand)
            {
                @in   = new SideBandInputStream(@in, monitor, GetMessageWriter());
                pckIn = new PacketLineIn(@in);
            }
            if (line.Length > 0)
            {
                Sharpen.Runtime.SetCharAt(line, 0, '\0');
            }
            return(line.ToString());
        }
Exemple #9
0
        /// <summary>Configure this connection with the directional pipes.</summary>
        /// <remarks>Configure this connection with the directional pipes.</remarks>
        /// <param name="myIn">
        /// input stream to receive data from the peer. Caller must ensure
        /// the input is buffered, otherwise read performance may suffer.
        /// </param>
        /// <param name="myOut">
        /// output stream to transmit data to the peer. Caller must ensure
        /// the output is buffered, otherwise write performance may
        /// suffer.
        /// </param>
        protected internal void Init(InputStream myIn, OutputStream myOut)
        {
            int timeout = transport.GetTimeout();

            if (timeout > 0)
            {
                Sharpen.Thread caller = Sharpen.Thread.CurrentThread();
                myTimer    = new InterruptTimer(caller.GetName() + "-Timer");
                timeoutIn  = new TimeoutInputStream(myIn, myTimer);
                timeoutOut = new TimeoutOutputStream(myOut, myTimer);
                timeoutIn.SetTimeout(timeout * 1000);
                timeoutOut.SetTimeout(timeout * 1000);
                myIn  = timeoutIn;
                myOut = timeoutOut;
            }
            @in         = myIn;
            @out        = myOut;
            pckIn       = new PacketLineIn(@in);
            pckOut      = new PacketLineOut(@out);
            outNeedsEnd = true;
        }
Exemple #10
0
 /// <summary>Execute the upload task on the socket.</summary>
 /// <remarks>Execute the upload task on the socket.</remarks>
 /// <param name="input">
 /// raw input to read client commands from. Caller must ensure the
 /// input is buffered, otherwise read performance may suffer.
 /// </param>
 /// <param name="output">
 /// response back to the Git network client, to write the pack
 /// data onto. Caller must ensure the output is buffered,
 /// otherwise write performance may suffer.
 /// </param>
 /// <param name="messages">
 /// secondary "notice" channel to send additional messages out
 /// through. When run over SSH this should be tied back to the
 /// standard error channel of the command execution. For most
 /// other network connections this should be null.
 /// </param>
 /// <exception cref="System.IO.IOException">System.IO.IOException</exception>
 public virtual void Upload(InputStream input, OutputStream output, OutputStream messages
                            )
 {
     try
     {
         rawIn  = input;
         rawOut = output;
         if (timeout > 0)
         {
             Sharpen.Thread caller = Sharpen.Thread.CurrentThread();
             timer = new InterruptTimer(caller.GetName() + "-Timer");
             TimeoutInputStream  i = new TimeoutInputStream(rawIn, timer);
             TimeoutOutputStream o = new TimeoutOutputStream(rawOut, timer);
             i.SetTimeout(timeout * 1000);
             o.SetTimeout(timeout * 1000);
             rawIn  = i;
             rawOut = o;
         }
         pckIn  = new PacketLineIn(rawIn);
         pckOut = new PacketLineOut(rawOut);
         Service();
     }
     finally
     {
         walk.Release();
         if (timer != null)
         {
             try
             {
                 timer.Terminate();
             }
             finally
             {
                 timer = null;
             }
         }
     }
 }
		public override void Close()
		{
			if (@out != null)
			{
				try
				{
					if (outNeedsEnd)
					{
						outNeedsEnd = false;
						pckOut.End();
					}
					@out.Close();
				}
				catch (IOException)
				{
				}
				finally
				{
					// Ignore any close errors.
					@out = null;
					pckOut = null;
				}
			}
			if (@in != null)
			{
				try
				{
					@in.Close();
				}
				catch (IOException)
				{
				}
				finally
				{
					// Ignore any close errors.
					@in = null;
					pckIn = null;
				}
			}
			if (myTimer != null)
			{
				try
				{
					myTimer.Terminate();
				}
				finally
				{
					myTimer = null;
					timeoutIn = null;
					timeoutOut = null;
				}
			}
		}
		/// <summary>Configure this connection with the directional pipes.</summary>
		/// <remarks>Configure this connection with the directional pipes.</remarks>
		/// <param name="myIn">
		/// input stream to receive data from the peer. Caller must ensure
		/// the input is buffered, otherwise read performance may suffer.
		/// </param>
		/// <param name="myOut">
		/// output stream to transmit data to the peer. Caller must ensure
		/// the output is buffered, otherwise write performance may
		/// suffer.
		/// </param>
		protected internal void Init(InputStream myIn, OutputStream myOut)
		{
			int timeout = transport.GetTimeout();
			if (timeout > 0)
			{
				Sharpen.Thread caller = Sharpen.Thread.CurrentThread();
				myTimer = new InterruptTimer(caller.GetName() + "-Timer");
				timeoutIn = new TimeoutInputStream(myIn, myTimer);
				timeoutOut = new TimeoutOutputStream(myOut, myTimer);
				timeoutIn.SetTimeout(timeout * 1000);
				timeoutOut.SetTimeout(timeout * 1000);
				myIn = timeoutIn;
				myOut = timeoutOut;
			}
			@in = myIn;
			@out = myOut;
			pckIn = new PacketLineIn(@in);
			pckOut = new PacketLineOut(@out);
			outNeedsEnd = true;
		}
Exemple #13
0
 // test support
 private void Init(string msg)
 {
     rawIn = new ByteArrayInputStream(Constants.EncodeASCII(msg));
     @in   = new PacketLineIn(rawIn);
 }
		private string EnableCapabilities(ProgressMonitor monitor)
		{
			StringBuilder line = new StringBuilder();
			capableReport = WantCapability(line, CAPABILITY_REPORT_STATUS);
			capableDeleteRefs = WantCapability(line, CAPABILITY_DELETE_REFS);
			capableOfsDelta = WantCapability(line, CAPABILITY_OFS_DELTA);
			capableSideBand = WantCapability(line, CAPABILITY_SIDE_BAND_64K);
			if (capableSideBand)
			{
				@in = new SideBandInputStream(@in, monitor, GetMessageWriter());
				pckIn = new PacketLineIn(@in);
			}
			if (line.Length > 0)
			{
				Sharpen.Runtime.SetCharAt(line, 0, '\0');
			}
			return line.ToString();
		}
Exemple #15
0
		/// <summary>Execute the upload task on the socket.</summary>
		/// <remarks>Execute the upload task on the socket.</remarks>
		/// <param name="input">
		/// raw input to read client commands from. Caller must ensure the
		/// input is buffered, otherwise read performance may suffer.
		/// </param>
		/// <param name="output">
		/// response back to the Git network client, to write the pack
		/// data onto. Caller must ensure the output is buffered,
		/// otherwise write performance may suffer.
		/// </param>
		/// <param name="messages">
		/// secondary "notice" channel to send additional messages out
		/// through. When run over SSH this should be tied back to the
		/// standard error channel of the command execution. For most
		/// other network connections this should be null.
		/// </param>
		/// <exception cref="System.IO.IOException">System.IO.IOException</exception>
		public virtual void Upload(InputStream input, OutputStream output, OutputStream messages
			)
		{
			try
			{
				rawIn = input;
				rawOut = output;
				if (timeout > 0)
				{
					Sharpen.Thread caller = Sharpen.Thread.CurrentThread();
					timer = new InterruptTimer(caller.GetName() + "-Timer");
					TimeoutInputStream i = new TimeoutInputStream(rawIn, timer);
					TimeoutOutputStream o = new TimeoutOutputStream(rawOut, timer);
					i.SetTimeout(timeout * 1000);
					o.SetTimeout(timeout * 1000);
					rawIn = i;
					rawOut = o;
				}
				pckIn = new PacketLineIn(rawIn);
				pckOut = new PacketLineOut(rawOut);
				Service();
			}
			finally
			{
				walk.Release();
				if (timer != null)
				{
					try
					{
						timer.Terminate();
					}
					finally
					{
						timer = null;
					}
				}
			}
		}
Exemple #16
0
		// test support
		private void Init(string msg)
		{
			rawIn = new ByteArrayInputStream(Constants.EncodeASCII(msg));
			@in = new PacketLineIn(rawIn);
		}
		/// <exception cref="System.IO.IOException"></exception>
		internal virtual void Execute(Socket sock)
		{
			rawIn = new BufferedInputStream(sock.GetInputStream());
			rawOut = new BufferedOutputStream(sock.GetOutputStream());
			if (0 < daemon.GetTimeout())
			{
				sock.ReceiveTimeout = daemon.GetTimeout() * 1000;
			}
			string cmd = new PacketLineIn(rawIn).ReadStringRaw();
			int nul = cmd.IndexOf('\0');
			if (nul >= 0)
			{
				// Newer clients hide a "host" header behind this byte.
				// Currently we don't use it for anything, so we ignore
				// this portion of the command.
				//
				cmd = Sharpen.Runtime.Substring(cmd, 0, nul);
			}
			DaemonService srv = GetDaemon().MatchService(cmd);
			if (srv == null)
			{
				return;
			}
			sock.ReceiveTimeout = 0;
			srv.Execute(this, cmd);
		}
		internal SideBandInputStream(InputStream @in, ProgressMonitor progress, TextWriter
			 messageStream)
		{
			rawIn = @in;
			pckIn = new PacketLineIn(rawIn);
			monitor = progress;
			messages = messageStream;
			currentTask = string.Empty;
		}