Merge pull request #14 from pastukhov/master
Add ability to serve gz compressed files. It should end with .gz
This commit is contained in:
commit
2162192d26
@ -2,11 +2,10 @@
|
|||||||
# User configuration
|
# User configuration
|
||||||
######################################################################
|
######################################################################
|
||||||
# Path to nodemcu-uploader (https://github.com/kmpm/nodemcu-uploader)
|
# Path to nodemcu-uploader (https://github.com/kmpm/nodemcu-uploader)
|
||||||
NODEMCU-UPLOADER=../nodemcu-uploader/nodemcu-uploader.py
|
NODEMCU-UPLOADER=nodemcu-uploader.py
|
||||||
# Serial port
|
# Serial port
|
||||||
PORT=/dev/cu.usbserial-A602HRAZ
|
PORT=/dev/ttyUSB0
|
||||||
# Bauds for the serial connection
|
SPEED=460800
|
||||||
SPEED=115200
|
|
||||||
|
|
||||||
######################################################################
|
######################################################################
|
||||||
# End of user config
|
# End of user config
|
||||||
BIN
http/jquery.min.js.gz
Normal file
BIN
http/jquery.min.js.gz
Normal file
Binary file not shown.
16
http/jquerytest.html
Normal file
16
http/jquerytest.html
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||||
|
<head>
|
||||||
|
<script type="text/javascript" src="/jquery.min.js.gz"></script>
|
||||||
|
<script type="text/javascript">
|
||||||
|
window.onload = function()
|
||||||
|
{
|
||||||
|
if (jQuery) { alert("Jquery loaded");}
|
||||||
|
else { alert("Jquery not loaded");}
|
||||||
|
};
|
||||||
|
|
||||||
|
</script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Jquery test page</h1>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@ -1,5 +1,6 @@
|
|||||||
local function sendHeader(connection)
|
local function sendHeader(connection)
|
||||||
connection:send("HTTP/1.0 200 OK\r\nContent-Type: text/html\rCache-Control: private, no-store\r\n\r\n")
|
connection:send("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nCache-Control: private, no-store\r\n\r\n")
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function sendAttr(connection, attr, val)
|
local function sendAttr(connection, attr, val)
|
||||||
|
|||||||
@ -24,6 +24,10 @@ end
|
|||||||
|
|
||||||
local function parseUri(uri)
|
local function parseUri(uri)
|
||||||
local r = {}
|
local r = {}
|
||||||
|
local filename
|
||||||
|
local ext
|
||||||
|
local fullExt = {}
|
||||||
|
|
||||||
if uri == nil then return r end
|
if uri == nil then return r end
|
||||||
if uri == "/" then uri = "/index.html" end
|
if uri == "/" then uri = "/index.html" end
|
||||||
questionMarkPos, b, c, d, e, f = uri:find("?")
|
questionMarkPos, b, c, d, e, f = uri:find("?")
|
||||||
@ -34,7 +38,12 @@ local function parseUri(uri)
|
|||||||
r.file = uri:sub(1, questionMarkPos - 1)
|
r.file = uri:sub(1, questionMarkPos - 1)
|
||||||
r.args = parseArgs(uri:sub(questionMarkPos+1, #uri))
|
r.args = parseArgs(uri:sub(questionMarkPos+1, #uri))
|
||||||
end
|
end
|
||||||
_, r.ext = r.file:match("(.+)%.(.+)")
|
filename = r.file
|
||||||
|
while filename:match("%.") do
|
||||||
|
filename,ext = filename:match("(.+)%.(.+)")
|
||||||
|
table.insert(fullExt,1,ext)
|
||||||
|
end
|
||||||
|
r.ext = table.concat(fullExt,".")
|
||||||
r.isScript = r.ext == "lua" or r.ext == "lc"
|
r.isScript = r.ext == "lua" or r.ext == "lc"
|
||||||
r.file = uriToFilename(r.file)
|
r.file = uriToFilename(r.file)
|
||||||
return r
|
return r
|
||||||
|
|||||||
@ -3,13 +3,25 @@
|
|||||||
-- Author: Marcos Kirsch
|
-- Author: Marcos Kirsch
|
||||||
|
|
||||||
local function getMimeType(ext)
|
local function getMimeType(ext)
|
||||||
|
local gzip = false
|
||||||
-- A few MIME types. Keep list short. If you need something that is missing, let's add it.
|
-- 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", json = "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
|
-- add comressed flag if file ends with gz
|
||||||
|
if ext:find("%.gz$") then
|
||||||
|
ext = ext:sub(1, -4)
|
||||||
|
gzip = true
|
||||||
|
end
|
||||||
|
if mt[ext] then contentType = mt[ext] else contentType = "text/plain" end
|
||||||
|
return {contentType = contentType, gzip = gzip }
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function sendHeader(connection, code, codeString, mimeType)
|
local function sendHeader(connection, code, codeString, mimeType)
|
||||||
connection:send("HTTP/1.0 " .. code .. " " .. codeString .. "\r\nServer: nodemcu-httpserver\r\nContent-Type: " .. mimeType .. "\r\nConnection: close\r\n\r\n")
|
connection:send("HTTP/1.0 " .. code .. " " .. codeString .. "\r\nServer: nodemcu-httpserver\r\nContent-Type: " .. mimeType["contentType"] .. "\r\n")
|
||||||
|
if mimeType["gzip"] then
|
||||||
|
connection:send("Content-Encoding: gzip\r\n")
|
||||||
|
end
|
||||||
|
connection:send("Connection: close\r\n\r\n")
|
||||||
end
|
end
|
||||||
|
|
||||||
return function (connection, args)
|
return function (connection, args)
|
||||||
|
|||||||
@ -73,7 +73,9 @@ return function (port)
|
|||||||
|
|
||||||
end
|
end
|
||||||
)
|
)
|
||||||
print("nodemcu-httpserver running at http://" .. wifi.sta.getip() .. ":" .. port)
|
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
|
return s
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
28
init.lua
28
init.lua
@ -1,10 +1,17 @@
|
|||||||
-- Tell the chip to connect to the access point
|
-- Tel--l the chip to connect to the access point
|
||||||
|
--wifi.setmode(wifi.STATIONAP)
|
||||||
wifi.setmode(wifi.STATION)
|
wifi.setmode(wifi.STATION)
|
||||||
print('set mode=STATION (mode='..wifi.getmode()..')')
|
print('set (mode='..wifi.getmode()..')')
|
||||||
print('MAC: ',wifi.sta.getmac())
|
print('MAC: ',wifi.sta.getmac())
|
||||||
print('chip: ',node.chipid())
|
print('chip: ',node.chipid())
|
||||||
print('heap: ',node.heap())
|
print('heap: ',node.heap())
|
||||||
wifi.sta.config("Internet","")
|
|
||||||
|
|
||||||
|
local 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.
|
-- Compile server code and remove original .lua files.
|
||||||
-- This only happens the first time afer the .lua files are uploaded.
|
-- This only happens the first time afer the .lua files are uploaded.
|
||||||
@ -12,6 +19,7 @@ wifi.sta.config("Internet","")
|
|||||||
local compileAndRemoveIfNeeded = function(f)
|
local compileAndRemoveIfNeeded = function(f)
|
||||||
if file.open(f) then
|
if file.open(f) then
|
||||||
file.close()
|
file.close()
|
||||||
|
print(f)
|
||||||
node.compile(f)
|
node.compile(f)
|
||||||
file.remove(f)
|
file.remove(f)
|
||||||
end
|
end
|
||||||
@ -25,14 +33,22 @@ serverFiles = nil
|
|||||||
|
|
||||||
-- Connect to the WiFi access point. Once the device is connected,
|
-- Connect to the WiFi access point. Once the device is connected,
|
||||||
-- you may start the HTTP server.
|
-- you may start the HTTP server.
|
||||||
|
|
||||||
|
local joincounter = 0
|
||||||
|
|
||||||
tmr.alarm(0, 3000, 1, function()
|
tmr.alarm(0, 3000, 1, function()
|
||||||
if wifi.sta.getip() == nil then
|
|
||||||
|
if wifi.sta.getip() == nil and joincounter < 5 then
|
||||||
print("Connecting to AP...")
|
print("Connecting to AP...")
|
||||||
|
joincounter = joincounter +1
|
||||||
else
|
else
|
||||||
tmr.stop(0)
|
tmr.stop(0)
|
||||||
print('IP: ',wifi.sta.getip())
|
-- print('IP: ',wifi.sta.getip())
|
||||||
-- Uncomment to automatically start the server in port 80
|
-- Uncomment to automatically start the server in port 80
|
||||||
-- dofile("httpserver.lc")(80)
|
joincounter = nil
|
||||||
|
collectgarbage()
|
||||||
|
dofile("httpserver.lc")(80)
|
||||||
end
|
end
|
||||||
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user