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 ["Last-Modified"] = date.to_http( stat.mtime )
68 -- 14.26 / If-None-Match
69 function if_none_match( req, stat )
71 local etag = mk_etag( stat )
73 -- Check for matching resource
74 if type(h['If-None-Match']) == "string" then
75 for ent in h['If-None-Match']:gmatch("([^, ]+)") do
76 if ( ent == '*' or ent == etag ) and stat ~= nil then
77 if req.request_method == "get" or
78 req.request_method == "head"
81 ["ETag"] = mk_etag( stat );
82 ["Last-Modified"] = date.to_http( stat.mtime )
95 function if_range( req, stat )
96 -- Sorry, no subranges (yet)
100 -- 14.28 / If-Unmodified-Since
101 function if_unmodified_since( req, stat )
102 local h = req.headers
105 if type(h['If-Unmodified-Since']) == "string" then
106 local since = date.to_unix( h['If-Unmodified-Since'] )
108 if stat ~= nil and since <= stat.mtime then