Giter Club home page Giter Club logo

Comments (3)

mholt avatar mholt commented on May 21, 2024

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
dreddpose_1024

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.

wallyqs avatar wallyqs commented on May 21, 2024

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.

robstrong avatar robstrong commented on May 21, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.