Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions src/Core/Helpers/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Threading.Tasks;
using System.Web;
using Microsoft.AspNetCore.Http;
using System.IO;

namespace AspNetCore.Proxy
{
Expand Down Expand Up @@ -106,16 +107,25 @@ internal static HttpContent ToHttpContent(this IFormCollection collection, HttpR
}
foreach (var file in collection.Files)
{
var content = new StreamContent(file.OpenReadStream());
foreach (var header in file.Headers.Where(h => !h.Key.Equals("Content-Disposition", StringComparison.OrdinalIgnoreCase)))
content.Headers.TryAddWithoutValidation(header.Key, (IEnumerable<string>)header.Value);
var ms = new MemoryStream();

// Force content-disposition header to use raw string to ensure UTF-8 is well encoded.
content.Headers.TryAddWithoutValidation("Content-Disposition",
new string(Encoding.UTF8.GetBytes($"form-data; name=\"{file.Name}\"; filename=\"{file.FileName}\"").
Select(b => (char)b).ToArray()));
using (var fs = file.OpenReadStream())
{
//copy content to a new stream to prevent exception
//when multiple files are included in the initial request
fs.CopyTo(ms);
var content = new StreamContent(ms);

multipart.Add(content);
foreach (var header in file.Headers.Where(h => !h.Key.Equals("Content-Disposition", StringComparison.OrdinalIgnoreCase)))
content.Headers.TryAddWithoutValidation(header.Key, (IEnumerable<string>)header.Value);

// Force content-disposition header to use raw string to ensure UTF-8 is well encoded.
content.Headers.TryAddWithoutValidation("Content-Disposition",
new string(Encoding.UTF8.GetBytes($"form-data; name=\"{file.Name}\"; filename=\"{file.FileName}\"").
Select(b => (char)b).ToArray()));

multipart.Add(content);
}
}
return multipart;
}
Expand Down