我被这种行为所欺骗,但我不完全确定为什么会这样。
我们有一个Lua HTTP客户端,它向我们的一个后端服务发出HTTP
请求。运行脚本后,我发现客户端没有在HOST标头中传递port
信息。
下面是我们的HTTP客户端
local ltn12 = assert(require('ltn12'))
local cjson = assert(require('cjson'))
local http = assert(require('socket.http'))
local debugger = assert(require('debugger'))
http.TIMEOUT = 120
local function execute()
print('-- COMMANDER: Starting... ----------')
os.execute('sleep 1')
local ivy_url = 'http://localhost:3000'
if(ivy_url == nil) then
print('-- COMMANDER: Error :: ivy_url not set ----------')
execute()
end
local _heartbeat = 0
local last_timestamp = nil
local url = nil
while true do
-- _heartbeat used to inform that loop is executing properly.
if _heartbeat % 3 == 0 then
_heartbeat = 0
print('-- COMMANDER: Heartbeat... ----------')
end
_heartbeat = _heartbeat + 1
local response = {}
local since = last_timestamp
last_timestamp = os.date("%Y-%m-%dT%H:%M:%S")
if (since == nil) then
url = ivy_url .. "/switch/sites"
else
url = ivy_url .. "/switch/sites?since=" .. since
end
local one, code, headers, status = http.request {
method = "GET",
url = url,
headers = { Authorization = "Basic " .. tostring("abcd")},
sink = ltn12.sink.table(response)
}
if(code == 200) then
response_data = cjson.decode(response[1])
for i, site_desc in pairs(response_data['data']) do
print(site_desc)
end
print('-- COMMANDER: Sent API request to Ivy to get site info updated_at ' .. tostring(since) .. ' ----------')
elseif(code == 400) then
last_timestamp = since
response_data = cjson.decode(response[1])
print('-- COMMANDER: Got error in API response : ' .. tostring(response_data['error']) .. ' at ' .. os.date("%Y-%m-%dT%H:%M:%S") .. ' ----------')
else
last_timestamp = since
print('-- COMMANDER: Got error in API response : ' .. tostring(code) .. ' at ' .. os.date("%Y-%m-%dT%H:%M:%S") .. ' ----------')
end
os.execute('sleep 10')
end
end
execute()
在我们的后端服务上,我们以localhost
而不是localhost:3000
的身份接收主机
考虑到RFC规范说[: port]
是可选的,但是我仍然知道几乎所有的curl,邮递员,甚至浏览器都通过它们的客户端。
所以我的问题是...
为什么luasocket API会忽略它们?