diff --git a/src/Core/Extensions/Http.cs b/src/Core/Extensions/Http.cs index b1fb8cf..a9d35c6 100644 --- a/src/Core/Extensions/Http.cs +++ b/src/Core/Extensions/Http.cs @@ -88,7 +88,9 @@ private static HttpRequestMessage CreateProxiedHttpRequest(this HttpContext cont requestMessage.Content = request.Form.ToHttpContent(request.ContentType); } else + { requestMessage.Content = new StreamContent(request.Body); + } } // Copy the request headers. diff --git a/src/Core/Helpers/Helpers.cs b/src/Core/Helpers/Helpers.cs index 496da3e..0bff089 100644 --- a/src/Core/Helpers/Helpers.cs +++ b/src/Core/Helpers/Helpers.cs @@ -1,13 +1,11 @@ using System; using System.Collections.Generic; -using System.IO; using System.Linq; using System.Net.Http; using System.Net.Http.Headers; using System.Threading.Tasks; using System.Web; using Microsoft.AspNetCore.Http; -using Microsoft.Extensions.Primitives; namespace AspNetCore.Proxy { @@ -76,6 +74,7 @@ internal static string TrimTrailingSlashes(this string s) internal static HttpContent ToHttpContent(this IFormCollection collection, string contentTypeHeader) { + // @PreferLinux: // Form content types resource: https://stackoverflow.com/questions/4526273/what-does-enctype-multipart-form-data-mean/28380690 // There are three possible form content types: // - text/plain, which should never be used and this does not handle (a request with that will not have IsFormContentType true anyway) @@ -91,7 +90,7 @@ internal static HttpContent ToHttpContent(this IFormCollection collection, strin return new FormUrlEncodedContent(collection.SelectMany(formItemList => formItemList.Value.Select(value => new KeyValuePair<string, string>(formItemList.Key, value)))); if (!contentType.MediaType.Equals("multipart/form-data", StringComparison.OrdinalIgnoreCase)) - throw new Exception($"Unknown form content type \"{contentType.MediaType}\""); + throw new Exception($"Unknown form content type `{contentType.MediaType}`."); // multipart/form-data specification https://tools.ietf.org/html/rfc7578 // It has each value separated by a boundary sequence, which is specified in the Content-Type header. diff --git a/src/Test/Unit/Other.cs b/src/Test/Unit/Other.cs new file mode 100644 index 0000000..b8d5fce --- /dev/null +++ b/src/Test/Unit/Other.cs @@ -0,0 +1,21 @@ + +using System; +using Xunit; + +namespace AspNetCore.Proxy.Tests +{ + public class Other + { + [Fact] + public void CanFailOnBadFormContentType() + { + var contentType = "text/plain"; + + var e = Assert.ThrowsAny<Exception>(() => { + var dummy = Helpers.ToHttpContent(null, contentType); + }); + + Assert.Equal($"Unknown form content type `{contentType}`.", e.Message); + } + } +} \ No newline at end of file