From 7b225a7747c84e907160cba5c7e3a256ebf51ce6 Mon Sep 17 00:00:00 2001 From: "kepler155c@gmail.com" Date: Thu, 28 May 2020 20:10:01 -0600 Subject: [PATCH] proper fix for builtin broken http.get --- sys/init/1.http.lua | 35 +++++++++++++++++++++++++++++++++++ sys/modules/opus/util.lua | 2 +- 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 sys/init/1.http.lua diff --git a/sys/init/1.http.lua b/sys/init/1.http.lua new file mode 100644 index 0000000..4fe167f --- /dev/null +++ b/sys/init/1.http.lua @@ -0,0 +1,35 @@ +--[[ +FIX for http.get +currently, when 2 requests are generated at same time (diff coroutines) they both +will receive the same file handle. This change will ensure each request gets the +proper response. +--]] + +local http = _G.http + +local reqs = { } + +local function wrapRequest(_url, ...) + local ok, err = http.request(...) + if ok then + while true do + local event, param1, param2, param3 = os.pullEvent() + + if event == "http_success" + and param1 == _url + and not reqs[tostring(param2)] then + + reqs[tostring(param2)] = true + return param2 + + elseif event == "http_failure" and param1 == _url then + return nil, param2, param3 + end + end + end + return nil, err +end + +http.safeGet = function(_url, _headers, _binary) + return wrapRequest(_url, _url, nil, _headers, _binary) +end diff --git a/sys/modules/opus/util.lua b/sys/modules/opus/util.lua index c1c152f..058529d 100644 --- a/sys/modules/opus/util.lua +++ b/sys/modules/opus/util.lua @@ -536,7 +536,7 @@ end --[[ loading and running functions ]] -- function Util.httpGet(url, headers, isBinary) - local h, msg = http.get(url, headers, isBinary) + local h, msg = http.safeGet(url, headers, isBinary) if h then local contents = h.readAll() h.close()