dotnet / WatsonWebserver

Watson is the fastest, easiest way to build scalable RESTful web servers and services in C#.
MIT License
412 stars 85 forks source link

Common incoming http-request handler #25

Closed timur4000 closed 5 years ago

timur4000 commented 5 years ago

Hello Joel!

Thank you for continuing to develop and improve Watson Web Server. This my topic is not an issue but suggestion.

Is it possible to introduce yet another event handler very similar to [Event.RequestReceived] but with more advanced functionality?

for example like this:

HttpResponse  PreliminaryCommonHandler(HttpRequest req)
{
....
/* It may returns null */
....
}

The principle of this handler's action is as follows:

  1. Any incoming request comes into this method.
  2. If this method returns [null], then further processing occurs as usual in accordance with the route.
  3. If this method returns a not-null value, then processing along the usual route is not performed and the client will receive exactly the value that this method returned.

Why such a handler might be useful?

The fact is that in many route handlers the programmer will be forced to write the same standard apikey (or ACCESS TOKEN) verification code and issue standard answers if an authorization violation is detected.

For example, if apikey is incorrect, then return the value "401" (Unauthorized), if the apikey is correct, but its validity has expired, then we will return some other error code.

And thus, the same code is repeated many times in many route handlers. If introduce the universal http-request pre-processor that I propose, then the apikey verification code will be written only one time in this pre-processor, which will save the programmer from annoying duplicating the code.

with respect Timur

jchristn commented 5 years ago

Hi Timur, love it. I did this for another Watson based project recently and you’re right, it makes perfect sense to have it there. I will work on it ASAP! Thanks

Sent from my iPhone

On Aug 10, 2019, at 10:44 PM, Timur Jernyaev notifications@github.com wrote:

Hello Joel!

Thank you for continuing to develop and improve Watson Web Server. This my topic is not an issue but suggestion.

Is it possible to introduce yet another event handler very similar to [Event.RequestReceived] but with more advanced functionality?

for example like this:

HttpResponse PreliminaryCommonHandler(HttpRequest req) { .... / It may returns null / .... } The principle of this handler's action is as follows:

Any incoming request comes into this method. If this method returns [null], then further processing occurs as usual in accordance with the route. If this method returns a not-null value, then processing along the usual route is not performed and the client will receive exactly the value that this method returned. Why such a handler might be useful?

The fact is that in many route handlers the programmer will be forced to write the same standard apikey (or ACCESS TOKEN) verification code and issue standard answers if an authorization violation is detected.

For example, if apikey is incorrect, then return the value "401" (Unauthorized), if the apikey is correct, but its validity has expired, then we will return some other error code.

And thus, the same code is repeated many times in many route handlers. If introduce the universal http-request pre-processor that I propose, then the apikey verification code will be written only one time in this pre-processor, which will save the programmer from annoying duplicating the code.

with respect Timur

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

jchristn commented 5 years ago

Hi @timur4000 check it out! Great suggestion, thank you for recommending it. Please refer to the Test.PreRouting project for a working example. Hope this meets your needs.

NuGet: https://www.nuget.org/packages/Watson/2.1.6 Commit: https://github.com/jchristn/WatsonWebserver/commit/b9cb8d4d51f1334bdc13da6bff4dc6a7085a52cc

Implementation:

using WatsonWebserver;

Server server = new Server(hostnames, 9000, false, RequestReceived);
server.PreRoutingHandler = PreRoutingHandler; 

static HttpResponse PreRoutingHandler(HttpRequest req)
{
  ...
}

static HttpResponse RequestReceived(HttpRequest req) 
{
  ...
}
timur4000 commented 5 years ago

It works! Thank you Joel!