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