mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
refactor(server): move utilities into pkg
like camlistore lets move these utilities into a `pkg` prefix.
This commit is contained in:
33
pkg/http/http.go
Normal file
33
pkg/http/http.go
Normal file
@@ -0,0 +1,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)
|
||||
}
|
||||
Reference in New Issue
Block a user