* libs/http: fix header handling in conditionals.lua
[project/luci.git] / libs / http / luasrc / http / protocol / conditionals.lua
1 --[[
2
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>
5
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
9
10         http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13
14 ]]--
15
16 module("luci.http.protocol.conditionals", package.seeall)
17
18 local date = require("luci.http.protocol.date")
19
20
21 -- 14.19 / ETag
22 function mk_etag( stat )
23         if stat ~= nil then
24                 return string.format( '"%x-%x-%x"', stat.ino, stat.size, stat.mtime )
25         end
26 end
27
28 -- 14.24 / If-Match
29 function if_match( req, stat )
30         local h    = req.headers
31         local etag = mk_etag( stat )
32
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
37                                 return true
38                         end
39                 end
40
41                 return false, 412
42         end
43
44         return true
45 end
46
47 -- 14.25 / If-Modified-Since
48 function if_modified_since( req, stat )
49         local h = req.headers
50
51         -- Compare mtimes
52         if type(h['If-Modified-Since']) == "string" then
53                 local since = date.to_unix( h['If-Modified-Since'] )
54
55                 if stat == nil or since < stat.mtime then
56                         return true
57                 end
58
59                 return false, 304, {
60                         ["ETag"]          = mk_etag( stat );
61                         ["Last-Modified"] = date.to_http( stat.mtime )
62                 }
63         end
64
65         return true
66 end
67
68 -- 14.26 / If-None-Match
69 function if_none_match( req, stat )
70         local h    = req.headers
71         local etag = mk_etag( stat )
72
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"
79                                 then
80                                         return false, 304, {
81                                                 ["ETag"]          = mk_etag( stat );
82                                                 ["Last-Modified"] = date.to_http( stat.mtime )
83                                         }
84                                 else
85                                         return false, 412
86                                 end
87                         end
88                 end
89         end
90
91         return true
92 end
93
94 -- 14.27 / If-Range
95 function if_range( req, stat )
96         -- Sorry, no subranges (yet)
97         return false, 412
98 end
99
100 -- 14.28 / If-Unmodified-Since
101 function if_unmodified_since( req, stat )
102         local h = req.headers
103
104         -- Compare mtimes
105         if type(h['If-Unmodified-Since']) == "string" then
106                 local since = date.to_unix( h['If-Unmodified-Since'] )
107
108                 if stat ~= nil and since <= stat.mtime then
109                         return false, 412
110                 end
111         end
112
113         return true
114 end