comparison http/src/luan/modules/http/Http.luan @ 503:92c3d22745b8

make _ENV optional
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 20 May 2015 23:24:46 -0600
parents ab9c2afefb47
children dbdf4b8193a8
comparison
equal deleted inserted replaced
502:d3183a330ff5 503:92c3d22745b8
7 local Html = require "luan:Html" 7 local Html = require "luan:Html"
8 local url_encode = Html.url_encode 8 local url_encode = Html.url_encode
9 local HttpServicer = require "java:luan.modules.http.HttpServicer" 9 local HttpServicer = require "java:luan.modules.http.HttpServicer"
10 local IoLuan = require "java:luan.modules.IoLuan" 10 local IoLuan = require "java:luan.modules.IoLuan"
11 11
12 local M = {}
12 13
13 local singular_metatable = {} 14 local singular_metatable = {}
14 15
15 function singular_metatable.__index(table,key) 16 function singular_metatable.__index(table,key)
16 local list = table.__plural[key] 17 local list = table.__plural[key]
37 set_metatable(this.header,singular_metatable) 38 set_metatable(this.header,singular_metatable)
38 return this 39 return this
39 end 40 end
40 41
41 42
42 function new_request(this) 43 function M.new_request(this)
43 this = new_common(this) 44 this = new_common(this)
44 this.method = "GET" -- default 45 this.method = "GET" -- default
45 -- this.path 46 -- this.path
46 -- this.protocol 47 -- this.protocol
47 this.scheme = "http" -- default 48 this.scheme = "http" -- default
77 end 78 end
78 79
79 return this 80 return this
80 end 81 end
81 82
82 STATUS = { 83 local STATUS = {
83 OK = 200; 84 OK = 200;
84 -- add more as needed 85 -- add more as needed
85 } 86 }
87 M.STATUS = STATUS
86 88
87 function new_response(this) 89 function M.new_response(this)
88 this = new_common(this) 90 this = new_common(this)
89 this.status = STATUS.OK 91 this.status = STATUS.OK
90 if this.java ~= nil then 92 if this.java ~= nil then
91 this.send_redirect = this.java.sendRedirect 93 this.send_redirect = this.java.sendRedirect
92 this.send_error = this.java.sendError 94 this.send_error = this.java.sendError
93 95
94 function this.set_cookie(name,value,is_persistent,domain) 96 function this.set_cookie(name,value,is_persistent,domain)
95 HttpServicer.setCookie(this.java,response.java,name,value,is_persistent,domain) 97 HttpServicer.setCookie(this.java,M.response.java,name,value,is_persistent,domain)
96 end 98 end
97 99
98 function this.remove_cookie(name,domain) 100 function this.remove_cookie(name,domain)
99 HttpServicer.removeCookie(this.java,response.java,name,domain) 101 HttpServicer.removeCookie(this.java,M.response.java,name,domain)
100 end 102 end
101 103
102 function this.text_writer() 104 function this.text_writer()
103 return IoLuan.textWriter(this.java.getWriter()) 105 return IoLuan.textWriter(this.java.getWriter())
104 end 106 end
111 end 113 end
112 114
113 -- request = new_request{} -- filled in by HttpServicer 115 -- request = new_request{} -- filled in by HttpServicer
114 -- response = new_response{} -- filled in by HttpServicer 116 -- response = new_response{} -- filled in by HttpServicer
115 117
116 118 return M
117
118 function init_for_test()
119
120 test = test or {}
121
122 test.welcome_file = "index.html"
123
124 function get_page(path)
125 if test.welcome_file ~= nil and path.matches ".*/" then
126 path = path .. test.welcome_file
127 end
128 local old_out = Io.stdout
129 local mod = require("site:"..path)
130 mod.respond()
131 test.text_writer.close()
132 Io.stdout = old_out
133 return result.read_text()
134 end
135
136 test.cookies = test.cookies or {}
137
138 request = new_request{}
139 request.cookies = test.cookies
140
141 response = new_response{
142
143 text_writer = function()
144 result = Io.uri "string:"
145 test.text_writer = result.text_writer()
146 return test.text_writer
147 end;
148
149 set_cookie = function(name,value)
150 test.cookies[name] = value
151 end;
152
153 remove_cookie = function(name)
154 test.cookies[name] = nil
155 end;
156
157 send_redirect = function(url)
158 response.redirect = url
159 end;
160
161 }
162
163 end