broken/ef
Paul Schneider 3 years ago
parent a8c9b7619f
commit c2f38800aa
6 changed files with 75 additions and 58 deletions

@ -8,12 +8,13 @@ namespace nuget_cli
{ {
public static class Helpers public static class Helpers
{ {
static readonly string clientVersion = nameof(Program) + " v1.0";
/// <summary> /// <summary>
/// Creates HTTP POST request & uploads database to server. Author : Farhan Ghumra /// Creates HTTP POST request & uploads database to server. Author : Farhan Ghumra
/// </summary> /// </summary>
static internal void UploadFilesToServer(this PushReport report, Uri uri, Dictionary<string, string> data, string fileName, string fileContentType, static internal void UploadFilesToServer(this PushReport report, Uri uri, Dictionary<string, string> data, FileInfo fi, string fileContentType,
string apikey, byte[] fileData) string apikey)
{ {
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
@ -23,6 +24,7 @@ namespace nuget_cli
httpWebRequest.ContentType = "multipart/form-data; boundary=" + boundary; httpWebRequest.ContentType = "multipart/form-data; boundary=" + boundary;
httpWebRequest.Method = "PUT"; httpWebRequest.Method = "PUT";
httpWebRequest.AllowAutoRedirect = false; httpWebRequest.AllowAutoRedirect = false;
httpWebRequest.Headers.Add("X-NuGet-Client-Version", clientVersion);
httpWebRequest.Headers.Add("X-NuGet-ApiKey", apikey); httpWebRequest.Headers.Add("X-NuGet-ApiKey", apikey);
httpWebRequest.BeginGetRequestStream((result) => httpWebRequest.BeginGetRequestStream((result) =>
@ -32,7 +34,7 @@ namespace nuget_cli
HttpWebRequest request = (HttpWebRequest)result.AsyncState; HttpWebRequest request = (HttpWebRequest)result.AsyncState;
using (Stream requestStream = request.EndGetRequestStream(result)) using (Stream requestStream = request.EndGetRequestStream(result))
{ {
WriteMultipartForm(requestStream, boundary, data, fileName, fileContentType, fileData); WriteMultipartForm(requestStream, boundary, data, fi, fileContentType);
} }
request.BeginGetResponse(a => request.BeginGetResponse(a =>
{ {
@ -73,20 +75,18 @@ namespace nuget_cli
httpWebRequest.GetResponse(); httpWebRequest.GetResponse();
} }
const int MAXSENDLEN = 65636;
/// <summary> /// <summary>
/// Writes multi part HTTP POST request. Author : Farhan Ghumra /// Writes multi part HTTP POST request. Author : Farhan Ghumra
/// </summary> /// </summary>
public static void WriteMultipartForm(this Stream s, string boundary, Dictionary<string, string> data, string fileName, string fileContentType, public static void WriteMultipartForm(this Stream s, string boundary, Dictionary<string, string> data, FileInfo fi, string fileContentType)
byte[] fileData)
{ {
/// The first boundary /// The first boundary
byte[] boundarybytes = Encoding.UTF8.GetBytes("--" + boundary + "\r\n"); byte[] boundarybytes = Encoding.UTF8.GetBytes("--" + boundary + "\r\n");
/// the last boundary. /// the last boundary.
byte[] trailer = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n"); byte[] trailer = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");
/// the form data, properly formatted /// the form data, properly formatted
string formdataTemplate = "Content-Disposition: form-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 /// the form-data file upload, properly formatted
@ -103,7 +103,7 @@ namespace nuget_cli
WriteToStream(s, "\r\n"); WriteToStream(s, "\r\n");
/// Write the boundary. /// Write the boundary.
WriteToStream(s, boundarybytes); WriteToStream(s, boundarybytes, boundarybytes.Length);
/// Write the key. /// Write the key.
WriteToStream(s, string.Format(formdataTemplate, key, data[key])); WriteToStream(s, string.Format(formdataTemplate, key, data[key]));
@ -115,11 +115,17 @@ namespace nuget_cli
if (bNeedsCRLF) if (bNeedsCRLF)
WriteToStream(s, "\r\n"); WriteToStream(s, "\r\n");
WriteToStream(s, boundarybytes); WriteToStream(s, boundarybytes, boundarybytes.Length);
WriteToStream(s, string.Format(fileheaderTemplate, "file", fileName, fileContentType)); WriteToStream(s, string.Format(fileheaderTemplate, "file", fi.Name, fileContentType));
/// Write the file data to the stream. /// Write the file data to the stream.
WriteToStream(s, fileData); using (var fss = fi.OpenRead())
WriteToStream(s, trailer); {
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);
} }
/// <summary> /// <summary>
@ -134,9 +140,9 @@ namespace nuget_cli
/// <summary> /// <summary>
/// Writes byte array to stream. Author : Farhan Ghumra /// Writes byte array to stream. Author : Farhan Ghumra
/// </summary> /// </summary>
private static void WriteToStream(Stream s, byte[] bytes) private static void WriteToStream(Stream s, byte[] bytes, int len)
{ {
s.Write(bytes, 0, bytes.Length); s.Write(bytes, 0, len);
} }
} }
} }

@ -1,9 +1,11 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace nuget_cli namespace nuget_cli
{ {
[Display(Name="nuget_cli")]
partial class Program partial class Program
{ {

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using Mono.Options; using Mono.Options;
using Newtonsoft.Json;
namespace nuget_cli namespace nuget_cli
{ {
@ -73,11 +74,12 @@ namespace nuget_cli
if (shouldShowPushHelp) if (shouldShowPushHelp)
{ {
// output the options // output the options
Console.WriteLine("Push Options:"); Console.Error.WriteLine("Push Options:");
pushoptions.WriteOptionDescriptions(Console.Out); pushoptions.WriteOptionDescriptions(Console.Out);
return; return;
} }
await PushPkgAsync(pargs); var reports = await PushPkgAsync(pargs);
Console.WriteLine(JsonConvert.SerializeObject(reports));
} }
}; };
commandSet.Add(push); commandSet.Add(push);

@ -20,20 +20,15 @@ namespace nuget_cli
FileInfo fi = new FileInfo(pkg); FileInfo fi = new FileInfo(pkg);
if (!fi.Exists) if (!fi.Exists)
throw new Exception("Le fichier n'existe pas"); throw new Exception("Le fichier n'existe pas");
if (fi.Length > MAXSENDLEN)
throw new Exception($"Le fichier ne passe pas, trop gros ({MAXSENDLEN}).");
var fparams = new Dictionary<string, string> { }; var fparams = new Dictionary<string, string> { };
using (var fss = fi.OpenRead())
{
try try
{ {
byte[] buffer = new byte[MAXSENDLEN];
var form_bytes_read = fss.Read(buffer, 0, MAXSENDLEN);
report.UploadFilesToServer(new Uri(source), report.UploadFilesToServer(new Uri(source),
fparams, fi.Name, "application/octet-stream", fparams, fi, "application/octet-stream",
apikey, buffer); apikey);
} }
catch (WebException ex) catch (WebException ex)
@ -52,7 +47,6 @@ namespace nuget_cli
await Console.Error.WriteLineAsync(ex.Message); await Console.Error.WriteLineAsync(ex.Message);
throw; throw;
} }
}
report.Executed = true; report.Executed = true;
return report; return report;
} }

@ -8,5 +8,6 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Mono.Options" Version="5.3.0" /> <PackageReference Include="Mono.Options" Version="5.3.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup> </ItemGroup>
</Project> </Project>

@ -47,6 +47,8 @@ namespace nuget_host.Controllers
var clientVersionId = Request.Headers["X-NuGet-Client-Version"]; var clientVersionId = Request.Headers["X-NuGet-Client-Version"];
var apiKey = Request.Headers["X-NuGet-ApiKey"]; var apiKey = Request.Headers["X-NuGet-ApiKey"];
ViewData["nuget client"] = "nuget {clientVersionId}"; ViewData["nuget client"] = "nuget {clientVersionId}";
var files = new List<string>();
ViewData["files"] = files;
var clearkey = protector.Unprotect(apiKey); var clearkey = protector.Unprotect(apiKey);
var apikey = dbContext.ApiKeys.SingleOrDefault(k => k.Id == clearkey); var apikey = dbContext.ApiKeys.SingleOrDefault(k => k.Id == clearkey);
@ -55,6 +57,9 @@ namespace nuget_host.Controllers
foreach (var file in Request.Form.Files) foreach (var file in Request.Form.Files)
{ {
try
{
files.Add(file.Name);
string initpath = "package.nupkg"; string initpath = "package.nupkg";
using (FileStream fw = new FileStream(initpath, FileMode.Create)) using (FileStream fw = new FileStream(initpath, FileMode.Create))
{ {
@ -96,6 +101,13 @@ namespace nuget_host.Controllers
} }
} }
catch (Exception ex)
{
logger.LogError($"400: {file.Name}");
logger.LogError(ex.Message);
return new BadRequestObjectResult(ViewData);
}
}
return Ok(ViewData); return Ok(ViewData);
} }

Loading…