Advertisement
Guest User

parse.lua

a guest
Feb 19th, 2017
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.53 KB | None | 0 0
  1. local parse = {}
  2.  
  3. function parse.split(str, separator, splits)
  4.     local parts = {}
  5.     local splitcount = 0
  6.     local s = 1
  7.     local e = 1
  8.  
  9.     for c in str:gmatch"." do
  10.         if(c == separator) then
  11.             table.insert(parts, str:sub(s, e-1))
  12.             s = e +1
  13.             splitcount = splitcount + 1
  14.         end
  15.  
  16.         e = e +1
  17.  
  18.         if(splitcount == splits) then
  19.             e = 0
  20.             break
  21.         end
  22.     end
  23.  
  24.     table.insert(parts, str:sub(s, e -1))
  25.  
  26.     return parts
  27. end
  28.  
  29. function parse.spliturl(url)
  30.     local s, e = url:find("https?://")
  31.     local protocol = "https"
  32.  
  33.     -- get protocol
  34.     if(s) then
  35.         protocol = parse.split(url:sub(s, e), ":", 1)[1]
  36.         url = url:sub(e+1, -1)
  37.     end
  38.  
  39.     -- separate host from request
  40.     local parts = parse.split(url, "/", 1)
  41.  
  42.     -- get request
  43.     local request = "/"
  44.     if(parts[2] ~= nil and parts[2] ~= '') then
  45.         request = "/" .. parts[2]
  46.     end
  47.    
  48.  
  49.     -- separate host and port
  50.     local host_port = parse.split(parts[1], ":", 1)
  51.  
  52.     -- get port
  53.     local port
  54.     if(host_port[2] == nil or host_port[2] == '') then
  55.         if(protocol == "http") then port = "80"
  56.         elseif(protocol == "https") then port = "443" end
  57.     else
  58.         port = host_port[2]
  59.     end
  60.  
  61.     return protocol, host_port[1], port, request -- protocol, host, port, path
  62. end
  63.  
  64. function separate_http(r)
  65.     local s, e = r:find("%s\r\n")
  66.     return r:sub(0, s-1), r:sub(e+1, -1) -- header, body
  67. end
  68.  
  69. return parse
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement