summaryrefslogtreecommitdiffhomepage
path: root/libs/http/luasrc
diff options
context:
space:
mode:
authorJo-Philipp Wich <jow@openwrt.org>2008-06-26 18:31:25 +0000
committerJo-Philipp Wich <jow@openwrt.org>2008-06-26 18:31:25 +0000
commit98b954a48f2dd11f48cbc92652de20b0fa5d460b (patch)
treee8e095177136e0b3363209e45422cff442c92628 /libs/http/luasrc
parent85ec6b0a753616726077642c5e9a8f3aca69f7b7 (diff)
* libs/http: added http mime helper lib
Diffstat (limited to 'libs/http/luasrc')
-rw-r--r--libs/http/luasrc/http/protocol/date.lua2
-rw-r--r--libs/http/luasrc/http/protocol/mime.lua60
2 files changed, 61 insertions, 1 deletions
diff --git a/libs/http/luasrc/http/protocol/date.lua b/libs/http/luasrc/http/protocol/date.lua
index b3b993866..b9b55cc33 100644
--- a/libs/http/luasrc/http/protocol/date.lua
+++ b/libs/http/luasrc/http/protocol/date.lua
@@ -192,7 +192,7 @@ function to_http(time)
return os.date( "%a, %d %b %Y %H:%M:%S GMT", time )
end
--- Compare to dates
+-- Compare two dates
function compare(d1, d2)
if d1:match("[^0-9]") then d1 = to_unix(d1) end
diff --git a/libs/http/luasrc/http/protocol/mime.lua b/libs/http/luasrc/http/protocol/mime.lua
new file mode 100644
index 000000000..d85b9025e
--- /dev/null
+++ b/libs/http/luasrc/http/protocol/mime.lua
@@ -0,0 +1,60 @@
+--[[
+
+HTTP protocol implementation for LuCI - mime handling
+(c) 2008 Freifunk Leipzig / Jo-Philipp Wich <xm@leipzig.freifunk.net>
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+$Id$
+
+]]--
+
+module("luci.http.protocol.mime", package.seeall)
+
+-- MIME mapping
+MIME_TYPES = {
+ ["txt"] = "text/plain";
+ ["js"] = "text/javascript";
+ ["css"] = "text/css";
+ ["htm"] = "text/html";
+ ["html"] = "text/html";
+
+ ["gif"] = "image/gif";
+ ["png"] = "image/png";
+ ["jpg"] = "image/jpeg";
+ ["jpeg"] = "image/jpeg";
+
+ ["xml"] = "application/xml";
+}
+
+-- extract extension from a filename and return corresponding mime-type or
+-- "application/octet-stream" if the extension is unknown
+function to_mime(filename)
+ if type(filename) == "string" then
+ local ext = filename:match("[^%.]+$")
+
+ if ext and MIME_TYPES[ext:lower()] then
+ return MIME_TYPES[ext:lower()]
+ end
+ end
+
+ return "application/octet-stream"
+end
+
+-- return corresponding extension for a given mime type or nil if the
+-- given mime-type is unknown
+function to_ext(mimetype)
+ if type(mimetype) == "string" then
+ for ext, type in luci.util.kspairs( MIME_TYPES ) do
+ if type == mimetype then
+ return ext
+ end
+ end
+ end
+
+ return nil
+end