diff --git a/src/nuget-cli/Helpers.cs b/src/nuget-cli/Helpers.cs index 83818eb..40ba1a7 100644 --- a/src/nuget-cli/Helpers.cs +++ b/src/nuget-cli/Helpers.cs @@ -2,147 +2,75 @@ using System; using System.Collections.Generic; using System.IO; using System.Net; +using System.Net.Http; +using System.Net.Http.Headers; using System.Text; +using System.Threading.Tasks; namespace nuget_cli { public static class Helpers { - static readonly string clientVersion = nameof(Program) + " v1.0"; + static readonly string clientVersion = "nuget_cli v1.0"; /// /// Creates HTTP POST request & uploads database to server. Author : Farhan Ghumra /// - static internal void UploadFilesToServer(this PushReport report, Uri uri, Dictionary data, FileInfo fi, string fileContentType, - string apikey) + static internal async Task UploadFilesToServer( + this PushReport report, Uri uri, + FileInfo fi, string apikey) { - ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; - - // "X-NuGet-ApiKey - string boundary = "----------" + DateTime.Now.Ticks.ToString("x"); - HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri); - httpWebRequest.ContentType = "multipart/form-data; boundary=" + boundary; - httpWebRequest.Method = "PUT"; - httpWebRequest.AllowAutoRedirect = false; - httpWebRequest.Headers.Add("X-NuGet-Client-Version", clientVersion); - httpWebRequest.Headers.Add("X-NuGet-ApiKey", apikey); - - httpWebRequest.BeginGetRequestStream((result) => + try { - try + ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; + + // using (MultipartContent content = new MultipartContent("ascasc")) + + using (var formdata = new MultipartFormDataContent("NKdKd9Yk")) { - HttpWebRequest request = (HttpWebRequest)result.AsyncState; - using (Stream requestStream = request.EndGetRequestStream(result)) + using (HttpClient client = new HttpClient()) { - WriteMultipartForm(requestStream, boundary, data, fi, fileContentType); + client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + + client.DefaultRequestHeaders.Add("X-NuGet-Client-Version", clientVersion); + client.DefaultRequestHeaders.Add("X-NuGet-ApiKey", apikey); + var dispo = new ContentDispositionHeaderValue("file"); + dispo.FileName = fi.Name; + dispo.CreationDate = fi.CreationTime; + dispo.DispositionType = "form-data"; + dispo.Size = fi.Length; + dispo.ModificationDate = fi.LastAccessTime; + + Stream fileStream = fi.OpenRead(); + var streamcontent = new StreamContent(fileStream); + streamcontent.Headers.ContentDisposition = dispo; + formdata.Add(streamcontent, "file", fi.Name); + + // content.Add(formdata); + + var response = await client.PutAsync(uri, formdata); + response.EnsureSuccessStatusCode(); + report.StatusCode = response.StatusCode.ToString(); + var respstream = await response.Content.ReadAsStreamAsync(); + var sr = new StreamReader(respstream); + + report.Message = await sr.ReadToEndAsync(); } - request.BeginGetResponse(a => - { - try - { - var response = request.EndGetResponse(a); - var responseStream = response.GetResponseStream(); - using (var sr = new StreamReader(responseStream)) - { - using (StreamReader streamReader = new StreamReader(response.GetResponseStream())) - { - string responseString = streamReader.ReadToEnd(); - //responseString is depend upon your web service. - if (responseString == "Success") - { - report.Message = "stored successfully on server."; - } - else - { - report.Message = "Error occurred while uploading packet on server."; - } - } - } - } - catch (Exception ex) - { - report.Message = ex.Message; - Console.Error.WriteLine(ex.Message); - } - }, null); } - catch (Exception rex) - { - report.Message = rex.Message; - Console.Error.WriteLine(rex.Message); - } - }, httpWebRequest); - httpWebRequest.GetResponse(); - } - const int MAXSENDLEN = 65636; - /// - /// Writes multi part HTTP POST request. Author : Farhan Ghumra - /// - public static void WriteMultipartForm(this Stream s, string boundary, Dictionary data, FileInfo fi, string fileContentType) - { - /// The first boundary - byte[] boundarybytes = Encoding.UTF8.GetBytes("--" + boundary + "\r\n"); - /// the last boundary. - byte[] trailer = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n"); - - /// the form data, properly formatted - string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}"; - /// the form-data file upload, properly formatted - string fileheaderTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\";\r\nContent-Type: {2}\r\n\r\n"; - - /// Added to track if we need a CRLF or not. - bool bNeedsCRLF = false; - if (data != null) - { - foreach (string key in data.Keys) - { - /// if we need to drop a CRLF, do that. - if (bNeedsCRLF) - WriteToStream(s, "\r\n"); - /// Write the boundary. - WriteToStream(s, boundarybytes, boundarybytes.Length); - /// Write the key. - WriteToStream(s, string.Format(formdataTemplate, key, data[key])); - bNeedsCRLF = true; - } - } - /// If we don't have keys, we don't need a crlf. - if (bNeedsCRLF) - WriteToStream(s, "\r\n"); - - WriteToStream(s, boundarybytes, boundarybytes.Length); - WriteToStream(s, string.Format(fileheaderTemplate, "file", fi.Name, fileContentType)); - /// Write the file data to the stream. - using (var fss = fi.OpenRead()) - { - byte[] buffer = new byte[MAXSENDLEN]; - var form_bytes_read = fss.Read(buffer, 0, MAXSENDLEN); - while (form_bytes_read>0) - WriteToStream(s, buffer, form_bytes_read); } - WriteToStream(s, trailer, trailer.Length); + catch (Exception rex) + { + report.Message = rex.Message; + report.StatusCode = "internal error"; + Console.Error.WriteLine(rex.Message); + } } + } - /// - /// Writes string to stream. Author : Farhan Ghumra - /// - private static void WriteToStream(Stream s, string txt) - { - byte[] bytes = Encoding.UTF8.GetBytes(txt); - s.Write(bytes, 0, bytes.Length); - } +} - /// - /// Writes byte array to stream. Author : Farhan Ghumra - /// - private static void WriteToStream(Stream s, byte[] bytes, int len) - { - s.Write(bytes, 0, len); - } - } -} \ No newline at end of file diff --git a/src/nuget-cli/Program.cs b/src/nuget-cli/Program.cs index 00e6d43..bfc87a2 100644 --- a/src/nuget-cli/Program.cs +++ b/src/nuget-cli/Program.cs @@ -78,7 +78,7 @@ namespace nuget_cli pushoptions.WriteOptionDescriptions(Console.Out); return; } - var reports = await PushPkgAsync(pargs); + List reports = await PushPkgAsync(pargs); Console.WriteLine(JsonConvert.SerializeObject(reports)); } }; diff --git a/src/nuget-cli/PushCommand.cs b/src/nuget-cli/PushCommand.cs index 49cb154..6a84d0b 100644 --- a/src/nuget-cli/PushCommand.cs +++ b/src/nuget-cli/PushCommand.cs @@ -9,44 +9,39 @@ namespace nuget_cli { internal class PushCommand { - private static readonly int MAXSENDLEN = 0xffff; - static internal async Task RunAsync(string pkg, string source, string apikey) { + FileInfo fi = new FileInfo(pkg); var report = new PushReport { - PkgName = pkg + PkgName = fi.Name }; - FileInfo fi = new FileInfo(pkg); if (!fi.Exists) - throw new Exception("Le fichier n'existe pas"); - var fparams = new Dictionary { }; - - - try - { - - report.UploadFilesToServer(new Uri(source), - fparams, fi, "application/octet-stream", - apikey); + { + report.StatusCode = "local"; + report.Message = "Le fichier n'existe pas"; + return report; + } - } - catch (WebException ex) - { - await Console.Error.WriteLineAsync(ex.Message); - report.StatusCode = ex.Status.ToString(); - using (var respStream = ex.Response.GetResponseStream()) - { - - StreamReader sr = new StreamReader(respStream); - report.Message = sr.ReadToEnd(); - } - } - catch (Exception ex) + try + { + await report.UploadFilesToServer(new Uri(source), fi, apikey); + } + catch (WebException ex) + { + await Console.Error.WriteLineAsync(ex.Message); + report.StatusCode = ex.Status.ToString(); + using (var respStream = ex.Response.GetResponseStream()) { - await Console.Error.WriteLineAsync(ex.Message); - throw; + StreamReader sr = new StreamReader(respStream); + report.Message = sr.ReadToEnd(); } + } + catch (Exception ex) + { + await Console.Error.WriteLineAsync(ex.Message); + throw; + } report.Executed = true; return report; } diff --git a/src/nuget-cli/PushReport.cs b/src/nuget-cli/PushReport.cs index d4062fd..b9f8815 100644 --- a/src/nuget-cli/PushReport.cs +++ b/src/nuget-cli/PushReport.cs @@ -1,11 +1,11 @@ namespace nuget_cli { - internal class PushReport + public class PushReport { - internal string PkgName { get; set; } - internal bool Executed { get; set; } - internal bool AlreadyPresent { get; set; } - internal string Message { get; set; } - internal string StatusCode { get; set; } + public string PkgName { get; set; } + public bool Executed { get; set; } + public bool AlreadyPresent { get; set; } + public string Message { get; set; } + public string StatusCode { get; set; } } } \ No newline at end of file diff --git a/test/nuget.host.tests/UnitTestWebHost.cs b/test/nuget.host.tests/UnitTestWebHost.cs index d763130..f00e5d1 100644 --- a/test/nuget.host.tests/UnitTestWebHost.cs +++ b/test/nuget.host.tests/UnitTestWebHost.cs @@ -20,7 +20,7 @@ namespace nuget.host.tests [Fact] - public async Task TestHaveTestDbContext() + public void TestHaveTestDbContext() { string envVar = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); Assert.Equal("Development", envVar); diff --git a/test/nuget.host.tests/nuget.host.tests.csproj b/test/nuget.host.tests/nuget.host.tests.csproj index 35fb49e..658bc4e 100644 --- a/test/nuget.host.tests/nuget.host.tests.csproj +++ b/test/nuget.host.tests/nuget.host.tests.csproj @@ -8,8 +8,8 @@ - +