redstone-dart / redstone

A metadata driven microframework for Dart.
http://redstone-dart.github.io/redstone
MIT License
342 stars 42 forks source link

redstone.redirct() not working unless set app.showErrorPage = false; #170

Closed lukechi1219 closed 8 years ago

lukechi1219 commented 8 years ago

redstone.redirect() just return 302 without real redirection and redstone treats a 302 as an error I suppose and displays an error page when that happens Don't think we should consider 302 as an error though

@Pacane

maybe can follow dart api https://api.dartlang.org/1.12.0/dart-io/HttpClientRequest/followRedirects.html

Automatic redirect will only happen for "GET" and "HEAD" requests and only for the status codes HttpHeaders.MOVED_PERMANENTLY (301), HttpStatus.FOUND (302), HttpStatus.MOVED_TEMPORARILY (302, alias for HttpStatus.FOUND), HttpStatus.SEE_OTHER (303) and HttpStatus.TEMPORARY_REDIRECT (307). For HttpStatus.SEE_OTHER (303) autmatic redirect will also happen for "POST" requests with the method changed to "GET" when following the redirect.

ref: https://github.com/redstone-dart/redstone/issues/130 http://redstonedart.org/doc/Error-handlers/

Since the version 0.6, Redstone generates an error page whenever a response with status code less than 200, or greater or equal than 300, is returned. To prevent this behavior, set the showErrorPage flag to false.

sestegra commented 8 years ago

3xx codes are dedicated to redirection. So, they aren't errors. https://en.wikipedia.org/wiki/List_of_HTTP_status_codes

The response with 3xx should be interpreted by the client with ignoring the response body and with redoing a request to the given URL in location header.

This behavior has already been implemented on web browsers (Chrome, Firefox, IE...). However, with curl, you have to add -Loption to follow redirection.

curl -L -v http://127.0.0.1:8080/
*   Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 8080 (#0)
> GET / HTTP/1.1
> Host: 127.0.0.1:8080
> User-Agent: curl/7.43.0
> Accept: */*
> 
< HTTP/1.1 302 Found
< location: /redirect
< date: Mon, 06 Jun 2016 12:36:02 GMT
< transfer-encoding: chunked
< x-frame-options: SAMEORIGIN
< content-type: text/plain; charset=utf-8
< x-xss-protection: 1; mode=block
< x-content-type-options: nosniff
< server: dart:io with Redstone.dart/Shelf
< 
* Ignoring the response-body
* Connection #0 to host 127.0.0.1 left intact
* Issue another request to this URL: 'http://127.0.0.1:8080/redirect'
* Found bundle for host 127.0.0.1: 0x7fe858403f60
* Re-using existing connection! (#0) with host 127.0.0.1
* Connected to 127.0.0.1 (127.0.0.1) port 8080 (#0)
> GET /redirect HTTP/1.1
> Host: 127.0.0.1:8080
> User-Agent: curl/7.43.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< transfer-encoding: chunked
< date: Mon, 06 Jun 2016 12:36:02 GMT
< x-frame-options: SAMEORIGIN
< content-type: text/plain
< x-xss-protection: 1; mode=block
< x-content-type-options: nosniff
< server: dart:io with Redstone.dart/Shelf
< 
* Connection #0 to host 127.0.0.1 left intact
Redirected