Skip to content

Instantly share code, notes, and snippets.

@tyrnov
Created November 1, 2016 11:40
Show Gist options
  • Save tyrnov/a23e3ddf992a4dd227641d9d6580f7d4 to your computer and use it in GitHub Desktop.
Save tyrnov/a23e3ddf992a4dd227641d9d6580f7d4 to your computer and use it in GitHub Desktop.
package main
import (
"errors"
"log"
"net/http"
"net/url"
)
type checkURL url.URL
func newCheckURL(rawurl string) (*checkURL, error) {
u, err := url.Parse(rawurl)
if err != nil {
return nil, err
}
cu := checkURL(*u)
return &cu, nil
}
func (c *checkURL) checkRedirect(req *http.Request, via []*http.Request) error {
if len(via) > 10 {
return errors.New("too many redirects")
}
if req.URL.Host != c.Host {
return errors.New("not valid host")
}
return nil
}
func main() {
client := &http.Client{}
newurl, err := newCheckURL("https://localhost")
if err != nil {
log.Fatal(err)
}
client.CheckRedirect = newurl.checkRedirect
// var url = "http://localhost"
// client.Get(...)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment