From dbd91e1b61e4c3cabedbb0c3322e993be9eedf40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= Date: Tue, 17 Apr 2018 18:58:06 +0000 Subject: [PATCH 1/2] Add HTTP::Server#bind_unix --- spec/std/http/server/server_spec.cr | 55 +++++++++++++++++++++++++++++ src/http/server.cr | 14 ++++++++ 2 files changed, 69 insertions(+) diff --git a/spec/std/http/server/server_spec.cr b/spec/std/http/server/server_spec.cr index c0c562433704..5907fcf55ae9 100644 --- a/spec/std/http/server/server_spec.cr +++ b/spec/std/http/server/server_spec.cr @@ -1,5 +1,6 @@ require "spec" require "http/server" +require "tempfile" private class RaiseErrno < IO def initialize(@value : Int32) @@ -40,6 +41,24 @@ private class ReverseResponseOutput < IO end end +# TODO: replace with `HTTP::Client` once it supports connecting to Unix socket. +private def unix_request(path) + UNIXSocket.open(path) do |io| + io << "GET / HTTP/1.1\r\n" + io << "X-Unix-Socket: #{path}\r\n" + io << "\r\n" + io.flush + + loop do + line = io.gets || break + + if line.empty? + return io.gets + end + end + end +end + module HTTP class Server describe Response do @@ -310,6 +329,42 @@ module HTTP end end end + + {% if flag?(:unix) %} + describe "#bind_unix" do + it "binds to different unix sockets" do + path1 = Tempfile.tempname + path2 = Tempfile.tempname + + begin + server = Server.new do |context| + # TODO: Replace custom header with local_address + context.response.puts "Test Server (#{context.request.headers["X-Unix-Socket"]?})" + context.response.close + end + + socket1 = UNIXServer.new(path1) + server.bind socket1 + socket2 = server.bind_unix path2 + + spawn server.listen + + Fiber.yield + + unix_request(path1).should eq "Test Server (#{path1})" + unix_request(path2).should eq "Test Server (#{path2})" + + server.close + + File.exists?(path1).should be_false + File.exists?(path2).should be_false + ensure + File.delete(path1) if File.exists?(path1) + File.delete(path2) if File.exists?(path2) + end + end + end + {% end %} end describe HTTP::Server::RequestProcessor do diff --git a/src/http/server.cr b/src/http/server.cr index b038a2fe9ab3..a792cfe0aadc 100644 --- a/src/http/server.cr +++ b/src/http/server.cr @@ -162,6 +162,20 @@ class HTTP::Server bind_tcp host, 0, reuse_port end + # Creates a `UNIXServer` bound to *path* and adds it as a socket. + # + # ``` + # server = HTTP::Server.new { } + # server.bind_unix "/tmp/my-socket.sock" + # ``` + def bind_unix(path : String) : Socket::UNIXAddress + server = UNIXServer.new(path) + + bind(server) + + server.local_address + end + # Adds a `Socket::Server` *socket* to this server. def bind(socket : Socket::Server) : Nil raise "Can't add socket to running server" if listening? From e0832250f9960017c7edf18eb09cb975c6a2b292 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= Date: Wed, 18 Apr 2018 08:20:03 +0000 Subject: [PATCH 2/2] fixup! Add HTTP::Server#bind_unix --- spec/std/http/server/server_spec.cr | 67 +++++++++++++---------------- 1 file changed, 30 insertions(+), 37 deletions(-) diff --git a/spec/std/http/server/server_spec.cr b/spec/std/http/server/server_spec.cr index 5907fcf55ae9..ffd157725db3 100644 --- a/spec/std/http/server/server_spec.cr +++ b/spec/std/http/server/server_spec.cr @@ -1,5 +1,6 @@ require "spec" require "http/server" +require "http/client/response" require "tempfile" private class RaiseErrno < IO @@ -41,21 +42,13 @@ private class ReverseResponseOutput < IO end end -# TODO: replace with `HTTP::Client` once it supports connecting to Unix socket. +# TODO: replace with `HTTP::Client` once it supports connecting to Unix socket (#2735) private def unix_request(path) UNIXSocket.open(path) do |io| - io << "GET / HTTP/1.1\r\n" - io << "X-Unix-Socket: #{path}\r\n" - io << "\r\n" - io.flush + request = HTTP::Request.new("GET", "/", HTTP::Headers{"X-Unix-Socket" => path}) + request.to_io(io) - loop do - line = io.gets || break - - if line.empty? - return io.gets - end - end + HTTP::Client::Response.from_io(io).body end end @@ -331,39 +324,39 @@ module HTTP end {% if flag?(:unix) %} - describe "#bind_unix" do - it "binds to different unix sockets" do - path1 = Tempfile.tempname - path2 = Tempfile.tempname - - begin - server = Server.new do |context| - # TODO: Replace custom header with local_address - context.response.puts "Test Server (#{context.request.headers["X-Unix-Socket"]?})" - context.response.close - end + describe "#bind_unix" do + it "binds to different unix sockets" do + path1 = Tempfile.tempname + path2 = Tempfile.tempname - socket1 = UNIXServer.new(path1) - server.bind socket1 - socket2 = server.bind_unix path2 + begin + server = Server.new do |context| + # TODO: Replace custom header with local_address (#5784) + context.response.print "Test Server (#{context.request.headers["X-Unix-Socket"]?})" + context.response.close + end - spawn server.listen + socket1 = UNIXServer.new(path1) + server.bind socket1 + socket2 = server.bind_unix path2 - Fiber.yield + spawn server.listen - unix_request(path1).should eq "Test Server (#{path1})" - unix_request(path2).should eq "Test Server (#{path2})" + Fiber.yield - server.close + unix_request(path1).should eq "Test Server (#{path1})" + unix_request(path2).should eq "Test Server (#{path2})" - File.exists?(path1).should be_false - File.exists?(path2).should be_false - ensure - File.delete(path1) if File.exists?(path1) - File.delete(path2) if File.exists?(path2) + server.close + + File.exists?(path1).should be_false + File.exists?(path2).should be_false + ensure + File.delete(path1) if File.exists?(path1) + File.delete(path2) if File.exists?(path2) + end end end - end {% end %} end