0

我正在尝试使用带有 luasec 的 lua 通过 https 从 Web 服务器检索页面。对于大多数页面,我的脚本按预期工作,但如果资源包含特殊字符(如,'é),我将被发送到带有 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 和尽可能少的附加依赖项来检索难以捉摸的页面?

4

1 回答 1

0

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

如果您有类似的问题,请深入检查您的外部库,以确保它们执行您认为的操作。

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

于 2017-03-17T00:37:10.737 回答