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

net/http: 301 redirect loses POST data #12456

Closed
johndaniels opened this issue Sep 2, 2015 · 2 comments

Comments

Projects
None yet
3 participants
@johndaniels
Copy link

commented Sep 2, 2015

What I did:

Because of a coding error, I was trying to make a POST request to a go webserver with the path "//create", instead of "/create"

Expected Behavior:

The server should give me a 404 or 400 error, or pass the request along normally. I'm not convinced that "//foo" is a valid path, but if it isn't the server should return an error rather then mangling the request passed to the handler.

Actual Behavior:

The Go server decided to claim that the request was a GET request with no content. Attached is a go server that exhibits this bug along with python code to make the strange request.

Python request script
import requests
data = {'a': 1}
requests.post("http://localhost:8081//foo", data=data)
Go Server
package main

import (
    "log"
    "net/http/httputil"
    "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
    data, _ := httputil.DumpRequest(r, true)
    log.Println(string(data))
}

func main() {
    http.HandleFunc("/foo", handler)
    http.ListenAndServe("127.0.0.1:8081", nil)
}

The output I get from the go script is the following

2015/09/02 13:32:45 GET /foo HTTP/1.1
Host: localhost:8081
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
User-Agent: python-requests/2.7.0 CPython/2.7.6 Windows/8

I'm using go 1.4.2 windows/amd64

@minux

This comment has been minimized.

Copy link
Member

commented Sep 2, 2015

If you look closely, this is what happened:

python requests send this:
POST //foo HTTP/1.1
Host: localhost:8081
Content-Length: 0
User-Agent: python-requests/2.7.0 CPython/2.7.10 Linux/2.6
Connection: keep-alive
Accept: /
Accept-Encoding: gzip, deflate

Go response with 301:
HTTP/1.1 301 Moved Permanently
Location: /foo
Date: Wed, 02 Sep 2015 18:07:41 GMT
Content-Length: 0
Content-Type: text/plain; charset=utf-8

requests resend the request as a GET:
GET /foo HTTP/1.1
Host: localhost:8081
Connection: keep-alive
Accept-Encoding: gzip, deflate
Accept: /
User-Agent: python-requests/2.7.0 CPython/2.7.10 Linux/2.6

And this is what the Go handler gets, and it responses with:
HTTP/1.1 200 OK
Date: Wed, 02 Sep 2015 18:07:41 GMT
Content-Length: 0
Content-Type: text/plain; charset=utf-8

I think this is working as intended.

/cc @bradfitz

@minux minux changed the title go doesn't handle url paths starting with "//" well. net/http: 301 redirect loses POST data Sep 2, 2015

@johndaniels

This comment has been minimized.

Copy link
Author

commented Sep 2, 2015

I guess that's fine then.

@johndaniels johndaniels closed this Sep 2, 2015

@golang golang locked and limited conversation to collaborators Sep 4, 2016

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.