diff --git a/src/nuget-cli/Helpers.cs b/src/nuget-cli/Helpers.cs new file mode 100644 index 0000000..1984d83 --- /dev/null +++ b/src/nuget-cli/Helpers.cs @@ -0,0 +1,143 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Net; +using System.Text; + +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, + string apikey, byte[] fileData) + { + // "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.BeginGetRequestStream((result) => + { + try + { + HttpWebRequest request = (HttpWebRequest)result.AsyncState; + using (Stream requestStream = request.EndGetRequestStream(result)) + { + WriteMultipartForm(requestStream, boundary, data, fileName, fileContentType, apikey, fileData); + } + 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(); + } + + + /// + /// 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) + { + /// 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}"; + /// the form-data file upload, properly formatted + string fileheaderTemplate = "Content-Dis-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) + { + /// if we need to drop a CRLF, do that. + if (bNeedsCRLF) + WriteToStream(s, "\r\n"); + + /// Write the boundary. + WriteToStream(s, boundarybytes); + + /// 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); + WriteToStream(s, string.Format(fileheaderTemplate, "file", fileName, fileContentType)); + /// Write the file data to the stream. + WriteToStream(s, fileData); + WriteToStream(s, trailer); + } + + /// + /// 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) + { + s.Write(bytes, 0, bytes.Length); + } + } +} \ No newline at end of file diff --git a/src/nuget-cli/PushCommand.cs b/src/nuget-cli/PushCommand.cs index e5e4ae9..534ae03 100644 --- a/src/nuget-cli/PushCommand.cs +++ b/src/nuget-cli/PushCommand.cs @@ -8,21 +8,39 @@ namespace nuget_cli { internal class PushCommand { + private static readonly int MAXSENDLEN = 0xffff; + static internal async Task RunAsync(string pkg, string source, string apikey) { - var report = new PushReport { - PkgName = pkg - }; + var report = new PushReport + { + PkgName = pkg + }; FileInfo fi = new FileInfo(pkg); if (!fi.Exists) throw new Exception("Le fichier n'existe pas"); - HttpClient client = new HttpClient(); - client.DefaultRequestHeaders.Add("api-key", apikey); - client.BaseAddress = new Uri(source); - var content = new StreamContent(fi.OpenRead()); - var response = await client.PutAsync(source, content); - report.StatusCode = response.StatusCode.ToString(); - report.Message = await response.Content.ReadAsStringAsync(); + if (fi.Length > MAXSENDLEN) + throw new Exception($"Le fichier ne passe pas, trop gros ({MAXSENDLEN})."); + + var fparams = new Dictionary { { "userid", "9" } }; + + 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); + + } + catch (Exception ex) + { + await Console.Error.WriteLineAsync(ex.Message); + throw; + } + } report.Executed = true; return report; } diff --git a/src/nuget-cli/nuget-cli.csproj b/src/nuget-cli/nuget-cli.csproj index fc7487f..8109df9 100644 --- a/src/nuget-cli/nuget-cli.csproj +++ b/src/nuget-cli/nuget-cli.csproj @@ -1,11 +1,12 @@ - - - - Exe - netcoreapp2.1 - nuget_cli - - - - - + + + + Exe + netcoreapp2.1 + nuget_cli + 45b74c62-05bc-4603-95b4-3e80ae2fdf50 + + + + + diff --git a/src/nuget-host/Controllers/PackagesController.cs b/src/nuget-host/Controllers/PackagesController.cs index 0a38106..78f244e 100644 --- a/src/nuget-host/Controllers/PackagesController.cs +++ b/src/nuget-host/Controllers/PackagesController.cs @@ -92,6 +92,7 @@ namespace nuget_host.Controllers if (!destdir.Exists) destdir.Create(); source.MoveTo(path); + logger.LogWarning($"200: {entry.Name}"); } } @@ -104,12 +105,13 @@ namespace nuget_host.Controllers } else { + logger.LogWarning("400"); return new BadRequestObjectResult(ViewData); } return Ok(ViewData); } - [HttpGet("Packages/{spec}")] + [HttpGet("packages/{spec}")] public IActionResult Index(string spec) { if (string.IsNullOrEmpty(spec))