Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

rewrite_url adds port twice

#1106

I discovered that sometimes url_for was giving me urls like "http://localhost:3000:3000/foo" and I tracked it to action_controller/url_rewriter.rb where we have (comments are mine):

  def rewrite_url(options)
    rewritten_url = ""

    unless options[:only_path]
      rewritten_url << (options[:protocol] || @request.protocol)
      rewritten_url << "://" unless rewritten_url.match("://")
      rewritten_url << rewrite_authentication(options)
      rewritten_url << (options[:host] || @request.host_with_port) # Note: WITH PORT
      rewritten_url << ":#{options.delete(:port)}" if options.key?(:port) # if options[:port] is set, but options[:host] is not, we get port twice
    end

....

The solution is to use @request.host instead of @request.host_with_port, and to use a similar fallback for the port

Reported by Dag Arneson · September 24th, 2008 @ 08:43 PM

State: stale
Milestone: 3.x
Assigned to: nobody
Importance: none

Activity

  1. josh
    josh
    • State changed from new to incomplete

    Unit tests please.

    December 20th, 2008 @ 04:42 PM

  2. heidmo
    heidmo
    • Assigned user set to josh

    Failing test case plus fix included with diff. This was pulled from master. Let me know if you have any questions.

    April 21st, 2009 @ 02:17 AM

  3. josh
    josh
    • Assigned user cleared.

    August 19th, 2009 @ 03:47 PM

  4. Marcello Barnaba
    Marcello Barnaba

    The .diff posted by heidmo has got Unit Tests, why wasn't it included in the latest releases?

    BTW, As a temporary solution, I came up with the following monkeypatch to ActionController::UrlRewriter:

    module ActionController

    class UrlRewriter
      def rewrite_with_double_port_fix(options = {})
        rewrite_without_double_port_fix(options).sub(/:\d+(:\d+\/?)/, '\1')
      end
      alias_method_chain :rewrite, :double_port_fix
    end
    

    end

    What do you think?

    ~Marcello

    December 9th, 2009 @ 12:27 PM

  5. Marcello Barnaba
    Marcello Barnaba

    Better solution, that takes care of reloading as well: http://gist.github.com/252496

    December 9th, 2009 @ 02:11 PM

  6. James Le Cuirot
  7. James Le Cuirot
    James Le Cuirot
    • Tag changed from actionpack, bug, controller, host, port, url, url_rewriter to actionpack, bug, controller, host, https, port, ssl, url, url_rewriter

    The unit test was okay but I don't like either of the solutions. The first one assumes port 80 and the second one seems a bit messy. Better to avoid the problem in the first place.

    Here's a patch against 2-3-stable that uses @request.port_string instead. It's similar to the first solution posted here but works with port 443 as well.

    I also faced a related problem, which is that if you're on https://foo.com:321, rewrite_url will still add :321 to HTTP URLs and vice-versa. It's probably always going to be the case that if you're changing protocol, you're going to be changing port as well. This additional patch (with test) resets the port when changing protocol, unless the port has been given explicitly. I thought about making this only happen if the hostname is not changing but I think it's more likely that if you have http://foo.com:8888, you're also going to have https://secure.foo.com rather than https://secure.foo.com:8888.

    PLEASE reopen this ticket. It is not incomplete anymore.

    February 19th, 2010 @ 02:32 PM

  8. James Le Cuirot
    James Le Cuirot

    Had to change the second patch slightly. "http" doesn't match "http://" :)

    February 23rd, 2010 @ 12:24 PM

  9. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  10. Josef Reidinger
    Josef Reidinger
    • Importance changed from to

    Hi, I found better solution. I attach my one which is really simple and working.
    I also think that milestone is bad, as this code is no longer in 3.x rails, so it should be fixed in 2.x branch.

    August 20th, 2010 @ 08:26 AM

  11. James Le Cuirot
    James Le Cuirot

    I think yours is effectively just the same as mine, written in a slightly more confusing way.

    August 20th, 2010 @ 08:34 AM

  12. Santiago Pastorino
    Santiago Pastorino
    • State changed from incomplete to open

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 2nd, 2011 @ 04:59 PM

  13. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 04:59 PM