Skip to content

Commit f811279

Browse files
committed
Adjusting doc, adding Content Type support on top of detection, adjusting comments
1 parent 8c81ad8 commit f811279

File tree

5 files changed

+34
-13
lines changed

5 files changed

+34
-13
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ This is a simple nanoFrmaework WebServer. Features:
1212
- Reflection for easy usage of controllers and notion of routes
1313
- Helpers to return error code directly facilitating REST API
1414
- HTTPS support
15+
- [URL decode/encode](https://github.com/nanoframework/lib-nanoFramework.System.Net.Http/blob/develop/nanoFramework.System.Net.Http/Http/System.Net.HttpUtility.cs)
1516

1617
Limitations:
17-
- Does not support any zip way
18-
- No URL decode/encode implemented yet
18+
- Does not support any zip in the request or response stream
1919

2020
## Usage
2121

WebServer.Sample/ControllerTest.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,23 @@ public void RouteAnyTest(WebServerEventArgs e)
2424
{
2525
WebServer.OutputHttpCode(e.Context.Response, HttpStatusCode.OK);
2626
}
27+
28+
[Route("urlencode")]
29+
public void UrlEncode(WebServerEventArgs e)
30+
{
31+
var rawUrl = e.Context.Request.RawUrl;
32+
var paramsUrl = WebServer.DecodeParam(rawUrl);
33+
string ret = "Parameters | Encoded | Decoded";
34+
foreach (var param in paramsUrl)
35+
{
36+
ret += $"{param.Name} | ";
37+
ret += $"{param.Value} | ";
38+
// Need to wait for latest version of System.Net
39+
// See https://github.com/nanoframework/lib-nanoFramework.System.Net.Http/blob/develop/nanoFramework.System.Net.Http/Http/System.Net.HttpUtility.cs
40+
// ret += $"{System.Web.HttpUtility.UrlDecode(param.Value)}";
41+
ret += "\r\n";
42+
}
43+
WebServer.OutPutStream(e.Context.Response, ret);
44+
}
2745
}
2846
}

WebServer.Sample/WebServer.Sample.nfproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
<SpecificVersion>True</SpecificVersion>
7575
</Reference>
7676
<Reference Include="System.Net.Http, Version=1.3.3.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
77-
<HintPath>..\packages\nanoFramework.System.Net.Http.1.3.3-preview.23\lib\System.Net.Http.dll</HintPath>
77+
<HintPath>..\packages\nanoFramework.System.Net.Http.1.3.3-preview.25\lib\System.Net.Http.dll</HintPath>
7878
<Private>True</Private>
7979
<SpecificVersion>True</SpecificVersion>
8080
</Reference>

WebServer.Sample/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<package id="nanoFramework.System.Collections" version="1.2.0-preview.14" targetFramework="netnanoframework10" />
77
<package id="nanoFramework.System.Device.Gpio" version="1.0.0-preview.14" targetFramework="netnanoframework10" />
88
<package id="nanoFramework.System.Net" version="1.6.3-preview.15" targetFramework="netnanoframework10" />
9-
<package id="nanoFramework.System.Net.Http" version="1.3.3-preview.23" targetFramework="netnanoframework10" />
9+
<package id="nanoFramework.System.Net.Http" version="1.3.3-preview.25" targetFramework="netnanoframework10" />
1010
<package id="nanoFramework.System.Text" version="1.1.1-preview.14" targetFramework="netnanoframework10" />
1111
<package id="nanoFramework.Windows.Devices.Wifi" version="1.3.2-preview.11" targetFramework="netnanoframework10" />
1212
<package id="nanoFramework.Windows.Storage" version="1.4.4-preview.25" targetFramework="netnanoframework10" />

WebServer/WebServer.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -416,16 +416,19 @@ public static void OutputHttpCode(HttpListenerResponse response, HttpStatusCode
416416
/// <summary>
417417
/// Return a file from Storage over HTTP response.
418418
/// </summary>
419-
public static void SendFileOverHTTP(HttpListenerResponse response, StorageFile strFilePath)
419+
/// <param name="response"><see cref="HttpListenerResponse"/> to send the content over.</param>
420+
/// <param name="strFilePath">The file to send</param>
421+
/// <param name="contentType">The type of file, if empty string, then will use auto detection</param>
422+
public static void SendFileOverHTTP(HttpListenerResponse response, StorageFile strFilePath, string contentType = "")
420423
{
421-
string ContentType = GetContentTypeFromFileName(strFilePath.FileType);
422-
424+
contentType = contentType == "" ? GetContentTypeFromFileName(strFilePath.FileType) : contentType;
425+
423426
try
424427
{
425428
IBuffer readBuffer = FileIO.ReadBuffer(strFilePath);
426429
long fileLength = readBuffer.Length;
427430

428-
response.ContentType = ContentType;
431+
response.ContentType = contentType;
429432
response.ContentLength64 = fileLength;
430433
// Now loops sending all the data.
431434

@@ -459,13 +462,14 @@ public static void SendFileOverHTTP(HttpListenerResponse response, StorageFile s
459462
/// <param name="response"><see cref="HttpListenerResponse"/> to send the content over.</param>
460463
/// <param name="fileName">Name of the file to send over <see cref="HttpListenerResponse"/>.</param>
461464
/// <param name="content">Content of the file to send.</param>
462-
public static void SendFileOverHTTP(HttpListenerResponse response, string fileName, byte[] content)
465+
/// /// <param name="contentType">The type of file, if empty string, then will use auto detection</param>
466+
public static void SendFileOverHTTP(HttpListenerResponse response, string fileName, byte[] content, string contentType = "")
463467
{
464-
string ContentType = GetContentTypeFromFileName(fileName.Substring(fileName.LastIndexOf('.')));
468+
contentType = contentType == "" ? GetContentTypeFromFileName(fileName.Substring(fileName.LastIndexOf('.'))) : contentType;
465469

466470
try
467471
{
468-
response.ContentType = ContentType;
472+
response.ContentType = contentType;
469473
response.ContentLength64 = content.Length;
470474

471475
// Now loop to send all the data.
@@ -480,7 +484,7 @@ public static void SendFileOverHTTP(HttpListenerResponse response, string fileNa
480484
response.OutputStream.Write(content, (int)bytesSent, (int)bytesToSend);
481485

482486
// 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
483-
487+
484488
// update bytes sent
485489
bytesSent += bytesToSend;
486490
}
@@ -658,7 +662,6 @@ private void ListInterfaces()
658662
}
659663
}
660664

661-
/// Get the MIME-type for a file name.
662665
/// <summary>
663666
/// Get the MIME-type for a file name.
664667
/// </summary>

0 commit comments

Comments
 (0)