Merge pull request #14 from pastukhov/master

Add ability to serve gz compressed files. It should end with .gz
This commit is contained in:
Marcos 2015-04-03 08:25:59 -05:00
commit 2162192d26
8 changed files with 102 additions and 47 deletions

View File

@ -1,34 +1,33 @@
######################################################################
# 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
# Bauds for the serial connection
SPEED=115200
######################################################################
# 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) -b $(SPEED) -p $(PORT) upload $(foreach f, $^, $(f))
# Upload httpserver lua files (init and server module)
upload_server: $(LUA_FILES)
@$(NODEMCU-UPLOADER) -b $(SPEED) -p $(PORT) upload $(foreach f, $^, $(f))
# Upload all
upload: $(LUA_FILES) $(HTTP_FILES)
@$(NODEMCU-UPLOADER) -b $(SPEED) -p $(PORT) upload $(foreach f, $^, $(f))
######################################################################
# User configuration
######################################################################
# Path to nodemcu-uploader (https://github.com/kmpm/nodemcu-uploader)
NODEMCU-UPLOADER=nodemcu-uploader.py
# Serial port
PORT=/dev/ttyUSB0
SPEED=460800
######################################################################
# 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) -b $(SPEED) -p $(PORT) upload $(foreach f, $^, $(f))
# Upload httpserver lua files (init and server module)
upload_server: $(LUA_FILES)
@$(NODEMCU-UPLOADER) -b $(SPEED) -p $(PORT) upload $(foreach f, $^, $(f))
# Upload all
upload: $(LUA_FILES) $(HTTP_FILES)
@$(NODEMCU-UPLOADER) -b $(SPEED) -p $(PORT) upload $(foreach f, $^, $(f))

BIN
http/jquery.min.js.gz Normal file

Binary file not shown.

16
http/jquerytest.html Normal file
View 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>

View File

@ -1,5 +1,6 @@
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
local function sendAttr(connection, attr, val)

View File

@ -24,6 +24,10 @@ end
local function parseUri(uri)
local r = {}
local filename
local ext
local fullExt = {}
if uri == nil then return r end
if uri == "/" then uri = "/index.html" end
questionMarkPos, b, c, d, e, f = uri:find("?")
@ -34,7 +38,12 @@ local function parseUri(uri)
r.file = uri:sub(1, questionMarkPos - 1)
r.args = parseArgs(uri:sub(questionMarkPos+1, #uri))
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.file = uriToFilename(r.file)
return r

View File

@ -3,13 +3,25 @@
-- Author: Marcos Kirsch
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.
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
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"}
-- 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
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
return function (connection, args)

View File

@ -73,7 +73,9 @@ return function (port)
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
end

View File

@ -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)
print('set mode=STATION (mode='..wifi.getmode()..')')
print('set (mode='..wifi.getmode()..')')
print('MAC: ',wifi.sta.getmac())
print('chip: ',node.chipid())
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.
-- This only happens the first time afer the .lua files are uploaded.
@ -12,6 +19,7 @@ wifi.sta.config("Internet","")
local compileAndRemoveIfNeeded = function(f)
if file.open(f) then
file.close()
print(f)
node.compile(f)
file.remove(f)
end
@ -25,14 +33,22 @@ serverFiles = nil
-- Connect to the WiFi access point. Once the device is connected,
-- you may start the HTTP server.
local joincounter = 0
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...")
else
joincounter = joincounter +1
else
tmr.stop(0)
print('IP: ',wifi.sta.getip())
-- print('IP: ',wifi.sta.getip())
-- Uncomment to automatically start the server in port 80
-- dofile("httpserver.lc")(80)
joincounter = nil
collectgarbage()
dofile("httpserver.lc")(80)
end
end)