Advertisement
Terrah

GFF Http server

Jan 2nd, 2019
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.05 KB | None | 0 0
  1. local _exit=Exit;Exit=function(ret)print(ret); GetKey(); return ret+1; end
  2.  
  3. local function HttpRequest(socket, data)
  4.  
  5.     local body = "IP: "..GetIP(socket).."<br>";
  6.  
  7.     body = body .. "Method: "..tostring(data.method).."<br>";
  8.     body = body .. "Request: "..tostring(data.request).."<br>";
  9.     body = body .. "Version: "..tostring(data.protocol).."<br>";
  10.     body = body .. "Headers: "..tostring(data.method).."<br>";
  11.    
  12.     for k,v in pairs(data.headers) do
  13.         body = body .. "- "..k.." = "..v.."<br>";
  14.     end
  15.    
  16.     local headers={};
  17.     headers["Connection"]="Closed";
  18.     headers["Content-Type"]="text/html";
  19.     headers["Content-Length"]=tostring(body:len());
  20.    
  21.     return CreateHttpResponse(data.protocol, 200, "OK", headers, body);
  22. end
  23.  
  24. NETEVENT_CONNECTED = 1;
  25. NETEVENT_DISCONNECTED = 2;
  26. NETEVENT_SEND = 3;
  27. NETEVENT_RECEIVE = 4;
  28.  
  29. Server.SetStartFunc(function(srv) print("STARTED", srv); end);
  30.  
  31. local socket = assert(Server.Start(5556));
  32.  
  33. function GetIP(sock)
  34.  
  35.     local result = socket:GetClients()[sock];
  36.    
  37.     if not result then
  38.         return "Unknown";
  39.     else
  40.         return result;
  41.     end
  42. end
  43.  
  44. local httprequests = {};
  45. local eventfuncs = {}
  46.  
  47. eventfuncs[NETEVENT_CONNECTED] = function(ev)
  48.     print("NETEVENT_CONNECTED",ev.socket, GetIP(ev.socket));
  49. end
  50.  
  51. eventfuncs[NETEVENT_DISCONNECTED] = function(ev)
  52.     print("NETEVENT_DISCONNECTED",ev.socket, GetIP(ev.socket));
  53.     httprequests[ev.socket] = nil;
  54. end
  55.  
  56. eventfuncs[NETEVENT_SEND] = function(ev)
  57.     print("NETEVENT_SEND",ev.socket, GetIP(ev.socket), ev.data);
  58. end
  59.  
  60. eventfuncs[NETEVENT_RECEIVE] = function(ev)
  61.    
  62.     print("NETEVENT_RECEIVE",ev.socket, GetIP(ev.socket));
  63.    
  64.     local request = httprequests[ev.socket] or {data="", timestamp=0};
  65.    
  66.     request.data = request.data .. ev.data;
  67.     request.timestamp = os.clock();
  68.    
  69.     httprequests[ev.socket] = request; 
  70. end
  71.  
  72. local function ToHttpRequest(raw)
  73.    
  74.     local data = {headers={}};
  75.    
  76.     local requestline, raw = raw:match("^(.-)\r?\n(.-)$");
  77.    
  78.     assert(requestline, "Missing request");
  79.    
  80.     local method, request, protocol = requestline:match("(.-)%s(.-)%s(.-)$");
  81.        
  82.     assert(method, "Missing method");
  83.     assert(request, "Missing request");
  84.     assert(protocol, "Missing protocol");
  85.        
  86.     data.method = method;
  87.     data.request = request;
  88.     data.protocol = protocol;
  89.  
  90.     if raw and raw ~= "" then
  91.    
  92.         local headersraw, raw = raw:match("(.-)\r?\n\r?\n(.-)$");
  93.  
  94.         data.body = raw;
  95.  
  96.         if headersraw and headersraw~="" then  
  97.        
  98.             headersraw = headersraw .. "\n";
  99.             for key, value in headersraw:gmatch("(.-):%s(.-)\r?\n") do
  100.                 data.headers[key] = value;
  101.             end
  102.         end
  103.     end
  104.  
  105.     return data;
  106. end
  107.  
  108. function CreateHttpResponse(httpver, code, codename, headers, body)
  109.  
  110.     local resp = httpver.." "..tostring(code).." "..codename.."\r\n";
  111.    
  112.     if headers then
  113.         for k,v in pairs(headers) do
  114.             resp = resp .. k .. ": " .. v .. "\r\n";
  115.         end
  116.     end
  117.  
  118.     resp = resp .. "\r\n";
  119.    
  120.     if body then
  121.         resp = resp .. body .. "\r\n";
  122.     end
  123.    
  124.     return resp;
  125. end
  126.  
  127. local function ProcessHttpRequests()
  128.  
  129.     local dead = nil;
  130.  
  131.     for sock, request in pairs(httprequests) do
  132.    
  133.         if os.clock() - request.timestamp > 0.1 then
  134.        
  135.             dead = dead or {};
  136.             table.insert(dead, sock);
  137.        
  138.             local ok, resp = pcall(ToHttpRequest, request.data);
  139.        
  140.             if ok then
  141.                 ok, resp = pcall(HttpRequest, sock, resp);
  142.             end
  143.        
  144.             if not ok then
  145.                 print(resp);
  146.                 socket:Send(sock, CreateHttpResponse("HTTP/1.0",500, "ERROR", {Connection="Closed"}, resp) );
  147.             else
  148.                 socket:Send(sock, tostring(resp));
  149.             end
  150.  
  151.             socket:Disconnect(sock);           
  152.         end
  153.     end
  154.    
  155.     if dead then
  156.         for n=1, #dead do
  157.             httprequests[dead[n]] = nil;
  158.         end
  159.     end
  160. end
  161.  
  162. local function ProcessSockets()
  163.  
  164.     local ev = socket:GetEvent();
  165.     while ev do
  166.    
  167.         local func = eventfuncs[ev.type];
  168.    
  169.         if func then
  170.             func(ev);
  171.         else
  172.             print("UNKNOWN COMMAND", ev.type);
  173.         end
  174.    
  175.         ev = socket:GetEvent();
  176.     end
  177. end
  178.  
  179. while true do
  180.    
  181.     local ok, err = pcall(ProcessSockets);
  182.    
  183.     if not ok then
  184.         print(err);
  185.     end
  186.    
  187.     ok, err = pcall(ProcessHttpRequests);
  188.    
  189.     if not ok then
  190.         print(err);
  191.     end
  192.    
  193.     Sleep(1);
  194. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement