unosquare / embedio

A tiny, cross-platform, module based web server for .NET
http://unosquare.github.io/embedio
Other
1.46k stars 176 forks source link

force keep-alive #514

Closed gyorokpeter closed 3 years ago

gyorokpeter commented 3 years ago

I would like to implement a web server that doesn't close connections, it should be up to the client to close them, even if the client doesn't explicitly specify "Connection: keep-alive" in the headers. This is because a client may send many requests and handling them in separate connections would be too much overhead. The clients are pre-existing applications which are known to support sending multiple requests through the same connection.

rdeago commented 3 years ago

Hello @gyorokpeter, thanks for using EmbedIO!

Your requirement being a blatant violation of the HTTP protocol, I strongly doubt you'll find any web server library supporting it out-of-the.-box, including EmbedIO. I don't think a "forced keep-alive" feature would make much sense, either.

That said, I understand that sometimes one has to work with what one is given. If you're willing to use a private fork of EmbedIO, here's how to force keep-alive with the least possibile modifications.

Just insert the following line of code below this line in src/EmbedIO/Net/Internal/HttpListenerRequest.cs (at the end of the FinishInitialization method):

            Headers.Set("Connection", "keep-alive");

As long as you use HttpListenerMode.EmbedIO, this will make EmbedIO treat all incoming requests as if they contained a Connection header with a value of keep-alive and act accordingly.

DISCLAIMERS

gyorokpeter commented 3 years ago

In that case I will keep my bespoke solution that implements HTTP over a standard TcpListener. The reason I wanted to use EmbedIO in the first place is because I wanted to integrate my code as a plugin to another app that already uses EmbedIO. So using a custom fork would not work.

rdeago commented 3 years ago

[...] my bespoke solution that implements HTTP over a standard TcpListener.

Is that a private project, or may I take a look at it? We have to find a replacement for HttpListener before its status goes from the current "shamefully obsolete" to "covered in cobwebs" and any input is welcome.

gyorokpeter commented 3 years ago

https://github.com/gyorokpeter/SteelSeriesServer/blob/f7442dfcbeed365ac560c4946accc81a2b0fd965/SteelSeriesServer.cs#L456 https://github.com/gyorokpeter/SteelSeriesServer/blob/f7442dfcbeed365ac560c4946accc81a2b0fd965/SteelSeriesServer.cs#L572