broken/ef
Paul Schneider 3 years ago
parent 512495d10b
commit 97dec15153
5 changed files with 75 additions and 78 deletions

@ -8,17 +8,23 @@ namespace nuget_cli
{
public static class Helpers
{
/// <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)
{
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 =>
{
@ -72,29 +78,22 @@ namespace nuget_cli
/// 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,
string apiKey, byte[] fileData)
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");
/// <summary>
/// Api Key header template
/// </summary>
/// <value></value>
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)

@ -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,18 +23,29 @@ namespace nuget_cli
if (fi.Length > MAXSENDLEN)
throw new Exception($"Le fichier ne passe pas, trop gros ({MAXSENDLEN}).");
var fparams = new Dictionary<string, string> { { "userid", "9" } };
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);
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)
{

@ -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 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"});
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)
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))
{
var archive = new System.IO.Compression.ZipArchive(fw);
using (FileStream fw = new FileStream(initpath, FileMode.Open))
{
var archive = new System.IO.Compression.ZipArchive(fw);
foreach (var entry in archive.Entries)
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);
}

@ -77,8 +77,6 @@ namespace nuget_host
app.UseStaticFiles();
app.UseHttpsRedirection();
app.UseAuthentication();

@ -16,7 +16,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../../src/nuget-host/nuget-host.csproj" />
<ProjectReference Include="..\..\src\nuget-host\nuget-host.csproj" />
<ProjectReference Include="..\..\src\nuget-cli\nuget-cli.csproj" />
</ItemGroup>

Loading…