3 HTTP protocol implementation for LuCI - RFC2616 / 14.19, 14.24 - 14.28
4 (c) 2008 Freifunk Leipzig / Jo-Philipp Wich <xm@leipzig.freifunk.net>
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
16 module("luci.http.protocol.conditionals", package.seeall)
18 local date = require("luci.http.protocol.date")
22 function mk_etag( stat )
24 return string.format( '"%x-%x-%x"', stat.ino, stat.size, stat.mtime )
29 function if_match( req, stat )
31 local etag = mk_etag( stat )
33 -- Check for matching resource
34 if type(h['If-Match']) == "string" then
35 for ent in h['If-Match']:gmatch("([^, ]+)") do
36 if ( ent == '*' or ent == etag ) and stat ~= nil then
47 -- 14.25 / If-Modified-Since
48 function if_modified_since( req, stat )
52 if type(h['If-Modified-Since']) == "string" then
53 local since = date.to_unix( h['If-Modified-Since'] )
55 if stat == nil or since < stat.mtime then
60 ["ETag"] = mk_etag( stat );
61 ["Date"] = date.to_http( os.time() );
62 ["Last-Modified"] = date.to_http( stat.mtime )
69 -- 14.26 / If-None-Match
70 function if_none_match( req, stat )
72 local etag = mk_etag( stat )
74 -- Check for matching resource
75 if type(h['If-None-Match']) == "string" then
76 for ent in h['If-None-Match']:gmatch("([^, ]+)") do
77 if ( ent == '*' or ent == etag ) and stat ~= nil then
78 if req.request_method == "get" or
79 req.request_method == "head"
82 ["ETag"] = mk_etag( stat );
83 ["Date"] = date.to_http( os.time() );
84 ["Last-Modified"] = date.to_http( stat.mtime )
97 function if_range( req, stat )
98 -- Sorry, no subranges (yet)
102 -- 14.28 / If-Unmodified-Since
103 function if_unmodified_since( req, stat )
104 local h = req.headers
107 if type(h['If-Unmodified-Since']) == "string" then
108 local since = date.to_unix( h['If-Unmodified-Since'] )
110 if stat ~= nil and since <= stat.mtime then