MDACSHTTPServer 1.2.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package MDACSHTTPServer --version 1.2.2
NuGet\Install-Package MDACSHTTPServer -Version 1.2.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MDACSHTTPServer" Version="1.2.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MDACSHTTPServer --version 1.2.2
#r "nuget: MDACSHTTPServer, 1.2.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install MDACSHTTPServer as a Cake Addin
#addin nuget:?package=MDACSHTTPServer&version=1.2.2

// Install MDACSHTTPServer as a Cake Tool
#tool nuget:?package=MDACSHTTPServer&version=1.2.2

Example Code

The following code creates a few examples of using the library and the API. You can enable HTTPS support by passing the path and password to a certificate PFX file. You can create a certificate PFX file from any other certificate format. See end of code below for example of using Let's Encrypt PEM files.

using MDACS.Server;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using static MDACS.Server.HTTPClient2;

namespace MDACSUniversalRegistry
{
    static class HandleSomething
    {
        public static async Task<Task> Action1(Object shandler, HTTPRequest request, Stream body, ProxyHTTPEncoder encoder)
        {
            await encoder.WriteQuickHeader(200, "OK");
            await encoder.BodyWriteSingleChunk("It works!");

            return Task.CompletedTask;
        }

        public static async Task<Task> Action2(Object shandler, HTTPRequest request, Stream body, ProxyHTTPEncoder encoder)
        {
            await encoder.WriteQuickHeader(200, "OK");

            var mystream = new DoubleEndedStream();

            // This is scheduled to run (without waiting on it), hence, the missing await.
            encoder.BodyWriteStream(mystream);

            await Task.Delay(5000);

            var something = "It works using the double ended stream!";
            var something_bytes = Encoding.UTF8.GetBytes(something);

            // The double ended stream can be used similarly to a stream.
            await mystream.WriteAsync(something_bytes, 0, something_bytes.Length);

            mystream.Dispose();

            // The client will get the response before this exists. It is waiting asynchronously.
            await Task.Delay(5000);

            return Task.CompletedTask;
        }

        public static async Task<Task> Action3(Object shandler, HTTPRequest request, Stream body, ProxyHTTPEncoder encoder)
        {
            await encoder.WriteQuickHeader(200, "OK");
            // Oppps.. I forgot to send the response, will it work? What will it do?

            return Task.CompletedTask;
        }

        public static async Task<Task> Action4(Object shandler, HTTPRequest request, Stream body, ProxyHTTPEncoder encoder)
        {
            await encoder.WriteQuickHeader(200, "OK");

            var mystream = new DoubleEndedStream();

            // This is scheduled to run (without waiting on it), hence, the missing await.
            encoder.BodyWriteStream(mystream);

            await Task.Yield();

            var something = "It works using the double ended stream!";
            var something_bytes = Encoding.UTF8.GetBytes(something);

            await mystream.WriteAsync(something_bytes, 0, something_bytes.Length);

            await Task.Yield();

            // We forgot to call dispose which is bad, but it should be closed for us.

            return Task.CompletedTask;

        }

        public static async Task<Task> Action5(Object shandler, HTTPRequest request, Stream body, ProxyHTTPEncoder encoder)
        {
            await encoder.WriteQuickHeader(200, "OK");

            var mystream = new DoubleEndedStream();

            // This is just one level more complicated that it looks. There is actually a return
            // value of Task<Task>. The first task consists of an ansynchronous blocking wait which
            // once complete creates a task and returns it, hence a Task as the result of a Task.
            var stream_copier = await encoder.BodyWriteStream(mystream);
            // The `stream_copier` is a new task (child) that works to copy from `mystream` out to
            // the transport/client/connection. If we abruptly exit without return it as a child task
            // or waiting on it then the system can, due to a race condition, think that we terminated
            // and it will try to send a response for us because it might detect no response having
            // been started.

            await Task.Yield();

            var something = "It works using the double ended stream!";
            var something_bytes = Encoding.UTF8.GetBytes(something);

            // This time we need another task to do something but we can not return like we
            // have before or the calling logic will think we are done. The way to do it is
            // to return a new Task object which will be awaited on by the calling code.

            var a_child_task = Task.Run(async () =>
            {
                await mystream.WriteAsync(something_bytes, 0, something_bytes.Length);
                mystream.Dispose();
            });

            // We forgot to call dispose which is bad, but it should be closed for us... however,
            // the system will try to detect this and call it implicitly.

            // Since we were using a stream we _do_ need to wait on it, but we also have a child task
            // so we need to wait on that child task too. Interestingly, in this specific case we could
            // omit the `a_child_task` since `stream_copier` will never complete until `a_child_task` calls
            // `Dispose` to close the double ended stream. But, for completeness we will await them both.
            return Task.WhenAll(stream_copier, a_child_task);

        }
    }

    class ServerState
    {

    }

    public class Program
    {
        public static void Main(string[] args)
        {
            var handlers = new Dictionary<string, SimpleServer<ServerState>.SimpleHTTPHandler>();

            handlers.Add("/", HandleSomething.Action1);
            handlers.Add("/2", HandleSomething.Action2);
            handlers.Add("/3", HandleSomething.Action3);
            handlers.Add("/4", HandleSomething.Action4);
            handlers.Add("/5", HandleSomething.Action5);

            var server_state = new ServerState();

            var server_task = SimpleServer<ServerState>.Create(
                server_state, 
                handlers, 
                8080,
                null, //"test.pfx", 
                null //"hello"
            );

            server_task.Wait();

            // To build a PFX file from, for example, a Let's Encrypt set of files.
            // openssl crl2pkcs7 -nocrl -inkey privkey.pem -certfile fullchain.pem -out test.p7b
            // openssl pkcs7 -print_certs -in test.p7b -out test.cer
            // openssl pkcs12 -export -in test.cer -inkey privkey.pem -out test.pfx -nodes
        }
    }
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.3.201.8021 1,171 1/8/2018
1.3.201.4014 1,143 1/4/2018
1.2.2 1,083 12/29/2017

ALPHA RELEASE