@codua/codeoutline-win32-x64 0.1.2 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,76 @@
1
+ local M = {}
2
+ local u = require('xutils')
3
+ function M.id(s)
4
+ return type(s) == 'string' and #s <= 64 and s:match('^[a-z][a-z0-9_-]*$') ~= nil
5
+ end
6
+ function M.version(s)
7
+ return type(s) == 'string' and #s <= 64 and s:match('^%d+%.%d+%.%d+$') ~= nil
8
+ end
9
+ function M.read(path, limit)
10
+ local f = io.open(path, 'rb'); if not f then return nil end
11
+ local data, err = f:read((limit or 67108864) + 1); f:close()
12
+ if err then return nil end
13
+ data = data or ''
14
+ if #data > (limit or 67108864) then return nil end
15
+ return data
16
+ end
17
+ function M.write(path, data)
18
+ assert(u.mkdir_p(assert(path:match('^(.*)/[^/]+$'))))
19
+ local f = assert(io.open(path, 'wb')); assert(f:write(data)); assert(f:close())
20
+ end
21
+ function M.atomic(path, data)
22
+ local temp = assert(u.temp_file(assert(path:match('^(.*)/[^/]+$'))))
23
+ local ok, err = pcall(function() M.write(temp, data); assert(u.replace_file(temp, path)) end)
24
+ if not ok then os.remove(temp); error(err) end
25
+ end
26
+ function M.path(s)
27
+ if type(s) ~= 'string' or #s == 0 or #s > 240 or s:find('[^%w_./-]') or s:sub(1,1) == '/' then return false end
28
+ for part in s:gmatch('[^/]+') do
29
+ if part == '.' or part == '..' or part:sub(-1) == '.' then return false end
30
+ local stem = part:match('^([^.]*)'):upper()
31
+ if stem == 'CON' or stem == 'PRN' or stem == 'AUX' or stem == 'NUL' or stem:match('^COM[1-9]$') or stem:match('^LPT[1-9]$') then return false end
32
+ end
33
+ return not s:find('//',1,true) and s:sub(-1) ~= '/'
34
+ end
35
+ function M.decode_json(s)
36
+ local ok, v = pcall(u.json_unpack, s or '')
37
+ if ok and type(v) == 'table' then return v end
38
+ end
39
+ function M.manifest(envelope, public_key)
40
+ assert(type(envelope) == 'table' and type(envelope.payload) == 'string' and #envelope.payload <= 1048576, 'invalid signed envelope')
41
+ assert(type(envelope.signature) == 'string' and #envelope.signature <= 1024, 'invalid signature')
42
+ local sig = u.base64_decode(envelope.signature)
43
+ assert(require('xupgate.signature').verify(public_key, envelope.payload, sig), 'manifest signature invalid')
44
+ local m = assert(M.decode_json(envelope.payload), 'invalid manifest JSON')
45
+ assert(m.schema == 1 and M.id(m.project) and M.version(m.version) and M.id(m.platform), 'invalid manifest identity')
46
+ assert(m.minUpdater == 1, 'unsupported updater version')
47
+ assert(type(m.sha256) == 'string' and #m.sha256 == 64 and m.sha256:match('^[0-9a-f]+$'), 'invalid package hash')
48
+ assert(type(m.size) == 'number' and m.size > 0 and m.size <= 50331648 and m.size % 1 == 0, 'invalid package size')
49
+ assert(type(m.sequence) == 'number' and m.sequence >= 1 and m.sequence % 1 == 0, 'invalid release sequence')
50
+ assert(type(m.entry) == 'string' and M.path(m.entry), 'invalid entry')
51
+ assert(m.notes==nil or (type(m.notes)=='string' and #m.notes<=16384),'invalid notes')
52
+ assert(m.runtime==nil or M.path(m.runtime),'invalid runtime')
53
+ return m
54
+ end
55
+ function M.bundle(data, manifest)
56
+ assert(#data == manifest.size and u.sha256_hex(data) == manifest.sha256, 'package checksum mismatch')
57
+ local b = assert(M.decode_json(data), 'invalid bundle')
58
+ assert(b.schema == 1 and type(b.files) == 'table' and #b.files > 0 and #b.files <= 10000, 'invalid files')
59
+ local seen, decoded, total, entry = {}, {}, 0, false
60
+ for _, f in ipairs(b.files) do
61
+ assert(type(f) == 'table' and M.path(f.path) and type(f.data) == 'string', 'unsafe file')
62
+ local key = f.path:lower(); assert(not seen[key], 'duplicate file'); seen[key] = true
63
+ local bytes = assert(u.base64_decode(f.data), 'bad base64')
64
+ total = total + #bytes; assert(total <= 50331648, 'expanded package too large')
65
+ decoded[#decoded+1] = { path = f.path, data = bytes, executable = f.executable == true }
66
+ if f.path == manifest.entry then entry = true end
67
+ end
68
+ for key in pairs(seen) do
69
+ local prefix = key:match('^(.*)/[^/]+$')
70
+ while prefix do assert(not seen[prefix], 'file/directory collision'); prefix = prefix:match('^(.*)/[^/]+$') end
71
+ end
72
+ assert(entry, 'entry missing from bundle')
73
+ assert(not manifest.runtime or seen[manifest.runtime:lower()], 'runtime missing from bundle')
74
+ return decoded
75
+ end
76
+ return M
@@ -0,0 +1,382 @@
1
+ -- xhttp_client.lua - asynchronous HTTP/HTTPS client built on xnet + xhttp_codec.
2
+ --
3
+ -- Non-blocking and callback-based: it drives requests through the same xnet
4
+ -- event loop as the rest of the project. Plaintext uses xnet.connect; TLS uses
5
+ -- xnet.connect_tls (only present when xnet was built WITH_HTTPS=1). Responses
6
+ -- are parsed with xhttp_codec.parse_response, so Content-Length, chunked,
7
+ -- gzip/deflate and Connection: close framing are all handled.
8
+ --
9
+ -- Usage:
10
+ -- local httpc = dofile('scripts/core/share/xhttp_client.lua')
11
+ -- httpc.get('https://example.com/', function(err, resp)
12
+ -- if err then return print('error: ' .. err) end
13
+ -- print(resp.status, #resp.body)
14
+ -- end)
15
+ --
16
+ -- The callback fires exactly once with either (err) or (nil, resp), where
17
+ -- resp = { status, version, headers, header_list, body }.
18
+
19
+ local codec = require('xupgate.http_codec')
20
+ local xproxy = require('xupgate.proxy')
21
+
22
+ local M = {}
23
+ M.codec = codec
24
+ M.parse_url = codec.parse_url
25
+
26
+ local DEFAULT_MAX_REDIRECTS = 5
27
+ local DEFAULT_USER_AGENT = 'xnet-httpc/1.0'
28
+ local MAX_RESPONSE = 64 * 1024 * 1024
29
+
30
+ -- Forward declarations (these functions reference one another).
31
+ local start_connection, handle_response, make_handler, start_tunnel
32
+
33
+ local function header_present(headers, name)
34
+ if type(headers) ~= 'table' then return false end
35
+ local lname = name:lower()
36
+ for k in pairs(headers) do
37
+ if tostring(k):lower() == lname then return true end
38
+ end
39
+ return false
40
+ end
41
+
42
+ local function default_port(scheme)
43
+ return scheme == 'https' and 443 or 80
44
+ end
45
+
46
+ local function authority_for(scheme, host, port)
47
+ if port == default_port(scheme) then return host end
48
+ return host .. ':' .. tostring(port)
49
+ end
50
+
51
+ local function build_request(req)
52
+ local out = { req.method .. ' ' .. req.path .. ' HTTP/1.1\r\n' }
53
+
54
+ local h = {}
55
+ if type(req.headers) == 'table' then
56
+ for k, v in pairs(req.headers) do h[k] = v end
57
+ end
58
+ if not header_present(h, 'host') then h['Host'] = req.host_hdr end
59
+ if not header_present(h, 'user-agent') then h['User-Agent'] = DEFAULT_USER_AGENT end
60
+ if not header_present(h, 'accept') then h['Accept'] = '*/*' end
61
+ if req.decompress and not header_present(h, 'accept-encoding') then
62
+ h['Accept-Encoding'] = 'gzip, deflate'
63
+ end
64
+ if not header_present(h, 'connection') then h['Connection'] = 'close' end
65
+ if req.body and #req.body > 0 and not header_present(h, 'content-length') then
66
+ h['Content-Length'] = tostring(#req.body)
67
+ end
68
+
69
+ for k, v in pairs(h) do
70
+ out[#out + 1] = codec.clean_header(k) .. ': ' .. codec.clean_header(v) .. '\r\n'
71
+ end
72
+ out[#out + 1] = '\r\n'
73
+ if req.body and #req.body > 0 then out[#out + 1] = req.body end
74
+ return table.concat(out)
75
+ end
76
+
77
+ -- Resolve a (possibly relative) Location against the request's current URL.
78
+ local function resolve_url(base, loc)
79
+ if loc:match('^%a[%w+.-]*://') then return loc end
80
+ local scheme, host, port, path = codec.parse_url(base)
81
+ if not scheme then return loc end
82
+ local authority = authority_for(scheme, host, port)
83
+ if loc:sub(1, 1) == '/' then
84
+ return scheme .. '://' .. authority .. loc
85
+ end
86
+ local dir = path:match('^(.*/)[^/]*$') or '/'
87
+ return scheme .. '://' .. authority .. dir .. loc
88
+ end
89
+
90
+ local function fire_once(state, err, resp)
91
+ if state.done then return end
92
+ state.done = true
93
+ if state.timer then
94
+ state.timer:del()
95
+ state.timer = nil
96
+ end
97
+ state.cb(err, resp)
98
+ end
99
+
100
+ handle_response = function(state, resp)
101
+ local status = resp.status
102
+ if status >= 300 and status < 400 and state.redirects_left > 0 then
103
+ local loc = resp.headers and resp.headers['location']
104
+ if loc and loc ~= '' then
105
+ state.redirects_left = state.redirects_left - 1
106
+ local nurl = resolve_url(state.url, loc)
107
+ local method, body = state.method, state.body
108
+ -- 303 always becomes GET; 301/302 on POST follow common browser
109
+ -- behaviour and switch to GET. 307/308 preserve method + body.
110
+ if status == 303 or ((status == 301 or status == 302) and method == 'POST') then
111
+ method, body = 'GET', nil
112
+ end
113
+ start_connection(state, nurl, method, body)
114
+ return
115
+ end
116
+ end
117
+ fire_once(state, nil, resp)
118
+ end
119
+
120
+ make_handler = function(state)
121
+ local buf = ''
122
+ local h = {}
123
+
124
+ function h.on_connect(conn)
125
+ local ok, serr = conn:send_raw(state.request_bytes)
126
+ if not ok then
127
+ conn:close('send_failed')
128
+ fire_once(state, 'send failed: ' .. tostring(serr))
129
+ end
130
+ end
131
+
132
+ local function on_data(conn, data)
133
+ buf = buf .. data
134
+ if #buf > MAX_RESPONSE then
135
+ conn:close('too_large')
136
+ fire_once(state, 'response too large')
137
+ return #data
138
+ end
139
+ local resp, _, perr = codec.parse_response(buf, 1, {
140
+ method = state.method,
141
+ decompress = state.decompress,
142
+ })
143
+ if resp then
144
+ state.parsed = true
145
+ conn:close('done')
146
+ handle_response(state, resp)
147
+ elseif perr and perr ~= 'incomplete' then
148
+ conn:close('parse_error')
149
+ fire_once(state, 'parse error: ' .. tostring(perr))
150
+ end
151
+ -- Consume everything the channel handed us; we keep our own buffer.
152
+ return #data
153
+ end
154
+ h.on_packet = on_data
155
+ h.on_recv = on_data
156
+
157
+ function h.on_close(_, reason)
158
+ if state.done or state.parsed then return end
159
+ -- The peer closed; if it used Connection: close framing (no length, not
160
+ -- chunked) the body is whatever we have buffered.
161
+ local resp = codec.parse_response(buf, 1, {
162
+ method = state.method,
163
+ decompress = state.decompress,
164
+ eof = true,
165
+ })
166
+ if resp then
167
+ handle_response(state, resp)
168
+ else
169
+ fire_once(state, 'connection closed before full response ('
170
+ .. tostring(reason) .. ')')
171
+ end
172
+ end
173
+
174
+ return h
175
+ end
176
+
177
+ -- ---------------------------------------------------------------------------
178
+ -- Proxy support (opt-in, per request via opts.proxy): SOCKS5/SOCKS5h for any
179
+ -- target, HTTP CONNECT for HTTPS targets, plain forward proxy for HTTP targets
180
+ -- via an HTTP proxy. The tunnel handshake lives in xproxy.lua (shared with
181
+ -- xhttp_stream.lua).
182
+ -- ---------------------------------------------------------------------------
183
+ M.parse_proxy = xproxy.parse
184
+
185
+ local function merge_headers(base, extra)
186
+ if not extra then return base end
187
+ local h = {}
188
+ if type(base) == 'table' then for k, v in pairs(base) do h[k] = v end end
189
+ for k, v in pairs(extra) do h[k] = v end
190
+ return h
191
+ end
192
+
193
+ -- Tunnel through the proxy, then hand the connection to the response handler
194
+ -- (TLS-upgraded for HTTPS origins). make_handler's on_connect sends the request.
195
+ start_tunnel = function(state, proxy)
196
+ state.tunnel = xproxy.open_tunnel(proxy, state.host, state.port, {
197
+ on_ready = function(conn, extra)
198
+ state.tunnel = nil
199
+ if state.done then return conn:close('done') end
200
+ local c, err = xproxy.attach(conn, extra, make_handler(state), {
201
+ tls = state.scheme == 'https', host = state.host, port = state.port,
202
+ verify = state.verify, ca_file = state.ca_file,
203
+ })
204
+ if not c then return fire_once(state, err) end
205
+ state.conn = c
206
+ end,
207
+ on_error = function(msg)
208
+ state.tunnel = nil
209
+ fire_once(state, msg)
210
+ end,
211
+ })
212
+ end
213
+
214
+ start_connection = function(state, url, method, body)
215
+ state.url = url
216
+ state.method = method
217
+ state.body = body
218
+
219
+ local scheme, host, port, path = codec.parse_url(url)
220
+ if not scheme then
221
+ fire_once(state, 'bad url: ' .. tostring(host))
222
+ return
223
+ end
224
+ if scheme ~= 'http' and scheme ~= 'https' then
225
+ fire_once(state, 'unsupported scheme: ' .. scheme)
226
+ return
227
+ end
228
+ state.scheme, state.host, state.port = scheme, host, port
229
+ state.parsed = false
230
+
231
+ local proxy = state.proxy
232
+ local authority = authority_for(scheme, host, port)
233
+
234
+ -- HTTP proxy + plaintext target: classic forward proxy with absolute-form
235
+ -- request line; no CONNECT tunnel needed.
236
+ if proxy and proxy.type == 'http' and scheme == 'http' then
237
+ local extra
238
+ local auth = xproxy.auth_header(proxy)
239
+ if auth then extra = { ['Proxy-Authorization'] = auth } end
240
+ state.request_bytes = build_request({
241
+ method = method,
242
+ path = scheme .. '://' .. authority .. path, -- absolute-form
243
+ host_hdr = authority,
244
+ headers = merge_headers(state.headers, extra),
245
+ body = body,
246
+ decompress = state.decompress,
247
+ })
248
+ local conn, cerr = xnet.connect(proxy.host, proxy.port, make_handler(state))
249
+ if not conn then
250
+ fire_once(state, 'proxy connect failed: ' .. tostring(cerr))
251
+ return
252
+ end
253
+ state.conn = conn
254
+ return
255
+ end
256
+
257
+ -- Origin-form request -- travels through the tunnel (proxy) or directly.
258
+ state.request_bytes = build_request({
259
+ method = method,
260
+ path = path,
261
+ host_hdr = authority,
262
+ headers = state.headers,
263
+ body = body,
264
+ decompress = state.decompress,
265
+ })
266
+
267
+ -- Tunnelled paths: SOCKS5/SOCKS5h (any scheme) or HTTP CONNECT (https only).
268
+ if proxy then
269
+ if scheme == 'https'
270
+ and (type(rawget(_G, 'xnet')) ~= 'table' or not xnet.connect_tls_fd) then
271
+ fire_once(state, 'https not supported: xnet built without HTTPS')
272
+ return
273
+ end
274
+ start_tunnel(state, proxy)
275
+ return
276
+ end
277
+
278
+ -- Direct connection (no proxy).
279
+ local handler = make_handler(state)
280
+ local conn, cerr
281
+ if scheme == 'https' then
282
+ if type(rawget(_G, 'xnet')) ~= 'table' or not xnet.connect_tls then
283
+ fire_once(state, 'https not supported: xnet built without HTTPS')
284
+ return
285
+ end
286
+ conn, cerr = xnet.connect_tls(host, port, handler, {
287
+ verify = state.verify,
288
+ ca_file = state.ca_file,
289
+ server_name = host,
290
+ })
291
+ else
292
+ conn, cerr = xnet.connect(host, port, handler)
293
+ end
294
+
295
+ if not conn then
296
+ fire_once(state, 'connect failed: ' .. tostring(cerr))
297
+ return
298
+ end
299
+ state.conn = conn
300
+ end
301
+
302
+ -- httpc.request(opts, cb)
303
+ -- opts: url | (scheme/host/port/path), method, headers, body,
304
+ -- timeout_ms, max_redirects, verify (TLS, default true), ca_file,
305
+ -- decompress (default true),
306
+ -- proxy (per-request proxy URL: socks5://, socks5h://, or http://,
307
+ -- with optional user:pass@; default none = direct connect).
308
+ -- cb(err, resp)
309
+ function M.request(opts, cb)
310
+ assert(type(opts) == 'table', 'xhttp_client.request: opts table required')
311
+ assert(type(cb) == 'function', 'xhttp_client.request: callback required')
312
+
313
+ local url = opts.url
314
+ if not url then
315
+ local scheme = opts.scheme or 'http'
316
+ assert(opts.host, 'xhttp_client.request: opts.url or opts.host required')
317
+ local port = opts.port or default_port(scheme)
318
+ url = string.format('%s://%s:%d%s', scheme, opts.host, port, opts.path or '/')
319
+ end
320
+
321
+ local decompress = opts.decompress
322
+ if decompress == nil then decompress = true end
323
+ local verify = opts.verify
324
+ if verify == nil then verify = true end
325
+
326
+ local state = {
327
+ cb = cb,
328
+ headers = opts.headers,
329
+ decompress = decompress,
330
+ verify = verify,
331
+ ca_file = opts.ca_file,
332
+ timeout_ms = opts.timeout_ms,
333
+ redirects_left = opts.max_redirects or DEFAULT_MAX_REDIRECTS,
334
+ done = false,
335
+ }
336
+
337
+ -- Optional per-request proxy (opt-in; no implicit global/env source).
338
+ if opts.proxy and opts.proxy ~= '' then
339
+ local p, perr = xproxy.parse(opts.proxy)
340
+ if not p then
341
+ fire_once(state, 'proxy config: ' .. tostring(perr))
342
+ return state
343
+ end
344
+ state.proxy = p
345
+ end
346
+
347
+ -- Optional request timeout, only when the timer subsystem is running.
348
+ local xt = rawget(_G, 'xtimer')
349
+ if state.timeout_ms and state.timeout_ms > 0
350
+ and type(xt) == 'table' and xt.inited and xt.inited() then
351
+ state.timer = xt.add(state.timeout_ms, function()
352
+ state.timer = nil
353
+ -- Either the direct/TLS conn or the in-flight proxy tunnel may be live.
354
+ if state.tunnel then state.tunnel:close('timeout'); state.tunnel = nil end
355
+ local c = state.conn
356
+ if c and not c:is_closed() then c:close('timeout') end
357
+ fire_once(state, 'timeout')
358
+ end, 1)
359
+ end
360
+
361
+ start_connection(state, url, (opts.method or 'GET'):upper(), opts.body)
362
+ return state
363
+ end
364
+
365
+ function M.get(url, opts, cb)
366
+ if type(opts) == 'function' then cb, opts = opts, nil end
367
+ opts = opts or {}
368
+ opts.url = url
369
+ opts.method = 'GET'
370
+ return M.request(opts, cb)
371
+ end
372
+
373
+ function M.post(url, body, opts, cb)
374
+ if type(opts) == 'function' then cb, opts = opts, nil end
375
+ opts = opts or {}
376
+ opts.url = url
377
+ opts.method = 'POST'
378
+ opts.body = body
379
+ return M.request(opts, cb)
380
+ end
381
+
382
+ return M