diff --git a/src/nuget-cli/Helpers.cs b/src/nuget-cli/Helpers.cs index 1984d83..360e3f8 100644 --- a/src/nuget-cli/Helpers.cs +++ b/src/nuget-cli/Helpers.cs @@ -8,17 +8,23 @@ namespace nuget_cli { public static class Helpers { + /// /// Creates HTTP POST request & uploads database to server. Author : Farhan Ghumra /// - static internal void UploadFilesToServer(this PushReport report, Uri uri, Dictionary data, string fileName, string fileContentType, + static internal void UploadFilesToServer(this PushReport report, Uri uri, Dictionary data, string fileName, string fileContentType, string apikey, byte[] fileData) { + 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-ApiKey", apikey); + httpWebRequest.BeginGetRequestStream((result) => { try @@ -26,7 +32,7 @@ namespace nuget_cli HttpWebRequest request = (HttpWebRequest)result.AsyncState; using (Stream requestStream = request.EndGetRequestStream(result)) { - WriteMultipartForm(requestStream, boundary, data, fileName, fileContentType, apikey, fileData); + WriteMultipartForm(requestStream, boundary, data, fileName, fileContentType, fileData); } request.BeginGetResponse(a => { @@ -71,30 +77,23 @@ namespace nuget_cli /// /// Writes multi part HTTP POST request. Author : Farhan Ghumra /// - public static void WriteMultipartForm(this Stream s, string boundary, Dictionary data, string fileName, string fileContentType, - string apiKey, byte[] fileData) + public static void WriteMultipartForm(this Stream s, string boundary, Dictionary data, string fileName, string fileContentType, + byte[] fileData) { /// The first boundary byte[] boundarybytes = Encoding.UTF8.GetBytes("--" + boundary + "\r\n"); /// the last boundary. byte[] trailer = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n"); - - /// - /// Api Key header template - /// - /// - string apiKeyHeaderTemplate = "X-NuGet-ApiKey: \"{0}\"\r\n"; + + /// the form data, properly formatted - string formdataTemplate = "Content-Dis-data; name=\"{0}\"\r\n\r\n{1}"; + string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}"; /// the form-data file upload, properly formatted - string fileheaderTemplate = "Content-Dis-data; name=\"{0}\"; filename=\"{1}\";\r\nContent-Type: {2}\r\n\r\n"; + 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; - - WriteToStream(s, string.Format(apiKeyHeaderTemplate, apiKey)); - if (data != null) { foreach (string key in data.Keys) diff --git a/src/nuget-cli/PushCommand.cs b/src/nuget-cli/PushCommand.cs index 534ae03..e8bac27 100644 --- a/src/nuget-cli/PushCommand.cs +++ b/src/nuget-cli/PushCommand.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Net; using System.Net.Http; using System.Threading.Tasks; @@ -22,19 +23,30 @@ namespace nuget_cli if (fi.Length > MAXSENDLEN) throw new Exception($"Le fichier ne passe pas, trop gros ({MAXSENDLEN})."); - var fparams = new Dictionary { { "userid", "9" } }; + var fparams = new Dictionary { }; using (var fss = fi.OpenRead()) { - try { byte[] buffer = new byte[MAXSENDLEN]; var form_bytes_read = fss.Read(buffer, 0, MAXSENDLEN); - report.UploadFilesToServer(new Uri(source), fparams, fi.Name, "application/octet-stream", - apikey, buffer); + report.UploadFilesToServer(new Uri(source), + fparams, fi.Name, "application/octet-stream", + apikey, buffer); } + 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) { await Console.Error.WriteLineAsync(ex.Message); diff --git a/src/nuget-host/Controllers/PackagesController.cs b/src/nuget-host/Controllers/PackagesController.cs index 78f244e..5eebe7f 100644 --- a/src/nuget-host/Controllers/PackagesController.cs +++ b/src/nuget-host/Controllers/PackagesController.cs @@ -39,75 +39,63 @@ namespace nuget_host.Controllers this.dbContext = dbContext; } - [HttpPut("packages/{*spec}")] - public IActionResult Put(string spec) + [HttpPut("packages")] + public IActionResult Put() { string path = null; - - if (string.IsNullOrEmpty(spec)) + + var clientVersionId = Request.Headers["X-NuGet-Client-Version"]; + var apiKey = Request.Headers["X-NuGet-ApiKey"]; + ViewData["nuget client"] = "nuget {clientVersionId}"; + + var clearkey = protector.Unprotect(apiKey); + var apikey = dbContext.ApiKeys.SingleOrDefault(k => k.Id == clearkey); + if (apikey == null) + return new BadRequestObjectResult(new { error = "api-key" }); + + foreach (var file in Request.Form.Files) { - var clientVersionId = Request.Headers["X-NuGet-Client-Version"]; - var apiKey = Request.Headers["X-NuGet-ApiKey"]; - ViewData["nuget client"] = "nuget {clientVersionId}"; - - var clearkey = protector.Unprotect(apiKey); - var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); - var apikey = dbContext.ApiKeys.SingleOrDefault(k => k.Id == clearkey); - if (apikey == null) - return new BadRequestObjectResult(new {error = "api-key"}); - - foreach (var file in Request.Form.Files) + string initpath = "package.nupkg"; + using (FileStream fw = new FileStream(initpath, FileMode.Create)) { - string initpath = "package.nupkg"; - using (FileStream fw = new FileStream(initpath, FileMode.Create)) - { - file.CopyTo(fw); - } + file.CopyTo(fw); + } - using (FileStream fw = new FileStream(initpath, FileMode.Open)) + using (FileStream fw = new FileStream(initpath, FileMode.Open)) + { + var archive = new System.IO.Compression.ZipArchive(fw); + + foreach (var entry in archive.Entries) { - var archive = new System.IO.Compression.ZipArchive(fw); - - foreach (var entry in archive.Entries) + if (entry.FullName.EndsWith(".nuspec")) { - if (entry.FullName.EndsWith(".nuspec")) - { - // var entry = archive.GetEntry(filename); - var specstr = entry.Open(); - NuGet.Packaging.Core.NuspecCoreReader reader = new NuspecCoreReader(specstr); - - string pkgdesc = reader.GetDescription(); - string pkgid = reader.GetId(); - var version = reader.GetVersion(); - - path = Path.Combine(nugetSettings.PackagesRootDir, - Path.Combine(pkgid, - Path.Combine(version.Version.ToString()), - $"{pkgid}-{version}.nupkg")); - var source = new FileInfo(initpath); - var dest = new FileInfo(path); - var destdir = new DirectoryInfo(dest.DirectoryName); - if (dest.Exists) - return BadRequest(new {error = "existant"}); - - if (!destdir.Exists) destdir.Create(); - source.MoveTo(path); - logger.LogWarning($"200: {entry.Name}"); - } - + // var entry = archive.GetEntry(filename); + var specstr = entry.Open(); + NuGet.Packaging.Core.NuspecCoreReader reader = new NuspecCoreReader(specstr); + + string pkgdesc = reader.GetDescription(); + string pkgid = reader.GetId(); + var version = reader.GetVersion(); + + path = Path.Combine(nugetSettings.PackagesRootDir, + Path.Combine(pkgid, + Path.Combine(version.Version.ToString()), + $"{pkgid}-{version}.nupkg")); + var source = new FileInfo(initpath); + var dest = new FileInfo(path); + var destdir = new DirectoryInfo(dest.DirectoryName); + if (dest.Exists) + return BadRequest(new { error = "existant" }); + + if (!destdir.Exists) destdir.Create(); + source.MoveTo(path); + logger.LogWarning($"200: {entry.Name}"); } } - - } } - else - { - logger.LogWarning("400"); - return new BadRequestObjectResult(ViewData); - } return Ok(ViewData); } diff --git a/src/nuget-host/Startup.cs b/src/nuget-host/Startup.cs index 8cf2307..b7f4079 100644 --- a/src/nuget-host/Startup.cs +++ b/src/nuget-host/Startup.cs @@ -77,8 +77,6 @@ namespace nuget_host app.UseStaticFiles(); - app.UseHttpsRedirection(); - app.UseAuthentication(); diff --git a/test/nuget.host.tests/nuget.host.tests.csproj b/test/nuget.host.tests/nuget.host.tests.csproj index eabbc57..35fb49e 100644 --- a/test/nuget.host.tests/nuget.host.tests.csproj +++ b/test/nuget.host.tests/nuget.host.tests.csproj @@ -16,7 +16,7 @@ - +