dotnet / WatsonWebserver

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

302 redirect: Headers don't seem to go through on response #130

Closed zedle closed 8 months ago

zedle commented 8 months ago

I'm trying to setup a 302 redirect from a route. I set the Location header and the status code in the ctx.Response object but the Location header never comes through.

ctx.Response.Headers.Add("Location", authorizationUrl);
ctx.Response.StatusCode = 302;
await ctx.Response.Send();

What am I doing wrong here?

zedle commented 8 months ago

Note I am using the .Lite variant

jchristn commented 8 months ago

Hi @zedle it looks correct. I just tested with curl -v http://localhost:8080, and also Chrome, against a change I made to the Test.Default project to implement your code. With normal Watson webserver, it worked great. But yes, you're right the .Lite variant doesn't work the same. It appears there are two differences: 1) the status description is different, and 2) the location header somehow doesn't make it to the output.

WatsonWebserver output (curl):

C:\Users\joelc>curl http://localhost:8080/foo -v
*   Trying [::1]:8080...
* Connected to localhost (::1) port 8080
> GET /foo HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/8.4.0
> Accept: */*
>
< HTTP/1.1 302 Moved Temporarily
< Cache-Control: no-cache
< Content-Length: 0
< Location: https://www.google.com
< Server: Microsoft-HTTPAPI/2.0
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: OPTIONS, HEAD, GET, PUT, POST, DELETE, PATCH
< Access-Control-Allow-Headers: *
< Access-Control-Expose-Headers:
< Accept: */*
< Accept-Language: en-US, en
< Accept-Charset: ISO-8859-1, utf-8
< Host: localhost:8000
< Date: Fri, 12 Jan 2024 14:36:52 GMT
< Connection: close
<
* Closing connection

WatsonWebserver.Lite output (curl):

C:\Users\joelc>curl http://localhost:8080/foo -v
*   Trying [::1]:8080...
* Connected to localhost (::1) port 8080
> GET /foo HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/8.4.0
> Accept: */*
>
< HTTP/1.1 302 Found
< Content-Length: 0
< Date: Fri, 12 Jan 2024 14:38:15 GMT
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: OPTIONS, HEAD, GET, PUT, POST, DELETE, PATCH
< Access-Control-Allow-Headers: *
< Access-Control-Expose-Headers:
< Accept: */*
< Accept-Language: en-US, en
< Accept-Charset: ISO-8859-1, utf-8
< Cache-Control: no-cache
< Connection: close
< Host: localhost:8000
<
* Closing connection

I will look into this ASAP. Thanks for letting me know.

jchristn commented 8 months ago

Hi @zedle I just published v6.1.6 to NuGet to fix this. Thanks for letting me know about the issue! Please re-open if this doesn't solve it for you.

Also I amended the Test.Default project to add a pre-auth static route for /redirect which will provide a status 302 and redirect to https://github.com/dotnet/watsonwebserver.

zedle commented 8 months ago

Works great now, thank you for the quick fix @jchristn !