Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong method with trailing slash #921

Closed
Yopes opened this issue Mar 8, 2018 · 1 comment
Closed

Wrong method with trailing slash #921

Yopes opened this issue Mar 8, 2018 · 1 comment
Labels
good first issue A user wrote a good first issue with clear instructions 🚨 prio:high 🤘 status:resolved 🐞 type:bug

Comments

@Yopes
Copy link

Yopes commented Mar 8, 2018

Hello,
I'm encountering an issue with the iris's routes matcher.
I'm using iris v10.2.1
Let's consider the following main:

package main                                                                                                                                                                                                        
                                                                                                                                                                                                                    
import "github.com/kataras/iris"                                                                                                                                                                                    
                                                                                                                                                                                                                    
                                                                                                                                                                                                                    
func main() {                                                                                                                                                                                                       
     app := iris.New()                                                                                                                                                                                              
     app.Handle("GET", "/foo", handler)                                                                                                                                                                          
     app.Handle("POST", "/foo", handler)                                                                                                                                                                         
     app.Handle("GET", "/foo/{bar}", handler)                                                                                                                                                                   
     app.Handle("POST", "/foo/{bar}", handler)                                                                                                                                                                  
     app.Run(iris.Addr(":8082"))                                                                                                                                                                                    
}                                                                                                                                                                                                                   
                                                                                                                                                                                                                    
func handler(ctx iris.Context){                                                                                                                                                                                     
    ctx.Writef("Hello from method: %s and path: %s", ctx.Method(), ctx.Path())                                                                                                                                      
}  

I ran this code and I tried it.

When I do a POST request on http://127.0.0.1:8082/foo => it's working
When I do a GET request on http://127.0.0.1:8082/foo => it's working
When I do a GET request on http://127.0.0.1:8082/foo/ => it's working

But when I do a POST request on http://127.0.0.1:8082/foo/ ,
The result is: "Hello from method: GET and path: /foo"

I guess the ctx.Method() is not supposed to return GET

Thanks for looking at this issue.

@kataras kataras added good first issue A user wrote a good first issue with clear instructions 🐞 type:bug 🚨 prio:high labels Mar 8, 2018
@kataras kataras closed this as completed in dc589d9 Mar 8, 2018
@kataras
Copy link
Owner

kataras commented Mar 8, 2018

Hello @Yopes, yes this is caused because of this: https://github.com/kataras/iris/blob/master/core/router/handler.go#L175 , it redirects with StatusMovedPermantly (301) to let client to cache the redirect in order to gain performance on the next requests with trailing slash.

When automatically redirecting a POST request after receiving a 301 status code, some existing HTTP/1.0 user agents will erroneously change it into a GET request. (that is hapenning here)

The StatusMovedPermantly redirects to GET methods, it doesn't permit POST redirects for security reasons: imagine a shop card, you can't just permantly redirect to one address with the data follows them. For that reason I fixed it with 307 temp redirect status code for both POST and PUT methods.

This was an easy to fix bug but it was important, I always recommend using the /path pattern instead of /path/ but you never know the client's behavior, you did well reporting this behavior, excellent job Kevin!

github-actions bot pushed a commit to goproxies/github.com-kataras-iris that referenced this issue Jul 27, 2020
Former-commit-id: f8560514a7b48f83121ddd21d74b4016af4e1b67
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue A user wrote a good first issue with clear instructions 🚨 prio:high 🤘 status:resolved 🐞 type:bug
Projects
None yet
Development

No branches or pull requests

2 participants