* libs/http: fix header handling in conditionals.lua
* libs/httpd: add support for RFC2616 / 14.24 - 14.28 in file handler, add Date and Expires headers to luci handler
This commit is contained in:
parent
00aceaf624
commit
e08b97565f
4 changed files with 66 additions and 29 deletions
|
@ -779,11 +779,13 @@ end
|
||||||
-- Status codes
|
-- Status codes
|
||||||
statusmsg = {
|
statusmsg = {
|
||||||
[200] = "OK",
|
[200] = "OK",
|
||||||
|
[304] = "Not Modified",
|
||||||
[400] = "Bad Request",
|
[400] = "Bad Request",
|
||||||
[403] = "Forbidden",
|
[403] = "Forbidden",
|
||||||
[404] = "Not Found",
|
[404] = "Not Found",
|
||||||
[405] = "Method Not Allowed",
|
[405] = "Method Not Allowed",
|
||||||
[411] = "Length Required",
|
[411] = "Length Required",
|
||||||
|
[412] = "Precondition Failed",
|
||||||
[500] = "Internal Server Error",
|
[500] = "Internal Server Error",
|
||||||
[503] = "Server Unavailable",
|
[503] = "Server Unavailable",
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,7 +21,7 @@ local date = require("luci.http.protocol.date")
|
||||||
-- 14.19 / ETag
|
-- 14.19 / ETag
|
||||||
function mk_etag( stat )
|
function mk_etag( stat )
|
||||||
if stat ~= nil then
|
if stat ~= nil then
|
||||||
return string.format( "%x-%x-%x", stat.ino, stat.size, stat.mtime )
|
return string.format( '"%x-%x-%x"', stat.ino, stat.size, stat.mtime )
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -56,7 +56,10 @@ function if_modified_since( req, stat )
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
return false, 304
|
return false, 304, {
|
||||||
|
["ETag"] = mk_etag( stat );
|
||||||
|
["Last-Modified"] = date.to_http( stat.mtime )
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
return true
|
return true
|
||||||
|
@ -74,10 +77,10 @@ function if_none_match( req, stat )
|
||||||
if req.request_method == "get" or
|
if req.request_method == "get" or
|
||||||
req.request_method == "head"
|
req.request_method == "head"
|
||||||
then
|
then
|
||||||
h['ETag'] = mk_etag( stat )
|
return false, 304, {
|
||||||
h['Last-Modified'] = date.to_http( stat.mtime )
|
["ETag"] = mk_etag( stat );
|
||||||
|
["Last-Modified"] = date.to_http( stat.mtime )
|
||||||
return false, 304
|
}
|
||||||
else
|
else
|
||||||
return false, 412
|
return false, 412
|
||||||
end
|
end
|
||||||
|
|
|
@ -31,10 +31,11 @@ function Simple.__init__(self, docroot, dirlist)
|
||||||
self.dirlist = dirlist and true or false
|
self.dirlist = dirlist and true or false
|
||||||
self.mime = luci.http.protocol.mime
|
self.mime = luci.http.protocol.mime
|
||||||
self.date = luci.http.protocol.date
|
self.date = luci.http.protocol.date
|
||||||
|
self.cond = luci.http.protocol.conditionals
|
||||||
end
|
end
|
||||||
|
|
||||||
function Simple.getfile(self, uri)
|
function Simple.getfile(self, uri)
|
||||||
local file = self.docroot .. uri:gsub("%.%./", "")
|
local file = self.docroot .. uri:gsub("%.%./+", "")
|
||||||
local stat = luci.fs.stat(file)
|
local stat = luci.fs.stat(file)
|
||||||
|
|
||||||
return file, stat
|
return file, stat
|
||||||
|
@ -47,8 +48,19 @@ function Simple.handle_get(self, request, sourcein, sinkerr)
|
||||||
if stat.type == "regular" then
|
if stat.type == "regular" then
|
||||||
|
|
||||||
-- Generate Entity Tag
|
-- Generate Entity Tag
|
||||||
local etag = luci.http.protocol.conditionals.mk_etag( stat )
|
local etag = self.cond.mk_etag( stat )
|
||||||
|
|
||||||
|
-- Check conditionals
|
||||||
|
local ok, code, hdrs
|
||||||
|
|
||||||
|
ok, code, hdrs = self.cond.if_modified_since( request, stat )
|
||||||
|
if ok then
|
||||||
|
ok, code, hdrs = self.cond.if_match( request, stat )
|
||||||
|
if ok then
|
||||||
|
ok, code, hdrs = self.cond.if_unmodified_since( request, stat )
|
||||||
|
if ok then
|
||||||
|
ok, code, hdrs = self.cond.if_none_match( request, stat )
|
||||||
|
if ok then
|
||||||
-- Send Response
|
-- Send Response
|
||||||
return Response(
|
return Response(
|
||||||
200, {
|
200, {
|
||||||
|
@ -59,6 +71,22 @@ function Simple.handle_get(self, request, sourcein, sinkerr)
|
||||||
["ETag"] = etag;
|
["ETag"] = etag;
|
||||||
}
|
}
|
||||||
), ltn12.source.file(io.open(file))
|
), ltn12.source.file(io.open(file))
|
||||||
|
else
|
||||||
|
return Response( code, hdrs or { } ),
|
||||||
|
ltn12.source.empty()
|
||||||
|
end
|
||||||
|
else
|
||||||
|
return Response( code, hdrs or { } ),
|
||||||
|
ltn12.source.empty()
|
||||||
|
end
|
||||||
|
else
|
||||||
|
return Response( code, hdrs or { } ),
|
||||||
|
ltn12.source.empty()
|
||||||
|
end
|
||||||
|
else
|
||||||
|
return Response( code, hdrs or { } ),
|
||||||
|
ltn12.source.empty()
|
||||||
|
end
|
||||||
else
|
else
|
||||||
return self:failure(403, "Unable to transmit " .. stat.type .. " " .. file)
|
return self:failure(403, "Unable to transmit " .. stat.type .. " " .. file)
|
||||||
end
|
end
|
||||||
|
|
|
@ -17,6 +17,7 @@ module("luci.httpd.handler.luci", package.seeall)
|
||||||
|
|
||||||
require("luci.dispatcher")
|
require("luci.dispatcher")
|
||||||
require("luci.http")
|
require("luci.http")
|
||||||
|
require("luci.http.protocol.date")
|
||||||
require("ltn12")
|
require("ltn12")
|
||||||
|
|
||||||
Luci = luci.util.class(luci.httpd.module.Handler)
|
Luci = luci.util.class(luci.httpd.module.Handler)
|
||||||
|
@ -79,5 +80,8 @@ function Luci.handle_get(self, request, sourcein, sinkerr)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
headers["Expires"] = luci.http.protocol.date.to_http( os.time() )
|
||||||
|
headers["Date"] = headers["Expires"]
|
||||||
|
|
||||||
return Response(status, headers), iter
|
return Response(status, headers), iter
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue