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

Commit

Permalink
Use filename* for files in multipart forms when available #651
Browse files Browse the repository at this point in the history
  • Loading branch information
JunTaoLuo committed Jun 29, 2016
1 parent 3c0c021 commit 4986137
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
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

0 comments on commit 4986137

Please sign in to comment.