Commits

Anonymous committed 2c884d5

eio.lua written as a module

Comments (0)

Files changed (2)

+local eio = require("eio")
+
+function http_handler(r)
+	print(r.req)
+	r:write("hello")
+end
+
+local http = eio.http_server(8080, {["/"] = http_handler})
+http:serve()
+
 local libeio = require("libeio")
 
 local http = {}
+http.__index = http
 
-function http:serve(port, handlers)
-	self.fds = {}
-	self.crs = {}
-	self.handlers = handlers
-
+--
+-- Create new HTTP server
+-- 
+function libeio.http_server(port, handlers)
 	local f = libeio.listen(port)
 	if f < 0 then error("listen(): "..f) end
-	self.fd = f
 
+	local s = {}
+	s.fd = f
+	s.fds = {}
+	s.crs = {}
+	s.handlers = handlers
+	setmetatable(s, http)
+	return s
+end
+
+--
+-- Dispatch HTTP clients in an endless loop
+--
+function http:serve()
 	while true do 
 		local n
 		local rfds, wfds = {}, {}
 	end
 end
 
+--
+-- Process new HTTP connection
+--
 function http:accept()
 	local fd = libeio.accept(self.fd)
 	print('accept')
 		local request = ""
 
 		while request:find("\r\n\r\n") == nil do
-		--while request:find("\n\n") == nil do
 			local n, buf = self:read(fd, 1)
 			if n > 0 then 
 				request = request..tostring(buf)
 	self.crs[fd] = coroutine.create(read_header)
 end
 
+--
+-- Read from HTTP client socket in co-routine
+--
 function http:read(fd, size)
 	if self.fds[fd].can_read then
 		self.fds[fd].can_read = false
 	end
 end
 
---------------------------
+return libeio
 
-function http_handler(r)
-	print(r.req)
-	r:write("hello")
-end
-
-h = {}
-http:serve(8080, {["/"] = http_handler})
-