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
{
static readonly string clientVersion = nameof(Program) + " v1.0";
/// <summary>
/// Creates HTTP POST request & uploads database to server. Author : Farhan Ghumra
/// </summary>
static internal void UploadFilesToServer(this PushReport report, Uri uri, Dictionary<string, string> data, string fileName, string fileContentType,
string apikey, byte[] fileData)
static internal void UploadFilesToServer(this PushReport report, Uri uri, Dictionary<string, string> data, FileInfo fi, string fileContentType,
string apikey)
{
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
@ -23,6 +24,7 @@ namespace nuget_cli
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) =>
@ -32,7 +34,7 @@ namespace nuget_cli
HttpWebRequest request = (HttpWebRequest)result.AsyncState;
using (Stream requestStream = request.EndGetRequestStream(result))
{
WriteMultipartForm(requestStream, boundary, data, fileName, fileContentType, fileData);
WriteMultipartForm(requestStream, boundary, data, fi, fileContentType);
}
request.BeginGetResponse(a =>
{
@ -73,20 +75,18 @@ namespace nuget_cli
httpWebRequest.GetResponse();
}
const int MAXSENDLEN = 65636;
/// <summary>
/// Writes multi part HTTP POST request. Author : Farhan Ghumra
/// </summary>
public static void WriteMultipartForm(this Stream s, string boundary, Dictionary<string, string> data, string fileName, string fileContentType,
byte[] fileData)
public static void WriteMultipartForm(this Stream s, string boundary, Dictionary<string, string> 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
@ -103,7 +103,7 @@ namespace nuget_cli
WriteToStream(s, "\r\n");
/// Write the boundary.
WriteToStream(s, boundarybytes);
WriteToStream(s, boundarybytes, boundarybytes.Length);
/// Write the key.
WriteToStream(s, string.Format(formdataTemplate, key, data[key]));
@ -115,11 +115,17 @@ namespace nuget_cli
if (bNeedsCRLF)
WriteToStream(s, "\r\n");
WriteToStream(s, boundarybytes);
WriteToStream(s, string.Format(fileheaderTemplate, "file", fileName, fileContentType));
WriteToStream(s, boundarybytes, boundarybytes.Length);
WriteToStream(s, string.Format(fileheaderTemplate, "file", fi.Name, fileContentType));
/// Write the file data to the stream.
WriteToStream(s, fileData);
WriteToStream(s, trailer);
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);
}
/// <summary>
@ -134,9 +140,9 @@ namespace nuget_cli
/// <summary>
/// Writes byte array to stream. Author : Farhan Ghumra
/// </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.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
namespace nuget_cli
{
[Display(Name="nuget_cli")]
partial class Program
{

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

@ -20,20 +20,15 @@ namespace nuget_cli
FileInfo fi = new FileInfo(pkg);
if (!fi.Exists)
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> { };
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);
fparams, fi, "application/octet-stream",
apikey);
}
catch (WebException ex)
@ -52,7 +47,6 @@ namespace nuget_cli
await Console.Error.WriteLineAsync(ex.Message);
throw;
}
}
report.Executed = true;
return report;
}

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

@ -47,6 +47,8 @@ namespace nuget_host.Controllers
var clientVersionId = Request.Headers["X-NuGet-Client-Version"];
var apiKey = Request.Headers["X-NuGet-ApiKey"];
ViewData["nuget client"] = "nuget {clientVersionId}";
var files = new List<string>();
ViewData["files"] = files;
var clearkey = protector.Unprotect(apiKey);
var apikey = dbContext.ApiKeys.SingleOrDefault(k => k.Id == clearkey);
@ -55,6 +57,9 @@ namespace nuget_host.Controllers
foreach (var file in Request.Form.Files)
{
try
{
files.Add(file.Name);
string initpath = "package.nupkg";
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);
}

Loading…