Skip to content

Commit

Permalink
add support for HTTP over unix sockets
Browse files Browse the repository at this point in the history
This adds support for using unix scheme in xxxAddress config options.
For example `hlsAddress: "unix://hls.sock"`.
  • Loading branch information
krystiancha committed Feb 9, 2025
1 parent b66efd6 commit 4dd2586
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
16 changes: 16 additions & 0 deletions internal/protocols/httpp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"log"
"net"
"net/http"
"os"
"strings"
"time"

"github.com/bluenviron/mediamtx/internal/certloader"
Expand Down Expand Up @@ -61,12 +63,26 @@ func (s *Server) Initialize() error {
}
}

if strings.HasPrefix(s.Network, "tcp") {
var isunix bool
s.Address, isunix = strings.CutPrefix(s.Address, "unix://")
if isunix {
s.Network = "unix"
}

os.Remove(s.Address)
}

var err error
s.ln, err = net.Listen(s.Network, s.Address)
if err != nil {
return err
}

if s.Network == "unix" {
os.Chmod(s.Address, 0775)
}

h := s.Handler
h = &handlerFilterRequests{h}
h = &handlerFilterRequests{h}
Expand Down
14 changes: 14 additions & 0 deletions internal/protocols/httpp/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,17 @@ func TestFilterEmptyPath(t *testing.T) {
_, err = io.ReadFull(conn, buf)
require.NoError(t, err)
}

func TestUnixSocket(t *testing.T) {
s := &Server{
Network: "tcp",
Address: "unix://http.sock",
ReadTimeout: 10 * time.Second,
Parent: test.NilLogger,
}
err := s.Initialize()
require.NoError(t, err)
defer s.Close()

require.Equal(t, s.Network, "unix")
}

0 comments on commit 4dd2586

Please sign in to comment.