Bug #13307

Changing scheme from http to https for the URI does not change the port number

Added by martinosis (Martin Chabot) over 3 years ago. Updated about 1 year ago.

Target version:
ruby -v:
ruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-darwin16]


If we change the scheme of an URI, it does not change the port number. The URI object become inconsistent. If we print the full URI it does not return the same port number as when we print the port.

See code bellow:

[1] pry(main)> require 'uri'
=> true
[2] pry(main)> url = ""
=> ""
[3] pry(main)> uri = URI.parse(url)
=> #<URI::HTTP>
[4] pry(main)> uri.scheme = "https"
=> "https"
[5] pry(main)> uri.port
=> 80
[6] pry(main)> uri.to_s
=> "" # port 443

Updated by nobu (Nobuyoshi Nakada) over 3 years ago

Changing scheme sounds nonsense to me.

Updated by shyouhei (Shyouhei Urabe) over 3 years ago

Nobuyoshi Nakada wrote:

Changing scheme sounds nonsense to me.

Me too but given the port number is inferred from the scheme, "changing scheme should update that part" sounds reasonable to me.

Updated by duerst (Martin Dürst) over 3 years ago

I agree with Shyouhei that the current behavior is inconsistent. When changing the scheme, either the object keeps the default port (which is 443 for https), or it keeps port 80. If it keeps port 80, then it has to print that out when the scheme is https, because port 80 isn't the default for https.

BTW, the examples should all have '//' after http(s):, but that doesn't affect the result.

Updated by shyouhei (Shyouhei Urabe) over 3 years ago

We looked at this issue in today's developer meeting.

The problem is, an URI created using URI.parse("http://...") is an URI::HTTP instance, not HTTPS. The class is determined from the URI's scheme. Given that, why is scheme modifiable? It seems rather troublesome. We might want to prohibit that. Martin, does that annoy you? Do you have use case when changing scheme is useful?


Updated by duerst (Martin Dürst) over 3 years ago

  • Status changed from Open to Feedback

Updated by jeremyevans0 (Jeremy Evans) about 1 year ago

  • Status changed from Feedback to Closed

Also available in: Atom PDF