Permalink
Browse files

Fix rack.url_scheme for SSL servers. Fixes #65

  • Loading branch information...
1 parent 7b77f13 commit 0471babe5fd82f8b9deee5a38dd5c2b8aba71039 @evanphx evanphx committed Apr 11, 2012
Showing with 59 additions and 2 deletions.
  1. +3 −2 lib/puma/server.rb
  2. +56 −0 test/test_puma_server.rb
View
@@ -134,11 +134,12 @@ def add_ssl_listener(host, port, ctx, optimize_for_latency=true, backlog=1024)
s.setsockopt(Socket::SOL_SOCKET,Socket::SO_REUSEADDR, true)
s.listen backlog
+ ssl = OpenSSL::SSL::SSLServer.new(s, ctx)
env = @proto_env.dup
env[HTTPS_KEY] = HTTPS
- @envs[s] = env
+ @envs[ssl] = env
- @ios << OpenSSL::SSL::SSLServer.new(s, ctx)
+ @ios << ssl
s
end
View
@@ -0,0 +1,56 @@
+require "rbconfig"
+require 'test/unit'
+require 'socket'
+require 'openssl'
+
+require 'puma/server'
+
+require 'net/https'
+
+class TestPumaServer < Test::Unit::TestCase
+
+ def setup
+ @port = 3212
+ @host = "127.0.0.1"
+
+ @app = lambda { |env| [200, {}, [env['rack.url_scheme']]] }
+
+ @events = Puma::Events.new STDOUT, STDERR
+ @server = Puma::Server.new @app, @events
+
+ @ssl_key = File.expand_path "../../examples/puma/puma_keypair.pem", __FILE__
+ @ssl_cert = File.expand_path "../../examples/puma/cert_puma.pem", __FILE__
+ end
+
+ def teardown
+ @server.stop
+ end
+
+ def test_url_scheme_for_https
+ ctx = OpenSSL::SSL::SSLContext.new
+
+ ctx.key = OpenSSL::PKey::RSA.new File.read(@ssl_key)
+
+ ctx.cert = OpenSSL::X509::Certificate.new File.read(@ssl_cert)
+
+ ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
+
+ @server.add_ssl_listener @host, @port, ctx
+ @server.run
+
+ http = Net::HTTP.new @host, @port
+ http.use_ssl = true
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
+
+ body = nil
+ http.start do
+ req = Net::HTTP::Get.new "/", {}
+
+ http.request(req) do |rep|
+ body = rep.body
+ end
+ end
+
+ assert_equal "https", body
+ end
+end

0 comments on commit 0471bab

Please sign in to comment.