Skip to content

Commit dbdf548

Browse files
authored
Added Path as possible argument type to UNIXSocket and UNIXServer (#15260)
1 parent 6801a98 commit dbdf548

File tree

6 files changed

+52
-8
lines changed

6 files changed

+52
-8
lines changed

spec/std/socket/address_spec.cr

+4
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,10 @@ end
456456
Socket::UNIXAddress.new("some_path").hash.should_not eq Socket::UNIXAddress.new("other_path").hash
457457
end
458458

459+
it "accepts `Path` input" do
460+
Socket::UNIXAddress.new(Path.new("some_path")).should eq Socket::UNIXAddress.new("some_path")
461+
end
462+
459463
describe ".parse" do
460464
it "parses relative" do
461465
address = Socket::UNIXAddress.parse "unix://foo.sock"

spec/std/socket/unix_server_spec.cr

+12
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ describe UNIXServer do
3333
end
3434
end
3535

36+
it "creates the socket file from `Path`" do
37+
with_tempfile("unix_server.sock") do |path|
38+
path = Path.new(path)
39+
UNIXServer.open(path) do
40+
File.exists?(path).should be_true
41+
File.info(path).type.socket?.should be_true
42+
end
43+
44+
File.exists?(path).should be_false
45+
end
46+
end
47+
3648
it "deletes socket file on close" do
3749
with_tempfile("unix_server-close.sock") do |path|
3850
server = UNIXServer.new(path)

spec/std/socket/unix_socket_spec.cr

+23
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,29 @@ describe UNIXSocket do
4343
end
4444
end
4545

46+
it "initializes with `Path` paths" do
47+
with_tempfile("unix_socket.sock") do |path|
48+
path_path = Path.new(path)
49+
UNIXServer.open(path_path) do |server|
50+
server.local_address.family.should eq(Socket::Family::UNIX)
51+
server.local_address.path.should eq(path)
52+
53+
UNIXSocket.open(path_path) do |client|
54+
client.local_address.family.should eq(Socket::Family::UNIX)
55+
client.local_address.path.should eq(path)
56+
57+
server.accept do |sock|
58+
sock.local_address.family.should eq(Socket::Family::UNIX)
59+
sock.local_address.path.should eq(path)
60+
61+
sock.remote_address.family.should eq(Socket::Family::UNIX)
62+
sock.remote_address.path.should eq(path)
63+
end
64+
end
65+
end
66+
end
67+
end
68+
4669
it "sync flag after accept" do
4770
with_tempfile("unix_socket-accept.sock") do |path|
4871
UNIXServer.open(path) do |server|

src/socket/address.cr

+2-1
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,8 @@ class Socket
765765
sizeof(typeof(LibC::SockaddrUn.new.sun_path)) - 1
766766
{% end %}
767767

768-
def initialize(@path : String)
768+
def initialize(path : Path | String)
769+
@path = path.to_s
769770
if @path.bytesize > MAX_PATH_SIZE
770771
raise ArgumentError.new("Path size exceeds the maximum size of #{MAX_PATH_SIZE} bytes")
771772
end

src/socket/unix_server.cr

+6-4
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ class UNIXServer < UNIXSocket
3737
# ```
3838
#
3939
# [Only the stream type is supported on Windows](https://devblogs.microsoft.com/commandline/af_unix-comes-to-windows/#unsupportedunavailable).
40-
def initialize(@path : String, type : Type = Type::STREAM, backlog : Int = 128)
40+
def initialize(path : Path | String, type : Type = Type::STREAM, backlog : Int = 128)
41+
@path = path = path.to_s
4142
super(Family::UNIX, type)
4243

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

5556
# Creates a UNIXServer from an already configured raw file descriptor
56-
def initialize(*, fd : Handle, type : Type = Type::STREAM, @path : String? = nil)
57-
super(fd: fd, type: type, path: @path)
57+
def initialize(*, fd : Handle, type : Type = Type::STREAM, path : Path | String? = nil)
58+
@path = path = path.to_s
59+
super(fd: fd, type: type, path: path)
5860
end
5961

6062
# Creates a new UNIX server and yields it to the block. Eventually closes the
6163
# server socket when the block returns.
6264
#
6365
# Returns the value of the block.
64-
def self.open(path, type : Type = Type::STREAM, backlog = 128, &)
66+
def self.open(path : Path | String, type : Type = Type::STREAM, backlog = 128, &)
6567
server = new(path, type, backlog)
6668
begin
6769
yield server

src/socket/unix_socket.cr

+5-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ class UNIXSocket < Socket
1818
getter path : String?
1919

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

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

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

3941
# Opens an UNIX socket to a filesystem pathname, yields it to the block, then
4042
# eventually closes the socket when the block returns.
4143
#
4244
# Returns the value of the block.
43-
def self.open(path, type : Type = Type::STREAM, &)
45+
def self.open(path : Path | String, type : Type = Type::STREAM, &)
4446
sock = new(path, type)
4547
begin
4648
yield sock

0 commit comments

Comments
 (0)