Minimalistic server can be started and returns hardcoded message, prior to cleanup
This commit is contained in:
parent
ba390bb855
commit
1c709b838b
130
httpserver.lua
130
httpserver.lua
@ -1,42 +1,15 @@
|
|||||||
-- httpserver
|
-- httpserver
|
||||||
-- Author: Marcos Kirsch
|
-- Author: Marcos Kirsch
|
||||||
-- This is a very simple HTTP server designed to work on nodemcu (http://nodemcu.com)
|
|
||||||
-- It can handle GET and POST.
|
|
||||||
require "printTable"
|
|
||||||
|
|
||||||
|
module("httpserver", package.seeall)
|
||||||
|
|
||||||
httpserver = {}
|
require("TablePrinter")
|
||||||
|
|
||||||
|
-- Functions below aren't part of the public API
|
||||||
-- Starts web server in the specified port.
|
-- Clients don't need to worry about them.
|
||||||
--function httpserver.start(port, clientTimeoutInSeconds, debug)
|
|
||||||
-- -- Server constants
|
|
||||||
-- server = net.createServer(net.TCP, clientTimeoutInSeconds) server:listen(port, private.handleRequest)
|
|
||||||
--end
|
|
||||||
|
|
||||||
|
|
||||||
httpserver.private = {} -- not part of the public API
|
|
||||||
|
|
||||||
function httpserver.private.onReceive(connection, payload)
|
|
||||||
print(payload) -- for debugging
|
|
||||||
|
|
||||||
-- parse payload and decide what to serve.
|
|
||||||
parsedRequest = private.parseRequest(payload)
|
|
||||||
httpserver.private.printTable(parsedRequest, 3)
|
|
||||||
|
|
||||||
--generates HTML web site
|
|
||||||
httpHeader200 = "HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nCache-Control: private, no-store\r\n\r\n"
|
|
||||||
html = "<h1>Hola mundo</h1>"
|
|
||||||
connection:send(httpHeader200 .. html)
|
|
||||||
end
|
|
||||||
|
|
||||||
function httpserver.private.handleRequest(connection)
|
|
||||||
connection:on("receive", onReceive)
|
|
||||||
connection:on("sent",function(connection) connection:close() end)
|
|
||||||
end
|
|
||||||
|
|
||||||
-- given an HTTP request, returns the method (i.e. GET)
|
-- given an HTTP request, returns the method (i.e. GET)
|
||||||
function httpserver.private.getRequestMethod(request)
|
local function getRequestMethod(request)
|
||||||
-- HTTP Request Methods.
|
-- HTTP Request Methods.
|
||||||
-- HTTP servers are required to implement at least the GET and HEAD methods
|
-- HTTP servers are required to implement at least the GET and HEAD methods
|
||||||
-- http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods
|
-- http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods
|
||||||
@ -51,29 +24,82 @@ function httpserver.private.getRequestMethod(request)
|
|||||||
return (httpMethods[found])
|
return (httpMethods[found])
|
||||||
end
|
end
|
||||||
|
|
||||||
-- given an HTTP request, returns a table with all the information.
|
---- given an HTTP request, returns a table with all the information.
|
||||||
function httpserver.private.parseRequest(request)
|
--local function parseRequest(request)
|
||||||
parsedRequest = {}
|
-- parsedRequest = {}
|
||||||
-- First get the method
|
--
|
||||||
|
-- -- First get the method
|
||||||
|
-- parsedRequest["method"] = getRequestMethod(request)
|
||||||
|
-- if parsedRequest["method"] == nil then
|
||||||
|
-- return nil
|
||||||
|
-- end
|
||||||
|
-- -- Now get each value out of the header, skip the first line
|
||||||
|
-- lineNumber = 0
|
||||||
|
-- for line in request:gmatch("[^\r\n]+") do
|
||||||
|
-- if lineNumber ~=0 then
|
||||||
|
-- -- tag / value are of the style "Host: 10.0.7.15". Break them up.
|
||||||
|
-- found, valueIndex = string.find(line, ": ")
|
||||||
|
-- if found == nil then
|
||||||
|
-- break
|
||||||
|
-- end
|
||||||
|
-- tag = string.sub(line, 1, found - 1)
|
||||||
|
-- value = string.sub(line, found + 2, #line)
|
||||||
|
-- parsedRequest[tag] = value
|
||||||
|
-- end
|
||||||
|
-- lineNumber = lineNumber + 1
|
||||||
|
-- end
|
||||||
|
-- return parsedRequest
|
||||||
|
--end
|
||||||
|
|
||||||
parsedRequest["method"] = httpserver.private.getRequestMethod(request)
|
function parseRequest(request)
|
||||||
if parsedRequest["method"] == nil then
|
local result = {}
|
||||||
return nil
|
local matchEnd = 0
|
||||||
|
|
||||||
|
local matchBegin = matchEnd + 1
|
||||||
|
matchEnd = string.find (request, " ", matchBegin)
|
||||||
|
result.method = string.sub(request, matchBegin, matchEnd-1)
|
||||||
|
|
||||||
|
matchBegin = matchEnd + 1
|
||||||
|
matchEnd = string.find(request, " ", matchBegin)
|
||||||
|
result.url = string.sub(request, matchBegin, matchEnd-1)
|
||||||
|
|
||||||
|
matchBegin = matchEnd + 1
|
||||||
|
matchEnd = string.find(request, "\r\n", matchBegin)
|
||||||
|
result.version = string.sub(request, matchBegin, matchEnd-1)
|
||||||
|
|
||||||
|
return result
|
||||||
end
|
end
|
||||||
-- Now get each value out of the header, skip the first line
|
|
||||||
lineNumber = 0
|
local function onReceive(connection, payload)
|
||||||
for line in request:gmatch("[^\r\n]+") do
|
print(payload) -- for debugging
|
||||||
if lineNumber ~=0 then
|
|
||||||
-- tag / value are of the style "Host: 10.0.7.15". Break them up.
|
-- parse payload and decide what to serve.
|
||||||
found, valueIndex = string.find(line, ": ")
|
parsedRequest = parseRequest(payload)
|
||||||
if found == nil then
|
--TablePrinter.print(parsedRequest, 3)
|
||||||
break
|
|
||||||
|
--generates HTML web site
|
||||||
|
httpHeader200 = "HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nCache-Control: private, no-store\r\n\r\n"
|
||||||
|
html = "<h1>Hola mundo</h1>"
|
||||||
|
connection:send(httpHeader200 .. html)
|
||||||
end
|
end
|
||||||
tag = string.sub(line, 1, found - 1)
|
|
||||||
value = string.sub(line, found + 2, #line)
|
local function handleRequest(connection)
|
||||||
parsedRequest[tag] = value
|
connection:on("receive", onReceive)
|
||||||
|
connection:on("sent", function(connection) connection:close() end)
|
||||||
end
|
end
|
||||||
lineNumber = lineNumber + 1
|
|
||||||
|
-- Starts web server in the specified port.
|
||||||
|
function httpserver.start(port, clientTimeoutInSeconds)
|
||||||
|
server = net.createServer(net.TCP, clientTimeoutInSeconds)
|
||||||
|
server:listen(port, handleRequest)
|
||||||
|
return server
|
||||||
end
|
end
|
||||||
return parsedRequest
|
|
||||||
|
-- Stops the server.
|
||||||
|
function httpserver.stop(server)
|
||||||
|
server:close()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
return mymodule
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user