Commits

Serge A. Zaitsev  committed 7534deb

added header parsing function

  • Participants
  • Parent commits 2c884d5

Comments (0)

Files changed (2)

 
 function http_handler(r)
 	print(r.req)
-	r:write("hello")
+	r:write("hello, root")
 end
 
-local http = eio.http_server(8080, {["/"] = http_handler})
+function http_handler_index(r)
+	print(r.req)
+	r:write("hello, index")
+end
+
+local http = eio.http_server(8080, {["/"] = http_handler, ["/index"] = http_handler_index})
 http:serve()
 
 	end
 end
 
+local function http_parse_header(h)
+	-- the first line is "METHOD URI VERSION"
+	local header = {}
+	local method, uri = h:match("^(%w+)%s+([^%s]+)")
+	print(method, uri)
+	h = h:sub(h:find("\n")+1) -- skip 1st line
+
+	for k,v in h:gmatch("([%w-]+)%s*:%s*(.-)\r\n") do
+		header[k] = v
+	end
+
+	return method, uri, header
+end
+
 --
 -- Process new HTTP connection
 --
 			end
 		end
 
+		local m, u, h = http_parse_header(request)
+
 		local r = {
-			req = request,
+			method = m,
+			uri = u,
+			header = h,
 			read = function(self,n) return self:read(fd, n) end,
 			write = function(self,s) return libeio.write(fd, s) end
 		}
-		print(request)
 
 		local handler = function(r) r:write("404 Not Found\r\n") end
 		for uri, h in pairs(self.handlers) do
-			if uri == "/" then
+			if r.uri == uri then
 				handler = h
 				break
 			end