Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Parse filename* in multipart form #661

Merged
merged 1 commit into from
Jun 29, 2016
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion src/Microsoft.AspNetCore.Http/Features/FormFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@ private async Task<IFormCollection> InnerReadFormAsync(CancellationToken cancell
await section.Body.DrainAsync(cancellationToken);

var name = HeaderUtilities.RemoveQuotes(contentDisposition.Name) ?? string.Empty;
var fileName = HeaderUtilities.RemoveQuotes(contentDisposition.FileName) ?? string.Empty;
var fileName = HeaderUtilities.RemoveQuotes(contentDisposition.FileNameStar) ??
HeaderUtilities.RemoveQuotes(contentDisposition.FileName) ??
string.Empty;

FormFile file;
if (section.BaseStreamOffset.HasValue)
Expand Down
58 changes: 58 additions & 0 deletions test/Microsoft.AspNetCore.Http.Tests/Features/FormFeatureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ public async Task ReadFormAsync_SimpleData_ReturnsParsedFormCollection(bool buff
"Content-Disposition: form-data; name=\"myfile1\"; filename=\"temp.html\"\r\n" +
"Content-Type: text/html\r\n" +
"\r\n" +
"<html><body>Hello World</body></html>\r\n";

private const string MultipartFormEncodedFilename = "--WebKitFormBoundary5pDRpGheQXaM8k3T\r\n" +
"Content-Disposition: form-data; name=\"myfile1\"; filename=\"temp.html\"; filename*=utf-8\'\'t%c3%a9mp.html\r\n" +
"Content-Type: text/html\r\n" +
"\r\n" +
"<html><body>Hello World</body></html>\r\n";

private const string MultipartFormWithField =
Expand All @@ -78,6 +84,10 @@ public async Task ReadFormAsync_SimpleData_ReturnsParsedFormCollection(bool buff
MultipartFormFile +
MultipartFormEnd;

private const string MultipartFormWithEncodedFilename =
MultipartFormEncodedFilename +
MultipartFormEnd;

[Theory]
[InlineData(true)]
[InlineData(false)]
Expand Down Expand Up @@ -198,6 +208,54 @@ public async Task ReadFormAsync_MultipartWithFile_ReturnsParsedFormCollection(bo
await responseFeature.CompleteAsync();
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task ReadFormAsync_MultipartWithEncodedFilename_ReturnsParsedFormCollection(bool bufferRequest)
{
var formContent = Encoding.UTF8.GetBytes(MultipartFormWithEncodedFilename);
var context = new DefaultHttpContext();
var responseFeature = new FakeResponseFeature();
context.Features.Set<IHttpResponseFeature>(responseFeature);
context.Request.ContentType = MultipartContentType;
context.Request.Body = new NonSeekableReadStream(formContent);

IFormFeature formFeature = new FormFeature(context.Request, new FormOptions() { BufferBody = bufferRequest });
context.Features.Set<IFormFeature>(formFeature);

var formCollection = await context.Request.ReadFormAsync();

Assert.NotNull(formCollection);

// Cached
formFeature = context.Features.Get<IFormFeature>();
Assert.NotNull(formFeature);
Assert.NotNull(formFeature.Form);
Assert.Same(formFeature.Form, formCollection);
Assert.Same(formCollection, context.Request.Form);

// Content
Assert.Equal(0, formCollection.Count);

Assert.NotNull(formCollection.Files);
Assert.Equal(1, formCollection.Files.Count);

var file = formCollection.Files["myfile1"];
Assert.Equal("myfile1", file.Name);
Assert.Equal("t\u00e9mp.html", file.FileName);
Assert.Equal("text/html", file.ContentType);
Assert.Equal(@"form-data; name=""myfile1""; filename=""temp.html""; filename*=utf-8''t%c3%a9mp.html", file.ContentDisposition);
var body = file.OpenReadStream();
using (var reader = new StreamReader(body))
{
Assert.True(body.CanSeek);
var content = reader.ReadToEnd();
Assert.Equal(content, "<html><body>Hello World</body></html>");
}

await responseFeature.CompleteAsync();
}

[Theory]
[InlineData(true)]
[InlineData(false)]
Expand Down