1 -- Copyright 2008 Freifunk Leipzig / Jo-Philipp Wich <jow@openwrt.org>
2 -- Licensed to the public under the Apache License 2.0.
4 --- LuCI http protocol implementation - HTTP/1.1 bits.
5 -- This class provides basic ETag handling and implements most of the
6 -- conditional HTTP/1.1 headers specified in RFC2616 Sct. 14.24 - 14.28 .
7 module("luci.http.protocol.conditionals", package.seeall)
9 local date = require("luci.http.protocol.date")
12 --- Implement 14.19 / ETag.
13 -- @param stat A file.stat structure
14 -- @return String containing the generated tag suitable for ETag headers
15 function mk_etag( stat )
17 return string.format( '"%x-%x-%x"', stat.ino, stat.size, stat.mtime )
22 -- Test whether the given message object contains an "If-Match" header and
23 -- compare it against the given stat object.
24 -- @param req HTTP request message object
25 -- @param stat A file.stat object
26 -- @return Boolean indicating whether the precondition is ok
27 -- @return Alternative status code if the precondition failed
28 function if_match( req, stat )
30 local etag = mk_etag( stat )
32 -- Check for matching resource
33 if type(h['If-Match']) == "string" then
34 for ent in h['If-Match']:gmatch("([^, ]+)") do
35 if ( ent == '*' or ent == etag ) and stat ~= nil then
46 --- 14.25 / If-Modified-Since
47 -- Test whether the given message object contains an "If-Modified-Since" header
48 -- and compare it against the given stat object.
49 -- @param req HTTP request message object
50 -- @param stat A file.stat object
51 -- @return Boolean indicating whether the precondition is ok
52 -- @return Alternative status code if the precondition failed
53 -- @return Table containing extra HTTP headers if the precondition failed
54 function if_modified_since( req, stat )
58 if type(h['If-Modified-Since']) == "string" then
59 local since = date.to_unix( h['If-Modified-Since'] )
61 if stat == nil or since < stat.mtime then
66 ["ETag"] = mk_etag( stat );
67 ["Date"] = date.to_http( os.time() );
68 ["Last-Modified"] = date.to_http( stat.mtime )
75 --- 14.26 / If-None-Match
76 -- Test whether the given message object contains an "If-None-Match" header and
77 -- compare it against the given stat object.
78 -- @param req HTTP request message object
79 -- @param stat A file.stat object
80 -- @return Boolean indicating whether the precondition is ok
81 -- @return Alternative status code if the precondition failed
82 -- @return Table containing extra HTTP headers if the precondition failed
83 function if_none_match( req, stat )
85 local etag = mk_etag( stat )
86 local method = req.env and req.env.REQUEST_METHOD or "GET"
88 -- Check for matching resource
89 if type(h['If-None-Match']) == "string" then
90 for ent in h['If-None-Match']:gmatch("([^, ]+)") do
91 if ( ent == '*' or ent == etag ) and stat ~= nil then
92 if method == "GET" or method == "HEAD" then
95 ["Date"] = date.to_http( os.time() );
96 ["Last-Modified"] = date.to_http( stat.mtime )
109 -- The If-Range header is currently not implemented due to the lack of general
110 -- byte range stuff in luci.http.protocol . This function will always return
111 -- false, 412 to indicate a failed precondition.
112 -- @param req HTTP request message object
113 -- @param stat A file.stat object
114 -- @return Boolean indicating whether the precondition is ok
115 -- @return Alternative status code if the precondition failed
116 function if_range( req, stat )
117 -- Sorry, no subranges (yet)
121 --- 14.28 / If-Unmodified-Since
122 -- Test whether the given message object contains an "If-Unmodified-Since"
123 -- header and compare it against the given stat object.
124 -- @param req HTTP request message object
125 -- @param stat A file.stat object
126 -- @return Boolean indicating whether the precondition is ok
127 -- @return Alternative status code if the precondition failed
128 function if_unmodified_since( req, stat )
129 local h = req.headers
132 if type(h['If-Unmodified-Since']) == "string" then
133 local since = date.to_unix( h['If-Unmodified-Since'] )
135 if stat ~= nil and since <= stat.mtime then