[proxy] refactoring out common marshalling logic

This commit is contained in:
Paul Bellamy authored and Matthias Radestock committed 2015-09-04 13:40:07 +01:00
1 parent e21a204eed
commit 19ceb424c3
5 files changed
+46 -65

No files matched your search

+39
View File
@@ -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 {
+2 -16
View File
@@ -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
+1 -11
View File
@@ -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
}
+2 -19
View File
@@ -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 {
+2 -19
View File
@@ -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)
}