예제 #1
0
        public override void ExecuteResult(ControllerContext context)
        {
            var response    = context.HttpContext.Response;
            var request     = context.HttpContext.Request;
            var realRequest = System.Web.HttpContext.Current.Request;

            response.ContentType  = "application/x-git-" + this.action + "-result";
            response.BufferOutput = false;

            using (var git = GitUtilities.Start(string.Format(this.commandFormat, this.action), this.repoPath, redirectInput: true))
            {
                var readThread = new Thread(() =>
                {
                    var readBuffer = new byte[4096];
                    int readCount;

                    Stream wrapperStream = null;
                    try
                    {
                        var input = realRequest.GetBufferlessInputStream(disableMaxRequestLength: true);
                        if (request.Headers["Content-Encoding"] == "gzip")
                        {
                            input = wrapperStream = new GZipStream(input, CompressionMode.Decompress);
                        }

                        while ((readCount = input.Read(readBuffer, 0, readBuffer.Length)) > 0)
                        {
                            git.StandardInput.BaseStream.Write(readBuffer, 0, readCount);
                        }
                    }
                    finally
                    {
                        if (wrapperStream != null)
                        {
                            wrapperStream.Dispose();
                        }
                    }

                    git.StandardInput.Close();
                });
                readThread.Start();

                var    writeBuffer = new byte[4096];
                int    writeCount;
                byte[] copy = null;

                while ((writeCount = git.StandardOutput.BaseStream.Read(writeBuffer, 0, writeBuffer.Length)) > 0)
                {
                    if (copy == null || copy.Length != writeCount)
                    {
                        copy = new byte[writeCount];
                    }

                    for (int i = 0; i < writeCount; i++)
                    {
                        copy[i] = writeBuffer[i];
                    }

                    response.BinaryWrite(copy);
                }

                readThread.Join();
                git.WaitForExit();
            }
        }