Control redirects, cookies and streaming
Control redirects, cookies and streaming
A client follows redirects by default subject to policy. CheckRedirect can limit hops and destinations. CookieJar stores cookies and selects applicable ones for later requests. Streaming processes the body incrementally instead of allocating it all at once. These examples use one local origin and synthetic data.
package main
import ( "fmt" "net/http" "net/http/httptest" "time")
func check(err error) { if err != nil { panic(err) }}func fixture(h http.HandlerFunc) (*httptest.Server, *http.Client) { s := httptest.NewServer(h) c := s.Client() c.Timeout = 2 * time.Second return s, c}func main() { s, c := fixture(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/" { http.Redirect(w, r, "/next", 302) return } fmt.Fprint(w, "next") }) defer s.Close() c.CheckRedirect = func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse } res, e := c.Get(s.URL) check(e) defer res.Body.Close() fmt.Println(res.StatusCode, res.Header.Get("Location"))}ErrUseLastResponse returns the last response with open body and nil error. The caller closes it. For 301/302/303 Go normally changes subsequent requests to GET (HEAD stays HEAD); 307/308 preserve method and body when replay is possible. Revalidate each destination and never assume credentials are safe to forward. Redirect policy alone is not a complete SSRF defense.
Expected output:
302 /nextRetain a synthetic cookie and scan lines
package main
import ( "bufio" "fmt" "io" "net/http" "net/http/cookiejar" "net/http/httptest" "time")
func check(err error) { if err != nil { panic(err) }}func fixture(h http.HandlerFunc) (*httptest.Server, *http.Client) { s := httptest.NewServer(h) c := s.Client() c.Timeout = 2 * time.Second return s, c}func main() { s, c := fixture(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/set" { http.SetCookie(w, &http.Cookie{Name: "theme", Value: "dark", Path: "/"}) w.WriteHeader(204) return } v, e := r.Cookie("theme") if e != nil { w.WriteHeader(400) return } fmt.Fprintln(w, v.Value) fmt.Fprintln(w, "ready") }) defer s.Close() jar, e := cookiejar.New(nil) check(e) c.Jar = jar res, e := c.Get(s.URL + "/set") check(e) check(res.Body.Close()) res, e = c.Get(s.URL + "/read") check(e) defer res.Body.Close() sc := bufio.NewScanner(io.LimitReader(res.Body, 100)) for sc.Scan() { fmt.Println(sc.Text()) } check(sc.Err())}The jar receives Set-Cookie and sends matching cookies automatically. A nil public suffix list is suitable only for this single-host fixture; use a maintained list for real multi-domain clients. Scanner has a token limit and requires an Err check. LimitReader alone truncates rather than detecting excess: acceptable only for this known tiny fixture. For untrusted streams detect overflow and bound total bytes and time. Go cookie jars do not implement every browser policy.
Expected output:
darkreadyTry it locally
Stop a local 307 redirect to /done and print status and Location.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
HTTP clients 06: Control redirects, cookies and streaming
Check the lesson objective before moving on.
What does ErrUseLastResponse return?
What is a CookieJar responsible for?