yahns Ruby server user/dev discussion
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: yahns-public@yhbt.net
Subject: [PATCH] https: ensure SERVER_PORT defaults to 443
Date: Sat, 20 Feb 2016 03:33:56 +0000	[thread overview]
Message-ID: <20160220033356.GA31676@dcvr.yhbt.net> (raw)

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

             reply	other threads:[~2016-02-20  3:33 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-20  3:33 Eric Wong [this message]
2016-02-20 21:44 ` [PATCH 2/1] test_ssl: check SERVER_PORT when parsed from Host: header Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://yhbt.net/yahns/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20160220033356.GA31676@dcvr.yhbt.net \
    --to=e@80x24.org \
    --cc=yahns-public@yhbt.net \
    --subject='Re: [PATCH] https: ensure SERVER_PORT defaults to 443' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Code repositories for project(s) associated with this inbox:

	../../yahns.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).