yahns Ruby server user/dev discussion
 help / color / Atom feed
* [PATCH] https: ensure SERVER_PORT defaults to 443
@ 2016-02-20  3:33 Eric Wong
  2016-02-20 21:44 ` [PATCH 2/1] test_ssl: check SERVER_PORT when parsed from Host: header Eric Wong
  0 siblings, 1 reply; 2+ messages in thread
From: Eric Wong @ 2016-02-20  3:33 UTC (permalink / raw)
  To: yahns-public

This helps Rack::Request#url and similar methods generate proper
URLs instead of the obviously wrong: "https://example.com:80/"

Note: we don't track the actual port the listener is bound to,
and it may not be worth it since the use of the Host: header
is long-established and Host: headers include the port number
if non-standard.
---
 lib/yahns/http_client.rb | 11 ++++++++++-
 test/test_ssl.rb         |  5 +++--
 2 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/lib/yahns/http_client.rb b/lib/yahns/http_client.rb
index c300742..272db85 100644
--- a/lib/yahns/http_client.rb
+++ b/lib/yahns/http_client.rb
@@ -206,8 +206,17 @@ def app_call(input)
       end
     end
 
+    env.merge!(k.app_defaults)
+
+    # workaround stupid unicorn_http parser behavior when it parses HTTP_HOST
+    if env['HTTPS'] == 'on'.freeze &&
+        env['HTTP_HOST'] &&
+        env['SERVER_PORT'] == '80'.freeze
+      env['SERVER_PORT'] = '443'.freeze
+    end
+
     # run the rack app
-    status, headers, body = k.app.call(env.merge!(k.app_defaults))
+    status, headers, body = k.app.call(env)
     return :ignore if app_hijacked?(env, body)
     if status.to_i == 100
       rv = http_100_response(env) and return rv
diff --git a/test/test_ssl.rb b/test/test_ssl.rb
index fe7e09e..5fc2b52 100644
--- a/test/test_ssl.rb
+++ b/test/test_ssl.rb
@@ -71,7 +71,7 @@ def test_ssl_basic
       cfg.instance_eval do
         ru = lambda do |env|
           case path_info = env['PATH_INFO']
-          when '/rack.url_scheme', '/HTTPS'
+          when '/rack.url_scheme', '/HTTPS', '/SERVER_PORT'
             s = env[path_info[1..-1]] # remove leading slash
             s = s.inspect if s.nil?
             [ 200, {
@@ -100,7 +100,8 @@ def test_ssl_basic
     buf = ''.dup
     { '/' => 'HI',
       '/rack.url_scheme' => 'https',
-      '/HTTPS' => 'on'
+      '/HTTPS' => 'on',
+      '/SERVER_PORT' => '443',
     }.each do |path, exp|
       client.write("GET #{path} HTTP/1.1\r\nHost: example.com\r\n\r\n")
       buf.clear
-- 
EW

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [PATCH 2/1] test_ssl: check SERVER_PORT when parsed from Host: header
  2016-02-20  3:33 [PATCH] https: ensure SERVER_PORT defaults to 443 Eric Wong
@ 2016-02-20 21:44 ` Eric Wong
  0 siblings, 0 replies; 2+ messages in thread
From: Eric Wong @ 2016-02-20 21:44 UTC (permalink / raw)
  To: yahns-public

We need to ensure SERVER_PORT is still parsed from the Host:
header when it is given, there.
---
 test/test_ssl.rb | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/test/test_ssl.rb b/test/test_ssl.rb
index 5fc2b52..e89a89d 100644
--- a/test/test_ssl.rb
+++ b/test/test_ssl.rb
@@ -114,11 +114,25 @@ def test_ssl_basic
       assert_match %r{\AHTTP/1\.\d 200 OK\r\n}, head
     end
 
+    # use port in Host: header (implemented by unicorn_http parser)
+    exp = '666'
+    client.write("GET /SERVER_PORT HTTP/1.1\r\nHost: example.com:#{exp}\r\n\r\n")
+    re = /#{Regexp.escape(exp)}\z/
+    buf.clear
+    Timeout.timeout(60) do
+      buf << client.readpartial(111) until buf =~ re
+    end
+    head, body = buf.split("\r\n\r\n", 2)
+    assert_equal exp, body
+    assert_match %r{\AHTTP/1\.\d 200 OK\r\n}, head
+
     Net::HTTP.start(insecure.addr[3], insecure.addr[1]) do |h|
       res = h.get('/rack.url_scheme')
       assert_equal 'http', res.body
       res = h.get('/HTTPS')
       assert_equal 'nil', res.body
+      res = h.get('/SERVER_PORT')
+      assert_equal insecure.addr[1].to_s, res.body
     end
 
     # read static file
-- 
EW

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, back to index

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-02-20  3:33 [PATCH] https: ensure SERVER_PORT defaults to 443 Eric Wong
2016-02-20 21:44 ` [PATCH 2/1] test_ssl: check SERVER_PORT when parsed from Host: header Eric Wong

yahns Ruby server user/dev discussion

Archives are clonable:
	git clone --mirror https://yhbt.net/yahns-public
	git clone --mirror http://ou63pmih66umazou.onion/yahns-public

Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.lang.ruby.yahns
	nntp://ou63pmih66umazou.onion/inbox.comp.lang.ruby.yahns

 note: .onion URLs require Tor: https://www.torproject.org/

AGPL code for this site: git clone https://public-inbox.org/ public-inbox