util.go 865 B

123456789101112131415161718192021222324252627282930313233
  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. redirectURL.Fragment = originalURL.Fragment
  25. log.Debugf("Redirect to %s", redirectURL.String())
  26. http.Redirect(w, req, redirectURL.String(), http.StatusTemporaryRedirect)
  27. }