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

Implement 'unix:' prefix for unix domain socket addresses #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,12 @@ func (srv *Server) Listen() (net.Listener, error) {
}
ln = listener[0]
} else {
network, name := "tcp", srv.Addr
if strings.HasPrefix(name, "unix:") {
network, name = "unix", name[5:]
}
var err error
ln, err = net.Listen("tcp", srv.Addr)
ln, err = net.Listen(network, name)
if err != nil {
return nil, err
}
Expand Down
19 changes: 19 additions & 0 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ package httpx_test

import (
"crypto/tls"
"io/ioutil"
"net"
"net/http"
"os"
"testing"

"github.com/bojanz/httpx"
Expand Down Expand Up @@ -97,4 +99,21 @@ func TestServer_Listen(t *testing.T) {
if err != nil {
t.Errorf("unexpected error %v", err)
}

// Name of a non existing file
fh, err := ioutil.TempFile("", "server-*.unix")
if err != nil {
t.Skip(err)
}
fh.Close()
// Race condition: if the file is created between Remove and Listen, we fail.
os.Remove(fh.Name())
server = httpx.NewServer("unix:"+fh.Name(), http.DefaultServeMux)
ln, err = server.Listen()
if _, ok := ln.(*net.UnixListener); !ok {
t.Errorf("got %T, want *net.UnixListener", ln)
}
if err != nil {
t.Errorf("unexpected error %+v", err)
}
}