-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.lua
More file actions
148 lines (137 loc) · 5.05 KB
/
http.lua
File metadata and controls
148 lines (137 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
local expect = require "cc.expect"
local handles = require "_handles"
local syscall = require "_syscall"
local os = require "craftos.os"
local http = {}
local function makeHTTPHandle(handle)
local h = {}
function h.read(n) return handle:read(n or 1) end
function h.readLine(nl) return handle:read(nl and "*L" or "*l") end
function h.readAll() return handle:read("*a") end
function h.getResponseCode() return handle:responseCode() end
function h.getResponseHeaders() return handle:responseHeaders() end
function h.close() return handle:close() end
return h
end
local function makeWSHandle(handle, _url)
local h = {}
function h.send(msg, binary) return handle:write(msg) end
function h.receive(timeout)
local tm
if timeout then tm = os.startTimer(timeout) end
while true do
if handle.status == "closed" then error('attempt to use a closed file', 2) end
local ev, url, param = os.pullEvent()
if ev == 'websocket_message' and url == _url then return param
elseif ev == 'websocket_closed' and url == _url and handle.status == "closed" then return nil
elseif ev == 'timer' and url == tm then return nil end
end
end
function h.close() return handle:close() end
return h
end
function http.get(url, headers, binary)
expect(1, url, "string", "table")
if type(url) == "string" then url = {url = url, headers = headers, binary = binary} end
expect.field(url, "url", "string")
expect.field(url, "headers", "table", "nil")
expect.field(url, "binary", "boolean", "nil")
expect.field(url, "method", "string", "nil")
expect.field(url, "redirect", "boolean", "nil")
url.method = "GET"
return http.post(url)
end
function http.post(url, body, headers, binary)
expect(1, url, "string", "table")
if type(url) == "string" then url = {url = url, body = body, headers = headers, binary = binary} end
expect.field(url, "url", "string")
expect.field(url, "body", "string", "nil")
expect.field(url, "headers", "table", "nil")
expect.field(url, "binary", "boolean", "nil")
expect.field(url, "method", "string", "nil")
expect.field(url, "redirect", "boolean", "nil")
url.encoding = url.binary and "binary" or "utf8"
local handle, err = syscall.connect(url)
if not handle then return nil, err end
local craftos = makeHTTPHandle(handle)
handles.http[handle.id] = {
url = url.url,
handle = handle,
craftos = craftos
}
if url.body then handle:write(url.body)
else handle:write() end
while true do
local event, p1 = os.pullEvent()
if event == "http_success" and p1 == url.url then return craftos
elseif event == "http_failure" and p1 == url.url then return nil, handle.error, craftos end
end
end
function http.request(url, body, headers, binary)
expect(1, url, "string", "table")
if type(url) == "string" then url = {url = url, body = body, headers = headers, binary = binary} end
expect.field(url, "url", "string")
expect.field(url, "body", "string", "nil")
expect.field(url, "headers", "table", "nil")
expect.field(url, "binary", "boolean", "nil")
expect.field(url, "method", "string", "nil")
expect.field(url, "redirect", "boolean", "nil")
url.encoding = url.binary and "binary" or "utf8"
local handle, err = syscall.connect(url)
if not handle then
os.queueEvent("http_failure", url.url, err)
return false, err
end
local craftos = makeHTTPHandle(handle)
handles.http[handle.id] = {
url = url.url,
handle = handle,
craftos = craftos
}
if url.body then handle:write(url.body)
else handle:write() end
return true
end
function http.checkURLAsync(url)
expect(1, url, "string")
local res = syscall.checkuri(url)
os.queueEvent("http_check", url, res)
end
function http.checkURL(url)
expect(1, url, "string")
return syscall.checkuri(url)
end
function http.websocketAsync(url, headers)
expect(1, url, "string")
expect(2, headers, "table", "nil")
local handle, err = syscall.connect({url = url, headers = headers})
if not handle then
os.queueEvent("websocket_failure", url.url, err)
return false, err
end
local craftos = makeWSHandle(handle, url)
handles.websocket[handle.id] = {
url = url.url,
handle = handle,
craftos = craftos
}
return true
end
function http.websocket(url, headers)
expect(1, url, "string")
expect(2, headers, "table", "nil")
local handle, err = syscall.connect({url = url, headers = headers})
if not handle then return nil, err end
local craftos = makeWSHandle(handle, url)
handles.websocket[handle.id] = {
url = url.url,
handle = handle,
craftos = craftos
}
while true do
local event, p1 = os.pullEvent()
if event == "websocket_success" and p1 == url.url then return craftos
elseif event == "websocket_failure" and p1 == url.url then return nil, handle.error end
end
end
return http