util.go 820 B

1234567891011121314151617181920212223242526272829303132
  1. package server
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "net/url"
  8. "github.com/coreos/etcd/log"
  9. )
  10. func decodeJsonRequest(req *http.Request, data interface{}) error {
  11. decoder := json.NewDecoder(req.Body)
  12. if err := decoder.Decode(&data); err != nil && err != io.EOF {
  13. log.Warnf("Malformed json request: %v", err)
  14. return fmt.Errorf("Malformed json request: %v", err)
  15. }
  16. return nil
  17. }
  18. func redirect(hostname string, w http.ResponseWriter, req *http.Request) {
  19. originalURL, _ := url.Parse(req.URL.String())
  20. redirectURL, _ := url.Parse(hostname)
  21. // we need the original path and raw query
  22. redirectURL.Path = originalURL.Path
  23. redirectURL.RawQuery = originalURL.RawQuery
  24. log.Debugf("Redirect to %s", redirectURL.String())
  25. http.Redirect(w, req, redirectURL.String(), http.StatusTemporaryRedirect)
  26. }