OpenComputers Von Neumann Machine Programs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.6 KiB

1 year ago
  1. local shell = require("shell")
  2. local fs = require("filesystem")
  3. local internet = require("internet")
  4. local args = {...}
  5. -- Split string based on delimiter
  6. function split(s, sep)
  7. if sep == nil then
  8. sep = "%s"
  9. end
  10. local t = {}
  11. for str in string.gmatch(s, "([^"..sep.."]+)") do
  12. table.insert(t, str)
  13. end
  14. return t
  15. end
  16. -- Slice table
  17. function slice(tbl, first, last, step)
  18. local sliced = {}
  19. for i = first or 1, last or #tbl, step or 1 do
  20. sliced[#sliced+1] = tbl[i]
  21. end
  22. return sliced
  23. end
  24. -- Error checking
  25. if args[1] == nil then
  26. print("URL must exist")
  27. return
  28. end
  29. local handle = internet.request(args[1])
  30. local result = ""
  31. for chunk in handle do result = result..chunk end
  32. -- Print the body of the HTTP response
  33. -- print(result)
  34. -- Grab the metatable for the handle. This contains the
  35. -- internal HTTPRequest object.
  36. local mt = getmetatable(handle)
  37. -- The response method grabs the information for
  38. -- the HTTP response code, the response message, and the
  39. -- response headers.
  40. local code, message, headers = mt.__index.response()
  41. print("code = "..tostring(code))
  42. print("message = "..tostring(message))
  43. -- Sandbox so we don't write to the screen
  44. sandbox_env = {
  45. assert = assert,
  46. require = require,
  47. pairs = pairs,
  48. pcall = pcall,
  49. tostring = tostring,
  50. string = string,
  51. io = {write = function(...) end, read = io.read, open = io.open, stderr = io.stderr}
  52. }
  53. -- pcall(fs.remove, fileName)
  54. -- shell.execute("wget -f "..args[1].." "..fileName, sandbox_env)
  55. -- -- Run downloaded file
  56. -- local c = fileName
  57. -- for _, i in ipairs(newArgs) do c = c.." "..i end
  58. -- shell.execute(c)function split(s, sep)