···368368 request.Header.Set("Host", siteURL.Host)
369369 }
370370371371+ // HTTP-to-HTTPS redirects are often implemented with 301 or 302 response codes which instruct
372372+ // the Go HTTP client to disregard the original request method and use GET instead. To prevent
373373+ // confusion, detect such redirects and preserve the original request method.
374374+ http.DefaultClient.CheckRedirect = func(req *http.Request, via []*http.Request) error {
375375+ last := via[len(via)-1]
376376+ newURL := req.URL.JoinPath()
377377+ lastURL := last.URL.JoinPath()
378378+ if lastURL.Scheme == "http" && newURL.Scheme == "https" {
379379+ lastURL.Scheme = newURL.Scheme
380380+ if lastURL.String() == newURL.String() {
381381+ req.Method = last.Method
382382+ return nil
383383+ }
384384+ }
385385+ if req.Method != last.Method {
386386+ return fmt.Errorf("unexpected redirect")
387387+ }
388388+ return nil
389389+ }
390390+371391 displayServer := *verboseFlag
372392 for {
373393 response, err := http.DefaultClient.Do(request)
374394 if err != nil {
395395+ if response.StatusCode == 301 || response.StatusCode == 302 {
396396+ if innerErr := errors.Unwrap(err); innerErr != nil {
397397+ // If the error is due to a redirect, that means it wasn't followed, but the
398398+ // original url.Error confusingly reports the new URL anyway.
399399+ err = innerErr
400400+ }
401401+ }
375402 fmt.Fprintf(os.Stderr, "error: %s\n", err)
376403 os.Exit(1)
377404 }