Support mapping extension-less files mimetype in FileExtensionContentTypeProvider
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 281
Description
### Is there an existing issue for this?
- [x] I have searched the existing issues
### Is your feature request related to a problem? Please describe the problem.
Some vendors mandate the hosting of extension-less static files for the use of their services, such as Apple with their `.well-known/apple-developer-merchantid-domain-association` or `.well-known/apple-app-site-association` files.
As far as I know, this is currently doable with .net only by enabling the `ServeUnknownFileTypes` option from `StaticFileOptions`, which is a lot "wider" than the need and is a bigger security risk than enabling an extension-less file mapping.
### Describe the solution you'd like
The `FileExtensionContentTypeProvider` alreayd accepts en empty extension mapping, like this:
```
var provider = new FileExtensionContentTypeProvider();
provider.Mappings[""] = "text/plain";
```
But its implementation short-circuit on extension-less path, causing the declaration to be useless.
Its GetExtension method yields null in such case, which causes the middleware to not try to lookup its extension table.
https://github.com/dotnet/aspnetcore/blob/8a687407a378a981dc1d7a4694d42ec8a735bdd6/src/Middleware/StaticFiles/src/FileExtensionContentTypeProvider.cs#L455-L459
If it were to yield the empty string in such case instead, it should allow to map extension-less files.
### Additional context
Currently we use this workaround:
```
app.UseStaticFiles(
new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", ".well-known")),
RequestPath = "/.well-known",
ServeUnknownFileTypes = true,
DefaultContentType = "text/plain"
});
```
We would rather use:
```
var provider = new FileExtensionContentTypeProvider();
provider.Mappings[""] = "text/plain";
app.UseStaticFiles(
new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", ".well-known")),
RequestPath = "/.well-known",
ContentTypeProvider = provider
});
```
By the way, that is still not "as good" as we wish. When served through IIS static file handler, we restrict it further to only the file path. But that does not seem doable with the .net API as currently is.
Example through IIS:
```
```
And in case we needed to host an `.well-known/apple-app-site-association` file too, we would be stuck not being able to specify two different mime types for these two extension-less files.
Contributor guide
Research direction
Start with src/Middleware/StaticFiles/src/FileExtensionContentTypeProvider.cs, especially the GetExtension implementation around the linked lines, and inspect nearby content-type provider tests if available. Verify the existing empty-string mapping is consulted for extension-less paths without enabling ServeUnknownFileTypes; done means the requested files can be served with the mapped MIME type.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- backend, web-dev
- Issue type
- Feature
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 68/100