Merged with main repository

This commit is contained in:
Marcos Kirsch
2016-02-15 20:54:11 -06:00
13 changed files with 334 additions and 166 deletions

View File

@@ -1,25 +1,26 @@
return function (connection, args)
dofile("httpserver-header.lc")(connection, 200, 'html')
connection:send([===[
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>Arguments</title></head><body><h1>Arguments</h1>
<form method="GET">
First name:<br><input type="text" name="firstName"><br>
Last name:<br><input type="text" name="lastName"><br>
<input type="radio" name="sex" value="male" checked>Male<input type="radio" name="sex" value="female">Female<br>
<input type="submit" name="submit" value="Submit">
</form>
]===])
coroutine.yield()
if args["submit"] ~= nil then
connection:send("<h2>Received the following values:</h2><ul>")
coroutine.yield()
for name, value in pairs(args) do
connection:send('<li><b>' .. name .. ':</b> ' .. tostring(value) .. "<br></li>\n")
coroutine.yield()
end
end
connection:send("</ul>\n</body></html>")
end
return function (connection, args)
dofile("httpserver-header.lc")(connection, 200, 'html')
connection:send([===[
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>Arguments</title></head><body><h1>Arguments</h1>
<form method="GET">
First name:<br><input type="text" name="firstName"><br>
Last name:<br><input type="text" name="lastName"><br>
<input type="radio" name="sex" value="male" checked>Male<input type="radio" name="sex" value="female">Female<br>
<input type="submit" name="submit" value="Submit">
</form>
]===])
coroutine.yield()
if args["submit"] ~= nil then
connection:send("<h2>Received the following values:</h2><ul>")
coroutine.yield()
for name, value in pairs(args) do
connection:send('<li><b>' .. name .. ':</b> ' .. tostring(value) .. "<br></li>\n")
coroutine.yield()
end
end
connection:send("</ul>\n</body></html>")
end

View File

@@ -1,27 +1,28 @@
return function (connection, args)
dofile("httpserver-header.lc")(connection, 200, 'html')
connection:send([===[
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>Server File Listing</title></head>
<body>
<h1>Server File Listing</h1>
]===])
coroutine.yield()
local remaining, used, total=file.fsinfo()
connection:send("<b>Total size: </b> " .. total .. " bytes<br/>\n" ..
"<b>In Use: </b> " .. used .. " bytes<br/>\n" ..
"<b>Free: </b> " .. remaining .. " bytes<br/>\n" ..
"<p>\n<b>Files:</b><br/>\n<ul>\n")
coroutine.yield()
for name, size in pairs(file.list()) do
local isHttpFile = string.match(name, "(http/)") ~= nil
if isHttpFile then
local url = string.match(name, ".*/(.*)")
connection:send(' <li><a href="' .. url .. '">' .. url .. "</a> (" .. size .. " bytes)</li>\n")
coroutine.yield()
end
end
connection:send("</ul>\n</p>\n</body></html>")
end
return function (connection, args)
dofile("httpserver-header.lc")(connection, 200, 'html')
connection:send([===[
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>Server File Listing</title></head>
<body>
<h1>Server File Listing</h1>
]===])
coroutine.yield()
local remaining, used, total=file.fsinfo()
connection:send("<b>Total size: </b> " .. total .. " bytes<br/>\n" ..
"<b>In Use: </b> " .. used .. " bytes<br/>\n" ..
"<b>Free: </b> " .. remaining .. " bytes<br/>\n" ..
"<p>\n<b>Files:</b><br/>\n<ul>\n")
coroutine.yield()
for name, size in pairs(file.list()) do
local isHttpFile = string.match(name, "(http/)") ~= nil
if isHttpFile then
local url = string.match(name, ".*/(.*)")
connection:send(' <li><a href="' .. url .. '">' .. url .. "</a> (" .. size .. " bytes)</li>\n")
coroutine.yield()
end
end
connection:send("</ul>\n</p>\n</body></html>")
end

View File

@@ -22,7 +22,7 @@ local function pushTheButton(connection, pin)
end
return function (connection, args)
return function (connection, req, args)
print('Garage door button was pressed!', args.door)
if args.door == "1" then pushTheButton(connection, 1) -- GPIO1
elseif args.door == "2" then pushTheButton(connection, 2) -- GPIO2

View File

@@ -22,7 +22,7 @@
<li><a href="index.html">Index</a>: This page (static)</li>
<li><a href="zipped.html.gz">Zipped</a>: A compressed file (static)</li>
<li><a href="args.lua">Arguments</a>: Parses arguments passed in the URL and prints them. (Lua)</li>
<li><a href="post.html">Post</a>: A form that uses POST method, should error. (static)</li>
<li><a href="post.lua">Post</a>: A form that uses POST method. Displays different content based on HTTP method. (Lua)</li>
<li><a href="garage_door_opener.html">Garage door opener</a>: Control GPIO lines via the server. (Lua)</li>
<li><a href="node_info.lua">NodeMCU info</a>: Shows some basic NodeMCU(Lua)</li>
<li><a href="file_list.lua">List all server files</a>: Displays a list of all the server files. (Lua)</li>

View File

@@ -1,7 +0,0 @@
<!DOCTYPE html>
<html lang="en"><head><meta charset="utf-8"><title>Post</title></head><body>
<h1>Post</h1>
This form uses POST method which is not supported by nodemcu-httpserver.<br>
You should get an error when you press submit.
<form method="POST"><input type="submit" value="Submit"></form>
</body></html>

33
http/post.lua Normal file
View File

@@ -0,0 +1,33 @@
return function (connection, req, args)
connection:send("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nCache-Control: private, no-store\r\n\r\n")
connection:send('<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>Arguments</title></head>')
connection:send('<body>')
connection:send('<h1>Arguments</h1>')
local form = [===[
<form method="POST">
First name:<br><input type="text" name="firstName"><br>
Last name:<br><input type="text" name="lastName"><br>
<input type="radio" name="sex" value="male" checked>Male<input type="radio" name="sex" value="female">Female<br>
<input type="submit" value="Submit">
</form>
]===]
if req.method == "GET" then
connection:send(form)
elseif req.method == "POST" then
local rd = req.getRequestData()
-- connection:send(cjson.encode(rd))
connection:send('<h2>Received the following values:</h2>')
connection:send("<ul>\n")
for name, value in pairs(rd) do
connection:send('<li><b>' .. name .. ':</b> ' .. tostring(value) .. "<br></li>\n")
end
connection:send("</ul>\n")
else
connection:send("NOT IMPLEMENTED")
end
connection:send('</body></html>')
end