Browse Source

Small wording improvement

Marcos Kirsch 9 years ago
parent
commit
453f38b52f
2 changed files with 39 additions and 1 deletions
  1. 3 1
      http/index.html
  2. 36 0
      httpserver-header.lua

+ 3 - 1
http/index.html

@@ -20,11 +20,13 @@
    <h3>Serve me some pages!</h3>
    <ul>
       <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="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>
-      <li><a href="foo.html">Foo</a>: A file that isn't actually in the server. Should error (404 error)</li>
+      <li><a href="foo.html">Foo</a>: A file that doesn't exist. Should error (404 error)</li>
    </ul>
 </body>
 </html>

+ 36 - 0
httpserver-header.lua

@@ -0,0 +1,36 @@
+-- httpserver-header.lua
+-- Part of nodemcu-httpserver, knows how to send an HTTP header.
+-- Author: Marcos Kirsch
+
+return function (connection, code, extension)
+
+   local function getHTTPStatusString(code)
+      if code == 200 then return "OK" end
+      if code == 404 then return "Not Found" end
+      if code == 400 then return "Bad Request" end
+      if code == 501 then return "Not Implemented" end
+      return "Unknown HTTP status"
+   end
+
+   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"}
+      -- 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 mimeType = getMimeType(extension)
+
+   connection:send("HTTP/1.0 " .. code .. " " .. getHTTPStatusString(code) .. "\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
+