Advertisement
Antwanr942

OC Internet 2

Jul 19th, 2019
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.96 KB | None | 0 0
  1. local buffer = require("buffer")
  2. local component = require("component")
  3. local event = require("event")
  4.  
  5. local internet = {}
  6.  
  7. local ltn12 = require("ltn12")
  8. local javaserialize = require("javaserialization").javaserialize
  9.  
  10. -------------------------------------------------------------------------------
  11.  
  12. function internet.request2(url, postdata, headers)
  13.   checkArg(1, url, "string")
  14.   checkArg(2, data, "string", "table", "nil")
  15.   checkArg(3, headers, "table", "nil")
  16.  
  17.   if not component.isAvailable("internet") then
  18.     error("no primary internet card found", 2)
  19.   end
  20.   local inet = component.internet
  21.  
  22.     -- TODO: Check for too many connections
  23.     url = string_trim(url)
  24.     -- TODO: Use url.parse
  25.     local protocol = url:match("(.-):")
  26.     if protocol == "http" then
  27.     elseif protocol == "https" then
  28.         if not httpsok then
  29.             return nil, "unsupported protocol"
  30.         end
  31.     elseif protocol == "ftp" or protocol == "mailto" then
  32.         return nil, "unsupported protocol"
  33.     else
  34.         return nil, "invalid address"
  35.     end
  36.     if type(postData) ~= "string" then
  37.         postData = nil
  38.     end
  39.     local cleanheaders = {}
  40.     if type(headers) == "table" then
  41.         for k, v in pairs(headers) do
  42.             if type(k) == "string" then
  43.                 cleanheaders[k:lower()] = javaserialize(v)
  44.             end
  45.         end
  46.     end
  47.     local body = {}
  48.     local reqt = {
  49.         method = (postData and "POST" or "GET"),
  50.         url = url,
  51.         headers = cleanheaders,
  52.         sink = ltn12.sink.table(body)
  53.     }
  54.     if postData then
  55.         reqt.source = ltn12.source.string(postData)
  56.         reqt.headers["content-length"] = #postData
  57.         reqt.headers["content-type"] = "application/x-www-form-urlencoded"
  58.     end
  59.     -- TODO: This works ... but is slow.
  60.     -- TODO: Infact so slow, it can trigger the machine's sethook, so we have to work around that.
  61.     local starttime = gettime()
  62.     local page, err, headers, status = inet.request(reqt)
  63.     local offset = gettime() - starttime
  64.     timeoffset = timeoffset + offset
  65.     cprint("(request.hack) Going back in time: " .. offset .. "s")
  66.     if not page then
  67.         cprint("(request) request failed",err)
  68.     else
  69.         page = table.concat(body)
  70.     end
  71.     -- Experimental fix for headers
  72.     if headers ~= nil then
  73.         local oldheaders = headers
  74.         headers = {}
  75.         for k,v in pairs(oldheaders) do
  76.             local name = k:gsub("^.",string.upper):gsub("%-.",string.upper)
  77.             if type(v) == "table" then
  78.                 v.n = #v
  79.                 headers[name] = v
  80.             else
  81.                 headers[name] = {v,n=1}
  82.             end
  83.         end
  84.     end
  85.     local procotol, code, message
  86.     local bad = false
  87.     if status then
  88.         protocol, code, message = status:match("(.-) (.-) (.*)")
  89.         code = tonumber(code)
  90.         if code >= 400 then
  91.             bad = true
  92.             if code == 404 or code == 410 then
  93.                 page = url
  94.             else
  95.                 page = "Server returned HTTP response code: " .. code .. " for URL: " .. url
  96.             end
  97.         end
  98.     end
  99.     local closed = false
  100.     local fakesocket = {
  101.         read = function(n)
  102.             cprint("(socket) read",n)
  103.             if n == nil then n = math.huge end
  104.             checkArg(1,n,"number")
  105.             if closed then
  106.                 return nil, "connection lost"
  107.             elseif headers == nil then
  108.                 return nil, "Connection refused"
  109.             elseif page == "" then
  110.                 return nil
  111.             elseif bad then
  112.                 return nil, page
  113.             else
  114.                 n = math.min(math.max(n, 0), settings.maxReadBuffer)
  115.                 local data
  116.                 if n == math.huge then
  117.                     data = page
  118.                     page = ""
  119.                 else
  120.                     data = page:sub(1,n)
  121.                     page = page:sub(n+1)
  122.                 end
  123.                 return data
  124.             end
  125.         end,
  126.         response = function()
  127.             cprint("(socket) response")
  128.             if headers == nil or bad then
  129.                 return nil
  130.             end
  131.             return code, message, headers
  132.         end,
  133.         close = function()
  134.             cprint("(request) close")
  135.             closed = true
  136.             page = nil
  137.         end,
  138.         finishConnect = function()
  139.             cprint("(socket) finishConnect")
  140.             if closed then
  141.                 return nil, "connection lost"
  142.             elseif headers == nil then
  143.                 return nil, "Connection refused"
  144.             elseif bad then
  145.                 return nil, page
  146.             end
  147.             return true
  148.         end
  149.     }
  150.     return fakesocket
  151. end
  152.  
  153. -------------------------------------------------------------------------------
  154.  
  155. return internet2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement