Fix some typos

This commit is contained in:
Artem Pastukhov 2015-03-31 10:02:33 +03:00
parent 71058e6b44
commit 0120924403
4 changed files with 133 additions and 150 deletions

View File

@ -4,7 +4,7 @@
local function getMimeType(ext)
-- A few MIME types. Keep list short. If you need something that is missing, let's add it.
local mt = {css = "text/css", gif = "image/gif", html = "text/html", ico = "image/x-icon", jpeg = "image/jpeg", jpg = "image/jpeg", js = "application/javascript", josn="application/json", png = "image/png"}
local mt = {css = "text/css", gif = "image/gif", html = "text/html", ico = "image/x-icon", jpeg = "image/jpeg", jpg = "image/jpeg", js = "application/javascript", json = "application/json", png = "image/png"}
if mt[ext] then return mt[ext] else return "text/plain" end
end

View File

@ -1,79 +1,81 @@
-- httpserver
-- Author: Marcos Kirsch
-- Starts web server in the specified port.
return function (port)
local s = net.createServer(net.TCP, 10) -- 10 seconds client timeout
s:listen(
port,
function (connection)
-- This variable holds the thread used for sending data back to the user.
-- We do it in a separate thread because we need to yield when sending lots
-- of data in order to avoid overflowing the mcu's buffer.
local connectionThread
local function onGet(connection, uri)
local fileServeFunction = nil
if #(uri.file) > 32 then
-- nodemcu-firmware cannot handle long filenames.
uri.args['code'] = 400
fileServeFunction = dofile("httpserver-error.lc")
else
local fileExists = file.open(uri.file, "r")
file.close()
if not fileExists then
uri.args['code'] = 404
fileServeFunction = dofile("httpserver-error.lc")
elseif uri.isScript then
collectgarbage()
fileServeFunction = dofile(uri.file)
else
uri.args['file'] = uri.file
uri.args['ext'] = uri.ext
fileServeFunction = dofile("httpserver-static.lc")
end
end
connectionThread = coroutine.create(fileServeFunction)
--print("Thread created", connectionThread)
coroutine.resume(connectionThread, connection, uri.args)
end
local function onReceive(connection, payload)
-- print(payload) -- for debugging
-- parse payload and decide what to serve.
local req = dofile("httpserver-request.lc")(payload)
print("Requested URI: " .. req.request)
if req.methodIsValid then
if req.method == "GET" then onGet(connection, req.uri)
else dofile("httpserver-static.lc")(conection, {code=501}) end
else
dofile("httpserver-static.lc")(conection, {code=400})
end
end
local function onSent(connection, payload)
local connectionThreadStatus = coroutine.status(connectionThread)
-- print (connectionThread, "status is", connectionThreadStatus)
if connectionThreadStatus == "suspended" then
-- Not finished sending file, resume.
-- print("Resume thread", connectionThread)
coroutine.resume(connectionThread)
elseif connectionThreadStatus == "dead" then
-- We're done sending file.
-- print("Done thread", connectionThread)
connection:close()
connectionThread = nil
end
end
connection:on("receive", onReceive)
connection:on("sent", onSent)
end
)
print("nodemcu-httpserver running at http://" .. wifi.sta.getip() .. ":" .. port)
return s
end
-- httpserver
-- Author: Marcos Kirsch
-- Starts web server in the specified port.
return function (port)
local s = net.createServer(net.TCP, 10) -- 10 seconds client timeout
s:listen(
port,
function (connection)
-- This variable holds the thread used for sending data back to the user.
-- We do it in a separate thread because we need to yield when sending lots
-- of data in order to avoid overflowing the mcu's buffer.
local connectionThread
local function onGet(connection, uri)
local fileServeFunction = nil
if #(uri.file) > 32 then
-- nodemcu-firmware cannot handle long filenames.
uri.args['code'] = 400
fileServeFunction = dofile("httpserver-error.lc")
else
local fileExists = file.open(uri.file, "r")
file.close()
if not fileExists then
uri.args['code'] = 404
fileServeFunction = dofile("httpserver-error.lc")
elseif uri.isScript then
collectgarbage()
fileServeFunction = dofile(uri.file)
else
uri.args['file'] = uri.file
uri.args['ext'] = uri.ext
fileServeFunction = dofile("httpserver-static.lc")
end
end
connectionThread = coroutine.create(fileServeFunction)
--print("Thread created", connectionThread)
coroutine.resume(connectionThread, connection, uri.args)
end
local function onReceive(connection, payload)
-- print(payload) -- for debugging
-- parse payload and decide what to serve.
local req = dofile("httpserver-request.lc")(payload)
print("Requested URI: " .. req.request)
if req.methodIsValid then
if req.method == "GET" then onGet(connection, req.uri)
else dofile("httpserver-static.lc")(conection, {code=501}) end
else
dofile("httpserver-static.lc")(conection, {code=400})
end
end
local function onSent(connection, payload)
local connectionThreadStatus = coroutine.status(connectionThread)
-- print (connectionThread, "status is", connectionThreadStatus)
if connectionThreadStatus == "suspended" then
-- Not finished sending file, resume.
-- print("Resume thread", connectionThread)
coroutine.resume(connectionThread)
elseif connectionThreadStatus == "dead" then
-- We're done sending file.
-- print("Done thread", connectionThread)
connection:close()
connectionThread = nil
end
end
connection:on("receive", onReceive)
connection:on("sent", onSent)
end
)
if wifi.sta.getip() then print("nodemcu-httpserver running at http://" .. wifi.sta.getip() .. ":" .. port)
else print("nodemcu-httpserver running at http://" .. wifi.ap.getip() .. ":" .. port)
end
return s
end

