Skip to content

Commit d1b4492

Browse files
committed
Core - Cef.GetMimeType custom/fallback mimeType mapping
- return application/octet-stream when no mapping found - Add custom dictionary with some additional mimeTypes, users can add their own to this dictionary - Add xunit test Resolves #3041
1 parent 8803f1c commit d1b4492

File tree

5 files changed

+93
-1
lines changed

5 files changed

+93
-1
lines changed

CefSharp.Core/Cef.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,17 @@ namespace CefSharp
805805
extension = extension->Substring(1, extension->Length - 1);
806806
}
807807

808-
return StringUtils::ToClr(CefGetMimeType(StringUtils::ToNative(extension)));
808+
auto mimeType = StringUtils::ToClr(CefGetMimeType(StringUtils::ToNative(extension)));
809+
810+
//Lookup to see if we have a custom mapping
811+
//MimeTypeMapping::GetCustomMapping will Fallback
812+
//to application/octet-stream if no mapping found
813+
if (String::IsNullOrEmpty(mimeType))
814+
{
815+
return MimeTypeMapping::GetCustomMapping(extension);
816+
}
817+
818+
return mimeType;
809819
}
810820

811821
/// <summary>

CefSharp.Test/CefSharp.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
<Compile Include="Framework\BinderFacts.cs" />
129129
<Compile Include="Framework\AsyncExtensionFacts.cs" />
130130
<Compile Include="Framework\ConcurrentMethodRunnerQueueFacts.cs" />
131+
<Compile Include="Framework\MimeTypeMappingFacts.cs" />
131132
<Compile Include="Framework\PathCheckFacts.cs" />
132133
<Compile Include="OffScreen\OffScreenBrowserBasicFacts.cs" />
133134
<Compile Include="CefSharpXunitTestFramework.cs" />
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright © 2020 The CefSharp Authors. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
4+
5+
using Xunit;
6+
7+
namespace CefSharp.Test.Framework
8+
{
9+
/// <summary>
10+
/// MimeTypeMappingFacts - Tests file extension to mimeType mapping
11+
/// </summary>
12+
public class MimeTypeMappingFacts
13+
{
14+
[Theory]
15+
[InlineData("html", "text/html")]
16+
[InlineData(".wasm", "application/wasm")]
17+
[InlineData(".ogg", "audio/ogg")]
18+
[InlineData(".oga", "audio/ogg")]
19+
[InlineData(".ogv", "video/ogg")]
20+
[InlineData(".opus", "audio/ogg")]
21+
[InlineData(".webm", "video/webm")]
22+
[InlineData(".weba", "audio/webm")]
23+
[InlineData(".webp", "image/webp")]
24+
[InlineData(".epub", "application/epub+zip")]
25+
[InlineData(".woff", "application/font-woff")]
26+
[InlineData(".woff2", "font/woff2")]
27+
[InlineData(".ttf", "font/ttf")]
28+
[InlineData(".otf", "font/otf")]
29+
[InlineData(".dummyextension", "application/octet-stream")]
30+
public void MapFileExtensionToMimeTypeTheory(string fileExtension, string expectedMimeType)
31+
{
32+
var actualMimeType = Cef.GetMimeType(fileExtension);
33+
Assert.Equal(expectedMimeType, actualMimeType);
34+
}
35+
}
36+
}

CefSharp/CefSharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
<Compile Include="DefaultApp.cs" />
105105
<Compile Include="Enums\SchemeOptions.cs" />
106106
<Compile Include="Internals\IBrowserRefCounter.cs" />
107+
<Compile Include="Internals\MimeTypeMapping.cs" />
107108
<Compile Include="Internals\NoOpBrowserRefCounter.cs" />
108109
<Compile Include="Internals\PathCheck.cs" />
109110
<Compile Include="Internals\BrowserRefCounter.cs" />
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright © 2020 The CefSharp Authors. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
4+
5+
6+
using System;
7+
using System.Collections.Generic;
8+
9+
namespace CefSharp.Internals
10+
{
11+
/// <summary>
12+
/// Internal Mime Type Mappings.
13+
/// </summary>
14+
public static class MimeTypeMapping
15+
{
16+
/// <summary>
17+
/// Dictionary containing our custom mimeType mapping, you can add your own file extension
18+
/// to mimeType mappings to this dictionary.
19+
/// </summary>
20+
public static readonly IDictionary<string, string> CustomMappings = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase)
21+
{
22+
// Recently added entries from
23+
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Complete_list_of_MIME_types
24+
// https://cs.chromium.org/chromium/src/net/base/mime_util.cc?sq=package:chromium&g=0&l=147
25+
// https://www.w3.org/TR/WOFF2/#IMT
26+
// https://tools.ietf.org/html/rfc8081#section-4.4.6
27+
{"woff2", "font/woff2"},
28+
{"ttf", "font/ttf"},
29+
{"otf", "font/otf"}
30+
};
31+
32+
/// <summary>
33+
/// Lookup MimeType from the <see cref="CustomMappings"/>
34+
/// dictionary based on file extension.
35+
/// </summary>
36+
/// <param name="extension">extension</param>
37+
/// <returns>custom mimeType or application/octet-stream if no mapping found </returns>
38+
public static string GetCustomMapping(string extension)
39+
{
40+
string mime;
41+
return CustomMappings.TryGetValue(extension, out mime) ? mime : "application/octet-stream";
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)