|
|
|
@ -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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|