forked from etcd-io/etcd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
33 lines (27 loc) · 840 Bytes
/
http.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package http
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"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)
}