Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix go1.20.6 host header for unix sockets #13

Merged
merged 2 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion container/kill_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ func TestKill(t *testing.T) {
assert.Check(t, errdefs.IsNotFound(err), err)

c, err := s.Create(ctx, "busybox:latest", WithCreateName(strings.ToLower(t.Name())), WithCreateTTY, WithCreateCmd("/bin/sh", "-c", "trap 'exit 0' SIGTERM; while true; do usleep 100000; done"))
assert.NilError(t, err)
defer func() {
assert.Check(t, s.Remove(ctx, c.ID(), WithRemoveForce))
}()
assert.NilError(t, err)
assert.NilError(t, c.Start(ctx))

err = c.Kill(ctx, WithKillSignal("FAKESIG"))
Expand Down
38 changes: 31 additions & 7 deletions transport/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http/httputil"
"net/url"
"path"
"strings"
"time"
)

Expand All @@ -31,10 +32,11 @@ type RequestOpt func(*http.Request) error
//
// Create a transport from one of the available helper functions.
type Transport struct {
c *http.Client
dial func(context.Context) (net.Conn, error)
host string
scheme string
c *http.Client
dial func(context.Context) (net.Conn, error)
host string
scheme string
transform func(*http.Request)
}

// Do implements the Doer.Do interface
Expand All @@ -50,6 +52,10 @@ func (t *Transport) Do(ctx context.Context, method, uri string, opts ...RequestO
return nil, err
}
}

if t.transform != nil {
t.transform(req)
}
resp, err := t.c.Do(req)
if err != nil {
return resp, err
Expand All @@ -71,6 +77,10 @@ func (t *Transport) DoRaw(ctx context.Context, method, uri string, opts ...Reque
}
}

if t.transform != nil {
t.transform(req)
}

conn, err := t.dial(ctx)
if err != nil {
return nil, err
Expand All @@ -84,9 +94,11 @@ func (t *Transport) DoRaw(ctx context.Context, method, uri string, opts ...Reque
}

cc := httputil.NewClientConn(conn, nil)
if retErr != nil {
cc.Close()
}
defer func() {
if retErr != nil {
cc.Close()
}
}()

resp, err := cc.Do(req)
if err != httputil.ErrPersistEOF {
Expand Down Expand Up @@ -171,3 +183,15 @@ func WithAddHeaders(headers map[string][]string) RequestOpt {
return nil
}
}

// go1.20.6 introduced a breaking change which makes paths an invalid value for a host header
// This is problematic for us because we use the path as the URI for the request.
// If req.Host is not set OR is the same as the socket path (basically unmodified by something else) then we can rewrite it.
// If its anything else then this was changed by something else and we should not touch it.
func go120Dot6HostTransform(sock string) func(req *http.Request) {
return func(req *http.Request) {
if req.Host == "" || req.Host == sock {
req.Host = strings.Replace(sock, "/", "_", -1)
}
}
}
3 changes: 2 additions & 1 deletion transport/unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func UnixSocketTransport(sock string, opts ...ConnectionOption) (*Transport, err
c: &http.Client{
Transport: t,
},
dial: dial,
dial: dial,
transform: go120Dot6HostTransform(sock),
}, nil
}