为什么 Web 服务器会回复 301 和请求的确切位置?



我正在尝试通过https从Web服务器检索页面,使用lua和luasec。对于大多数页面,我的脚本按预期工作,但如果 ressource 包含特殊字符(如 ,'é),我将被发送到包含 301 个响应的循环中。

让这个代码片段说明我的困境(实际的服务器细节被编辑以保护无辜者):

local https = require "ssl.https"
local prefix = "https://www.example.com"
local suffix = "/S%C3%A9ance"
local body,code,headers,status = https.request(prefix .. suffix)
print(status .. " - GET was for "" .. prefix .. suffix .. """)
print("headers are " .. myTostring(headers))
print("body is " .. myTostring(body))
if suffix == headers.location then
print("equal")
else
print("not equal")
end
local body,code,headers,status = https.request(prefix .. headers.location)
print(status .. " - GET was for "" .. prefix .. suffix .. """)

这导致了

矛盾HTTP/1.1 301 Moved Permanently - GET was for "https://www.example.com/S%C3%A9ance" headers are { ["content-type"]="text/html; charset=UTF-8";["set-cookie"]="PHPSESSID=e80oo5dkouh8gh0ruit7mj28t6; path=/";["content-length"]="0";["connection"]="close";["date"]="Wed, 15 Mar 2017 19:31:24 GMT";["location"]="S%C3%A9ance";} body is "" equal HTTP/1.1 301 Moved Permanently - GET was for "https://www.example.com/S%C3%A9ance"

如何使用lua和尽可能少的额外依赖项来检索难以捉摸的页面?

看起来很明显,也许请求的 url 确实与实际位置不同。

如果您有类似的问题,请在外部库中深入检查,以确保它们按照您的想法进行操作。

在这种情况下,luasocket 做了 urldecode,然后对 url 进行了 urlencode 编码,因此最终的请求并不是它看起来的样子。

最新更新