Comments (3)
Interesting, thanks for the report. I've never experienced this (or heard of it) -- so I had to look it up... the author of the linked article didn't cite any source (nor is there a date on the article! I had to look in the page source, sigh).
After some digging I don't think this is still an issue (if it ever was) - resp is always set if err is nil.
And to quote Dave Cheney:
if err is nil
then you get a resp
call resp.Body.Close()
it's the law
Although a nil check wouldn't hurt, I don't think it's necessary, and I'd rather keep the code simple, where possible, for brevity's sake.
from curl-to-go.
Thanks for taking a look! And agree on keeping the code simple where possible :)
Also +1 on sources in original post... though I think the author refers to the behavior described here (also in link you shared):
https://github.com/golang/go/blob/release-branch.go1.5/src/net/http/client.go#L427
golang/go@dfd7f18
There is a test for this in Go describing that behavior so I believe it is correct... https://github.com/golang/go/blob/dfd7f18130e538c53a2974988caecacd53d473f1/src/pkg/net/http/client_test.go#L238
Below is a test adapted from test above which shows behavior in latest versions of Go:
package testing
import (
"fmt"
. "net/http"
"net/http/httptest"
"strconv"
"testing"
)
var robotsTxtHandler = HandlerFunc(func(w ResponseWriter, r *Request) {
w.Header().Set("Last-Modified", "sometime")
fmt.Fprintf(w, "User-agent: go\nDisallow: /something/")
})
// Snippet based on https://github.com/golang/go/blob/dfd7f18130e538c53a2974988caecacd53d473f1/src/pkg/net/http/client_test.go#L177
func TestFailedRedirect(t *testing.T) {
var ts *httptest.Server
ts = httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
n, _ := strconv.Atoi(r.FormValue("n"))
// Test Referer header. (7 is arbitrary position to test at)
if n == 7 {
if g, e := r.Referer(), ts.URL+"/?n=6"; e != g {
t.Errorf("on request ?n=7, expected referer of %q; got %q", e, g)
}
}
if n < 15 {
Redirect(w, r, fmt.Sprintf("/?n=%d", n+1), StatusFound)
return
}
fmt.Fprintf(w, "n=%d", n)
}))
defer ts.Close()
c := &Client{}
res, err := c.Get(ts.URL)
if res != nil && err != nil {
t.Logf("Response: %+v", res)
t.Logf("Error is: %+v", err)
} else {
t.Fatalf("Expected both response and error to be non-nil")
}
}
from curl-to-go.
I was confused about whether the response ever had to be checked on error and it appears it does not because the response body will already be closed. According to the docs, it says:
On error, any Response can be ignored. A non-nil Response with a non-nil error only occurs when CheckRedirect fails, and even then the returned Response.Body is already closed.
from curl-to-go.
Related Issues (20)
- Can I use --data-urlencode ? HOT 1
- Send file to file.io HOT 4
- Support $'...' strings HOT 9
- --data-binary should do POST not GET HOT 1
- --form POST HOT 6
- support --upload-file HOT 4
- can we get a list of ignored options? HOT 2
- thanks man HOT 1
- curl -k parameter
- -F form encoded values doesnt gets implemented
- The code is wrong HOT 1
- Support on --resolve
- Handling the -F option of Curl HOT 1
- Bug: newlines not working HOT 2
- curl --socks5 127.0.0.1:1080 url
- curl --digest not working HOT 7
- Command Line Version HOT 1
- Add checks for http status code HOT 2
- Flags -G -d works incorrectly
- --data-urlencode flag does not work
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from curl-to-go.