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: can't read 301 response without a Location header #49281
Comments
You could use |
cc @neild |
RFC 7231 says that "the server SHOULD generate a Location header field in the response" for a 301 or 302. I'm not sure why we're treating it as an error for the Location header to be missing, since it is not required. My first thought is that we should just return the response with no error for any 3xx status with no Location header. |
@neild It seems like this exact issue has occurred in the past with status codes 307 and 308 as referenced in issue #17773. if resp.Header.Get("Location") == "" {
// 308s have been observed in the wild being served
// without Location headers. Since Go 1.7 and earlier
// didn't follow these codes, just stop here instead
// of returning an error.
// See Issue 17773.
shouldRedirect = false
break
} Couldn't this be applied to the 301, 302, and 303 status codes as well? Something like: switch resp.StatusCode {
case 301, 302, 303:
redirectMethod = reqMethod
shouldRedirect = true
includeBody = false
if resp.Header.Get("Location") == "" {
shouldRedirect = false
break
} |
Change https://golang.org/cl/375354 mentions this issue: |
I just tested the fix using gotip and it works great. Thanks! |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
I am trying to use
net/http
to send a request to https://example.s3.us-west-1.amazonaws.com/ and read the response in order to get the actual bucket location (the correct AWS region). This response is missing aLocation
header, sonet/http
just returns an error.This does not run on play.golang.org because there is no network access. https://play.golang.org/p/BiEmt_5iEvJ
Output will be:
It is impossible to get the response to read the
x-amz-bucket-region
header and get the actual region. One could argue that S3 does not conform to HTTP specs by returning a 301 response code without a Location header. But Go's refusal to give me back the response means that I'm not able to request and handle this particular case.It would be nice to just return the request in this case, since there is no redirect to follow.
https://cs.opensource.google/go/go/+/master:src/net/http/client.go;l=643-646;drc=aa4e0f528e1e018e2847decb549cfc5ac07ecf20
A little bit below that, there's a place where both a response and an error is returned (the comment says that it is to maintain Go 1 compatibility). Perhaps a similar handling could be added here?
Something like:
This is just an idea, I'm sure there may be a better way!
What did you expect to see?
I want the ability to get
x-amz-bucket-region: us-east-1
header from the response. But since the Location header is missing it is not possible to usenet/http
to get it.What did you see instead?
The text was updated successfully, but these errors were encountered: