etcd/server/util.go
Ben Johnson 41e7266ff7 Merge branch '0.2' into config
Conflicts:
	Documentation/external-documentation.md
	server/config.go
	server/config_test.go
	server/tls_info.go
	server/util.go
2013-10-23 13:59:28 -06:00

47 lines
1.2 KiB
Go

package server
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"github.com/coreos/etcd/log"
)
func decodeJsonRequest(req *http.Request, data interface{}) error {
decoder := json.NewDecoder(req.Body)
if err := decoder.Decode(&data); err != nil && err != io.EOF {
log.Warnf("Malformed json request: %v", err)
return fmt.Errorf("Malformed json request: %v", err)
}
return nil
}
func redirect(hostname string, w http.ResponseWriter, req *http.Request) {
originalURL := req.URL
redirectURL, _ := url.Parse(hostname)
// we need the original path and raw query
redirectURL.Path = originalURL.Path
redirectURL.RawQuery = originalURL.RawQuery
redirectURL.Fragment = originalURL.Fragment
log.Debugf("Redirect to %s", redirectURL.String())
http.Redirect(w, req, redirectURL.String(), http.StatusTemporaryRedirect)
}
// trimsplit slices s into all substrings separated by sep and returns a
// slice of the substrings between the separator with all leading and trailing
// white space removed, as defined by Unicode.
func trimsplit(s, sep string) []string {
raw := strings.Split(s, ",")
trimmed := make([]string, 0)
for _, r := range raw {
trimmed = append(trimmed, strings.TrimSpace(r))
}
return trimmed
}