1
0
Fork 0
mirror of https://github.com/puma/puma.git synced 2022-11-09 13:48:40 -05:00

Merge branch 'removes-connected-port' of https://github.com/drews256/puma into drews256-removes-connected-port

This commit is contained in:
Nate Berkopec 2020-02-27 10:27:00 -06:00
commit d91c112fee
No known key found for this signature in database
GPG key ID: BDD7A4B8E43906A6
11 changed files with 47 additions and 38 deletions

View file

@ -226,19 +226,22 @@ module Puma
end
host = host[1..-2] if host and host[0..0] == '['
s = TCPServer.new(host, port)
tcp_server = TCPServer.new(host, port)
if optimize_for_latency
s.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
tcp_server.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
end
s.setsockopt(Socket::SOL_SOCKET,Socket::SO_REUSEADDR, true)
s.listen backlog
@connected_port = s.addr[1]
tcp_server.setsockopt(Socket::SOL_SOCKET,Socket::SO_REUSEADDR, true)
tcp_server.listen backlog
@ios << s
s
@ios << tcp_server
tcp_server
end
attr_reader :connected_port
attr_reader :connected_ports
def connected_ports
ios.map { |io| io.addr[1] }
end
def inherit_tcp_listener(host, port, fd)
if fd.kind_of? TCPServer

View file

@ -187,9 +187,9 @@ module Puma
@binder.close_unix_paths
end
# Return which tcp port the launcher is using, if it's using TCP
def connected_port
@binder.connected_port
# Return all tcp ports the launcher may be using, TCP or SSL
def connected_ports
@binder.connected_ports
end
def restart_args

View file

@ -270,6 +270,10 @@ module Puma
Socket.new io, engine
end
def addr
@socket.addr
end
def close
@socket.close unless @socket.closed? # closed? call is for Windows
end

View file

@ -92,7 +92,7 @@ module Puma
attr_accessor :binder, :leak_stack_on_error, :early_hints
def_delegators :@binder, :add_tcp_listener, :add_ssl_listener, :add_unix_listener, :connected_port
def_delegators :@binder, :add_tcp_listener, :add_ssl_listener, :add_unix_listener, :connected_ports
def inherit_binder(bind)
@binder = bind

View file

