301 is reported as 404 for http post

fix https://github.com/chrislusf/seaweedfs/issues/512
This commit is contained in:
Chris Lu 2017-06-15 21:21:32 -07:00
parent 761c0eb1ed
commit 72e89b615b
2 changed files with 11 additions and 11 deletions

View file

@ -189,14 +189,12 @@ func postFollowingOneRedirect(target string, contentType string, b bytes.Buffer)
return err return err
} }
defer resp.Body.Close() defer resp.Body.Close()
reply, _ := ioutil.ReadAll(resp.Body)
statusCode := resp.StatusCode statusCode := resp.StatusCode
data, _ := ioutil.ReadAll(resp.Body)
reply := string(data)
if statusCode == http.StatusMovedPermanently { if strings.HasPrefix(reply, "\"http") {
var urlStr string urlStr := reply[1 : len(reply)-1]
if urlStr = resp.Header.Get("Location"); urlStr == "" {
return fmt.Errorf("%d response missing Location header", resp.StatusCode)
}
glog.V(0).Infoln("Post redirected to ", urlStr) glog.V(0).Infoln("Post redirected to ", urlStr)
resp2, err2 := http.Post(urlStr, contentType, backupReader) resp2, err2 := http.Post(urlStr, contentType, backupReader)
@ -204,13 +202,13 @@ func postFollowingOneRedirect(target string, contentType string, b bytes.Buffer)
return err2 return err2
} }
defer resp2.Body.Close() defer resp2.Body.Close()
reply, _ = ioutil.ReadAll(resp2.Body) data, _ = ioutil.ReadAll(resp2.Body)
statusCode = resp2.StatusCode statusCode = resp2.StatusCode
} }
glog.V(0).Infoln("Post returned status: ", statusCode, string(reply)) glog.V(0).Infoln("Post returned status: ", statusCode, string(data))
if statusCode != http.StatusOK { if statusCode != http.StatusOK {
return errors.New(string(reply)) return errors.New(string(data))
} }
return nil return nil

View file

@ -44,8 +44,10 @@ func (s *RaftServer) HandleFunc(pattern string, handler func(http.ResponseWriter
func (s *RaftServer) redirectToLeader(w http.ResponseWriter, req *http.Request) { func (s *RaftServer) redirectToLeader(w http.ResponseWriter, req *http.Request) {
if leader, e := s.topo.Leader(); e == nil { if leader, e := s.topo.Leader(); e == nil {
//http.StatusMovedPermanently does not cause http POST following redirection //http.StatusMovedPermanently does not cause http POST following redirection
glog.V(0).Infoln("Redirecting to", http.StatusMovedPermanently, "http://"+leader+req.URL.Path) learderLocation := "http://" + leader + req.URL.Path
http.Redirect(w, req, "http://"+leader+req.URL.Path, http.StatusMovedPermanently) glog.V(0).Infoln("Redirecting to", learderLocation)
writeJsonQuiet(w, req, http.StatusOK, learderLocation)
// http.Redirect(w, req, "http://"+leader+req.URL.Path, http.StatusFound) // not working any more
} else { } else {
glog.V(0).Infoln("Error: Leader Unknown") glog.V(0).Infoln("Error: Leader Unknown")
http.Error(w, "Leader unknown", http.StatusInternalServerError) http.Error(w, "Leader unknown", http.StatusInternalServerError)