Mercurial Hosting > luan
annotate src/luan/modules/http/Http.luan @ 1618:a37ffe2d1b14
fix not_found_handler
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 14 Jun 2021 18:45:17 -0600 |
parents | fa066aaa068c |
children | 224af797b1f9 |
rev | line source |
---|---|
1353
8d95711f6615
replace java() with require "java"
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
1 require "java" |
693
ca169567ce07
module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
629
diff
changeset
|
2 local Luan = require "luan:Luan.luan" |
508 | 3 local error = Luan.error |
572
f1601a4ce1aa
fix stack when calling meta-methods
Franklin Schmidt <fschmidt@gmail.com>
parents:
568
diff
changeset
|
4 local ipairs = Luan.ipairs or error() |
f1601a4ce1aa
fix stack when calling meta-methods
Franklin Schmidt <fschmidt@gmail.com>
parents:
568
diff
changeset
|
5 local pairs = Luan.pairs or error() |
1152
21d157b153fe
change http parameters interface
Franklin Schmidt <fschmidt@gmail.com>
parents:
1150
diff
changeset
|
6 local type = Luan.type or error() |
1578 | 7 local set_metatable = Luan.set_metatable or error() |
8 local get_local_cloned = Luan.get_local_cloned or error() | |
9 local set_local_cloned = Luan.set_local_cloned or error() | |
10 local get_local_only = Luan.get_local_only or error() | |
11 local set_local_only = Luan.set_local_only or error() | |
12 local raw_set = Luan.raw_set or error() | |
693
ca169567ce07
module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
629
diff
changeset
|
13 local Io = require "luan:Io.luan" |
ca169567ce07
module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
629
diff
changeset
|
14 local Html = require "luan:Html.luan" |
ca169567ce07
module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
629
diff
changeset
|
15 local Table = require "luan:Table.luan" |
580 | 16 local clear = Table.clear or error() |
1261
198d6af7330a
rename Luan.to_table to Table.java_to_table_shallow and Luan.to_luan to Table.java_to_table_deep
Franklin Schmidt <fschmidt@gmail.com>
parents:
1260
diff
changeset
|
17 local java_to_table_deep = Table.java_to_table_deep or error() |
1607 | 18 local case_insensitive = Table.case_insensitive or error() |
693
ca169567ce07
module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
629
diff
changeset
|
19 local Package = require "luan:Package.luan" |
ca169567ce07
module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
629
diff
changeset
|
20 local String = require "luan:String.luan" |
1166
7ef40e1923b7
add back Thread.global
Franklin Schmidt <fschmidt@gmail.com>
parents:
1165
diff
changeset
|
21 local lower = String.lower or error() |
583
1368ca798ccc
add Http.uncache_site
Franklin Schmidt <fschmidt@gmail.com>
parents:
580
diff
changeset
|
22 local matches = String.matches or error() |
1263
382c444a6c77
add Http.eval_in_root and Http.handle_error
Franklin Schmidt <fschmidt@gmail.com>
parents:
1261
diff
changeset
|
23 local trim = String.trim or error() |
1546
2b4471d310b3
better time zone handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1438
diff
changeset
|
24 local Time = require "luan:Time.luan" |
2b4471d310b3
better time zone handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1438
diff
changeset
|
25 local time_format = Time.format or error() |
1280 | 26 local Boot = require "luan:Boot.luan" |
1259
e8020216dee7
add Luan.to_luan and fix multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
1257
diff
changeset
|
27 local LuanJava = require "java:luan.Luan" |
1402
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1383
diff
changeset
|
28 local Request = require "java:goodjava.webserver.Request" |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1383
diff
changeset
|
29 local Response = require "java:goodjava.webserver.Response" |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1383
diff
changeset
|
30 local ResponseOutputStream = require "java:goodjava.webserver.ResponseOutputStream" |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1383
diff
changeset
|
31 local Status = require "java:goodjava.webserver.Status" |
1160 | 32 local OutputStreamWriter = require "java:java.io.OutputStreamWriter" |
1163 | 33 local HashMap = require "java:java.util.HashMap" |
1263
382c444a6c77
add Http.eval_in_root and Http.handle_error
Franklin Schmidt <fschmidt@gmail.com>
parents:
1261
diff
changeset
|
34 local Logging = require "luan:logging/Logging.luan" |
382c444a6c77
add Http.eval_in_root and Http.handle_error
Franklin Schmidt <fschmidt@gmail.com>
parents:
1261
diff
changeset
|
35 local logger = Logging.logger "Http" |
320
fed1893821bf
remove global namespace
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
300
diff
changeset
|
36 |
1152
21d157b153fe
change http parameters interface
Franklin Schmidt <fschmidt@gmail.com>
parents:
1150
diff
changeset
|
37 |
1088
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
1082
diff
changeset
|
38 local Http = {} |
1578 | 39 local mt = {} |
40 function mt.__index(tbl,key) | |
1618
a37ffe2d1b14
fix not_found_handler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1607
diff
changeset
|
41 if key=="error_priority" or key=="not_found_handler" then |
1578 | 42 return get_local_cloned(tbl,key) |
43 elseif key=="request" or key=="response" then | |
44 return get_local_only(tbl,key) | |
45 else | |
46 return nil | |
47 end | |
48 end | |
49 function mt.__new_index(tbl,key,value) | |
1618
a37ffe2d1b14
fix not_found_handler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1607
diff
changeset
|
50 if key=="error_priority" or key=="not_found_handler" then |
1578 | 51 set_local_cloned(tbl,key,value) |
52 elseif key=="request" or key=="response" then | |
53 set_local_only(tbl,key,value) | |
54 else | |
55 raw_set(tbl,key,value) | |
56 end | |
57 end | |
58 set_metatable(Http,mt) | |
320
fed1893821bf
remove global namespace
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
300
diff
changeset
|
59 |
1261
198d6af7330a
rename Luan.to_table to Table.java_to_table_shallow and Luan.to_luan to Table.java_to_table_deep
Franklin Schmidt <fschmidt@gmail.com>
parents:
1260
diff
changeset
|
60 local old_java_to_table_shallow = Table.java_to_table_shallow or error() |
1259
e8020216dee7
add Luan.to_luan and fix multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
1257
diff
changeset
|
61 |
1261
198d6af7330a
rename Luan.to_table to Table.java_to_table_shallow and Luan.to_luan to Table.java_to_table_deep
Franklin Schmidt <fschmidt@gmail.com>
parents:
1260
diff
changeset
|
62 local function java_to_table_shallow(obj) |
1259
e8020216dee7
add Luan.to_luan and fix multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
1257
diff
changeset
|
63 if type(obj)=="java" and obj.instanceof(Request.MultipartFile) then |
e8020216dee7
add Luan.to_luan and fix multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
1257
diff
changeset
|
64 return { |
e8020216dee7
add Luan.to_luan and fix multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
1257
diff
changeset
|
65 filename = obj.filename |
e8020216dee7
add Luan.to_luan and fix multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
1257
diff
changeset
|
66 content_type = obj.contentType |
e8020216dee7
add Luan.to_luan and fix multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
1257
diff
changeset
|
67 content = obj.content |
e8020216dee7
add Luan.to_luan and fix multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
1257
diff
changeset
|
68 } |
e8020216dee7
add Luan.to_luan and fix multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
1257
diff
changeset
|
69 end |
1261
198d6af7330a
rename Luan.to_table to Table.java_to_table_shallow and Luan.to_luan to Table.java_to_table_deep
Franklin Schmidt <fschmidt@gmail.com>
parents:
1260
diff
changeset
|
70 return old_java_to_table_shallow(obj) |
1259
e8020216dee7
add Luan.to_luan and fix multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
1257
diff
changeset
|
71 end |
e8020216dee7
add Luan.to_luan and fix multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
1257
diff
changeset
|
72 |
1160 | 73 function Http.new_request(java) |
74 local this = {} | |
75 Http.request = this | |
76 if java == nil then | |
77 this.method = "GET" | |
1237 | 78 this.scheme = "http" |
1607 | 79 this.headers = case_insensitive{} |
1160 | 80 this.parameters = {} |
81 this.cookies = {} | |
82 else | |
83 this.java = java | |
1179
f9136432847e
add request.raw_head
Franklin Schmidt <fschmidt@gmail.com>
parents:
1171
diff
changeset
|
84 this.raw_head = java.rawHead or error() |
1437 | 85 this.body = java.body |
1160 | 86 this.method = java.method or error() |
87 this.raw_path = java.rawPath or error() | |
1371
4c5548a61d4f
add Request.originalPath
Franklin Schmidt <fschmidt@gmail.com>
parents:
1353
diff
changeset
|
88 this.original_path = java.originalPath or error() |
1160 | 89 this.path = java.path or error() |
90 this.protocol = java.protocol or error() | |
1237 | 91 this.scheme = java.scheme or error() |
1607 | 92 this.headers = case_insensitive(java_to_table_deep(java.headers)) |
1261
198d6af7330a
rename Luan.to_table to Table.java_to_table_shallow and Luan.to_luan to Table.java_to_table_deep
Franklin Schmidt <fschmidt@gmail.com>
parents:
1260
diff
changeset
|
93 this.parameters = java_to_table_deep(java.parameters,java_to_table_shallow) |
198d6af7330a
rename Luan.to_table to Table.java_to_table_shallow and Luan.to_luan to Table.java_to_table_deep
Franklin Schmidt <fschmidt@gmail.com>
parents:
1260
diff
changeset
|
94 this.cookies = java_to_table_deep(java.cookies) |
1160 | 95 end |
497
55f9f74f1e55
Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
495
diff
changeset
|
96 |
499 | 97 function this.url() |
1607 | 98 return this.scheme.."://"..this.headers["Host"]..this.raw_path |
497
55f9f74f1e55
Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
495
diff
changeset
|
99 end |
55f9f74f1e55
Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
495
diff
changeset
|
100 |
55f9f74f1e55
Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
495
diff
changeset
|
101 return this |
55f9f74f1e55
Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
495
diff
changeset
|
102 end |
55f9f74f1e55
Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
495
diff
changeset
|
103 |
503 | 104 local STATUS = { |
1109 | 105 OK = 200 |
106 MOVED_PERMANENTLY = 301 | |
1160 | 107 FOUND = 302 |
1383
a3d0d1c2ce89
add NotFound to luanhost
Franklin Schmidt <fschmidt@gmail.com>
parents:
1371
diff
changeset
|
108 NOT_FOUND = 404 |
498
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
109 -- add more as needed |
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
110 } |
1088
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
1082
diff
changeset
|
111 Http.STATUS = STATUS |
498
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
112 |
1163 | 113 function Http.new_response() |
1160 | 114 local this = {} |
115 Http.response = this | |
498
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
116 |
1163 | 117 function this.reset() |
118 this.java = Response.new() | |
1607 | 119 this.headers = case_insensitive{} |
1163 | 120 this.status = STATUS.OK |
121 this.writer = nil | |
122 end | |
498
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
123 |
1163 | 124 this.reset() |
1153
1f4da56abd4f
change http cookies interface
Franklin Schmidt <fschmidt@gmail.com>
parents:
1152
diff
changeset
|
125 |
1160 | 126 function this.send_redirect(location) |
127 this.status = STATUS.FOUND | |
1607 | 128 this.headers["Location"] = location |
1160 | 129 end |
130 | |
1161 | 131 function this.send_error(status,msg) |
1163 | 132 this.reset() |
1161 | 133 this.status = status |
134 if msg ~= nil then | |
1607 | 135 this.headers["Content-Type"] = "text/plain; charset=utf-8" |
1161 | 136 local writer = this.text_writer() |
137 writer.write(msg) | |
138 end | |
139 end | |
140 | |
1160 | 141 function this.set_cookie(name,value,attributes) |
1163 | 142 attributes = attributes or {} |
1213 | 143 attributes["Path"] = attributes["Path"] or "/" |
1163 | 144 local attrMap = HashMap.new() |
145 for attr_name, attr_value in pairs(attributes) do | |
1164 | 146 type(attr_name)=="string" or "cookie attribute name must be string" |
147 type(attr_value)=="string" or "cookie attribute value must be string" | |
1163 | 148 attrMap.put(attr_name,attr_value) |
149 end | |
150 this.java.setCookie(name,value,attrMap) | |
1160 | 151 end |
498
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
152 |
1160 | 153 function this.set_persistent_cookie(name,value,attributes) |
154 attributes = attributes or {} | |
155 attributes["Max-Age"] = "10000000" | |
156 this.set_cookie(name,value,attributes) | |
157 end | |
158 | |
159 function this.remove_cookie(name,attributes) | |
160 attributes = attributes or {} | |
161 attributes["Max-Age"] = "0" | |
162 this.set_cookie(name,"delete",attributes) | |
163 end | |
568
aa17ad66a370
fix http response headers
Franklin Schmidt <fschmidt@gmail.com>
parents:
509
diff
changeset
|
164 |
1160 | 165 function this.text_writer() |
1559 | 166 this.writer and error "writer already set" |
1160 | 167 this.writer = ResponseOutputStream.new(this.java) |
168 this.writer = OutputStreamWriter.new(this.writer) | |
1280 | 169 return Boot.text_writer(this.writer) |
1160 | 170 end |
500
ab9c2afefb47
add response.binary_writer
Franklin Schmidt <fschmidt@gmail.com>
parents:
499
diff
changeset
|
171 |
1160 | 172 function this.binary_writer() |
1559 | 173 this.writer and error "writer already set" |
1160 | 174 this.writer = ResponseOutputStream.new(this.java) |
1280 | 175 return Boot.binary_writer(this.writer) |
1160 | 176 end |
580 | 177 |
498
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
178 return this |
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
179 end |
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
180 |
1163 | 181 function Http.finish() -- called only from java |
182 local response = Http.response or error() | |
183 local java = response.java or error() | |
184 java.status = Status.getStatus(response.status) | |
185 for name, value in pairs(response.headers) do | |
1164 | 186 type(name)=="string" or "header name must be string" |
1259
e8020216dee7
add Luan.to_luan and fix multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
1257
diff
changeset
|
187 value = LuanJava.toJava(value) |
1163 | 188 java.headers.put(name,value) |
189 end | |
190 response.writer and response.writer.close() | |
191 return java | |
192 end | |
193 | |
1438 | 194 function Http.error_priority(e) |
1263
382c444a6c77
add Http.eval_in_root and Http.handle_error
Franklin Schmidt <fschmidt@gmail.com>
parents:
1261
diff
changeset
|
195 return "error" |
382c444a6c77
add Http.eval_in_root and Http.handle_error
Franklin Schmidt <fschmidt@gmail.com>
parents:
1261
diff
changeset
|
196 end |
382c444a6c77
add Http.eval_in_root and Http.handle_error
Franklin Schmidt <fschmidt@gmail.com>
parents:
1261
diff
changeset
|
197 |
382c444a6c77
add Http.eval_in_root and Http.handle_error
Franklin Schmidt <fschmidt@gmail.com>
parents:
1261
diff
changeset
|
198 function Http.handle_error(java_request,e) |
1438 | 199 Http.new_request(java_request) |
200 local call = Http.error_priority(e) | |
1263
382c444a6c77
add Http.eval_in_root and Http.handle_error
Franklin Schmidt <fschmidt@gmail.com>
parents:
1261
diff
changeset
|
201 local err = e.get_stack_trace_string() |
382c444a6c77
add Http.eval_in_root and Http.handle_error
Franklin Schmidt <fschmidt@gmail.com>
parents:
1261
diff
changeset
|
202 logger[call](err.."\n"..trim(java_request.rawHead).."\n") |
382c444a6c77
add Http.eval_in_root and Http.handle_error
Franklin Schmidt <fschmidt@gmail.com>
parents:
1261
diff
changeset
|
203 local msg = "Internel Server Error\n\n"..err |
382c444a6c77
add Http.eval_in_root and Http.handle_error
Franklin Schmidt <fschmidt@gmail.com>
parents:
1261
diff
changeset
|
204 return Response.errorResponse( Status.INTERNAL_SERVER_ERROR, msg ) |
382c444a6c77
add Http.eval_in_root and Http.handle_error
Franklin Schmidt <fschmidt@gmail.com>
parents:
1261
diff
changeset
|
205 end |
382c444a6c77
add Http.eval_in_root and Http.handle_error
Franklin Schmidt <fschmidt@gmail.com>
parents:
1261
diff
changeset
|
206 |
1315 | 207 Http.domain = nil -- set in domain specific cases |
208 | |
1414 | 209 Http.is_serving = false |
210 | |
1546
2b4471d310b3
better time zone handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1438
diff
changeset
|
211 function Http.format_date(date) |
1607 | 212 return time_format(date,"EEE, dd MMM yyyy HH:mm:ss z","GMT") |
1546
2b4471d310b3
better time zone handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1438
diff
changeset
|
213 end |
2b4471d310b3
better time zone handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1438
diff
changeset
|
214 |
1088
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
1082
diff
changeset
|
215 return Http |