From d43914febe79686b29cb1fbfd8f6bbfc2573d7a6 Mon Sep 17 00:00:00 2001 From: Laurent Ellerbach Date: Wed, 25 Oct 2023 14:58:28 +0200 Subject: [PATCH] Splitting library to remove dependency of Windows.Storage (#227) --- README.md | 63 ++++++--- azure-pipelines.yml | 38 ++++- nanoFramework.WebServer/key.snk => key.snk | Bin nanoFramework.WebServer.FileSystem.nuspec | 38 +++++ .../Properties/AssemblyInfo.cs | 18 +++ .../nanoFramework.WebServer.FileSystem.nfproj | 131 ++++++++++++++++++ .../packages.config | 13 ++ .../packages.lock.json | 67 +++++++++ nanoFramework.WebServer.nuspec | 7 +- nanoFramework.WebServer.sln | 8 ++ .../Properties/AssemblyInfo.cs | 5 - nanoFramework.WebServer/WebServer.cs | 73 ++++++---- .../nanoFramework.WebServer.nfproj | 12 +- nanoFramework.WebServer/packages.config | 2 - nanoFramework.WebServer/packages.lock.json | 12 -- version.json | 2 +- 16 files changed, 407 insertions(+), 82 deletions(-) rename nanoFramework.WebServer/key.snk => key.snk (100%) create mode 100644 nanoFramework.WebServer.FileSystem.nuspec create mode 100644 nanoFramework.WebServer.FileSystem/Properties/AssemblyInfo.cs create mode 100644 nanoFramework.WebServer.FileSystem/nanoFramework.WebServer.FileSystem.nfproj create mode 100644 nanoFramework.WebServer.FileSystem/packages.config create mode 100644 nanoFramework.WebServer.FileSystem/packages.lock.json diff --git a/README.md b/README.md index 7c3bdf6..63243f8 100644 --- a/README.md +++ b/README.md @@ -11,15 +11,16 @@ | Component | Build Status | NuGet Package | |:-|---|---| | nanoFramework.WebServer | [![Build Status](https://dev.azure.com/nanoframework/nanoFramework.WebServer/_apis/build/status/nanoFramework.WebServer?repoName=nanoframework%2FnanoFramework.WebServer&branchName=main)](https://dev.azure.com/nanoframework/nanoFramework.WebServer/_build/latest?definitionId=65&repoName=nanoframework%2FnanoFramework.WebServer&branchName=main) | [![NuGet](https://img.shields.io/nuget/v/nanoFramework.WebServer.svg?label=NuGet&style=flat&logo=nuget)](https://www.nuget.org/packages/nanoFramework.WebServer/) | +| nanoFramework.WebServer.FileSystem | [![Build Status](https://dev.azure.com/nanoframework/nanoFramework.WebServer/_apis/build/status/nanoFramework.WebServer?repoName=nanoframework%2FnanoFramework.WebServer&branchName=main)](https://dev.azure.com/nanoframework/nanoFramework.WebServer/_build/latest?definitionId=65&repoName=nanoframework%2FnanoFramework.WebServer&branchName=main) | [![NuGet](https://img.shields.io/nuget/v/nanoFramework.WebServer.FileSystem.svg?label=NuGet&style=flat&logo=nuget)](https://www.nuget.org/packages/nanoFramework.WebServer.FileSystem/) | ## .NET nanoFramework WebServer -This library was coded by [Laurent Ellerbach](@Ellerbach) who generously offered it to the .NET **nanoFramework** project. +This library was coded by [Laurent Ellerbach](https://github.com/Ellerbach) who generously offered it to the .NET **nanoFramework** project. This is a simple nanoFramework WebServer. Features: - Handle multi-thread requests -- Serve static files on any storage +- Serve static files from any storage using [`nanoFramework.WebServer.FileSystem` NuGet](https://www.nuget.org/packages/nanoFramework.WebServer.FileSystem). Requires a target device with support for storage (having `System.IO.FileSystem` capability). - Handle parameter in URL - Possible to have multiple WebServer running at the same time - supports GET/PUT and any other word @@ -31,6 +32,7 @@ This is a simple nanoFramework WebServer. Features: - [URL decode/encode](https://github.com/nanoframework/lib-nanoFramework.System.Net.Http/blob/develop/nanoFramework.System.Net.Http/Http/System.Net.HttpUtility.cs) Limitations: + - Does not support any zip in the request or response stream ## Usage @@ -93,9 +95,10 @@ The `RouteAnyTest`is called whenever the url is `test/any` whatever the method i There is a more advance example with simple REST API to get a list of Person and add a Person. Check it in the [sample](./WebServer.Sample/ControllerPerson.cs). -**Important** -* By default the routes are not case sensitive and the attribute **must** be lowercase -* If you want to use case sensitive routes like in the previous example, use the attribute `CaseSensitive`. As in the previous example, you **must** write the route as you want it to be responded to. +> [!Important] +> +> By default the routes are not case sensitive and the attribute **must** be lowercase. +> If you want to use case sensitive routes like in the previous example, use the attribute `CaseSensitive`. As in the previous example, you **must** write the route as you want it to be responded to. ## A simple GPIO controller REST API @@ -113,13 +116,13 @@ You will find in simple [GPIO controller sample](https://github.com/nanoframewor Controllers support authentication. 3 types of authentications are currently implemented on controllers only: - Basic: the classic user and password following the HTTP standard. Usage: - - `[Authentication("Basic")]` will use the default credential of the webserver - - `[Authentication("Basic:myuser mypassword")]` will use myuser as a user and my password as a password. Note: the user cannot contains spaces. + - `[Authentication("Basic")]` will use the default credential of the webserver + - `[Authentication("Basic:myuser mypassword")]` will use myuser as a user and my password as a password. Note: the user cannot contains spaces. - APiKey in header: add ApiKey in headers with the API key. Usage: - - `[Authentication("ApiKey")]` will use the default credential of the webserver - - `[Authentication("ApiKeyc:akey")]` will use akey as ApiKey. + - `[Authentication("ApiKey")]` will use the default credential of the webserver + - `[Authentication("ApiKeyc:akey")]` will use akey as ApiKey. - None: no authentication required. Usage: - - `[Authentication("None")]` will use the default credential of the webserver + - `[Authentication("None")]` will use the default credential of the webserver The Authentication attribute applies to both public Classes an public Methods. @@ -184,9 +187,9 @@ using (WebServer server = new WebServer(80, HttpProtocol.Http, new Type[] { type With the previous example the following happens: - All the controller by default, even when nothing is specified will use the controller credentials. In our case, the Basic authentication with the default user (topuser) and password (topPassword) will be used. - - When calling http://yoururl/authbasic from a browser, you will be prompted for the user and password, use the default one topuser and topPassword to get access - - When calling http://yoururl/authnone, you won't be prompted because the authentication has been overridden for no authentication - - When calling http://yoururl/authbasicspecial, the user and password are different from the defautl ones, user2 and password is the right couple here + - When calling http://yoururl/authbasic from a browser, you will be prompted for the user and password, use the default one topuser and topPassword to get access + - When calling http://yoururl/authnone, you won't be prompted because the authentication has been overridden for no authentication + - When calling http://yoururl/authbasicspecial, the user and password are different from the defautl ones, user2 and password is the right couple here - If you would have define in the controller a specific user and password like `[Authentication("Basic:myuser mypassword")]`, then the default one for all the controller would have been myuser and mypassword - When calling http://yoururl/authapi, you must pass the header `ApiKey` (case sensitive) with the value `superKey1234` to get authorized, this is overridden the default Basic authentication - When calling http://yoururl/authdefaultapi, the default key `ATopSecretAPIKey1234` will be used so you have to pass it in the headers of the request @@ -246,12 +249,31 @@ if (url.ToLower().IndexOf("/param.htm") == 0) And server static files: ```csharp -var files = storage.GetFiles(); -foreach (var file in files) +// E = USB storage +// D = SD Card +// I = Internal storage +// Adjust this based on your configuration +const string DirectoryPath = "I:\\"; +string[] _listFiles; + +// Gets the list of all files in a specific directory +// See the MountExample for more details if you need to mount an SD card and adjust here +// https://github.com/nanoframework/Samples/blob/main/samples/System.IO.FileSystem/MountExample/Program.cs +_listFiles = Directory.GetFiles(DirectoryPath); +// Remove the root directory +for (int i = 0; i < _listFiles.Length; i++) { - if (file.Name == url) + _listFiles[i] = _listFiles[i].Substring(DirectoryPath.Length); +} + +var fileName = url.Substring(1); +// Note that the file name is case sensitive +// Very simple example serving a static file on an SD card +foreach (var file in _listFiles) +{ + if (file == fileName) { - WebServer.SendFileOverHTTP(e.Context.Response, file); + WebServer.SendFileOverHTTP(e.Context.Response, DirectoryPath + file); return; } } @@ -259,6 +281,10 @@ foreach (var file in files) WebServer.OutputHttpCode(e.Context.Response, HttpStatusCode.NotFound); ``` +> [!Important] +> +> Serving files requires the `nanoFramework.WebServer.FileSystem` nuget **AND** that the device supports storage so `System.IO.FileSystem`. + And also **REST API** is supported, here is a comprehensive example: ```csharp @@ -363,7 +389,8 @@ using (WebServer server = new WebServer(443, HttpProtocol.Https) } ``` -> IMPORTANT: because the certificate above is not issued from a Certificate Authority it won't be recognized as a valid certificate. If you want to access the nanoFramework device with your browser, for example, you'll have to add the (CRT file)[WebServer.Sample\webserver-cert.crt] as a trusted one. On Windows, you just have to double click on the CRT file and then click "Install Certificate...". +> [!IMPORTANT] +> Because the certificate above is not issued from a Certificate Authority it won't be recognized as a valid certificate. If you want to access the nanoFramework device with your browser, for example, you'll have to add the [CRT file](WebServer.Sample\webserver-cert.crt) as a trusted one. On Windows, you just have to double click on the CRT file and then click "Install Certificate...". You can of course use the routes as defined earlier. Both will work, event or route with the notion of controller. diff --git a/azure-pipelines.yml b/azure-pipelines.yml index efec0f8..a2dbafb 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -46,10 +46,46 @@ steps: # step from template @ nf-tools repo # all build, update and publish steps - - template: azure-pipelines-templates/class-lib-build.yml@templates + - template: azure-pipelines-templates/class-lib-build-only.yml@templates parameters: sonarCloudProject: 'nanoframework_lib-nanoframework.WebServer' + # build the 2 libs step + - template: azure-pipelines-templates/class-lib-package.yml@templates + parameters: + nugetPackageName: 'nanoFramework.WebServer' + + - template: azure-pipelines-templates/class-lib-package.yml@templates + parameters: + nugetPackageName: 'nanoFramework.WebServer.FileSystem' + + # publish the 2 libs + - template: azure-pipelines-templates/class-lib-publish.yml@templates + + # create GitHub release build from main branch + - task: GithubRelease@1 + condition: >- + and( + succeeded(), + eq(variables['System.PullRequest.PullRequestId'], ''), + startsWith(variables['Build.SourceBranch'], 'refs/heads/main'), + not(contains(variables['Build.SourceBranch'], 'preview')), + eq(variables['StartReleaseCandidate'], false) + ) + displayName: Create/Update GitHub release + inputs: + action: edit + gitHubConnection: 'github.com_nano-$(System.TeamProject)' + tagSource: userSpecifiedTag + tag: v$(MY_NUGET_VERSION) + title: '$(nugetPackageName) Library v$(MY_NUGET_VERSION)' + releaseNotesSource: inline + releaseNotesInline: 'Check the [changelog]($(Build.Repository.Uri)/blob/$(Build.SourceBranchName)/CHANGELOG.md).

Install from NuGet


The following NuGet packages are available for download from this release:
:package: [nanoFramework.WebServer](https://www.nuget.org/packages/$(nugetPackageName)/$(MY_NUGET_VERSION)) v$(MY_NUGET_VERSION).
:package: [nanoFramework.WebServer.FileSystem (requires support of storage through System.IO.FileSystem)](https://www.nuget.org/packages/nanoFramework.WebServer.FileSystem/$(MY_NUGET_VERSION)) v$(MY_NUGET_VERSION)' + assets: '$(Build.ArtifactStagingDirectory)/*.nupkg' + assetUploadMode: replace + isPreRelease: false + addChangeLog: false + # step from template @ nf-tools repo # report error - template: azure-pipelines-templates/discord-webhook-task.yml@templates diff --git a/nanoFramework.WebServer/key.snk b/key.snk similarity index 100% rename from nanoFramework.WebServer/key.snk rename to key.snk diff --git a/nanoFramework.WebServer.FileSystem.nuspec b/nanoFramework.WebServer.FileSystem.nuspec new file mode 100644 index 0000000..87c85c4 --- /dev/null +++ b/nanoFramework.WebServer.FileSystem.nuspec @@ -0,0 +1,38 @@ + + + + nanoFramework.WebServer.FileServer + nanoFramework.WebServer.FileServer + $version$ + Laurent Ellerbach,nanoframework + false + LICENSE.md + + + docs\README.md + false + https://github.com/nanoframework/nanoFramework.WebServer + images\nf-logo.png + + Copyright (c) .NET Foundation and Contributors + This is a simple multithread WebServer supporting simple controller and event based calls. +Perfect for .NET nanoFramework REST API based project. Support all type of Http Methods. +Perfect for simple embedded web pages, with Support of file on a storage (USB, SD Card, in Memory). +Supports both HTTPS and HTTP. +Use this version if you want to serve local files and have support for System.IO.FileSystem on your device. +Otherwise use 'nanoFramework.WebServer' nuget. + http https webserver net netmf nf nanoframework + + + + + + + + + + + + + + \ No newline at end of file diff --git a/nanoFramework.WebServer.FileSystem/Properties/AssemblyInfo.cs b/nanoFramework.WebServer.FileSystem/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..4c98c10 --- /dev/null +++ b/nanoFramework.WebServer.FileSystem/Properties/AssemblyInfo.cs @@ -0,0 +1,18 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("nanoFramework.WebServer")] +[assembly: AssemblyCompany("nanoFramework Contributors")] +[assembly: AssemblyProduct("nanoFramework.WebServer.FileSystem")] +[assembly: AssemblyCopyright("Copyright (c) .NET Foundation and Contributors")] + + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + diff --git a/nanoFramework.WebServer.FileSystem/nanoFramework.WebServer.FileSystem.nfproj b/nanoFramework.WebServer.FileSystem/nanoFramework.WebServer.FileSystem.nfproj new file mode 100644 index 0000000..4c461bc --- /dev/null +++ b/nanoFramework.WebServer.FileSystem/nanoFramework.WebServer.FileSystem.nfproj @@ -0,0 +1,131 @@ + + + + $(MSBuildExtensionsPath)\nanoFramework\v1.0\ + + + + Debug + AnyCPU + {11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 9d8a2d18-8036-4880-b46b-d5218247257d + Library + Properties + 512 + nanoFramework.WebServer + nanoFramework.WebServer + v1.0 + bin\$(Configuration)\nanoFramework.WebServer.xml + true + true + FILESYSTEM; + + + true + + + ..\key.snk + + + false + + + + + + Authentication.cs + + + AuthenticationAttirbute.cs + + + AuthenticationType.cs + + + CallbackRoutes.cs + + + CaseSensitiveAttribute.cs + + + HttpProtocol.cs + + + WebServerEventArgs.cs + + + Header.cs + + + MethodAttribute.cs + + + RouteAttribute.cs + + + UrlParameter.cs + + + WebServer.cs + + + + + + ..\packages\nanoFramework.CoreLibrary.1.14.2\lib\mscorlib.dll + True + + + ..\packages\nanoFramework.Runtime.Events.1.11.6\lib\nanoFramework.Runtime.Events.dll + True + + + ..\packages\nanoFramework.System.Collections.1.5.18\lib\nanoFramework.System.Collections.dll + True + + + ..\packages\nanoFramework.System.Text.1.2.37\lib\nanoFramework.System.Text.dll + True + + + ..\packages\nanoFramework.System.IO.Streams.1.1.38\lib\System.IO.Streams.dll + True + + + ..\packages\nanoFramework.System.Net.1.10.62\lib\System.Net.dll + True + + + ..\packages\nanoFramework.System.Net.Http.Server.1.5.104\lib\System.Net.Http.dll + True + + + ..\packages\nanoFramework.System.Threading.1.1.19\lib\System.Threading.dll + True + + + ..\packages\nanoFramework.System.IO.FileSystem.1.1.23\lib\System.IO.FileSystem.dll + True + + + + + + + + + + + + + + + + + This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105.The missing file is {0}. + + + + + + \ No newline at end of file diff --git a/nanoFramework.WebServer.FileSystem/packages.config b/nanoFramework.WebServer.FileSystem/packages.config new file mode 100644 index 0000000..178b3b8 --- /dev/null +++ b/nanoFramework.WebServer.FileSystem/packages.config @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/nanoFramework.WebServer.FileSystem/packages.lock.json b/nanoFramework.WebServer.FileSystem/packages.lock.json new file mode 100644 index 0000000..971adbb --- /dev/null +++ b/nanoFramework.WebServer.FileSystem/packages.lock.json @@ -0,0 +1,67 @@ +{ + "version": 1, + "dependencies": { + ".NETnanoFramework,Version=v1.0": { + "nanoFramework.CoreLibrary": { + "type": "Direct", + "requested": "[1.14.2, 1.14.2]", + "resolved": "1.14.2", + "contentHash": "j1mrz4mitl5LItvmHMsw1aHzCAfvTTgIkRxA0mhs5mSpctJ/BBcuNwua5j3MspfRNKreCQPy/qZy/D9ADLL/PA==" + }, + "nanoFramework.Runtime.Events": { + "type": "Direct", + "requested": "[1.11.6, 1.11.6]", + "resolved": "1.11.6", + "contentHash": "xkltRh/2xKaZ9zmPHbVr32s1k+e17AInUBhzxKKkUDicJKF39yzTShSklb1OL6DBER5z71SpkGLyl9IdMK9l6w==" + }, + "nanoFramework.System.Collections": { + "type": "Direct", + "requested": "[1.5.18, 1.5.18]", + "resolved": "1.5.18", + "contentHash": "F8FzaUC5D2xd6eQja33EYcknOFqWT+YEcnEn849ILJTGSedVFylUREEuoGULbCrnuuLx417hDzWny+B78Qi67g==" + }, + "nanoFramework.System.IO.FileSystem": { + "type": "Direct", + "requested": "[1.1.23, 1.1.23]", + "resolved": "1.1.23", + "contentHash": "W8HMgL81baCutM9isIqeOHU5SM6J3z1g1XF16quVtK8DARQzKdwLV+fJweXyOUTnpyXV/oEkr9SrkCdGdBPliQ==" + }, + "nanoFramework.System.IO.Streams": { + "type": "Direct", + "requested": "[1.1.38, 1.1.38]", + "resolved": "1.1.38", + "contentHash": "qEtu/lMDtr5kPKc939vO3uX8h+W0/+Qx2N3Zx005JxqGiL71e4ScecEyGPIp8v1MzRd9pkoxInUb6jOAh+eyXA==" + }, + "nanoFramework.System.Net": { + "type": "Direct", + "requested": "[1.10.62, 1.10.62]", + "resolved": "1.10.62", + "contentHash": "MR4PIS7J+LyPU6jSbVu0+tdeBb3lXo7xPun+C0Q3zMpc9YPPCsI7euko4O3Q4T7VSbtjeA3dDfI0fsefcgVRUA==" + }, + "nanoFramework.System.Net.Http.Server": { + "type": "Direct", + "requested": "[1.5.104, 1.5.104]", + "resolved": "1.5.104", + "contentHash": "zU0NkMd3269In8AnVKPsg9Ai3Px49rjw4aldQahxSRgqTDoej0YrM/ielgIsGQT3vas0gJiEpcF6fV0i5gf4WA==" + }, + "nanoFramework.System.Text": { + "type": "Direct", + "requested": "[1.2.37, 1.2.37]", + "resolved": "1.2.37", + "contentHash": "ORgRq0HSynSBhlXRTHdhzZiOdq/nRhdnX+DeIGw56y9OSc8dvqUz6elm97Jz+4WQ6ikpvs5PFGINAa35kBebwQ==" + }, + "nanoFramework.System.Threading": { + "type": "Direct", + "requested": "[1.1.19, 1.1.19]", + "resolved": "1.1.19", + "contentHash": "HjnY0DNoFnU+t1GiH8Wgf7pJCD9yMOcLVtAZXVAhstMKisVN/MDj9TvfXxZScRIz7ZDoZYUnb+Ixfl6rqyppvA==" + }, + "Nerdbank.GitVersioning": { + "type": "Direct", + "requested": "[3.6.133, 3.6.133]", + "resolved": "3.6.133", + "contentHash": "VZWMd5YAeDxpjWjAP/X6bAxnRMiEf6tES/ITN0X5CHJgkWLLeHGmEALivmTAfYM6P+P/3Szy6VCITUAkqjcHVw==" + } + } + } +} \ No newline at end of file diff --git a/nanoFramework.WebServer.nuspec b/nanoFramework.WebServer.nuspec index 3fbc816..e45d98a 100644 --- a/nanoFramework.WebServer.nuspec +++ b/nanoFramework.WebServer.nuspec @@ -16,14 +16,13 @@ Copyright (c) .NET Foundation and Contributors This is a simple multithread WebServer supporting simple controller and event based calls. -Perfect for .NET nanoFramework REST API based project. Support all type of Http Methods. -Perfect for simple embedded web pages, with Support of file on a storage (USB, SD Card, in Memory). -Supports both HTTPS and HTTP. +Perfect for .NET nanoFramework REST API based project. Supports both HTTPS and HTTP. +If serving files from local storage is a requirement, please use instead the 'nanoFramework.WebServer.FileSystem' nuget. + http https webserver net netmf nf nanoframework - diff --git a/nanoFramework.WebServer.sln b/nanoFramework.WebServer.sln index 99b9541..ce816cf 100644 --- a/nanoFramework.WebServer.sln +++ b/nanoFramework.WebServer.sln @@ -11,6 +11,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution version.json = version.json EndProjectSection EndProject +Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "nanoFramework.WebServer.FileSystem", "nanoFramework.WebServer.FileSystem\nanoFramework.WebServer.FileSystem.nfproj", "{9D8A2D18-8036-4880-B46B-D5218247257D}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -23,6 +25,12 @@ Global {87AAA5FE-CBB6-497F-97B7-7AF21B9A0C4E}.Release|Any CPU.ActiveCfg = Release|Any CPU {87AAA5FE-CBB6-497F-97B7-7AF21B9A0C4E}.Release|Any CPU.Build.0 = Release|Any CPU {87AAA5FE-CBB6-497F-97B7-7AF21B9A0C4E}.Release|Any CPU.Deploy.0 = Release|Any CPU + {9D8A2D18-8036-4880-B46B-D5218247257D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9D8A2D18-8036-4880-B46B-D5218247257D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9D8A2D18-8036-4880-B46B-D5218247257D}.Debug|Any CPU.Deploy.0 = Debug|Any CPU + {9D8A2D18-8036-4880-B46B-D5218247257D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9D8A2D18-8036-4880-B46B-D5218247257D}.Release|Any CPU.Build.0 = Release|Any CPU + {9D8A2D18-8036-4880-B46B-D5218247257D}.Release|Any CPU.Deploy.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/nanoFramework.WebServer/Properties/AssemblyInfo.cs b/nanoFramework.WebServer/Properties/AssemblyInfo.cs index 9c19f88..adc9caf 100644 --- a/nanoFramework.WebServer/Properties/AssemblyInfo.cs +++ b/nanoFramework.WebServer/Properties/AssemblyInfo.cs @@ -16,8 +16,3 @@ using System.Runtime.InteropServices; // COM, set the ComVisible attribute to true on that type. [assembly: ComVisible(false)] -///////////////////////////////////////////////////////////////// -// This attribute is mandatory when building Interop libraries // -// update this whenever the native assembly signature changes // -[assembly: AssemblyNativeVersion("0.0.0.0")] -///////////////////////////////////////////////////////////////// diff --git a/nanoFramework.WebServer/WebServer.cs b/nanoFramework.WebServer/WebServer.cs index e9cecf5..75538d5 100644 --- a/nanoFramework.WebServer/WebServer.cs +++ b/nanoFramework.WebServer/WebServer.cs @@ -6,15 +6,15 @@ using System; using System.Collections; using System.Diagnostics; +#if FILESYSTEM +using System.IO; +#endif using System.Net; using System.Net.NetworkInformation; using System.Net.Security; using System.Security.Cryptography.X509Certificates; using System.Text; using System.Threading; -using Windows.Storage; -using Windows.Storage.Streams; - namespace nanoFramework.WebServer { @@ -281,16 +281,16 @@ namespace nanoFramework.WebServer var space = strAuth.IndexOf(' '); if (space < 0) { - throw new ArgumentException($"Authentication attribute Basic should be 'Basic:user passowrd'"); + throw new ArgumentException($"Authentication attribute Basic should be 'Basic:user password'"); } var user = strAuth.Substring(sep + 1, space - sep - 1); var password = strAuth.Substring(space + 1); - authentication = new Authentication(new NetworkCredential(user, password, System.Net.AuthenticationType.Basic)); + authentication = new Authentication(new NetworkCredential(user, password, (global::System.Net.AuthenticationType)AuthenticationType.Basic)); } else { - throw new ArgumentException($"Authentication attribute Basic should be 'Basic:user passowrd'"); + throw new ArgumentException($"Authentication attribute Basic should be 'Basic:user password'"); } } } @@ -386,7 +386,7 @@ namespace nanoFramework.WebServer Thread.Sleep(100); _serverThread.Abort(); _serverThread = null; - Debug.WriteLine("Stoped server in thread "); + Debug.WriteLine("Stopped server in thread "); } /// @@ -424,43 +424,42 @@ namespace nanoFramework.WebServer response.StatusCode = (int)code; } +#if FILESYSTEM /// /// Return a file from Storage over HTTP response. /// /// to send the content over. /// The file to send /// The type of file, if empty string, then will use auto detection - public static void SendFileOverHTTP(HttpListenerResponse response, StorageFile strFilePath, string contentType = "") + public static void SendFileOverHTTP(HttpListenerResponse response, string strFilePath, string contentType = "") { - contentType = contentType == "" ? GetContentTypeFromFileName(strFilePath.FileType) : contentType; - IBuffer readBuffer = FileIO.ReadBuffer(strFilePath); - long fileLength = readBuffer.Length; + contentType = contentType == string.Empty ? GetContentTypeFromFileName(strFilePath.Substring(strFilePath.LastIndexOf(".") + 1)) : contentType; + byte[] buf = new byte[MaxSizeBuffer]; + using FileStream dataReader = new FileStream(strFilePath, FileMode.Open, FileAccess.Read); + + long fileLength = dataReader.Length; response.ContentType = contentType; response.ContentLength64 = fileLength; response.SendChunked = true; // Now loops sending all the data. - - byte[] buf = new byte[MaxSizeBuffer]; - using (DataReader dataReader = DataReader.FromBuffer(readBuffer)) + for (long bytesSent = 0; bytesSent < fileLength;) { - for (long bytesSent = 0; bytesSent < fileLength;) - { - // Determines amount of data left. - long bytesToRead = fileLength - bytesSent; - bytesToRead = bytesToRead < MaxSizeBuffer ? bytesToRead : MaxSizeBuffer; + // Determines amount of data left. + long bytesToRead = fileLength - bytesSent; + bytesToRead = bytesToRead < MaxSizeBuffer ? bytesToRead : MaxSizeBuffer; - // Reads the data. - dataReader.ReadBytes(buf); + // Reads the data. + dataReader.Read(buf, 0,(int) bytesToRead); - // Writes data to browser - response.OutputStream.Write(buf, 0, (int)bytesToRead); + // Writes data to browser + response.OutputStream.Write(buf, 0, (int)bytesToRead); - // Updates bytes read. - bytesSent += bytesToRead; - } + // Updates bytes read. + bytesSent += bytesToRead; } } +#endif /// /// Send file content over HTTP response. @@ -627,8 +626,24 @@ namespace nanoFramework.WebServer } else { - context.Response.Close(); - context.Close(); + try + { + context.Response.Close(); + } + catch + { + // Nothing on purpose + } + + try + { + context.Close(); + } + catch + { + // Nothing on purpose + } + } } }).Start(); @@ -755,6 +770,6 @@ namespace nanoFramework.WebServer } } - #endregion +#endregion } } diff --git a/nanoFramework.WebServer/nanoFramework.WebServer.nfproj b/nanoFramework.WebServer/nanoFramework.WebServer.nfproj index 8f0e540..4d498bf 100644 --- a/nanoFramework.WebServer/nanoFramework.WebServer.nfproj +++ b/nanoFramework.WebServer/nanoFramework.WebServer.nfproj @@ -24,7 +24,7 @@ true - key.snk + ..\key.snk false @@ -44,7 +44,7 @@ - + @@ -79,14 +79,6 @@ ..\packages\nanoFramework.System.Threading.1.1.19\lib\System.Threading.dll True - - ..\packages\nanoFramework.Windows.Storage.1.5.33\lib\Windows.Storage.dll - True - - - ..\packages\nanoFramework.Windows.Storage.Streams.1.14.24\lib\Windows.Storage.Streams.dll - True - diff --git a/nanoFramework.WebServer/packages.config b/nanoFramework.WebServer/packages.config index b3d409c..d6588f7 100644 --- a/nanoFramework.WebServer/packages.config +++ b/nanoFramework.WebServer/packages.config @@ -8,7 +8,5 @@ - - \ No newline at end of file diff --git a/nanoFramework.WebServer/packages.lock.json b/nanoFramework.WebServer/packages.lock.json index baa806a..f952125 100644 --- a/nanoFramework.WebServer/packages.lock.json +++ b/nanoFramework.WebServer/packages.lock.json @@ -50,18 +50,6 @@ "resolved": "1.1.19", "contentHash": "HjnY0DNoFnU+t1GiH8Wgf7pJCD9yMOcLVtAZXVAhstMKisVN/MDj9TvfXxZScRIz7ZDoZYUnb+Ixfl6rqyppvA==" }, - "nanoFramework.Windows.Storage": { - "type": "Direct", - "requested": "[1.5.33, 1.5.33]", - "resolved": "1.5.33", - "contentHash": "9BDmxivx3h/2MfoXcTAUYhZny6iaDN9DwAngx7OrQNvXFgzdbOTMTB17Qcy0iA9EUqTr6HG8TuE2tv2loyNeIA==" - }, - "nanoFramework.Windows.Storage.Streams": { - "type": "Direct", - "requested": "[1.14.24, 1.14.24]", - "resolved": "1.14.24", - "contentHash": "dApgYplMFRUTp7izZxvduXgl9B7oqVPqqagkYXFf6cRJRC3Og0y2JZTVJdhGPW/ud0U0+rvjqXOU+1CtEzv88A==" - }, "Nerdbank.GitVersioning": { "type": "Direct", "requested": "[3.6.133, 3.6.133]", diff --git a/version.json b/version.json index 8888844..02628a2 100644 --- a/version.json +++ b/version.json @@ -1,6 +1,6 @@ { "$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json", - "version": "1.1", + "version": "1.2", "assemblyVersion": { "precision": "minor" },