2021-07-08 04:44:27 +00:00
|
|
|
-- Fetch() API Demo
|
|
|
|
|
|
|
|
local function WriteForm(url)
|
|
|
|
Write('<!doctype html>\r\n')
|
2022-04-24 16:59:22 +00:00
|
|
|
Write([[
|
2021-07-08 04:44:27 +00:00
|
|
|
<title>redbean fetch demo</title>
|
|
|
|
<style>
|
|
|
|
body {
|
|
|
|
padding: 1em;
|
|
|
|
}
|
|
|
|
h1 a {
|
|
|
|
color: inherit;
|
|
|
|
text-decoration: none;
|
|
|
|
}
|
|
|
|
h1 img {
|
2021-07-12 06:17:47 +00:00
|
|
|
border: none;
|
2021-07-08 04:44:27 +00:00
|
|
|
vertical-align: middle;
|
|
|
|
}
|
|
|
|
input {
|
|
|
|
margin: 1em;
|
|
|
|
padding: .5em;
|
|
|
|
}
|
|
|
|
p {
|
|
|
|
word-break: break-word;
|
|
|
|
}
|
2022-03-18 09:33:37 +00:00
|
|
|
dd {
|
|
|
|
margin-top: 1em;
|
|
|
|
margin-bottom: 1em;
|
|
|
|
}
|
|
|
|
.hdr {
|
2021-07-08 04:44:27 +00:00
|
|
|
text-indent: -1em;
|
|
|
|
padding-left: 1em;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
<h1>
|
|
|
|
<a href="/"><img src="/redbean.png"></a>
|
|
|
|
<a href="fetch.lua">redbean fetch demo</a>
|
|
|
|
</h1>
|
|
|
|
<form action="fetch.lua" method="post">
|
|
|
|
<input type="text" id="url" name="url" size="70"
|
|
|
|
value="%s" placeholder="uri" autofocus>
|
|
|
|
<input type="submit" value="fetch">
|
|
|
|
</form>
|
2022-04-24 16:59:22 +00:00
|
|
|
]] % {EscapeHtml(url)})
|
2021-07-08 04:44:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local function main()
|
|
|
|
if IsPublicIp(GetClientAddr()) then
|
|
|
|
ServeError(403)
|
|
|
|
elseif GetMethod() == 'GET' or GetMethod() == 'HEAD' then
|
|
|
|
WriteForm("https://www.cloudflare.com/robots.txt")
|
|
|
|
elseif GetMethod() == 'POST' then
|
|
|
|
status, headers, payload = Fetch(GetParam('url'))
|
|
|
|
WriteForm(GetParam('url'))
|
|
|
|
Write('<dl>\r\n')
|
|
|
|
Write('<dt>Status\r\n')
|
2022-04-24 16:59:22 +00:00
|
|
|
Write('<dd><p>%d %s\r\n' % {status, GetHttpReason(status)})
|
2021-07-08 04:44:27 +00:00
|
|
|
Write('<dt>Headers\r\n')
|
2022-03-18 09:33:37 +00:00
|
|
|
Write('<dd>\r\n')
|
2021-07-08 04:44:27 +00:00
|
|
|
for k,v in pairs(headers) do
|
2022-03-18 09:33:37 +00:00
|
|
|
Write('<div class="hdr"><strong>')
|
2021-07-08 04:44:27 +00:00
|
|
|
Write(EscapeHtml(k))
|
|
|
|
Write('</strong>: ')
|
|
|
|
Write(EscapeHtml(v))
|
2022-03-18 09:33:37 +00:00
|
|
|
Write('</div>\r\n')
|
2021-07-08 04:44:27 +00:00
|
|
|
end
|
|
|
|
Write('<dt>Payload\r\n')
|
|
|
|
Write('<dd><pre>')
|
|
|
|
Write(EscapeHtml(VisualizeControlCodes(payload)))
|
|
|
|
Write('</pre>\r\n')
|
|
|
|
Write('</dl>\r\n')
|
|
|
|
else
|
|
|
|
ServeError(405)
|
|
|
|
SetHeader('Allow', 'GET, HEAD, POST')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
main()
|