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

Provide custom CheckRedirect for r2.Request.Client. #175

Closed
dhermes opened this issue Jan 31, 2020 · 3 comments
Closed

Provide custom CheckRedirect for r2.Request.Client. #175

dhermes opened this issue Jan 31, 2020 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@dhermes
Copy link
Contributor

dhermes commented Jan 31, 2020

By default, if a (net/http).Client hits 10 redirects, it will give up with:

func defaultCheckRedirect(req *Request, via []*Request) error {
	if len(via) >= 10 {
		return errors.New("stopped after 10 redirects")
	}
	return nil
}

but this default can be overridden on a Client.

@dhermes dhermes added the enhancement New feature or request label Jan 31, 2020
@dhermes dhermes assigned dhermes and wcharczuk and unassigned dhermes Jan 31, 2020
@dhermes
Copy link
Contributor Author

dhermes commented Feb 1, 2020

As a way to test redirects in general:

package main

import (
	"fmt"
	"net/http"
	"net/http/httptest"
	"net/url"

	"github.com/blend/go-sdk/r2"
)

func main() {
	path := "/v1/auth/token/lookup"
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.URL.Path == path {
			http.Redirect(w, r, "/more", http.StatusTemporaryRedirect)
			return
		}
		fmt.Fprintln(w, "Hello, client")
	}))
	defer ts.Close()
	u, err := url.Parse(ts.URL)
	if err != nil {
		panic(err)
	}

	// Do a GET.
	fmt.Println("--- Doing a GET ---")
	opts := []r2.Option{
		r2.OptScheme("http"),
		r2.OptHeaderValue("X-Vault-Token", "bar"),
		r2.OptHost(u.Host),
		r2.OptGet(),
		r2.OptPathf(path),
	}
	body, res, err := r2.New("", opts...).Bytes()
	if err != nil {
		panic(err)
	}
	fmt.Printf("body: %q\n\n%#v\n", string(body), res)

	// Do a POST.
	fmt.Println("--- Doing a POST ---")
	opts = []r2.Option{
		r2.OptScheme("http"),
		r2.OptHeaderValue("X-Vault-Token", "bar"),
		r2.OptHost(u.Host),
		r2.OptPost(),
		r2.OptPathf(path),
		r2.OptJSONBody(map[string]string{"token": "foo"}),
	}
	body, res, err = r2.New("", opts...).Bytes()
	if err != nil {
		panic(err)
	}
	fmt.Printf("body: %q\n\n%#v\n", string(body), res)
}

which produces

--- Doing a GET ---
body: "Hello, client\n"

&http.Response{Status:"200 OK", StatusCode:200, Proto:"HTTP/1.1", ProtoMajor:1, ProtoMinor:1, Header:http.Header{"Content-Length":[]string{"14"}, "Content-Type":[]string{"text/plain; charset=utf-8"}, "Date":[]string{"Sat, 01 Feb 2020 00:01:16 GMT"}}, Body:(*http.bodyEOFSignal)(0xc0001bc100), ContentLength:14, TransferEncoding:[]string(nil), Close:false, Uncompressed:false, Trailer:http.Header(nil), Request:(*http.Request)(0xc0001c8000), TLS:(*tls.ConnectionState)(nil)}
--- Doing a POST ---
body: ""

&http.Response{Status:"307 Temporary Redirect", StatusCode:307, Proto:"HTTP/1.1", ProtoMajor:1, ProtoMinor:1, Header:http.Header{"Content-Length":[]string{"0"}, "Date":[]string{"Sat, 01 Feb 2020 00:01:16 GMT"}, "Location":[]string{"/more"}}, Body:http.noBody{}, ContentLength:0, TransferEncoding:[]string(nil), Close:false, Uncompressed:false, Trailer:http.Header(nil), Request:(*http.Request)(0xc000176420), TLS:(*tls.ConnectionState)(nil)}

@dhermes
Copy link
Contributor Author

dhermes commented Feb 1, 2020

Also more relevant (directly) to this issue would be the case of too many redirects:

package main

import (
	"fmt"
	"net/http"
	"net/http/httptest"
	"net/url"

	"github.com/blend/go-sdk/r2"
)

func main() {
	path := "/v1/auth/token/lookup"
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		http.Redirect(w, r, path, http.StatusTemporaryRedirect)
	}))
	defer ts.Close()
	u, err := url.Parse(ts.URL)
	if err != nil {
		panic(err)
	}

	// Do a GET.
	fmt.Println("--- Doing a GET ---")
	opts := []r2.Option{
		r2.OptScheme("http"),
		r2.OptHeaderValue("X-Vault-Token", "bar"),
		r2.OptHost(u.Host),
		r2.OptGet(),
		r2.OptPathf(path),
	}
	_, _, err = r2.New("", opts...).Bytes()
	if err == nil {
		panic("SHOULD ERR")
	}
	unwrapped, ok := err.(*url.Error)
	if !ok {
		panic("SHOULD UNWRAP")
	}

	fmt.Printf("%#v\n", unwrapped)
	fmt.Printf("%#v\n", unwrapped.Err)
}

which produces

--- Doing a GET ---
&url.Error{Op:"Get", URL:"/v1/auth/token/lookup", Err:(*errors.errorString)(0xc0001d4220)}
&errors.errorString{s:"stopped after 10 redirects"}

@dhermes
Copy link
Contributor Author

dhermes commented Feb 1, 2020

Closing this since #176 is merged. I opened #177 to track just the 307 for POST bits.

@dhermes dhermes closed this as completed Feb 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants