Skip to content

Commit d43914f

Browse files
authored
Splitting library to remove dependency of Windows.Storage (#227)
1 parent 9d2162c commit d43914f

16 files changed

+407
-82
lines changed

README.md

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@
1111
| Component | Build Status | NuGet Package |
1212
|:-|---|---|
1313
| 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/) |
14+
| 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/) |
1415

1516
## .NET nanoFramework WebServer
1617

17-
This library was coded by [Laurent Ellerbach](@Ellerbach) who generously offered it to the .NET **nanoFramework** project.
18+
This library was coded by [Laurent Ellerbach](https://github.com/Ellerbach) who generously offered it to the .NET **nanoFramework** project.
1819

1920
This is a simple nanoFramework WebServer. Features:
2021

2122
- Handle multi-thread requests
22-
- Serve static files on any storage
23+
- 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).
2324
- Handle parameter in URL
2425
- Possible to have multiple WebServer running at the same time
2526
- supports GET/PUT and any other word
@@ -31,6 +32,7 @@ This is a simple nanoFramework WebServer. Features:
3132
- [URL decode/encode](https://github.com/nanoframework/lib-nanoFramework.System.Net.Http/blob/develop/nanoFramework.System.Net.Http/Http/System.Net.HttpUtility.cs)
3233

3334
Limitations:
35+
3436
- Does not support any zip in the request or response stream
3537

3638
## Usage
@@ -93,9 +95,10 @@ The `RouteAnyTest`is called whenever the url is `test/any` whatever the method i
9395

9496
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).
9597

96-
**Important**
97-
* By default the routes are not case sensitive and the attribute **must** be lowercase
98-
* 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.
98+
> [!Important]
99+
>
100+
> By default the routes are not case sensitive and the attribute **must** be lowercase.
101+
> 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.
99102
100103
## A simple GPIO controller REST API
101104

