Skip to content

Commit

Permalink
add multipart/form-data support
Browse files Browse the repository at this point in the history
add multipart/form-data support
  • Loading branch information
myall200 committed Nov 13, 2020
1 parent c3a0cf1 commit 72cb5e6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/Ocelot/Multiplexer/MultiplexingMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ private HttpContext Copy(HttpContext source)
target.Request.PathBase = source.Request.PathBase;
target.Request.Protocol = source.Request.Protocol;
target.Request.Query = source.Request.Query;
target.Request.Form = source.Request.Form;
target.Request.QueryString = source.Request.QueryString;
target.Request.Scheme = source.Request.Scheme;
target.Request.IsHttps = source.Request.IsHttps;
Expand Down
47 changes: 41 additions & 6 deletions src/Ocelot/Request/Mapper/RequestMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ public async Task<Response<HttpRequestMessage>> Map(HttpRequest request, Downstr
return new ErrorResponse<HttpRequestMessage>(new UnmappableRequestError(ex));
}
}

private bool IsMultipartContentType(string contentType)
{
return !string.IsNullOrEmpty(contentType)
&& contentType.IndexOf("multipart/form-data", StringComparison.OrdinalIgnoreCase) >= 0;
}
private async Task<HttpContent> MapContent(HttpRequest request)
{
if (request.Body == null || (request.Body.CanSeek && request.Body.Length <= 0))
Expand All @@ -46,12 +50,43 @@ private async Task<HttpContent> MapContent(HttpRequest request)
}

// Never change this to StreamContent again, I forgot it doesnt work in #464.
var content = new ByteArrayContent(await ToByteArray(request.Body));

if (!string.IsNullOrEmpty(request.ContentType))
HttpContent content = null;
// Never change this to StreamContent again, I forgot it doesnt work in #464.


if (this.IsMultipartContentType(request.ContentType))
{
content.Headers
.TryAddWithoutValidation("Content-Type", new[] { request.ContentType });
content = new MultipartFormDataContent();
if (request.Form != null && request.Form.Files != null)
{
foreach (var f in request.Form.Files)
{
using (var memStream = new MemoryStream())
{
await f.CopyToAsync(memStream);
var fileContent = new ByteArrayContent(memStream.ToArray());
((MultipartFormDataContent)content).Add(fileContent, f.Name, f.FileName);
}

}
}
if (request.Form != null)
{
foreach (var key in request.Form.Keys)
{
var strContent = new StringContent(request.Form[key]);
((MultipartFormDataContent)content).Add(strContent, key);
}
}
}
else
{
content = new ByteArrayContent(await ToByteArray(request.Body));
if (!string.IsNullOrEmpty(request.ContentType))
{
content.Headers
.TryAddWithoutValidation("Content-Type", new[] { request.ContentType });
}
}

AddHeaderIfExistsOnRequest("Content-Language", content, request);
Expand Down

0 comments on commit 72cb5e6

Please sign in to comment.