Skip to content

Commit e457003

Browse files
authored
Move the host request parsing to a separate method. (#85)
Allows for someone to override the parsing to accommodate "alternatives". One could write the following to allow underscores in host names. require 'webrick/httprequest' module WEBrick class HTTPRequest private def parse_host_request_line(host, scheme) uri = URI.parse("#{scheme}://#{host}") [uri.host, uri.port] end end end Also adding the "o" option to the regex so the regex is only built once.
1 parent d42c291 commit e457003

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

lib/webrick/httprequest.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -491,8 +491,7 @@ def parse_uri(str, scheme="http")
491491
if @forwarded_host
492492
host, port = @forwarded_host, @forwarded_port
493493
elsif self["host"]
494-
pattern = /\A(#{URI::REGEXP::PATTERN::HOST})(?::(\d+))?\z/n
495-
host, port = *self['host'].scan(pattern)[0]
494+
host, port = parse_host_request_line(self["host"])
496495
elsif @addr.size > 0
497496
host, port = @addr[2], @addr[1]
498497
else
@@ -504,6 +503,11 @@ def parse_uri(str, scheme="http")
504503
return URI::parse(uri.to_s)
505504
end
506505

506+
def parse_host_request_line(host)
507+
pattern = /\A(#{URI::REGEXP::PATTERN::HOST})(?::(\d+))?\z/no
508+
host.scan(pattern)[0]
509+
end
510+
507511
def read_body(socket, block)
508512
return unless socket
509513
if tc = self['transfer-encoding']

0 commit comments

Comments
 (0)