@@ -113,13 +116,13 @@ You will find in simple [GPIO controller sample](https://github.com/nanoframewor
113116
Controllers support authentication. 3 types of authentications are currently implemented on controllers only:
114117

115118
- Basic: the classic user and password following the HTTP standard. Usage:
116-
- `[Authentication("Basic")]` will use the default credential of the webserver
117-
- `[Authentication("Basic:myuser mypassword")]` will use myuser as a user and my password as a password. Note: the user cannot contains spaces.
119+
- `[Authentication("Basic")]` will use the default credential of the webserver
120+
- `[Authentication("Basic:myuser mypassword")]` will use myuser as a user and my password as a password. Note: the user cannot contains spaces.
118121
- APiKey in header: add ApiKey in headers with the API key. Usage:
119-
- `[Authentication("ApiKey")]` will use the default credential of the webserver
120-
- `[Authentication("ApiKeyc:akey")]` will use akey as ApiKey.
122+
- `[Authentication("ApiKey")]` will use the default credential of the webserver
123+
- `[Authentication("ApiKeyc:akey")]` will use akey as ApiKey.
121124
- None: no authentication required. Usage:
122-
- `[Authentication("None")]` will use the default credential of the webserver
125+
- `[Authentication("None")]` will use the default credential of the webserver
123126

124127
The Authentication attribute applies to both public Classes an public Methods.
125128

@@ -184,9 +187,9 @@ using (WebServer server = new WebServer(80, HttpProtocol.Http, new Type[] { type
184187
With the previous example the following happens:
185188

186189
- 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.
187-
- 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
188-
- When calling http://yoururl/authnone, you won't be prompted because the authentication has been overridden for no authentication
189-
- When calling http://yoururl/authbasicspecial, the user and password are different from the defautl ones, user2 and password is the right couple here
190+
- 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
191+
- When calling http://yoururl/authnone, you won't be prompted because the authentication has been overridden for no authentication
192+
- When calling http://yoururl/authbasicspecial, the user and password are different from the defautl ones, user2 and password is the right couple here
190193
- 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
191194
- 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
192195
- 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,19 +249,42 @@ if (url.ToLower().IndexOf("/param.htm") == 0)
246249
And server static files:
247250

248251
```csharp
249-
var files = storage.GetFiles();
250-
foreach (var file in files)
252+
// E = USB storage
253+
// D = SD Card
254+
// I = Internal storage
255+
// Adjust this based on your configuration
256+
const string DirectoryPath = "I:\\";
257+
string[] _listFiles;
258+
259+
// Gets the list of all files in a specific directory
260+
// See the MountExample for more details if you need to mount an SD card and adjust here
261+
// https://github.com/nanoframework/Samples/blob/main/samples/System.IO.FileSystem/MountExample/Program.cs
262+
_listFiles = Directory.GetFiles(DirectoryPath);
263+
// Remove the root directory
264+
for (int i = 0; i < _listFiles.Length; i++)
251265
{
252-
if (file.Name == url)
266+
_listFiles[i] = _listFiles[i].Substring(DirectoryPath.Length);
267+
}
268+
269+
var fileName = url.Substring(1);
270+
// Note that the file name is case sensitive
271+
// Very simple example serving a static file on an SD card
272+
foreach (var file in _listFiles)
273+
{
274+
if (file == fileName)
253275
{
254-
WebServer.SendFileOverHTTP(e.Context.Response, file);
276+
WebServer.SendFileOverHTTP(e.Context.Response, DirectoryPath + file);
255277
return;
256278
}
257279
}
258280

259281
WebServer.OutputHttpCode(e.Context.Response, HttpStatusCode.NotFound);
260282
```
261283

284+
> [!Important]
285+
>
286+
> Serving files requires the `nanoFramework.WebServer.FileSystem` nuget **AND** that the device supports storage so `System.IO.FileSystem`.
287+
262288
And also **REST API** is supported, here is a comprehensive example:
263289

264290
```csharp
@@ -363,7 +389,8 @@ using (WebServer server = new WebServer(443, HttpProtocol.Https)
363389
}
364390
```
365391

366-
> 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...".
392+
> [!IMPORTANT]
393+
> 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...".
367394

368395
You can of course use the routes as defined earlier. Both will work, event or route with the notion of controller.
369396

azure-pipelines.yml

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,46 @@ steps:
4646

4747
# step from template @ nf-tools repo
4848
# all build, update and publish steps
49-
- template: azure-pipelines-templates/class-lib-build.yml@templates
49+
- template: azure-pipelines-templates/class-lib-build-only.yml@templates
5050
parameters:
5151
sonarCloudProject: 'nanoframework_lib-nanoframework.WebServer'
5252

53+
# build the 2 libs step
54+
- template: azure-pipelines-templates/class-lib-package.yml@templates
55+
parameters:
56+
nugetPackageName: 'nanoFramework.WebServer'
57+
58+
- template: azure-pipelines-templates/class-lib-package.yml@templates
59+
parameters:
60+
nugetPackageName: 'nanoFramework.WebServer.FileSystem'
61+
62+
# publish the 2 libs
63+
- template: azure-pipelines-templates/class-lib-publish.yml@templates
64+
65+
# create GitHub release build from main branch
66+
- task: GithubRelease@1
67+
condition: >-
68+
and(
69+
succeeded(),
70+
eq(variables['System.PullRequest.PullRequestId'], ''),
71+
startsWith(variables['Build.SourceBranch'], 'refs/heads/main'),
72+
not(contains(variables['Build.SourceBranch'], 'preview')),
73+
eq(variables['StartReleaseCandidate'], false)
74+
)
75+
displayName: Create/Update GitHub release
76+
inputs:
77+
action: edit
78+
gitHubConnection: 'github.com_nano-$(System.TeamProject)'
79+
tagSource: userSpecifiedTag
80+
tag: v$(MY_NUGET_VERSION)
81+
title: '$(nugetPackageName) Library v$(MY_NUGET_VERSION)'
82+
releaseNotesSource: inline
83+
releaseNotesInline: 'Check the [changelog]($(Build.Repository.Uri)/blob/$(Build.SourceBranchName)/CHANGELOG.md).<br><br><h4>Install from NuGet</h4><br>The following NuGet packages are available for download from this release:<br>:package: [nanoFramework.WebServer](https://www.nuget.org/packages/$(nugetPackageName)/$(MY_NUGET_VERSION)) v$(MY_NUGET_VERSION).<br>: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)'
84+
assets: '$(Build.ArtifactStagingDirectory)/*.nupkg'
85+
assetUploadMode: replace
86+
isPreRelease: false
87+
addChangeLog: false
88+
5389
# step from template @ nf-tools repo
5490
# report error
5591
- template: azure-pipelines-templates/discord-webhook-task.yml@templates
File renamed without changes.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
3+
<metadata>
4+
<id>nanoFramework.WebServer.FileServer</id>
5+
<title>nanoFramework.WebServer.FileServer</title>
6+
<version>$version$</version>
7+
<authors>Laurent Ellerbach,nanoframework</authors>
8+
<requireLicenseAcceptance>false</requireLicenseAcceptance>
9+
<license type="file">LICENSE.md</license>
10+
<releaseNotes>
11+
</releaseNotes>
12+
<readme>docs\README.md</readme>
13+
<developmentDependency>false</developmentDependency>
14+
<projectUrl>https://github.com/nanoframework/nanoFramework.WebServer</projectUrl>
15+
<icon>images\nf-logo.png</icon>
16+
<repository type="git" url="https://github.com/nanoframework/nanoFramework.WebServer" commit="$commit$" />
17+
<copyright>Copyright (c) .NET Foundation and Contributors</copyright>
18+
<description>This is a simple multithread WebServer supporting simple controller and event based calls.
19+
Perfect for .NET nanoFramework REST API based project. Support all type of Http Methods.
20+
Perfect for simple embedded web pages, with Support of file on a storage (USB, SD Card, in Memory).
21+
Supports both HTTPS and HTTP.
22+
Use this version if you want to serve local files and have support for System.IO.FileSystem on your device.
23+
Otherwise use 'nanoFramework.WebServer' nuget.</description>
24+
<tags>http https webserver net netmf nf nanoframework</tags>
25+
<dependencies>
26+
<dependency id="nanoFramework.CoreLibrary" version="1.14.2" />
27+
<dependency id="nanoFramework.System.Net.Http.Server" version="1.5.104" />
28+
<dependency id="nanoFramework.System.IO.FileSystem" version="1.1.23" />
29+
</dependencies>
30+
</metadata>
31+
<files>
32+
<file src="nanoFramework.WebServer\bin\Release\nanoFramework.WebServer.*" target="lib" />
33+
<file src="assets\readme.txt" target="" />
34+
<file src="README.md" target="docs\" />
35+
<file src="assets\nf-logo.png" target="images" />
36+
<file src="LICENSE.md" target="" />
37+
</files>
38+
</package>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("nanoFramework.WebServer")]
9+
[assembly: AssemblyCompany("nanoFramework Contributors")]
10+
[assembly: AssemblyProduct("nanoFramework.WebServer.FileSystem")]
11+
[assembly: AssemblyCopyright("Copyright (c) .NET Foundation and Contributors")]
12+
13+
14+
// Setting ComVisible to false makes the types in this assembly not visible
15+
// to COM components. If you need to access a type in this assembly from
16+
// COM, set the ComVisible attribute to true on that type.
17+
[assembly: ComVisible(false)]
18+

0 commit comments

Comments
 (0)