-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Closed
Description
There are 3 or 4 separate places in http.lua where a scheme is translated into default ports, typically with a code like this:
local port = someport
if not port then
if scheme == "http" then
port = 80
elseif scheme == "https" then
port = 443
end
endThe following patch moves this code into a separate function, resulting in substantial code clarity.
(The patch assumes that #766 has been already merged in.)
* Provides a common function for obtaining the default port number for
a given scheme
--- a/nselib/http.lua
+++ b/nselib/http.lua
@@ -157,16 +157,21 @@
end
end
+--- Provide the default port for a given scheme.
+local function get_default_port (scheme)
+ local ports = {http=80, https=443}
+ return ports[scheme]
+end
+
--- Get a value suitable for the Host header field.
-- See RFC 2616 sections 14.23 and 5.2.
local function get_host_field(host, port)
if not host then return nil end
- local ssl = shortport.ssl(host, port)
- local pn = port.number
- if not ssl and pn == 80 or ssl and pn == 443 then
+ local scheme = shortport.ssl(host, port) and "https" or "http"
+ if port.number == get_default_port(scheme) then
return stdnse.get_hostname(host)
else
- return stdnse.get_hostname(host) .. ":" .. pn
+ return stdnse.get_hostname(host) .. ":" .. port.number
end
end
@@ -1485,14 +1490,7 @@
function (url, host, port)
-- port fixup, adds default ports 80 and 443 in case no url.port was
-- defined, we do this based on the url scheme
- local url_port = url.port
- if ( not(url_port) ) then
- if ( url.scheme == "http" ) then
- url_port = 80
- elseif( url.scheme == "https" ) then
- url_port = 443
- end
- end
+ local url_port = url.port or get_default_port(url.scheme)
if (not url_port) or tonumber(url_port) == port.number then
return true
end
@@ -1570,11 +1568,7 @@
u.path = ((u.path:sub(1,1) == "/" and "" ) or "/" ) .. u.path -- ensuring leading slash
end
-- do port fixup
- if ( not(u.port) ) then
- if ( u.scheme == "http" ) then u.port = 80
- elseif ( u.scheme == "https") then u.port = 443
- else u.port = port.number end
- end
+ u.port = u.port or get_default_port(u.scheme) or port.number
if ( not(u.path) ) then
u.path = "/"
end
@@ -1668,15 +1662,7 @@
local port = {}
port.service = parsed.scheme
- port.number = parsed.port
-
- if not port.number then
- if parsed.scheme == 'https' then
- port.number = 443
- else
- port.number = 80
- end
- end
+ port.number = parsed.port or get_default_port(parsed.scheme)
local path = parsed.path or "/"
if parsed.query then
Please let me know if you have any questions or concerns; otherwise I will commit the patch in a few weeks.
Metadata
Metadata
Assignees
Labels
No labels