Skip to content

Commit

Permalink
Added Path as possible argument type to UNIXSocket and UNIXServer (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
BigBoyBarney authored Jan 13, 2025
1 parent 6801a98 commit dbdf548
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 8 deletions.
4 changes: 4 additions & 0 deletions spec/std/socket/address_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,10 @@ end
Socket::UNIXAddress.new("some_path").hash.should_not eq Socket::UNIXAddress.new("other_path").hash
end

it "accepts `Path` input" do
Socket::UNIXAddress.new(Path.new("some_path")).should eq Socket::UNIXAddress.new("some_path")
end

describe ".parse" do
it "parses relative" do
address = Socket::UNIXAddress.parse "unix://foo.sock"
Expand Down
12 changes: 12 additions & 0 deletions spec/std/socket/unix_server_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ describe UNIXServer do
end
end

it "creates the socket file from `Path`" do
with_tempfile("unix_server.sock") do |path|
path = Path.new(path)
UNIXServer.open(path) do
File.exists?(path).should be_true
File.info(path).type.socket?.should be_true
end

File.exists?(path).should be_false
end
end

it "deletes socket file on close" do
with_tempfile("unix_server-close.sock") do |path|
server = UNIXServer.new(path)
Expand Down
23 changes: 23 additions & 0 deletions spec/std/socket/unix_socket_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,29 @@ describe UNIXSocket do
end
end

it "initializes with `Path` paths" do
with_tempfile("unix_socket.sock") do |path|
path_path = Path.new(path)
UNIXServer.open(path_path) do |server|
server.local_address.family.should eq(Socket::Family::UNIX)
server.local_address.path.should eq(path)

UNIXSocket.open(path_path) do |client|
client.local_address.family.should eq(Socket::Family::UNIX)
client.local_address.path.should eq(path)

server.accept do |sock|
sock.local_address.family.should eq(Socket::Family::UNIX)
sock.local_address.path.should eq(path)

sock.remote_address.family.should eq(Socket::Family::UNIX)
sock.remote_address.path.should eq(path)
end
end
end
end
end

it "sync flag after accept" do
with_tempfile("unix_socket-accept.sock") do |path|
UNIXServer.open(path) do |server|
Expand Down
3 changes: 2 additions & 1 deletion src/socket/address.cr
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,8 @@ class Socket
sizeof(typeof(LibC::SockaddrUn.new.sun_path)) - 1
{% end %}

def initialize(@path : String)
def initialize(path : Path | String)
@path = path.to_s
if @path.bytesize > MAX_PATH_SIZE
raise ArgumentError.new("Path size exceeds the maximum size of #{MAX_PATH_SIZE} bytes")
end
Expand Down
10 changes: 6 additions & 4 deletions src/socket/unix_server.cr
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ class UNIXServer < UNIXSocket
# ```
#
# [Only the stream type is supported on Windows](https://devblogs.microsoft.com/commandline/af_unix-comes-to-windows/#unsupportedunavailable).
def initialize(@path : String, type : Type = Type::STREAM, backlog : Int = 128)
def initialize(path : Path | String, type : Type = Type::STREAM, backlog : Int = 128)
@path = path = path.to_s
super(Family::UNIX, type)

system_bind(UNIXAddress.new(path), path) do |error|
Expand All @@ -53,15 +54,16 @@ class UNIXServer < UNIXSocket
end

# Creates a UNIXServer from an already configured raw file descriptor
def initialize(*, fd : Handle, type : Type = Type::STREAM, @path : String? = nil)
super(fd: fd, type: type, path: @path)
def initialize(*, fd : Handle, type : Type = Type::STREAM, path : Path | String? = nil)
@path = path = path.to_s
super(fd: fd, type: type, path: path)
end

# Creates a new UNIX server and yields it to the block. Eventually closes the
# server socket when the block returns.
#
# Returns the value of the block.
def self.open(path, type : Type = Type::STREAM, backlog = 128, &)
def self.open(path : Path | String, type : Type = Type::STREAM, backlog = 128, &)
server = new(path, type, backlog)
begin
yield server
Expand Down
8 changes: 5 additions & 3 deletions src/socket/unix_socket.cr
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class UNIXSocket < Socket
getter path : String?

# Connects a named UNIX socket, bound to a filesystem pathname.
def initialize(@path : String, type : Type = Type::STREAM)
def initialize(path : Path | String, type : Type = Type::STREAM)
@path = path = path.to_s
super(Family::UNIX, type, Protocol::IP)

connect(UNIXAddress.new(path)) do |error|
Expand All @@ -32,15 +33,16 @@ class UNIXSocket < Socket
end

# Creates a UNIXSocket from an already configured raw file descriptor
def initialize(*, fd : Handle, type : Type = Type::STREAM, @path : String? = nil)
def initialize(*, fd : Handle, type : Type = Type::STREAM, path : Path | String? = nil)
@path = path.to_s
super fd, Family::UNIX, type, Protocol::IP
end

# Opens an UNIX socket to a filesystem pathname, yields it to the block, then
# eventually closes the socket when the block returns.
#
# Returns the value of the block.
def self.open(path, type : Type = Type::STREAM, &)
def self.open(path : Path | String, type : Type = Type::STREAM, &)
sock = new(path, type)
begin
yield sock
Expand Down

0 comments on commit dbdf548

Please sign in to comment.