View File

@ -1,38 +1,51 @@
-- Tell the chip to connect to the access point
wifi.setmode(wifi.STATION)
print('set mode=STATION (mode='..wifi.getmode()..')')
print('MAC: ',wifi.sta.getmac())
print('chip: ',node.chipid())
print('heap: ',node.heap())
wifi.sta.config("Internet","")
-- Compile server code and remove original .lua files.
-- This only happens the first time afer the .lua files are uploaded.
local compileAndRemoveIfNeeded = function(f)
if file.open(f) then
file.close()
node.compile(f)
file.remove(f)
end
end
local serverFiles = {'httpserver.lua', 'httpserver-request.lua', 'httpserver-static.lua', 'httpserver-error.lua'}
for i, f in ipairs(serverFiles) do compileAndRemoveIfNeeded(f) end
compileAndRemoveIfNeeded = nil
serverFiles = nil
-- Connect to the WiFi access point. Once the device is connected,
-- you may start the HTTP server.
tmr.alarm(0, 3000, 1, function()
if wifi.sta.getip() == nil then
print("Connecting to AP...")
else
tmr.stop(0)
print('IP: ',wifi.sta.getip())
-- Uncomment to automatically start the server in port 80
-- dofile("httpserver.lc")(80)
end
end)
-- Tel--l the chip to connect to the access point
--wifi.setmode(wifi.STATIONAP)
wifi.setmode(wifi.STATION)
print('set (mode='..wifi.getmode()..')')
print('MAC: ',wifi.sta.getmac())
print('chip: ',node.chipid())
print('heap: ',node.heap())
local joincounter = 0
cfg={}
cfg.ssid="ESP-"..node.chipid()
cfg.pwd="ESP-"..node.chipid()
wifi.ap.config(cfg)
cfg = nil
-- Compile server code and remove original .lua files.
-- This only happens the first time afer the .lua files are uploaded.
local compileAndRemoveIfNeeded = function(f)
if file.open(f) then
file.close()
node.compile(f)
file.remove(f)
end
end
local serverFiles = {'httpserver.lua', 'httpserver-request.lua', 'httpserver-static.lua', 'httpserver-error.lua'}
for i, f in ipairs(serverFiles) do compileAndRemoveIfNeeded(f) end
compileAndRemoveIfNeeded = nil
serverFiles = nil
-- Connect to the WiFi access point. Once the device is connected,
-- you may start the HTTP server.
tmr.alarm(0, 3000, 1, function()
if wifi.sta.getip() == nil and joincounter < 5 then
print("Connecting to AP...")
joincounter = joincounter +1
else
tmr.stop(0)
-- print('IP: ',wifi.sta.getip())
-- Uncomment to automatically start the server in port 80
joincounter = nil
collectgarbage()
dofile("httpserver.lc")(80)
end
end)

View File

@ -1,32 +0,0 @@
######################################################################
# User configuration
######################################################################
# Path to nodemcu-uploader (https://github.com/kmpm/nodemcu-uploader)
NODEMCU-UPLOADER=../nodemcu-uploader/nodemcu-uploader.py
# Serial port
PORT=/dev/cu.usbserial-A602HRAZ
######################################################################
# End of user config
######################################################################
HTTP_FILES := $(wildcard http/*)
LUA_FILES := init.lua httpserver.lua httpserver-request.lua httpserver-static.lua httpserver-error.lua
# Print usage
usage:
@echo "make upload_http to upload files to be served"
@echo "make upload_server to upload the server code and init.lua"
@echo "make upload to upload all"
# Upload HTTP files only
upload_http: $(HTTP_FILES)
@$(NODEMCU-UPLOADER) -p $(PORT) upload $(foreach f, $^, -f $(f) -d $(f))
# Upload httpserver lua files (init and server module)
upload_server: $(LUA_FILES)
@$(NODEMCU-UPLOADER) -p $(PORT) upload $(foreach f, $^, -f $(f) -d $(f))
# Upload all
upload: $(LUA_FILES) $(HTTP_FILES)
@$(NODEMCU-UPLOADER) -p $(PORT) upload $(foreach f, $^, -f $(f) -d $(f))