Skip to content

Commit 27441ef

Browse files
authored
Fixing basic MIME types (#214)
1 parent 1569782 commit 27441ef

File tree

5 files changed

+61
-29
lines changed

5 files changed

+61
-29
lines changed

nanoFramework.WebServer/Authentication.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
namespace nanoFramework.WebServer
1111
{
1212
/// <summary>
13-
/// The authentication to be used by the server
13+
/// The authentication to be used by the server.
1414
/// </summary>
1515
public class Authentication
1616
{
@@ -20,7 +20,7 @@ public class Authentication
2020
public AuthenticationType AuthenticationType { get; internal set; }
2121

2222
/// <summary>
23-
/// The network credential user and password
23+
/// The network credential user and password.
2424
/// </summary>
2525
public NetworkCredential Credentials { get; internal set; } = null;
2626

@@ -29,18 +29,29 @@ public class Authentication
2929
/// </summary>
3030
public string ApiKey { get; internal set; } = null;
3131

32+
/// <summary>
33+
/// Creates an autentication class from a credential.
34+
/// </summary>
35+
/// <param name="credential">The credentials.</param>
3236
public Authentication(NetworkCredential credential)
3337
{
3438
AuthenticationType = AuthenticationType.Basic;
3539
Credentials = credential;
3640
}
3741

42+
/// <summary>
43+
/// Creates an authentication from a key.
44+
/// </summary>
45+
/// <param name="apiKey">The key.</param>
3846
public Authentication(string apiKey)
3947
{
4048
AuthenticationType = AuthenticationType.ApiKey;
4149
ApiKey = apiKey;
4250
}
4351

52+
/// <summary>
53+
/// Creates an empty authenticate.
54+
/// </summary>
4455
public Authentication()
4556
{
4657
AuthenticationType = AuthenticationType.None;

nanoFramework.WebServer/MethodAttribute.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,20 @@
88
namespace nanoFramework.WebServer
99
{
1010
/// <summary>
11-
/// The HTTP Method
11+
/// The HTTP Method.
1212
/// </summary>
1313
[AttributeUsage(AttributeTargets.Method)]
1414
public class MethodAttribute : Attribute
1515
{
16+
/// <summary>
17+
/// Gets or sets the method.
18+
/// </summary>
1619
public string Method { get; set; }
1720

21+
/// <summary>
22+
/// Creates a method attribute.
23+
/// </summary>
24+
/// <param name="method">The method.</param>
1825
public MethodAttribute(string method)
1926
{
2027
Method = method;

nanoFramework.WebServer/RouteAttribute.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,20 @@
88
namespace nanoFramework.WebServer
99
{
1010
/// <summary>
11-
/// Route custom attribute
11+
/// Route custom attribute.
1212
/// </summary>
1313
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
1414
public class RouteAttribute : Attribute
1515
{
16+
/// <summary>
17+
/// Gets or sets the route.
18+
/// </summary>
1619
public string Route { get; set; }
1720

1821
/// <summary>
19-
/// A route attribute
22+
/// A route attribute.
2023
/// </summary>
21-
/// <param name="route">The route like route/second/third</param>
24+
/// <param name="route">The complete route like 'route/second/third'.</param>
2225
public RouteAttribute(string route)
2326
{
2427
Route = route;

nanoFramework.WebServer/WebServer.cs

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -465,10 +465,11 @@ public static void SendFileOverHTTP(HttpListenerResponse response, StorageFile s
465465
/// <param name="response"><see cref="HttpListenerResponse"/> to send the content over.</param>
466466
/// <param name="fileName">Name of the file to send over <see cref="HttpListenerResponse"/>.</param>
467467
/// <param name="content">Content of the file to send.</param>
468-
/// /// <param name="contentType">The type of file, if empty string, then will use auto detection</param>
468+
/// <param name="contentType">The type of file, if empty string, then will use auto detection.</param>
469469
public static void SendFileOverHTTP(HttpListenerResponse response, string fileName, byte[] content, string contentType = "")
470470
{
471-
contentType = contentType == "" ? GetContentTypeFromFileName(fileName.Substring(fileName.LastIndexOf('.'))) : contentType;
471+
// If no extension, we will get the full file name
472+
contentType = contentType == "" ? GetContentTypeFromFileName(fileName.Substring(fileName.LastIndexOf('.') + 1)) : contentType;
472473
response.ContentType = contentType;
473474
response.ContentLength64 = content.Length;
474475

@@ -483,8 +484,6 @@ public static void SendFileOverHTTP(HttpListenerResponse response, string fileNa
483484
// Writes data to output stream
484485
response.OutputStream.Write(content, (int)bytesSent, (int)bytesToSend);
485486

486-
// allow some time to physically send the bits. Can be reduce to 10 or even less if not too much other code running in parallel
487-
488487
// update bytes sent
489488
bytesSent += bytesToSend;
490489
}
@@ -676,49 +675,58 @@ private void ListInterfaces()
676675
/// <summary>
677676
/// Get the MIME-type for a file name.
678677
/// </summary>
679-
/// <param name="fileName">File name to get content type for.</param>
678+
/// <param name="fileExt">File extension to get content type for.</param>
680679
/// <returns>The MIME-type for the file name.</returns>
681-
private static string GetContentTypeFromFileName(string fileName)
680+
private static string GetContentTypeFromFileName(string fileExt)
682681
{
683682
// normalize to lower case to speed comparison
684-
fileName = fileName.ToLower();
683+
fileExt = fileExt.ToLower();
685684

686685
string contentType = "text/html";
687686

688687
//determine the type of file for the http header
689-
if (fileName == ".cs" ||
690-
fileName == ".txt" ||
691-
fileName == ".csproj"
692-
)
688+
if (fileExt == "cs" ||
689+
fileExt == "txt" ||
690+
fileExt == "csproj")
693691
{
694692
contentType = "text/plain";
695693
}
696-
else if (fileName == ".jpg" ||
697-
fileName == ".bmp" ||
698-
fileName == ".jpeg" ||
699-
fileName == ".png"
700-
)
694+
else if (fileExt == "jpg" ||
695+
fileExt == "jpeg" ||
696+
fileExt == "jpe")
697+
{
698+
contentType = "image/jpeg";
699+
}
700+
else if (fileExt == "bmp" ||
701+
fileExt == "png" ||
702+
fileExt == "gif" ||
703+
fileExt == "ief")
701704
{
702-
contentType = "image";
705+
contentType = $"image/{fileExt}";
703706
}
704-
else if (fileName == ".htm" ||
705-
fileName == ".html"
706-
)
707+
else if (fileExt == "htm" ||
708+
fileExt == "html")
707709
{
708710
contentType = "text/html";
709711
}
710-
else if (fileName == ".mp3")
712+
else if (fileExt == "mp3")
711713
{
712714
contentType = "audio/mpeg";
713715
}
714-
else if (fileName == ".css")
716+
else if (fileExt == "css")
715717
{
716718
contentType = "text/css";
717719
}
718-
else if (fileName == ".ico")
720+
else if (fileExt == "ico")
719721
{
720722
contentType = "image/x-icon";
721723
}
724+
else if (fileExt == "zip" ||
725+
fileExt == "json" ||
726+
fileExt == "pdf")
727+
{
728+
contentType = $"application/{fileExt}";
729+
}
722730

723731
return contentType;
724732
}

nanoFramework.WebServer/nanoFramework.WebServer.nfproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@
9191
<ItemGroup>
9292
<None Include="packages.config" />
9393
</ItemGroup>
94+
<ItemGroup>
95+
<Content Include="packages.lock.json" />
96+
</ItemGroup>
9497
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
9598
<ProjectExtensions>
9699
<ProjectCapabilities>

0 commit comments

Comments
 (0)