From 72e89b615b02f1af03d0f838ea6feb394ab1d401 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Thu, 15 Jun 2017 21:21:32 -0700 Subject: [PATCH] 301 is reported as 404 for http post fix https://github.com/chrislusf/seaweedfs/issues/512 --- weed/server/raft_server.go | 16 +++++++--------- weed/server/raft_server_handlers.go | 6 ++++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/weed/server/raft_server.go b/weed/server/raft_server.go index 35b802753..e3914d9f8 100644 --- a/weed/server/raft_server.go +++ b/weed/server/raft_server.go @@ -189,14 +189,12 @@ func postFollowingOneRedirect(target string, contentType string, b bytes.Buffer) return err } defer resp.Body.Close() - reply, _ := ioutil.ReadAll(resp.Body) statusCode := resp.StatusCode + data, _ := ioutil.ReadAll(resp.Body) + reply := string(data) - if statusCode == http.StatusMovedPermanently { - var urlStr string - if urlStr = resp.Header.Get("Location"); urlStr == "" { - return fmt.Errorf("%d response missing Location header", resp.StatusCode) - } + if strings.HasPrefix(reply, "\"http") { + urlStr := reply[1 : len(reply)-1] glog.V(0).Infoln("Post redirected to ", urlStr) resp2, err2 := http.Post(urlStr, contentType, backupReader) @@ -204,13 +202,13 @@ func postFollowingOneRedirect(target string, contentType string, b bytes.Buffer) return err2 } defer resp2.Body.Close() - reply, _ = ioutil.ReadAll(resp2.Body) + data, _ = ioutil.ReadAll(resp2.Body) 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 { - return errors.New(string(reply)) + return errors.New(string(data)) } return nil diff --git a/weed/server/raft_server_handlers.go b/weed/server/raft_server_handlers.go index 335ba668f..0a794ce6c 100644 --- a/weed/server/raft_server_handlers.go +++ b/weed/server/raft_server_handlers.go @@ -44,8 +44,10 @@ func (s *RaftServer) HandleFunc(pattern string, handler func(http.ResponseWriter func (s *RaftServer) redirectToLeader(w http.ResponseWriter, req *http.Request) { if leader, e := s.topo.Leader(); e == nil { //http.StatusMovedPermanently does not cause http POST following redirection - glog.V(0).Infoln("Redirecting to", http.StatusMovedPermanently, "http://"+leader+req.URL.Path) - http.Redirect(w, req, "http://"+leader+req.URL.Path, http.StatusMovedPermanently) + learderLocation := "http://" + leader + req.URL.Path + 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 { glog.V(0).Infoln("Error: Leader Unknown") http.Error(w, "Leader unknown", http.StatusInternalServerError)