Browse Source

some fixes

Artem Pastukhov 9 years ago
parent
commit
a37de51189
3 changed files with 34 additions and 11 deletions
  1. 14 1
      httpserver-request.lua
  2. 13 2
      httpserver-static.lua
  3. 7 8
      init.lua

+ 14 - 1
httpserver-request.lua

@@ -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,16 @@ 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.ext = r.file:match("(.+)%.(.+)")
+
+
    r.isScript = r.ext == "lua" or r.ext == "lc"
    r.file = uriToFilename(r.file)
    return r

+ 13 - 2
httpserver-static.lua

@@ -3,16 +3,27 @@
 -- 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
+   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)
+   --print(args.ext)
    sendHeader(connection, 200, "OK", getMimeType(args.ext))
    --print("Begin sending:", args.file)
    -- Send file in little chunks

+ 7 - 8
init.lua

@@ -5,18 +5,14 @@ print('set (mode='..wifi.getmode()..')')
 print('MAC: ',wifi.sta.getmac())
 print('chip: ',node.chipid())
 print('heap: ',node.heap())
-<<<<<<< HEAD
-
-local joincounter = 0
-
+
+wifi.sta.config("BT_WiFi", "PlacNinObOj9")
+
 cfg={}
 cfg.ssid="ESP-"..node.chipid()
 cfg.pwd="ESP-"..node.chipid()
 wifi.ap.config(cfg)
 cfg = nil
-=======
-wifi.sta.config("Kirsch Extreme","1151511515")
->>>>>>> parent of 4989410... Update init.lua
 
 -- Compile server code and remove original .lua files.
 -- This only happens the first time afer the .lua files are uploaded.
@@ -29,7 +25,7 @@ local compileAndRemoveIfNeeded = function(f)
    end
 end
 
-local serverFiles = {'httpserver.lua', 'httpserver-request.lua', 'httpserver-static.lua', 'httpserver-error.lua','http/ual.lua'}
+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
@@ -37,6 +33,9 @@ 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 and joincounter < 5 then