proxy interceptors implicitly modify arguments, instead of returning them
This commit is contained in:
1 parent
126d4f4079
commit
2f8c5d7bd9
4 files changed
+26
-26
No files matched your search
+3
-3
@@ -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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
Reference in new issue
Block a user