![]() |
VOOZH | about |
dotnet add package HttpServerLite --version 2.1.5
NuGet\Install-Package HttpServerLite -Version 2.1.5
<PackageReference Include="HttpServerLite" Version="2.1.5" />
<PackageVersion Include="HttpServerLite" Version="2.1.5" />Directory.Packages.props
<PackageReference Include="HttpServerLite" />Project file
paket add HttpServerLite --version 2.1.5
#r "nuget: HttpServerLite, 2.1.5"
#:package HttpServerLite@2.1.5
#addin nuget:?package=HttpServerLite&version=2.1.5Install as a Cake Addin
#tool nuget:?package=HttpServerLite&version=2.1.5Install as a Cake Tool
TCP-based user-space HTTP and HTTPS server, written in C#, with no dependency on http.sys.
HostBuilder feature to quickly build servers, thank you @sapurtcomputer30!HttpContext.HttpRequest.Data not ending, thank you @ChZhongPengCheng33I'd like to extend a special thanks to those that have provided motivation or otherwise directly helped make HttpServerLite better.
HttpServerLite is quite fast, however, it's in user-space and may be slower than other webservers that have the benefit of a kernel-mode driver (such as http.sys and IIS or Watson).
Refer to the Test project for a working example.
It is important to under that that HttpServerLite is minimalistic and leaves control to you on which headers are set. Thus it is important to understand the following:
server.Settings.Headers contains default values for a series of HTTP headers
server.Settings.Headers can be written directly, or
ctx.Response.Headers.Add("[header]", "[value]")ctx.Response.Headers will override any value in server.Settings.Headers for that response onlyConnection is an example of one of these headers. By default it is set to close, therefore you should:
ctx.Response.Headers.Add("connection", "value"), orserver.Settings.Headers.Connectionctx.Response.ContentLength should be set if you want the Content-Length header to be sentserver.Settings.Headers.Host should be set when instantiating the server though it is not requiredusing System;
using System.Threading.Tasks;
using HttpServerLite;
namespace Test
{
class Program
{
static Webserver _Server;
static void Main(string[] args)
{
Webserver server = new Webserver("localhost", 9000, false, null, null, DefaultRoute);
server.Settings.Headers.Host = "https://localhost:9000";
server.Start();
Console.WriteLine("HttpServerLite listening on http://localhost:9000");
Console.WriteLine("ENTER to exit");
Console.ReadLine();
}
static async Task DefaultRoute(HttpContext ctx)
{
string resp = "Hello from HttpServerLite!";
ctx.Response.StatusCode = 200;
ctx.Response.ContentLength = resp.Length;
ctx.Response.ContentType = "text/plain";
await ctx.Response.SendAsync(resp);
}
}
}
HttpServerLite includes the following routing capabilities. These are listed in the other in which they are processed within HttpServerLite:
server.Settings.AccessControl - access control based on IP address
Mode to either be DefaultPermit or DefaultDeny
DefaultPermit will allow everything unless explicitly blocked through DenyListDefaultDeny will deny everything unless explicitly permitted through PermitListDefaultPermitserver.Routes.Preflight - a default route to use when the HTTP verb is OPTIONS
server.OptionsRouteserver.Routes.PreRouting - a route through which all requests will pass, useful for authentication, logging, and other functions
true from this task if you wish to terminate the connectionfalse to allow routing to continueserver.Routes.Content - serve GET and HEAD requests for static content based on URL path
server.Routes.Content.BaseDirectory plus the URL pathserver.Routes.Static - invoke functions based on specific HTTP method and URL combinationsserver.Routes.Parameter - invoke functions based on specific HTTP method and URLs with embedded parameters. These values are returned in HttpContext.HttpRequest.Url.Parametersserver.Routes.Dynamic - invoke functions based on specific HTTP method and a regular expression for the URLserver.Routes.Default - any request that did not match a content route, static route, or dynamic route, is routed hereAdditionally, you can annotate your own methods using the StaticRoute, ParameterRoute, or DynamicRoute attributes. Methods decorated with these attributes must be marked as public.
Webserver server = new Webserver("localhost", 9000, false, null, null, DefaultRoute);
server.Start();
[StaticRoute(HttpMethod.GET, "/static")]
public static async Task MyStaticRoute(HttpContext ctx)
{
string resp = "Hello from the static route";
ctx.Response.StatusCode = 200;
ctx.Response.ContentType = "text/plain";
ctx.Response.ContentLength = resp.Length;
await ctx.Response.SendAsync(resp);
return;
}
[ParameterRoute(HttpMethod.GET, "/{version}/api/{id}")]
public static async Task MyParameterRoute(HttpContext ctx)
{
string resp = "Hello from parameter route version " + ctx.Request.Url.Parameters["version"] + " for ID " + ctx.Request.Url.Parameters["id"];
ctx.Response.StatusCode = 200;
ctx.Response.ContentType = "text/plain";
ctx.Response.ContentLength = resp.Length;
await ctx.Response.SendAsync(resp);
return;
}
[DynamicRoute(HttpMethod.GET, "^/dynamic/\\d+$")]
public static async Task MyDynamicRoute(HttpContext ctx)
{
string resp = "Hello from the dynamic route";
ctx.Response.StatusCode = 200;
ctx.Response.ContentType = "text/plain";
ctx.Response.ContentLength = resp.Length;
await ctx.Response.SendAsync(resp);
return;
}
server.Callbacks.AuthorizeConnection = AuthorizeConnection;
private static bool AuthorizeConnection(string ipAddress, int port)
{
// evaluate the IP address and port
return true; // permit
return false; // deny
}
HostBuilder helps you set up your server much more easily by introducing a chain of settings and routes instead of using the server class directly.
using WatsonWebserver.Extensions.HostBuilderExtension;
Server server = new HostBuilder("127.0.0.1", 8000, false, DefaultRoute)
.MapStaticRoute(WatsonWebserver.HttpMethod.GET, GetUrlsRoute, "/links")
.MapStaticRoute(WatsonWebserver.HttpMethod.POST, CheckLoginRoute, "/login")
.MapStaticRoute(WatsonWebserver.HttpMethod.POST, TestRoute, "/test")
.Build();
server.Start();
Console.WriteLine("Server started");
Console.ReadKey();
static async Task DefaultRoute(HttpContext ctx) =>
await ctx.Response.SendAsync("Hello from default route!");
static async Task GetUrlsRoute(HttpContext ctx) =>
await ctx.Response.SendAsync("Here are your links!");
static async Task CheckLoginRoute(HttpContext ctx) =>
await ctx.Response.SendAsync("Checking your login!");
static async Task TestRoute(HttpContext ctx) =>
await ctx.Response.SendAsync("Hello from the test route!");
When you configure HttpServerLite to listen on 127.0.0.1 or localhost, it will only respond to requests received from within the local machine.
To configure access from other nodes outside of localhost, use the following:
* or +localhost or 127.0.0.1, you may have to run HttpServerLite as administrator (operating system dependent)Refer to CHANGELOG.md for version history.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 is compatible. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 is compatible. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 was computed. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 is compatible. |
| .NET Standard | netstandard2.1 netstandard2.1 is compatible. |
| .NET Framework | net461 net461 is compatible. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 is compatible. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
This package is not used by any NuGet packages.
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 2.1.5 | 9,531 | 9/28/2023 |
| 2.1.4 | 2,085 | 8/3/2023 |
| 2.1.3 | 368 | 8/2/2023 |
| 2.1.2 | 2,075 | 7/5/2023 |
| 2.1.1 | 317 | 7/4/2023 |
| 2.1.0 | 326 | 7/4/2023 |
| 2.0.2 | 1,097 | 5/18/2023 |
| 2.0.1 | 299 | 5/18/2023 |
| 2.0.0 | 1,059 | 3/14/2023 |
| 1.2.12 | 1,357 | 1/27/2023 |
| 1.2.11 | 1,238 | 1/11/2023 |
| 1.2.10 | 1,980 | 11/26/2022 |
| 1.2.9 | 1,364 | 10/24/2022 |
| 1.2.8 | 731 | 10/12/2022 |
| 1.2.7 | 581 | 10/12/2022 |
| 1.2.6 | 2,434 | 8/8/2022 |
| 1.2.4 | 784 | 6/1/2022 |
| 1.2.3 | 830 | 5/19/2022 |
| 1.2.2 | 710 | 5/4/2022 |
| 1.2.1.11 | 783 | 4/1/2022 |
HostBuilder feature from @sapurtcomputer30