Created
          November 1, 2016 11:40 
        
      - 
      
- 
        Save tyrnov/a23e3ddf992a4dd227641d9d6580f7d4 to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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