Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local _exit=Exit;Exit=function(ret)print(ret); GetKey(); return ret+1; end
- local function HttpRequest(socket, data)
- local body = "IP: "..GetIP(socket).."<br>";
- body = body .. "Method: "..tostring(data.method).."<br>";
- body = body .. "Request: "..tostring(data.request).."<br>";
- body = body .. "Version: "..tostring(data.protocol).."<br>";
- body = body .. "Headers: "..tostring(data.method).."<br>";
- for k,v in pairs(data.headers) do
- body = body .. "- "..k.." = "..v.."<br>";
- end
- local headers={};
- headers["Connection"]="Closed";
- headers["Content-Type"]="text/html";
- headers["Content-Length"]=tostring(body:len());
- return CreateHttpResponse(data.protocol, 200, "OK", headers, body);
- end
- NETEVENT_CONNECTED = 1;
- NETEVENT_DISCONNECTED = 2;
- NETEVENT_SEND = 3;
- NETEVENT_RECEIVE = 4;
- Server.SetStartFunc(function(srv) print("STARTED", srv); end);
- local socket = assert(Server.Start(5556));
- function GetIP(sock)
- local result = socket:GetClients()[sock];
- if not result then
- return "Unknown";
- else
- return result;
- end
- end
- local httprequests = {};
- local eventfuncs = {}
- eventfuncs[NETEVENT_CONNECTED] = function(ev)
- print("NETEVENT_CONNECTED",ev.socket, GetIP(ev.socket));
- end
- eventfuncs[NETEVENT_DISCONNECTED] = function(ev)
- print("NETEVENT_DISCONNECTED",ev.socket, GetIP(ev.socket));
- httprequests[ev.socket] = nil;
- end
- eventfuncs[NETEVENT_SEND] = function(ev)
- print("NETEVENT_SEND",ev.socket, GetIP(ev.socket), ev.data);
- end
- eventfuncs[NETEVENT_RECEIVE] = function(ev)
- print("NETEVENT_RECEIVE",ev.socket, GetIP(ev.socket));
- local request = httprequests[ev.socket] or {data="", timestamp=0};
- request.data = request.data .. ev.data;
- request.timestamp = os.clock();
- httprequests[ev.socket] = request;
- end
- local function ToHttpRequest(raw)
- local data = {headers={}};
- local requestline, raw = raw:match("^(.-)\r?\n(.-)$");
- assert(requestline, "Missing request");
- local method, request, protocol = requestline:match("(.-)%s(.-)%s(.-)$");
- assert(method, "Missing method");
- assert(request, "Missing request");
- assert(protocol, "Missing protocol");
- data.method = method;
- data.request = request;
- data.protocol = protocol;
- if raw and raw ~= "" then
- local headersraw, raw = raw:match("(.-)\r?\n\r?\n(.-)$");
- data.body = raw;
- if headersraw and headersraw~="" then
- headersraw = headersraw .. "\n";
- for key, value in headersraw:gmatch("(.-):%s(.-)\r?\n") do
- data.headers[key] = value;
- end
- end
- end
- return data;
- end
- function CreateHttpResponse(httpver, code, codename, headers, body)
- local resp = httpver.." "..tostring(code).." "..codename.."\r\n";
- if headers then
- for k,v in pairs(headers) do
- resp = resp .. k .. ": " .. v .. "\r\n";
- end
- end
- resp = resp .. "\r\n";
- if body then
- resp = resp .. body .. "\r\n";
- end
- return resp;
- end
- local function ProcessHttpRequests()
- local dead = nil;
- for sock, request in pairs(httprequests) do
- if os.clock() - request.timestamp > 0.1 then
- dead = dead or {};
- table.insert(dead, sock);
- local ok, resp = pcall(ToHttpRequest, request.data);
- if ok then
- ok, resp = pcall(HttpRequest, sock, resp);
- end
- if not ok then
- print(resp);
- socket:Send(sock, CreateHttpResponse("HTTP/1.0",500, "ERROR", {Connection="Closed"}, resp) );
- else
- socket:Send(sock, tostring(resp));
- end
- socket:Disconnect(sock);
- end
- end
- if dead then
- for n=1, #dead do
- httprequests[dead[n]] = nil;
- end
- end
- end
- local function ProcessSockets()
- local ev = socket:GetEvent();
- while ev do
- local func = eventfuncs[ev.type];
- if func then
- func(ev);
- else
- print("UNKNOWN COMMAND", ev.type);
- end
- ev = socket:GetEvent();
- end
- end
- while true do
- local ok, err = pcall(ProcessSockets);
- if not ok then
- print(err);
- end
- ok, err = pcall(ProcessHttpRequests);
- if not ok then
- print(err);
- end
- Sleep(1);
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement