From 2f8c5d7bd95c4da5eeaff76ee24702bedd3ae4d3 Mon Sep 17 00:00:00 2001 From: Paul Bellamy Date: Tue, 19 May 2015 09:59:36 +0100 Subject: [PATCH] proxy interceptors implicitly modify arguments, instead of returning them --- proxy/client.go | 6 +++--- proxy/create_container_interceptor.go | 18 +++++++++--------- proxy/interceptor.go | 12 ++++++------ proxy/start_container_interceptor.go | 16 ++++++++-------- 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/proxy/client.go b/proxy/client.go index 08e0a59f..3b094dde 100644 --- a/proxy/client.go +++ b/proxy/client.go @@ -27,7 +27,7 @@ func newClient(dial func() (net.Conn, error), i interceptor) *client { } func (c *client) ServeHTTP(w http.ResponseWriter, r *http.Request) { - req, err := c.InterceptRequest(r) + err := c.InterceptRequest(r) if err == docker.ErrNoSuchImage { http.NotFound(w, r) return @@ -47,13 +47,13 @@ func (c *client) ServeHTTP(w http.ResponseWriter, r *http.Request) { client := httputil.NewClientConn(conn, nil) defer client.Close() - resp, err := client.Do(req) + resp, err := client.Do(r) if err != nil && err != httputil.ErrPersistEOF { http.Error(w, fmt.Sprintf("Could not make request to target: %v", err), http.StatusInternalServerError) Warning.Print("Error forwarding request: ", err) return } - resp, err = c.InterceptResponse(resp) + err = c.InterceptResponse(resp) if err != nil { http.Error(w, "Unable to intercept response", http.StatusInternalServerError) Warning.Print("Error intercepting response: ", err) diff --git a/proxy/create_container_interceptor.go b/proxy/create_container_interceptor.go index d08cb2d8..35841a9b 100644 --- a/proxy/create_container_interceptor.go +++ b/proxy/create_container_interceptor.go @@ -21,36 +21,36 @@ type createContainerRequestBody struct { MacAddress string `json:"MacAddress,omitempty" yaml:"MacAddress,omitempty"` } -func (i *createContainerInterceptor) InterceptRequest(r *http.Request) (*http.Request, error) { +func (i *createContainerInterceptor) InterceptRequest(r *http.Request) error { body, err := ioutil.ReadAll(r.Body) if err != nil { - return nil, err + return err } r.Body.Close() container := createContainerRequestBody{} if err := json.Unmarshal(body, &container); err != nil { - return nil, err + return err } if _, ok := weaveCIDRsFromConfig(container.Config); ok { container.HostConfig.VolumesFrom = append(container.HostConfig.VolumesFrom, "weaveproxy") if err := i.setWeaveWaitEntrypoint(container.Config); err != nil { - return nil, err + return err } if err := i.setWeaveDNS(&container); err != nil { - return nil, err + return err } } newBody, err := json.Marshal(container) if err != nil { - return nil, err + return err } r.Body = ioutil.NopCloser(bytes.NewReader(newBody)) r.ContentLength = int64(len(newBody)) - return r, nil + return nil } func (i *createContainerInterceptor) setWeaveWaitEntrypoint(container *docker.Config) error { @@ -100,6 +100,6 @@ func (i *createContainerInterceptor) setWeaveDNS(container *createContainerReque return nil } -func (i *createContainerInterceptor) InterceptResponse(r *http.Response) (*http.Response, error) { - return r, nil +func (i *createContainerInterceptor) InterceptResponse(r *http.Response) error { + return nil } diff --git a/proxy/interceptor.go b/proxy/interceptor.go index 203a0d03..69d903e2 100644 --- a/proxy/interceptor.go +++ b/proxy/interceptor.go @@ -3,17 +3,17 @@ package proxy import "net/http" type interceptor interface { - InterceptRequest(*http.Request) (*http.Request, error) - InterceptResponse(*http.Response) (*http.Response, error) + InterceptRequest(*http.Request) error + InterceptResponse(*http.Response) error } type nullInterceptor struct { } -func (i nullInterceptor) InterceptRequest(r *http.Request) (*http.Request, error) { - return r, nil +func (i nullInterceptor) InterceptRequest(r *http.Request) error { + return nil } -func (i nullInterceptor) InterceptResponse(r *http.Response) (*http.Response, error) { - return r, nil +func (i nullInterceptor) InterceptResponse(r *http.Response) error { + return nil } diff --git a/proxy/start_container_interceptor.go b/proxy/start_container_interceptor.go index 4049a107..88c391b3 100644 --- a/proxy/start_container_interceptor.go +++ b/proxy/start_container_interceptor.go @@ -16,15 +16,15 @@ type startContainerInterceptor struct { withDNS bool } -func (i *startContainerInterceptor) InterceptRequest(r *http.Request) (*http.Request, error) { - return r, nil +func (i *startContainerInterceptor) InterceptRequest(r *http.Request) error { + return nil } -func (i *startContainerInterceptor) InterceptResponse(r *http.Response) (*http.Response, error) { +func (i *startContainerInterceptor) InterceptResponse(r *http.Response) error { containerID := "" if subs := containerIDRegexp.FindStringSubmatch(r.Request.URL.Path); subs == nil { Warning.Printf("No container id found in request with path %s", r.Request.URL.Path) - return r, nil + return nil } else { containerID = subs[1] } @@ -32,13 +32,13 @@ func (i *startContainerInterceptor) InterceptResponse(r *http.Response) (*http.R container, err := i.client.InspectContainer(containerID) if err != nil { Warning.Printf("Error inspecting container %s: %v", containerID, err) - return r, nil + return nil } cidrs, ok := weaveCIDRsFromConfig(container.Config) if !ok { Debug.Print("No Weave CIDR, ignoring") - return r, nil + return nil } Info.Printf("Container %s was started with CIDR \"%s\"", containerID, strings.Join(cidrs, " ")) args := []string{"attach"} @@ -46,7 +46,7 @@ func (i *startContainerInterceptor) InterceptResponse(r *http.Response) (*http.R args = append(args, containerID) if _, err := callWeave(args...); err != nil { Warning.Printf("Attaching container %s to weave failed: %v", containerID, err) - return r, nil + return nil } - return r, nil + return nil }