From e4570039c135e13fadd93df4766b61407098577f Mon Sep 17 00:00:00 2001 From: John W Higgins Date: Thu, 26 Jan 2023 15:07:49 -0800 Subject: [PATCH] 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. --- lib/webrick/httprequest.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/webrick/httprequest.rb b/lib/webrick/httprequest.rb index ff2c8a8..680ac65 100644 --- a/lib/webrick/httprequest.rb +++ b/lib/webrick/httprequest.rb @@ -491,8 +491,7 @@ def parse_uri(str, scheme="http") if @forwarded_host host, port = @forwarded_host, @forwarded_port elsif self["host"] - pattern = /\A(#{URI::REGEXP::PATTERN::HOST})(?::(\d+))?\z/n - host, port = *self['host'].scan(pattern)[0] + host, port = parse_host_request_line(self["host"]) elsif @addr.size > 0 host, port = @addr[2], @addr[1] else @@ -504,6 +503,11 @@ def parse_uri(str, scheme="http") return URI::parse(uri.to_s) end + def parse_host_request_line(host) + pattern = /\A(#{URI::REGEXP::PATTERN::HOST})(?::(\d+))?\z/no + host.scan(pattern)[0] + end + def read_body(socket, block) return unless socket if tc = self['transfer-encoding']