Example #1
0
        private void PushBinary(string targetUri, byte[] bytes)
        {
            try
            {
                using (var client = new AccurateWebClient(bytes.Length))
                {
                    Console.WriteLine("HTTP PUT " + targetUri);
                    var       done   = false;
                    byte[]    result = null;
                    Exception ex     = null;
                    var       uploadProgressRenderer = new UploadProgressRenderer();
                    client.UploadDataCompleted += (sender, e) => {
                        if (e.Error != null)
                        {
                            ex = e.Error;
                        }

                        result = e.Result;
                        done   = true;
                    };
                    client.UploadProgressChanged += (sender, e) => {
                        if (!done)
                        {
                            uploadProgressRenderer.Update(e.ProgressPercentage, e.BytesSent / 1024);
                        }
                    };
                    client.UploadDataAsync(new Uri(targetUri), "PUT", bytes);
                    while (!done)
                    {
                        System.Threading.Thread.Sleep(0);
                    }

                    uploadProgressRenderer.Finalize();

                    if (ex != null)
                    {
                        throw new InvalidOperationException("Upload error", ex);
                    }

                    return;
                }
            }
            catch (WebException)
            {
                if (targetUri.StartsWith("https://"))
                {
                    // Attempt fallback to HTTP.
                    Console.Error.WriteLine("Web exception while using HTTPS; attempting HTTP fallback...");
                    targetUri = "http" + targetUri.Substring("https".Length);
                    PushBinary(targetUri, bytes);
                    return;
                }

                Console.WriteLine("Web exception when sending to: " + targetUri);
                throw;
            }
        }
        private void PushBinary(string targetUri, string file)
        {
            byte[] bytes;
            using (var stream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.None))
            {
                bytes = new byte[(int)stream.Length];
                stream.Read(bytes, 0, bytes.Length);
            }

            try
            {
                using (var client = new AccurateWebClient(bytes.Length))
                {
                    Console.WriteLine("HTTP PUT " + targetUri);
                    var       done   = false;
                    byte[]    result = null;
                    Exception ex     = null;
                    var       uploadProgressRenderer = new UploadProgressRenderer();
                    client.UploadDataCompleted += (sender, e) => {
                        if (e.Error != null)
                        {
                            ex = e.Error;
                        }

                        result = e.Result;
                        done   = true;
                    };
                    client.UploadProgressChanged += (sender, e) => {
                        if (!done)
                        {
                            uploadProgressRenderer.Update(e.ProgressPercentage, e.BytesSent / 1024);
                        }
                    };
                    client.UploadDataAsync(new Uri(targetUri), "PUT", bytes);
                    while (!done)
                    {
                        System.Threading.Thread.Sleep(0);
                    }

                    Console.WriteLine();

                    if (ex != null)
                    {
                        throw new InvalidOperationException("Upload error", ex);
                    }

                    return;
                }
            }
            catch (WebException)
            {
                Console.WriteLine("Web exception when sending to: " + targetUri);
                throw;
            }
        }
        private void PushBinary(string targetUri, byte[] bytes)
        {
            using (var client = new RetryableWebClient())
            {
                var       done   = false;
                byte[]    result = null;
                Exception ex     = null;
                var       uploadProgressRenderer = new UploadProgressRenderer();
                client.UploadDataCompleted += (sender, e) => {
                    if (e.Error != null)
                    {
                        ex = e.Error;
                    }

                    result = e.Result;
                    done   = true;
                };
                client.UploadProgressChanged += (sender, e) => {
                    if (!done)
                    {
                        uploadProgressRenderer.Update(e.ProgressPercentage, e.BytesSent / 1024);
                    }
                };
                client.UploadDataAsync(new Uri(targetUri), "PUT", bytes);
                while (!done)
                {
                    System.Threading.Thread.Sleep(0);
                }

                uploadProgressRenderer.FinalizeRendering();

                if (ex != null)
                {
                    throw new InvalidOperationException("Upload error", ex);
                }

                return;
            }
        }
        private void PushBinary(string targetUri, string file)
        {
            byte[] bytes;
            using (var stream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.None))
            {
                bytes = new byte[(int)stream.Length];
                stream.Read(bytes, 0, bytes.Length);
            }

            try
            {
                using (var client = new AccurateWebClient(bytes.Length))
                {
                    Console.WriteLine("HTTP PUT " + targetUri);
                    var done = false;
                    byte[] result = null;
                    Exception ex = null;
                    var uploadProgressRenderer = new UploadProgressRenderer();
                    client.UploadDataCompleted += (sender, e) => {
                        if (e.Error != null)
                        {
                            ex = e.Error;
                        }

                        result = e.Result;
                        done = true;
                    };
                    client.UploadProgressChanged += (sender, e) => {
                        if (!done)
                        {
                            uploadProgressRenderer.Update(e.ProgressPercentage, e.BytesSent / 1024);
                        }
                    };
                    client.UploadDataAsync(new Uri(targetUri), "PUT", bytes);
                    while (!done)
                    {
                        System.Threading.Thread.Sleep(0);
                    }

                    uploadProgressRenderer.Finalize();

                    if (ex != null)
                    {
                        throw new InvalidOperationException("Upload error", ex);
                    }

                    return;
                }
            }
            catch (WebException)
            {
                if (targetUri.StartsWith("https://"))
                {
                    // Attempt fallback to HTTP.
                    Console.Error.WriteLine("Web exception while using HTTPS; attempting HTTP fallback...");
                    targetUri = "http" + targetUri.Substring("https".Length);
                    PushBinary(targetUri, file);
                    return;
                }

                Console.WriteLine("Web exception when sending to: " + targetUri);
                throw;
            }
        }
        private void PushBinary(string targetUri, byte[] bytes)
        {
            try
            {
                using (var client = new AccurateWebClient(bytes.Length))
                {
                    Console.WriteLine("HTTP PUT " + targetUri);
                    var done = false;
                    byte[] result = null;
                    Exception ex = null;
                    var uploadProgressRenderer = new UploadProgressRenderer();
                    client.UploadDataCompleted += (sender, e) => {
                        if (e.Error != null)
                        {
                            ex = e.Error;
                        }

                        result = e.Result;
                        done = true;
                    };
                    client.UploadProgressChanged += (sender, e) => {
                        if (!done)
                        {
                            uploadProgressRenderer.Update(e.ProgressPercentage, e.BytesSent / 1024);
                        }
                    };
                    client.UploadDataAsync(new Uri(targetUri), "PUT", bytes);
                    while (!done)
                    {
                        System.Threading.Thread.Sleep(0);
                    }

                    uploadProgressRenderer.Finalize();

                    if (ex != null)
                    {
                        throw new InvalidOperationException("Upload error", ex);
                    }

                    return;
                }
            }
            catch (WebException)
            {
                Console.WriteLine("Web exception when sending to: " + targetUri);
                throw;
            }
        }
        private async Task <int> PushNuGetBinaryAsync(string targetUri, string apiKey, MemoryStream file, bool ignoreOnExisting)
        {
            byte[] fileBytes;
            fileBytes = new byte[(int)file.Length];
            file.Read(fileBytes, 0, fileBytes.Length);

            const string ApiKeyHeader = "X-NuGet-ApiKey";

            var boundary = Guid.NewGuid().ToString();

            byte[] boundaryBytes = Encoding.UTF8.GetBytes("--" + boundary + "\r\n");
            byte[] trailer       = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");
            byte[] header        =
                Encoding.UTF8.GetBytes(
                    string.Format(
                        "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\";\r\nContent-Type: {2}\r\n\r\n",
                        "package", "package.nupkg",
                        "application/octet-stream"));

            var requestLength   = boundaryBytes.Length + header.Length + trailer.Length + fileBytes.Length;
            var combinedContent = new byte[requestLength];

            if (combinedContent == null)
            {
                throw new InvalidOperationException("Unable to create byte array " + requestLength + " bytes long for upload!");
            }

            boundaryBytes.CopyTo(combinedContent, 0);
            header.CopyTo(combinedContent, boundaryBytes.Length);
            fileBytes.CopyTo(combinedContent, boundaryBytes.Length + header.Length);
            trailer.CopyTo(combinedContent, boundaryBytes.Length + header.Length + fileBytes.Length);

            using (var client = new RetryableWebClient())
            {
                var       done   = false;
                byte[]    result = null;
                Exception ex     = null;
                var       uploadProgressRenderer = new UploadProgressRenderer();
                client.UploadDataCompleted += (sender, e) => {
                    if (e.Error != null)
                    {
                        ex = e.Error;
                    }

                    try
                    {
                        result = e.Result;
                    }
                    catch { }
                    done = true;
                };
                client.UploadProgressChanged += (sender, e) => {
                    if (!done)
                    {
                        uploadProgressRenderer.Update(e.ProgressPercentage, e.BytesSent / 1024);
                    }
                };

                client.SetHeader("Content-Type", "multipart/form-data; boundary=\"" + boundary + "\"");
                if (!string.IsNullOrWhiteSpace(apiKey))
                {
                    client.SetHeader(ApiKeyHeader, apiKey);
                }

                client.SetHeader("X-NuGet-Protocol-Version", "4.1.0");
                client.SetHeader("User-Agent", "Protobuild");

                client.UploadDataAsync(new Uri(targetUri), "PUT", combinedContent);
                while (!done)
                {
                    System.Threading.Thread.Sleep(0);
                }

                uploadProgressRenderer.FinalizeRendering();

                if (ex != null)
                {
                    var webException = ex as WebException;
                    if (webException != null)
                    {
                        var httpResponse = webException.Response as HttpWebResponse;
                        if (httpResponse != null)
                        {
                            RedirectableConsole.ErrorWriteLine(httpResponse.StatusDescription);

                            if (httpResponse.StatusCode == HttpStatusCode.Conflict)
                            {
                                if (ignoreOnExisting)
                                {
                                    // This is okay - the user doesn't care if there's an existing package with the same version.
                                    return(0);
                                }
                                else
                                {
                                    return(1);
                                }
                            }

                            return(1);
                        }
                    }
                    else
                    {
                        throw new InvalidOperationException("Upload error", ex);
                    }
                }

                if (result != null)
                {
                    var resultDecoded = Encoding.UTF8.GetString(result);
                    if (!string.IsNullOrWhiteSpace(resultDecoded))
                    {
                        RedirectableConsole.WriteLine(Encoding.UTF8.GetString(result));
                    }
                    else
                    {
                        RedirectableConsole.WriteLine("Package uploaded successfully");
                    }
                }
                else
                {
                    RedirectableConsole.WriteLine("Package uploaded successfully");
                }

                return(0);
            }
        }