1
0
Fork 0

CLI follows 301 redirect

master
Philip O'Toole 8 years ago
parent 461848cca9
commit 5ff3a885e7

@ -2,6 +2,7 @@
- [PR #151](https://github.com/rqlite/rqlite/pull/151): Support configurable Raft heartbeat timeout. - [PR #151](https://github.com/rqlite/rqlite/pull/151): Support configurable Raft heartbeat timeout.
- [PR #149](https://github.com/rqlite/rqlite/pull/149): Support configurable Raft snapshot thresholds. - [PR #149](https://github.com/rqlite/rqlite/pull/149): Support configurable Raft snapshot thresholds.
- [PR #148](https://github.com/rqlite/rqlite/pull/148): Support pprof information over HTTP. - [PR #148](https://github.com/rqlite/rqlite/pull/148): Support pprof information over HTTP.
- [PR #154](https://github.com/rqlite/rqlite/pull/154): CLI now redirects to leader if necessary.
## 3.2.1 (May 22nd 2016) ## 3.2.1 (May 22nd 2016)
- [PR #143](https://github.com/rqlite/rqlite/pull/143): Use DELETE as HTTP method to remove nodes. - [PR #143](https://github.com/rqlite/rqlite/pull/143): Use DELETE as HTTP method to remove nodes.

@ -11,6 +11,8 @@ import (
"github.com/mkideal/cli" "github.com/mkideal/cli"
) )
const maxRedirect = 21
type argT struct { type argT struct {
cli.Helper cli.Helper
Protocol string `cli:"s,scheme" usage:"protocol scheme(http or https)" dft:"http"` Protocol string `cli:"s,scheme" usage:"protocol scheme(http or https)" dft:"http"`
@ -73,17 +75,34 @@ func makeJSONBody(line string) string {
func sendRequest(ctx *cli.Context, urlStr string, line string, ret interface{}) error { func sendRequest(ctx *cli.Context, urlStr string, line string, ret interface{}) error {
data := makeJSONBody(line) data := makeJSONBody(line)
resp, err := http.Post(urlStr, "application/json", strings.NewReader(data)) url := urlStr
if err != nil {
return err nRedirect := 0
} for {
defer resp.Body.Close() resp, err := http.Post(url, "application/json", strings.NewReader(data))
body, err := ioutil.ReadAll(resp.Body) if err != nil {
if err != nil { return err
return err }
} defer resp.Body.Close()
if err := json.Unmarshal(body, ret); err != nil {
return fmt.Errorf(string(body)) // Check for redirect.
if resp.StatusCode == http.StatusMovedPermanently {
nRedirect++
if nRedirect > maxRedirect {
return fmt.Errorf("maximum leader redirect limit exceeded")
}
url = resp.Header["Location"][0]
continue
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if err := json.Unmarshal(body, ret); err != nil {
return err
}
return nil
} }
return nil
} }

Loading…
Cancel
Save