diff --git a/proxy/common.go b/proxy/common.go index 9da06f82..323b9611 100644 --- a/proxy/common.go +++ b/proxy/common.go @@ -40,6 +40,19 @@ func callWeave(args ...string) ([]byte, []byte, error) { return stdout.Bytes(), stderr.Bytes(), err } +func unmarshalRequestBody(r *http.Request, target interface{}) error { + body, err := ioutil.ReadAll(r.Body) + if err != nil { + return err + } + if err := r.Body.Close(); err != nil { + return err + } + r.Body = ioutil.NopCloser(bytes.NewReader(body)) + + return json.Unmarshal(body, &target) +} + func marshalRequestBody(r *http.Request, body interface{}) error { newBody, err := json.Marshal(body) if err != nil { @@ -50,6 +63,32 @@ func marshalRequestBody(r *http.Request, body interface{}) error { return nil } +func unmarshalResponseBody(r *http.Response, target interface{}) error { + body, err := ioutil.ReadAll(r.Body) + if err != nil { + return err + } + if err := r.Body.Close(); err != nil { + return err + } + r.Body = ioutil.NopCloser(bytes.NewReader(body)) + + return json.Unmarshal(body, &target) +} + +func marshalResponseBody(r *http.Response, body interface{}) error { + newBody, err := json.Marshal(body) + if err != nil { + return err + } + r.Body = ioutil.NopCloser(bytes.NewReader(newBody)) + r.ContentLength = int64(len(newBody)) + // Stop it being chunked, because that hangs + r.TransferEncoding = nil + return nil + +} + func inspectContainerInPath(client *docker.Client, path string) (*docker.Container, error) { subs := containerIDRegexp.FindStringSubmatch(path) if subs == nil { diff --git a/proxy/create_container_interceptor.go b/proxy/create_container_interceptor.go index 759ec18e..4674351f 100644 --- a/proxy/create_container_interceptor.go +++ b/proxy/create_container_interceptor.go @@ -1,8 +1,6 @@ package proxy import ( - "bytes" - "encoding/json" "errors" "fmt" "io/ioutil" @@ -42,15 +40,8 @@ func (err *ErrNoSuchImage) Error() string { } func (i *createContainerInterceptor) InterceptRequest(r *http.Request) error { - body, err := ioutil.ReadAll(r.Body) - if err != nil { - return err - } - r.Body.Close() - r.Body = ioutil.NopCloser(bytes.NewReader(body)) - container := createContainerRequestBody{} - if err := json.Unmarshal(body, &container); err != nil { + if err := unmarshalRequestBody(r, &container); err != nil { return err } @@ -74,12 +65,7 @@ func (i *createContainerInterceptor) InterceptRequest(r *http.Request) error { return err } - newBody, err := json.Marshal(container) - if err != nil { - return err - } - r.Body = ioutil.NopCloser(bytes.NewReader(newBody)) - r.ContentLength = int64(len(newBody)) + return marshalRequestBody(r, container) } return nil diff --git a/proxy/create_exec_interceptor.go b/proxy/create_exec_interceptor.go index b015a55b..1308f9cc 100644 --- a/proxy/create_exec_interceptor.go +++ b/proxy/create_exec_interceptor.go @@ -1,9 +1,6 @@ package proxy import ( - "bytes" - "encoding/json" - "io/ioutil" "net/http" "strings" @@ -14,15 +11,8 @@ import ( type createExecInterceptor struct{ proxy *Proxy } func (i *createExecInterceptor) InterceptRequest(r *http.Request) error { - body, err := ioutil.ReadAll(r.Body) - if err != nil { - return err - } - r.Body.Close() - r.Body = ioutil.NopCloser(bytes.NewReader(body)) - options := docker.CreateExecOptions{} - if err := json.Unmarshal(body, &options); err != nil { + if err := unmarshalRequestBody(r, &options); err != nil { return err } diff --git a/proxy/inspect_container_interceptor.go b/proxy/inspect_container_interceptor.go index 791fd653..2010660b 100644 --- a/proxy/inspect_container_interceptor.go +++ b/proxy/inspect_container_interceptor.go @@ -1,9 +1,6 @@ package proxy import ( - "bytes" - "encoding/json" - "io/ioutil" "net/http" "github.com/fsouza/go-dockerclient" @@ -21,14 +18,8 @@ func (i *inspectContainerInterceptor) InterceptResponse(r *http.Response) error return nil } - body, err := ioutil.ReadAll(r.Body) - if err != nil { - return err - } - r.Body.Close() - container := &docker.Container{} - if err := json.Unmarshal(body, container); err != nil { + if err := unmarshalResponseBody(r, container); err != nil { return err } @@ -36,15 +27,7 @@ func (i *inspectContainerInterceptor) InterceptResponse(r *http.Response) error Log.Warningf("Inspecting container %s failed: %s", container.ID, err) } - newBody, err := json.Marshal(container) - if err != nil { - return err - } - r.Body = ioutil.NopCloser(bytes.NewReader(newBody)) - r.ContentLength = int64(len(newBody)) - r.TransferEncoding = nil // Stop it being chunked, because that hangs - - return nil + return marshalResponseBody(r, container) } func updateContainerNetworkSettings(container *docker.Container) error { diff --git a/proxy/inspect_exec_interceptor.go b/proxy/inspect_exec_interceptor.go index f12ecbcb..d787fcdf 100644 --- a/proxy/inspect_exec_interceptor.go +++ b/proxy/inspect_exec_interceptor.go @@ -1,9 +1,6 @@ package proxy import ( - "bytes" - "encoding/json" - "io/ioutil" "net/http" "github.com/fsouza/go-dockerclient" @@ -21,14 +18,8 @@ func (i *inspectExecInterceptor) InterceptResponse(r *http.Response) error { return nil } - body, err := ioutil.ReadAll(r.Body) - if err != nil { - return err - } - r.Body.Close() - exec := &docker.ExecInspect{} - if err := json.Unmarshal(body, exec); err != nil { + if err := unmarshalResponseBody(r, &exec); err != nil { return err } @@ -36,13 +27,5 @@ func (i *inspectExecInterceptor) InterceptResponse(r *http.Response) error { Log.Warningf("Inspecting exec %s failed: %s", exec.ID, err) } - newBody, err := json.Marshal(exec) - if err != nil { - return err - } - r.Body = ioutil.NopCloser(bytes.NewReader(newBody)) - r.ContentLength = int64(len(newBody)) - r.TransferEncoding = nil // Stop it being chunked, because that hangs - - return nil + return marshalResponseBody(r, exec) }