File tree Expand file tree Collapse file tree 2 files changed +32
-10
lines changed
Expand file tree Collapse file tree 2 files changed +32
-10
lines changed Original file line number Diff line number Diff line change 11require 'cgi'
22require 'forwardable'
33require 'webmachine/constants'
4+ require 'ipaddr'
45
56module Webmachine
67 # Request represents a single HTTP request sent from a client. It
@@ -165,19 +166,40 @@ def options?
165166
166167 private
167168
169+ IPV6_MATCH = /\A \[ (?<address> .* )\] :(?<port> \d + )\z /x . freeze # string like "[::1]:80"
170+ IPV4_MATCH = /\A (?<address> [^:]+ ):(?<port> \d + )\z /x . freeze # string like "127.0.0.1:80"
171+
172+ def parse_addr ( string )
173+ # Split host and port number from string.
174+ case string
175+ when IPV6_MATCH
176+ address = $~[ :address ]
177+ port = $~[ :port ]
178+ when IPV4_MATCH
179+ address = $~[ :address ]
180+ port = $~[ :port ]
181+ else # string with no port number
182+ address = string
183+ port = nil
184+ end
185+
186+ # Pass address, port to Addrinfo.tcp. It will raise SocketError if address or port is not valid.
187+ Addrinfo . tcp ( address , port )
188+ end
189+
168190 def build_uri ( uri , headers )
169191 uri = URI ( uri )
192+ if uri . host
193+ return uri
194+ end
170195
171- host , _ , port = headers . fetch ( HOST , "" ) . rpartition ( COLON )
172- return uri if host . empty?
173-
174- host = "[#{ host } ]" if host . include? ( COLON )
175- port = 80 if port . empty?
196+ addr = parse_addr ( headers . fetch ( HOST ) )
176197
177198 uri . scheme = HTTP
178- uri . host , uri . port = host , port . to_i
199+ uri . host = addr . ip_address
200+ uri . port = addr . ip_port == 0 ? 80 : addr . ip_port
179201
180- URI . parse ( uri . to_s )
202+ uri
181203 end
182204
183205 end # class Request
Original file line number Diff line number Diff line change 1- require "webmachine/spec/test_resource"
1+ require "webmachine/spec/test_resource"
22require "net/http"
33
44shared_examples_for :adapter_lint do
5757 let ( :address ) { "::1" }
5858
5959 it "provides the IPv6 request URI" do
60- if RUBY_VERSION =~ /^2\. (0|1)\. /
61- skip "Net::HTTP regression in Ruby 2.(0|1)"
60+ if RUBY_VERSION =~ /^2\. (0|1\2 )\. /
61+ skip "Net::HTTP regression in Ruby 2.(0|1|2 )"
6262 end
6363
6464 request = Net ::HTTP ::Get . new ( "/test" )
You can’t perform that action at this time.
0 commit comments