mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-22 21:32:31 +00:00
Add HTTP/HTTPS Fetch() API to redbean
You can now say the following in your redbean Lua code: status,headers,payload = Fetch("https://foo.example") The following Lua APIs have been introduced: - Fetch(str) → str,{str:str},str - GetHttpReason(int) → str - GetHttpReason(int) → str - ProgramSslFetchVerify(bool) - ProgramSslClientVerify(bool) The following flags have been introduced: - `-j` enables client SSL verification - `-k` disables Fetch() SSL verification - `-t INT` may now be passed a negative value for keepalive Lua exceptions now invoke Cosmopolitan's garbage collector when unwinding the stack. So it's now safe to use _gc() w/ Lua 𝔱𝔥𝔯𝔬𝔴 See #97
This commit is contained in:
parent
36b2710e1a
commit
c89bc56f6a
35 changed files with 1611 additions and 591 deletions
74
tool/net/demo/fetch.lua
Normal file
74
tool/net/demo/fetch.lua
Normal file
|
@ -0,0 +1,74 @@
|
|||
-- Fetch() API Demo
|
||||
|
||||
local function WriteForm(url)
|
||||
Write('<!doctype html>\r\n')
|
||||
Write(string.format([[
|
||||
<title>redbean fetch demo</title>
|
||||
<style>
|
||||
body {
|
||||
padding: 1em;
|
||||
}
|
||||
h1 a {
|
||||
color: inherit;
|
||||
text-decoration: none;
|
||||
}
|
||||
h1 img {
|
||||
vertical-align: middle;
|
||||
}
|
||||
input {
|
||||
margin: 1em;
|
||||
padding: .5em;
|
||||
}
|
||||
p {
|
||||
word-break: break-word;
|
||||
}
|
||||
p span {
|
||||
display: block;
|
||||
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>
|
||||
]], EscapeHtml(url)))
|
||||
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')
|
||||
Write(string.format('<dd><p>%d %s\r\n', status, GetHttpReason(status)))
|
||||
Write('<dt>Headers\r\n')
|
||||
Write('<dd><p>\r\n')
|
||||
for k,v in pairs(headers) do
|
||||
Write('<span><strong>')
|
||||
Write(EscapeHtml(k))
|
||||
Write('</strong>: ')
|
||||
Write(EscapeHtml(v))
|
||||
Write('</span>\r\n')
|
||||
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()
|
Loading…
Add table
Add a link
Reference in a new issue