forked from marcoskirsch/nodemcu-httpserver
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpserver-header.lua
More file actions
Latest commit
31 lines (25 loc) · 1.4 KB
/
Copy pathhttpserver-header.lua
File metadata and controls
31 lines (25 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
-- httpserver-header.lua
-- Part of nodemcu-httpserver, knows how to send an HTTP header.
-- Author: Marcos Kirsch
returnfunction (connection, code, extension, gzip)
localfunctiongetHTTPStatusString(code)
localcodez= {[200]="OK", [400]="Bad Request", [404]="Not Found",}
localmyResult=codez[code]
-- enforce returning valid http codes all the way throughout?
ifmyResultthenreturnmyResultelsereturn"Not Implemented" end
end
localfunctiongetMimeType(ext)
localgzip=false
-- A few MIME types. Keep list short. If you need something that is missing, let's add it.
localmt= {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", xml="text/xml"}
ifmt[ext] thencontentType=mt[ext] elsecontentType="text/plain" end
return {contentType=contentType, gzip=gzip}
end
localmimeType=getMimeType(extension)
connection:send("HTTP/1.0 " ..code.."" ..getHTTPStatusString(code) .."\r\nServer: nodemcu-httpserver\r\nContent-Type: " ..mimeType["contentType"] .."\r\n")
ifgzipthen
connection:send("Cache-Control: max-age=2592000\r\n")
connection:send("Content-Encoding: gzip\r\n")
end
connection:send("Connection: close\r\n\r\n")
end