All Projects → jchristn → HttpServerLite

jchristn / HttpServerLite

Licence: MIT license
TCP-based simple HTTP and HTTPS server, written in C#.

Programming Languages

C#
18002 projects
HTML
75241 projects

Projects that are alternatives of or similar to HttpServerLite

Generator Http Fake Backend
Yeoman generator for building a fake backend by providing the content of JSON files or JavaScript objects through configurable routes.
Stars: ✭ 49 (+11.36%)
Mutual labels:  restful, http-server, restful-api
wine
A lightweight and flexible framework to help build elegant web API
Stars: ✭ 39 (-11.36%)
Mutual labels:  webserver, http-server, restful-api
Farwest
Framework for building RESTful HATEOAS-driven applications.
Stars: ✭ 18 (-59.09%)
Mutual labels:  restful, http-server, restful-api
Restbed
Corvusoft's Restbed framework brings asynchronous RESTful functionality to C++14 applications.
Stars: ✭ 1,551 (+3425%)
Mutual labels:  restful, http-server, restful-api
Http Fake Backend
Build a fake backend by providing the content of JSON files or JavaScript objects through configurable routes.
Stars: ✭ 253 (+475%)
Mutual labels:  restful, http-server, restful-api
httoop
HTTOOP - a fully object oriented HTTP protocol library written in python
Stars: ✭ 15 (-65.91%)
Mutual labels:  webserver, restful, http-server
restana
Super fast and minimalist framework for building REST micro-services.
Stars: ✭ 380 (+763.64%)
Mutual labels:  webserver, restful, http-server
Restana
Super fast and minimalist framework for building REST micro-services.
Stars: ✭ 341 (+675%)
Mutual labels:  webserver, restful, http-server
Elli
Simple, robust and performant Erlang web server
Stars: ✭ 194 (+340.91%)
Mutual labels:  webserver, http-server
Fiery
A flexible and lightweight web server
Stars: ✭ 203 (+361.36%)
Mutual labels:  webserver, http-server
Wok
A cherrypy framework for multi-purpose plug-ins
Stars: ✭ 215 (+388.64%)
Mutual labels:  webserver, restful
Rayo.js
Micro framework for Node.js
Stars: ✭ 170 (+286.36%)
Mutual labels:  webserver, http-server
Pure Http
✨ The simple web framework for Node.js with zero dependencies.
Stars: ✭ 139 (+215.91%)
Mutual labels:  webserver, http-server
Tinywebserver
🔥 Linux下C++轻量级Web服务器
Stars: ✭ 4,720 (+10627.27%)
Mutual labels:  webserver, http-server
Octane
A web server modeled after express in Rust.
Stars: ✭ 136 (+209.09%)
Mutual labels:  webserver, http-server
Proxy.py
⚡⚡⚡Fast, Lightweight, Pluggable, TLS interception capable proxy server focused on Network monitoring, controls & Application development, testing, debugging
Stars: ✭ 1,291 (+2834.09%)
Mutual labels:  webserver, http-server
Criollo
A powerful Cocoa web framework and HTTP server for macOS, iOS and tvOS.
Stars: ✭ 229 (+420.45%)
Mutual labels:  webserver, http-server
Router.cr
Minimum High Performance Middleware for Crystal Web Server.
Stars: ✭ 231 (+425%)
Mutual labels:  webserver, http-server
EthernetWebServer SSL
Simple TLS/SSL Ethernet WebServer, HTTP Client and WebSocket Client library for for AVR, Portenta_H7, Teensy, SAM DUE, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, nRF52 and RASPBERRY_PI_PICO boards using Ethernet shields W5100, W5200, W5500, ENC28J60 or Teensy 4.1 NativeEthernet/QNEthernet. It now supports Ethernet TLS/SSL Client. The library supports …
Stars: ✭ 40 (-9.09%)
Mutual labels:  webserver, http-server
edap
No description or website provided.
Stars: ✭ 22 (-50%)
Mutual labels:  restful, http-server

alt tag

HttpServerLite

NuGet Version NuGet