@ -37,12 +37,10 @@ class TestCLI < Minitest::Test
end
def test_control_for_tcp
tcp = UniquePort.call
cntl = UniquePort.call
url = "tcp://127.0.0.1:#{cntl}/"
cli = Puma::CLI.new ["-b", "tcp://127.0.0.1:#{tcp}",
"--control-url", url,
cli = Puma::CLI.new [ "--control-url", url,
"--control-token", "",
"test/rackup/lobster.ru"], @events
@ -65,14 +63,12 @@ class TestCLI < Minitest::Test
end
def test_control_for_ssl
app_port = UniquePort.call
control_port = UniquePort.call
control_host = "127.0.0.1"
control_url = "ssl://#{control_host}:#{control_port}?#{ssl_query}"
token = "token"
cli = Puma::CLI.new ["-b", "tcp://127.0.0.1:#{app_port}",
"--control-url", control_url,
cli = Puma::CLI.new ["--control-url", control_url,
"--control-token", token,
"test/rackup/lobster.ru"], @events

View file

@ -167,7 +167,8 @@ class TestEvents < Minitest::Test
server.add_tcp_listener host, port
server.run
sock = TCPSocket.new host, server.connected_port
port = server.connected_ports[0]
sock = TCPSocket.new host, port
path = "/"
params = "a"*1024*10

View file

@ -39,14 +39,16 @@ class TestPumaServer < Minitest::Test
end
def send_http_and_read(req)
sock = TCPSocket.new @host, @server.connected_port
port = @server.connected_ports[0]
sock = TCPSocket.new @host, port
@ios << sock
sock << req
sock.read
end
def send_http(req)
sock = TCPSocket.new @host, @server.connected_port
port = @server.connected_ports[0]
sock = TCPSocket.new @host, port
@ios << sock
sock << req
sock
@ -137,7 +139,8 @@ class TestPumaServer < Minitest::Test
req = Net::HTTP::Get.new '/'
req['HOST'] = 'example.com'
res = Net::HTTP.start @host, @server.connected_port do |http|
port = @server.connected_ports[0]
res = Net::HTTP.start @host, port do |http|
http.request(req)
end
@ -153,7 +156,8 @@ class TestPumaServer < Minitest::Test
req['HOST'] = "example.com"
req['X_FORWARDED_PROTO'] = "https,http"
res = Net::HTTP.start @host, @server.connected_port do |http|
port = @server.connected_ports[0]
res = Net::HTTP.start @host, port do |http|
http.request(req)
end
@ -745,7 +749,7 @@ EOF
}
sock = send_http "GET / HTTP/1.1\r\nConnection: close\r\nTransfer-Encoding: chunked\r\n\r\n1\r\nh\r\n"
sleep 1
sleep 3
sock << "4\r\nello\r\n0\r\n\r\n"
sock.gets

View file

@ -45,7 +45,7 @@ class TestPumaServerSSL < Minitest::Test
# yields ctx to block, use for ctx setup & configuration
def start_server
@port = UniquePort.call
@port = 0
@host = "127.0.0.1"
app = lambda { |env| [200, {}, [env['rack.url_scheme']]] }
@ -69,7 +69,7 @@ class TestPumaServerSSL < Minitest::Test
@ssl_listener = @server.add_ssl_listener @host, @port, ctx
@server.run
@http = Net::HTTP.new @host, @port
@http = Net::HTTP.new @host, @server.connected_ports[0]
@http.use_ssl = true
@http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
@ -93,7 +93,8 @@ class TestPumaServerSSL < Minitest::Test
# Open a connection and give enough data to trigger a read, then wait
ctx = OpenSSL::SSL::SSLContext.new
ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
socket = OpenSSL::SSL::SSLSocket.new TCPSocket.new(@host, @port), ctx
port = @server.connected_ports[0]
socket = OpenSSL::SSL::SSLSocket.new TCPSocket.new(@host, port), ctx
socket.write "x"
sleep 0.1
@ -206,7 +207,7 @@ class TestPumaServerSSLClient < Minitest::Test
parallelize_me!
def assert_ssl_client_error_match(error, subject=nil, &blk)
host = "127.0.0.1"
port = UniquePort.call
port = 0
app = lambda { |env| [200, {}, [env['rack.url_scheme']]] }
@ -226,7 +227,7 @@ class TestPumaServerSSLClient < Minitest::Test
server.add_ssl_listener host, port, ctx
server.run
http = Net::HTTP.new host, port
http = Net::HTTP.new host, server.connected_ports[0]
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE

View file

@ -49,7 +49,7 @@ class TestPathHandler < Minitest::Test
host = windows? ? "127.0.1.1" : "0.0.0.0"
opts = { Host: host }
in_handler(app, opts) do |launcher|
hit(["http://#{host}:#{ launcher.connected_port }/test"])
hit(["http://#{host}:#{ launcher.connected_ports[0] }/test"])
assert_equal("/test", @input["PATH_INFO"])
end
end

View file

@ -55,7 +55,7 @@ class TestRackServer < Minitest::Test
@server.run
hit(["http://127.0.0.1:#{ @server.connected_port }/test"])
hit(["http://127.0.0.1:#{ @server.connected_ports[0] }/test"])
stop
@ -70,7 +70,7 @@ class TestRackServer < Minitest::Test
big = "x" * (1024 * 16)
Net::HTTP.post_form URI.parse("http://127.0.0.1:#{ @server.connected_port }/test"),
Net::HTTP.post_form URI.parse("http://127.0.0.1:#{ @server.connected_ports[0] }/test"),
{ "big" => big }
stop
@ -83,7 +83,7 @@ class TestRackServer < Minitest::Test
@server.app = lambda { |env| input = env; @simple.call(env) }
@server.run
hit(["http://127.0.0.1:#{ @server.connected_port }/test/a/b/c"])
hit(["http://127.0.0.1:#{ @server.connected_ports[0] }/test/a/b/c"])
stop
@ -100,7 +100,7 @@ class TestRackServer < Minitest::Test
@server.run
hit(["http://127.0.0.1:#{ @server.connected_port }/test"])
hit(["http://127.0.0.1:#{ @server.connected_ports[0] }/test"])
stop
@ -116,7 +116,7 @@ class TestRackServer < Minitest::Test
@server.run
hit(["http://127.0.0.1:#{ @server.connected_port }/test"])
hit(["http://127.0.0.1:#{ @server.connected_ports[0] }/test"])
stop

View file

@ -34,7 +34,7 @@ class WebServerTest < Minitest::Test
end
def test_simple_server
hit(["http://127.0.0.1:#{@server.connected_port}/test"])
hit(["http://127.0.0.1:#{@server.connected_ports[0]}/test"])
assert @tester.ran_test, "Handler didn't really run"
end
@ -83,7 +83,7 @@ class WebServerTest < Minitest::Test
def do_test(string, chunk)
# Do not use instance variables here, because it needs to be thread safe
socket = TCPSocket.new("127.0.0.1", @server.connected_port);
socket = TCPSocket.new("127.0.0.1", @server.connected_ports[0]);
request = StringIO.new(string)
chunks_out = 0
@ -96,7 +96,7 @@ class WebServerTest < Minitest::Test
def do_test_raise(string, chunk, close_after = nil)
# Do not use instance variables here, because it needs to be thread safe
socket = TCPSocket.new("127.0.0.1", @server.connected_port);
socket = TCPSocket.new("127.0.0.1", @server.connected_ports[0]);
request = StringIO.new(string)
chunks_out = 0