This project is archived and is in readonly mode.
rewrite_url adds port twice
Reported by Dag Arneson | September 24th, 2008 @ 08:43 PM | in 3.x
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
Comments and changes to this ticket
-
heidmo April 21st, 2009 @ 02:17 AM
- 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.
-
josh August 19th, 2009 @ 03:47 PM
- Assigned user cleared.
-
Marcello Barnaba December 9th, 2009 @ 12:27 PM
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
-
Marcello Barnaba December 9th, 2009 @ 02:11 PM
Better solution, that takes care of reloading as well: http://gist.github.com/252496
-
James Le Cuirot February 19th, 2010 @ 12:27 PM
- no changes were found...
-
James Le Cuirot February 19th, 2010 @ 02:32 PM
- 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.
-
James Le Cuirot February 23rd, 2010 @ 12:24 PM
Had to change the second patch slightly. "http" doesn't match "http://" :)
-
Josef Reidinger August 20th, 2010 @ 08:26 AM
- 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. -
James Le Cuirot August 20th, 2010 @ 08:34 AM
I think yours is effectively just the same as mine, written in a slightly more confusing way.
-
Santiago Pastorino February 2nd, 2011 @ 04:59 PM
- 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.
-
Santiago Pastorino February 2nd, 2011 @ 04:59 PM
- State changed from open to stale
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Attachments
Referenced by
- 2676 UrlRewriter rewrite_url doubles ports This is a duplicate of ticket #1106.