isn/src/nuget-cli/Helpers.cs

142 lines
5.6 KiB
C#
Raw Normal View History

2021-05-11 02:38:52 +01:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
namespace nuget_cli
{
public static class Helpers
{
2021-05-11 23:01:11 +01:00
2021-05-11 02:38:52 +01:00
/// <summary>
/// Creates HTTP POST request & uploads database to server. Author : Farhan Ghumra
/// </summary>
2021-05-11 23:01:11 +01:00
static internal void UploadFilesToServer(this PushReport report, Uri uri, Dictionary<string, string> data, string fileName, string fileContentType,
2021-05-11 02:38:52 +01:00
string apikey, byte[] fileData)
{
2021-05-11 23:01:11 +01:00
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
2021-05-11 02:38:52 +01:00
// "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";
2021-05-11 23:01:11 +01:00
httpWebRequest.AllowAutoRedirect = false;
httpWebRequest.Headers.Add("X-NuGet-ApiKey", apikey);
2021-05-11 02:38:52 +01:00
httpWebRequest.BeginGetRequestStream((result) =>
{
try
{
HttpWebRequest request = (HttpWebRequest)result.AsyncState;
using (Stream requestStream = request.EndGetRequestStream(result))
{
2021-05-11 23:01:11 +01:00
WriteMultipartForm(requestStream, boundary, data, fileName, fileContentType, fileData);
2021-05-11 02:38:52 +01:00
}
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();
}
/// <summary>
/// Writes multi part HTTP POST request. Author : Farhan Ghumra
/// </summary>
2021-05-11 23:01:11 +01:00
public static void WriteMultipartForm(this Stream s, string boundary, Dictionary<string, string> data, string fileName, string fileContentType,
byte[] fileData)
2021-05-11 02:38:52 +01:00
{
/// The first boundary
byte[] boundarybytes = Encoding.UTF8.GetBytes("--" + boundary + "\r\n");
/// the last boundary.
byte[] trailer = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");
2021-05-11 23:01:11 +01:00
2021-05-11 02:38:52 +01:00
/// the form data, properly formatted
2021-05-11 23:01:11 +01:00
string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
2021-05-11 02:38:52 +01:00
/// the form-data file upload, properly formatted
2021-05-11 23:01:11 +01:00
string fileheaderTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\";\r\nContent-Type: {2}\r\n\r\n";
2021-05-11 02:38:52 +01:00
/// 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);
/// 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);
WriteToStream(s, string.Format(fileheaderTemplate, "file", fileName, fileContentType));
/// Write the file data to the stream.
WriteToStream(s, fileData);
WriteToStream(s, trailer);
}
/// <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)
{
s.Write(bytes, 0, bytes.Length);
}
}
}