TCP-based user-space HTTP and HTTPS server, written in C#, with no dependency on http.sys.

New in v1.2.x

  • Less restrictive handling of reading chunks
  • Added constructor with X509Certificate2, thank you @samisil
  • Add a event WebserverEvents.ConnectionDenied
  • Added Callbacks object with callback AuthorizeConnection
  • Added Callbacks object with callback AuthorizeConnection
  • Parameter routes
  • Breaking changes to synchronize HttpRequest properties with Watson Webserver
  • More efficiency in internal send methods, thank you @marcussacana
  • Removal of Newtonsoft.Json
  • Dependency update

Special Thanks

I'd like to extend a special thanks to those that have provided motivation or otherwise directly helped make HttpServerLite better.

  • @winkmichael @Job79 @MartyIX @sqlnew @SaintedPsycho @Return25 @marcussacana @samisil

Performance

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

Getting Started

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
    • These will be included in every response if they have a value assigned
    • The values in server.Settings.Headers can be written directly, or
      • You can modify per-response values by using ctx.Response.Headers.Add("[header]", "[value]")
      • Values set in ctx.Response.Headers will override any value in server.Settings.Headers for that response only
    • The headers automatically set if a value is supplied include
      • Access-Control-Allow-[Origin|Methods|Headers]
      • Access-Control-Expose-Headers
      • Accept
      • Accept-[Language|Charset]
      • Connection
      • Host
    • Connection is an example of one of these headers. By default it is set to close, therefore you should:
      • Leave it as is
      • Explicitly set it prior to sending a response using ctx.Response.Headers.Add("connection", "value"), or
      • Set the default value in server.Settings.Headers.Connection
  • ctx.Response.ContentLength should be set if you want the Content-Length header to be sent
  • server.Settings.Headers.Host should be set when instantiating the server though it is not required

Simple Server

using 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);
    }
  }
} 

Routing

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
    • You can specify the Mode to either be DefaultPermit or DefaultDeny
      • DefaultPermit will allow everything unless explicitly blocked through DenyList
      • DefaultDeny will deny everything unless explicitly permitted through PermitList
      • The default value is DefaultPermit
  • server.Routes.Preflight - a default route to use when the HTTP verb is OPTIONS
    • When set, the connection is terminated after being handled by server.OptionsRoute
  • server.Routes.PreRouting - a route through which all requests will pass, useful for authentication, logging, and other functions
    • If defined, return true from this task if you wish to terminate the connection
    • Otherwise return false to allow routing to continue
  • server.Routes.Content - serve GET and HEAD requests for static content based on URL path
    • Content will be read from the server.Routes.Content.BaseDirectory plus the URL path
    • An entire directory can be listed as a content route when adding the route
  • server.Routes.Static - invoke functions based on specific HTTP method and URL combinations
  • server.Routes.Parameter - invoke functions based on specific HTTP method and URLs with embedded parameters. These values are returned in HttpContext.HttpRequest.Url.Parameters
  • server.Routes.Dynamic - invoke functions based on specific HTTP method and a regular expression for the URL
  • server.Routes.Default - any request that did not match a content route, static route, or dynamic route, is routed here

Additionally, 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;
}

Authorizing or Declining a Connection

server.Callbacks.AuthorizeConnection = AuthorizeConnection;

private static bool AuthorizeConnection(string ipAddress, int port)
{
  // evaluate the IP address and port
  return true;  // permit
  return false; // deny
}

Accessing from Outside Localhost

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:

  • Specify the IP address on which HttpServerLite should listen in the Server constructor.
  • If you want to listen on more than one IP address, use * or +
  • If you listen on anything other than localhost or 127.0.0.1, you may have to run HttpServerLite as administrator (operating system dependent)
  • If you want to use a port number less than 1024, you MUST run HttpServerLite as administrator (this is an operating system limitation)
  • Open a port on your firewall to permit traffic on the TCP port upon which HttpServerLite is listening
  • If you're still having problems, please do not hesitate to file an issue here, and I will do my best to help and update the documentation

Version History

Refer to CHANGELOG.md for version history.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].