Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit d487844

Browse files
committed
Expand MimeTypes to include better binary detection support
1 parent 9409f78 commit d487844

File tree

1 file changed

+110
-14
lines changed

1 file changed

+110
-14
lines changed

src/ServiceStack.Text/HttpUtils.cs

Lines changed: 110 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,7 +1240,11 @@ public static class MimeTypes
12401240
public const string MarkdownText = "text/markdown";
12411241
public const string MsgPack = "application/x-msgpack";
12421242
public const string Wire = "application/x-wire";
1243+
public const string Compressed = "application/x-compressed";
12431244
public const string NetSerializer = "application/x-netserializer";
1245+
public const string Excel = "application/excel";
1246+
public const string MsWord = "application/msword";
1247+
public const string Cert = "application/x-x509-ca-cert";
12441248

12451249
public const string ImagePng = "image/png";
12461250
public const string ImageGif = "image/gif";
@@ -1343,10 +1347,21 @@ public static bool IsBinary(string contentType)
13431347
case Binary:
13441348
case Bson:
13451349
case Wire:
1350+
case Cert:
1351+
case Excel:
1352+
case MsWord:
13461353
return true;
13471354
}
13481355

1356+
// Text format exceptions to below heuristics
1357+
switch (realContentType)
1358+
{
1359+
case ImageSvg:
1360+
return false;
1361+
}
1362+
13491363
var primaryType = realContentType.LeftPart('/');
1364+
var secondaryType = realContentType.RightPart('/');
13501365
switch (primaryType)
13511366
{
13521367
case "image":
@@ -1355,6 +1370,12 @@ public static bool IsBinary(string contentType)
13551370
return true;
13561371
}
13571372

1373+
if (secondaryType.StartsWith("pkc")
1374+
|| secondaryType.StartsWith("x-pkc")
1375+
|| secondaryType.StartsWith("font")
1376+
|| secondaryType.StartsWith("vnd.ms-"))
1377+
return true;
1378+
13581379
return false;
13591380
}
13601381

@@ -1386,7 +1407,7 @@ public static string GetMimeType(string fileNameOrExt)
13861407
return "image/tiff";
13871408

13881409
case "svg":
1389-
return "image/svg+xml";
1410+
return ImageSvg;
13901411

13911412
case "ico":
13921413
return "image/x-icon";
@@ -1406,21 +1427,33 @@ public static string GetMimeType(string fileNameOrExt)
14061427
return "text/jsx";
14071428

14081429
case "csv":
1430+
return Csv;
14091431
case "css":
1432+
return Css;
1433+
14101434
case "sgml":
14111435
return "text/" + fileExt;
14121436

14131437
case "txt":
14141438
return "text/plain";
14151439

1416-
case "wav":
1417-
return "audio/wav";
1418-
14191440
case "mp3":
14201441
return "audio/mpeg3";
14211442

1443+
case "au":
1444+
case "snd":
1445+
return "audio/basic";
1446+
1447+
case "aac":
1448+
case "ac3":
1449+
case "aiff":
1450+
case "m4a":
1451+
case "m4b":
1452+
case "m4p":
14221453
case "mid":
1423-
return "audio/midi";
1454+
case "midi":
1455+
case "wav":
1456+
return "audio/" + fileExt;
14241457

14251458
case "qt":
14261459
case "mov":
@@ -1429,22 +1462,25 @@ public static string GetMimeType(string fileNameOrExt)
14291462
case "mpg":
14301463
return "video/mpeg";
14311464

1465+
case "ogv":
1466+
return "video/ogg";
1467+
1468+
case "3gpp":
14321469
case "avi":
1433-
case "mp4":
1470+
case "dv":
1471+
case "divx":
14341472
case "ogg":
1473+
case "mp4":
14351474
case "webm":
14361475
return "video/" + fileExt;
14371476

1438-
case "ogv":
1439-
return "video/ogg";
1440-
14411477
case "rtf":
14421478
return "application/" + fileExt;
14431479

14441480
case "xls":
14451481
case "xlt":
14461482
case "xla":
1447-
return "application/x-excel";
1483+
return Excel;
14481484

14491485
case "xlsx":
14501486
return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
@@ -1453,7 +1489,7 @@ public static string GetMimeType(string fileNameOrExt)
14531489

14541490
case "doc":
14551491
case "dot":
1456-
return "application/msword";
1492+
return MsWord;
14571493

14581494
case "docx":
14591495
return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
@@ -1475,10 +1511,36 @@ public static string GetMimeType(string fileNameOrExt)
14751511

14761512
case "mdb":
14771513
return "application/vnd.ms-access";
1478-
1514+
1515+
case "cer":
1516+
case "crt":
1517+
case "der":
1518+
return Cert;
1519+
1520+
case "p10":
1521+
return "application/pkcs10";
1522+
case "p12":
1523+
return "application/x-pkcs12";
1524+
case "p7b":
1525+
case "spc":
1526+
return "application/x-pkcs7-certificates";
1527+
case "p7c":
1528+
case "p7m":
1529+
return "application/pkcs7-mime";
1530+
case "p7r":
1531+
return "application/x-pkcs7-certreqresp";
1532+
case "p7s":
1533+
return "application/pkcs7-signature";
1534+
case "sst":
1535+
return "application/vnd.ms-pki.certstore";
1536+
14791537
case "gz":
14801538
case "tgz":
1481-
return "application/x-compressed";
1539+
case "zip":
1540+
case "rar":
1541+
case "lzh":
1542+
case "z":
1543+
return Compressed;
14821544

14831545
case "eot":
14841546
return "application/vnd.ms-fontobject";
@@ -1491,8 +1553,42 @@ public static string GetMimeType(string fileNameOrExt)
14911553
case "woff2":
14921554
return "application/font-woff2";
14931555

1556+
case "aaf":
1557+
case "aca":
1558+
case "asd":
1559+
case "bin":
1560+
case "cab":
1561+
case "chm":
1562+
case "class":
1563+
case "cur":
1564+
case "dat":
1565+
case "deploy":
14941566
case "dll":
1495-
return "application/octet-stream";
1567+
case "dsp":
1568+
case "exe":
1569+
case "fla":
1570+
case "ics":
1571+
case "inf":
1572+
case "java":
1573+
case "mix":
1574+
case "msi":
1575+
case "mso":
1576+
case "obj":
1577+
case "ocx":
1578+
case "prm":
1579+
case "prx":
1580+
case "psd":
1581+
case "psp":
1582+
case "qxd":
1583+
case "sea":
1584+
case "snp":
1585+
case "so":
1586+
case "toc":
1587+
case "u32":
1588+
case "xmp":
1589+
case "xsn":
1590+
case "xtp":
1591+
return Binary;
14961592

14971593
case "wasm":
14981594
return "application/wasm";

0 commit comments

Comments
 (0)