|
|
@ -2,147 +2,75 @@ using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.IO;
|
|
|
|
using System.Net;
|
|
|
|
using System.Net;
|
|
|
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
|
|
|
using System.Net.Http.Headers;
|
|
|
|
using System.Text;
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
|
|
namespace nuget_cli
|
|
|
|
namespace nuget_cli
|
|
|
|
{
|
|
|
|
{
|
|
|
|
public static class Helpers
|
|
|
|
public static class Helpers
|
|
|
|
{
|
|
|
|
{
|
|
|
|
static readonly string clientVersion = nameof(Program) + " v1.0";
|
|
|
|
static readonly string clientVersion = "nuget_cli 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, FileInfo fi, string fileContentType,
|
|
|
|
static internal async Task UploadFilesToServer(
|
|
|
|
string apikey)
|
|
|
|
this PushReport report, Uri uri,
|
|
|
|
|
|
|
|
FileInfo fi, string apikey)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
|
|
|
|
try
|
|
|
|
|
|
|
|
|
|
|
|
// "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-Client-Version", clientVersion);
|
|
|
|
|
|
|
|
httpWebRequest.Headers.Add("X-NuGet-ApiKey", apikey);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
httpWebRequest.BeginGetRequestStream((result) =>
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
try
|
|
|
|
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// using (MultipartContent content = new MultipartContent("ascasc"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using (var formdata = new MultipartFormDataContent("NKdKd9Yk"))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
HttpWebRequest request = (HttpWebRequest)result.AsyncState;
|
|
|
|
using (HttpClient client = new HttpClient())
|
|
|
|
using (Stream requestStream = request.EndGetRequestStream(result))
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
WriteMultipartForm(requestStream, boundary, data, fi, fileContentType);
|
|
|
|
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
client.DefaultRequestHeaders.Add("X-NuGet-Client-Version", clientVersion);
|
|
|
|
|
|
|
|
client.DefaultRequestHeaders.Add("X-NuGet-ApiKey", apikey);
|
|
|
|
|
|
|
|
var dispo = new ContentDispositionHeaderValue("file");
|
|
|
|
|
|
|
|
dispo.FileName = fi.Name;
|
|
|
|
|
|
|
|
dispo.CreationDate = fi.CreationTime;
|
|
|
|
|
|
|
|
dispo.DispositionType = "form-data";
|
|
|
|
|
|
|
|
dispo.Size = fi.Length;
|
|
|
|
|
|
|
|
dispo.ModificationDate = fi.LastAccessTime;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Stream fileStream = fi.OpenRead();
|
|
|
|
|
|
|
|
var streamcontent = new StreamContent(fileStream);
|
|
|
|
|
|
|
|
streamcontent.Headers.ContentDisposition = dispo;
|
|
|
|
|
|
|
|
formdata.Add(streamcontent, "file", fi.Name);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// content.Add(formdata);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var response = await client.PutAsync(uri, formdata);
|
|
|
|
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
|
|
|
|
report.StatusCode = response.StatusCode.ToString();
|
|
|
|
|
|
|
|
var respstream = await response.Content.ReadAsStreamAsync();
|
|
|
|
|
|
|
|
var sr = new StreamReader(respstream);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
report.Message = await sr.ReadToEndAsync();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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, 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
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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, boundarybytes.Length);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// 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, boundarybytes.Length);
|
|
|
|
|
|
|
|
WriteToStream(s, string.Format(fileheaderTemplate, "file", fi.Name, fileContentType));
|
|
|
|
|
|
|
|
/// Write the file data to the stream.
|
|
|
|
|
|
|
|
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);
|
|
|
|
catch (Exception rex)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
report.Message = rex.Message;
|
|
|
|
|
|
|
|
report.StatusCode = "internal error";
|
|
|
|
|
|
|
|
Console.Error.WriteLine(rex.Message);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
}
|
|
|
|
/// Writes string to stream. Author : Farhan Ghumra
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
private static void WriteToStream(Stream s, string txt)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
byte[] bytes = Encoding.UTF8.GetBytes(txt);
|
|
|
|
|
|
|
|
s.Write(bytes, 0, bytes.Length);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// Writes byte array to stream. Author : Farhan Ghumra
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
private static void WriteToStream(Stream s, byte[] bytes, int len)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
s.Write(bytes, 0, len);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|