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