webmcp
diff libraries/rocketcgi/rocketcgi.lua @ 16:944642a3e488
New set_cookie functions; Added inline documentation; Make set_allowed_json_request_slots work in interactive shell
| author | jbe/bsw |
|---|---|
| date | Thu Mar 25 17:37:03 2010 +0100 (2010-03-25) |
| parents | d76a8857ba62 |
| children | 486ad118c277 |
line diff
1.1 --- a/libraries/rocketcgi/rocketcgi.lua Sat Feb 20 21:01:57 2010 +0100 1.2 +++ b/libraries/rocketcgi/rocketcgi.lua Thu Mar 25 17:37:03 2010 +0100 1.3 @@ -32,6 +32,16 @@ 1.4 1.5 data_sent = false 1.6 1.7 +--[[-- 1.8 +rocketcgi.add_header( 1.9 + string_part1, -- string 1.10 + string_part2, -- optional second part of string to be concatted 1.11 + ... 1.12 +) 1.13 + 1.14 +Sends a header line to the browser. Multiple arguments are concatted to form a single string. 1.15 + 1.16 +--]]-- 1.17 function add_header(...) 1.18 if data_sent then 1.19 error("Can not add header after data has been sent.", 2) 1.20 @@ -39,7 +49,18 @@ 1.21 io.stdout:write(...) 1.22 io.stdout:write("\r\n") 1.23 end 1.24 +--//-- 1.25 1.26 +--[[-- 1.27 +rocketcgi.send_data( 1.28 + string_part1, -- string 1.29 + string_part2, -- optional second part of string to be concatted 1.30 + ... 1.31 +) 1.32 + 1.33 +Sends document data to the browser. Multiple arguments are concatted to form a single string. 1.34 + 1.35 +--]]-- 1.36 function send_data(...) 1.37 if not data_sent then 1.38 io.stdout:write("\r\n") 1.39 @@ -47,19 +68,84 @@ 1.40 end 1.41 io.stdout:write(...) 1.42 end 1.43 +--//-- 1.44 1.45 +--[[-- 1.46 +rocketcgi.set_status( 1.47 + status -- Status code and description, e.g. "404 Not Found" 1.48 +) 1.49 + 1.50 +Sends a header line to the browser, indicating a given HTTP status. 1.51 + 1.52 +--]]-- 1.53 function set_status(status) 1.54 add_header("Status: ", status) 1.55 end 1.56 +--//-- 1.57 1.58 +--[[-- 1.59 +rocketcgi.redirect( 1.60 + status -- Absolute URL to redirect the browser to 1.61 +) 1.62 + 1.63 +Redirects the browser to the given absolute URL, using a 303 Redirect. 1.64 + 1.65 +--]]-- 1.66 function redirect(location) 1.67 set_status("303 See Other") 1.68 add_header("Location: ", location) 1.69 end 1.70 +--//-- 1.71 1.72 +--[[-- 1.73 +rocketcgi.set_status( 1.74 + status -- Status code and description, e.g. "404 Not Found" 1.75 +) 1.76 + 1.77 +Sends a header line specifying the content-type to the browser. 1.78 + 1.79 +--]]-- 1.80 function set_content_type(content_type) 1.81 add_header("Content-Type: ", content_type) 1.82 end 1.83 +--//-- 1.84 + 1.85 +--[[-- 1.86 +rocketcgi.set_cookie{ 1.87 + name = name, -- name of cookie 1.88 + value = value, -- value of cookie 1.89 + domain = domain, -- domain where cookie is transmitted 1.90 + path = path, -- path where cookie is transmitted 1.91 + secure = secure -- boolean, indicating if cookie should only be transmitted over HTTPS 1.92 +} 1.93 + 1.94 +Sends a header line setting a cookie. NOTE: Currently only session cookies are supported. 1.95 + 1.96 +--]]-- 1.97 +function set_cookie(args) 1.98 + assert(string.find(args.name, "^[0-9A-Za-z%%._~-]+$"), "Illegal cookie name") 1.99 + assert(string.find(args.value, "^[0-9A-Za-z%%._~-]+$"), "Illegal cookie value") 1.100 + local parts = {"Set-Cookie: " .. args.name .. "=" .. args.value} 1.101 + if args.domain then 1.102 + assert( 1.103 + string.find(args.path, "^[0-9A-Za-z%%/._~-]+$"), 1.104 + "Illegal cookie domain" 1.105 + ) 1.106 + parts[#parts+1] = "domain=" .. args.domain 1.107 + end 1.108 + if args.path then 1.109 + assert( 1.110 + string.find(args.path, "^[0-9A-Za-z%%/._~-]+$"), 1.111 + "Illegal cookie path" 1.112 + ) 1.113 + parts[#parts+1] = "path=" .. args.path 1.114 + end 1.115 + if args.secure then 1.116 + parts[#parts+1] = "secure" 1.117 + end 1.118 + add_header(table.concat(parts, "; ")) 1.119 +end 1.120 +--//-- 1.121 1.122 method = os.getenv("REQUEST_METHOD") or false 1.123 query = os.getenv("QUERY_STRING") or false