Mercurial Hosting > luan
annotate src/luan/modules/http/Http.luan @ 1353:8d95711f6615
replace java() with require "java"
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 20 Mar 2019 17:03:29 -0600 |
parents | 643cf1c37723 |
children | 4c5548a61d4f |
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() |
693
ca169567ce07
module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
629
diff
changeset
|
7 local Io = require "luan:Io.luan" |
ca169567ce07
module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
629
diff
changeset
|
8 local Html = require "luan:Html.luan" |
ca169567ce07
module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
629
diff
changeset
|
9 local Table = require "luan:Table.luan" |
580 | 10 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
|
11 local java_to_table_deep = Table.java_to_table_deep or error() |
693
ca169567ce07
module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
629
diff
changeset
|
12 local Package = require "luan:Package.luan" |
ca169567ce07
module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
629
diff
changeset
|
13 local String = require "luan:String.luan" |
1166
7ef40e1923b7
add back Thread.global
Franklin Schmidt <fschmidt@gmail.com>
parents:
1165
diff
changeset
|
14 local lower = String.lower or error() |
583
1368ca798ccc
add Http.uncache_site
Franklin Schmidt <fschmidt@gmail.com>
parents:
580
diff
changeset
|
15 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
|
16 local trim = String.trim or error() |
1280 | 17 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
|
18 local LuanJava = require "java:luan.Luan" |
1347
643cf1c37723
move webserver to lib and bug fixes
Franklin Schmidt <fschmidt@gmail.com>
parents:
1315
diff
changeset
|
19 local Request = require "java:luan.lib.webserver.Request" |
643cf1c37723
move webserver to lib and bug fixes
Franklin Schmidt <fschmidt@gmail.com>
parents:
1315
diff
changeset
|
20 local Response = require "java:luan.lib.webserver.Response" |
643cf1c37723
move webserver to lib and bug fixes
Franklin Schmidt <fschmidt@gmail.com>
parents:
1315
diff
changeset
|
21 local ResponseOutputStream = require "java:luan.lib.webserver.ResponseOutputStream" |
643cf1c37723
move webserver to lib and bug fixes
Franklin Schmidt <fschmidt@gmail.com>
parents:
1315
diff
changeset
|
22 local Status = require "java:luan.lib.webserver.Status" |
1160 | 23 local OutputStreamWriter = require "java:java.io.OutputStreamWriter" |
1163 | 24 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
|
25 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
|
26 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
|
27 |
1152
21d157b153fe
change http parameters interface
Franklin Schmidt <fschmidt@gmail.com>
parents:
1150
diff
changeset
|
28 |
1088
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
1082
diff
changeset
|
29 local Http = {} |
320
fed1893821bf
remove global namespace
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
300
diff
changeset
|
30 |
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
|
31 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
|
32 |
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
|
33 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
|
34 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
|
35 return { |
e8020216dee7
add Luan.to_luan and fix multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
1257
diff
changeset
|
36 filename = obj.filename |
e8020216dee7
add Luan.to_luan and fix multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
1257
diff
changeset
|
37 content_type = obj.contentType |
e8020216dee7
add Luan.to_luan and fix multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
1257
diff
changeset
|
38 content = obj.content |
e8020216dee7
add Luan.to_luan and fix multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
1257
diff
changeset
|
39 } |
e8020216dee7
add Luan.to_luan and fix multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
1257
diff
changeset
|
40 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
|
41 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
|
42 end |
e8020216dee7
add Luan.to_luan and fix multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
1257
diff
changeset
|
43 |
1160 | 44 function Http.new_request(java) |
45 local this = {} | |
46 Http.request = this | |
47 if java == nil then | |
48 this.method = "GET" | |
1237 | 49 this.scheme = "http" |
1160 | 50 this.headers = {} |
51 this.parameters = {} | |
52 this.cookies = {} | |
53 else | |
54 this.java = java | |
1179
f9136432847e
add request.raw_head
Franklin Schmidt <fschmidt@gmail.com>
parents:
1171
diff
changeset
|
55 this.raw_head = java.rawHead or error() |
1160 | 56 this.method = java.method or error() |
57 this.raw_path = java.rawPath or error() | |
58 this.path = java.path or error() | |
59 this.protocol = java.protocol or error() | |
1237 | 60 this.scheme = java.scheme 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
|
61 this.headers = java_to_table_deep(java.headers) |
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 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
|
63 this.cookies = java_to_table_deep(java.cookies) |
1160 | 64 end |
497
55f9f74f1e55
Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
495
diff
changeset
|
65 |
499 | 66 function this.url() |
1160 | 67 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
|
68 end |
55f9f74f1e55
Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
495
diff
changeset
|
69 |
55f9f74f1e55
Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
495
diff
changeset
|
70 return this |
55f9f74f1e55
Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
495
diff
changeset
|
71 end |
55f9f74f1e55
Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
495
diff
changeset
|
72 |
503 | 73 local STATUS = { |
1109 | 74 OK = 200 |
75 MOVED_PERMANENTLY = 301 | |
1160 | 76 FOUND = 302 |
498
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
77 -- add more as needed |
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
78 } |
1088
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
1082
diff
changeset
|
79 Http.STATUS = STATUS |
498
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
80 |
1163 | 81 function Http.new_response() |
1160 | 82 local this = {} |
83 Http.response = this | |
498
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
84 |
1163 | 85 function this.reset() |
86 this.java = Response.new() | |
87 this.headers = {} | |
88 this.status = STATUS.OK | |
89 this.writer = nil | |
90 end | |
498
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
91 |
1163 | 92 this.reset() |
1153
1f4da56abd4f
change http cookies interface
Franklin Schmidt <fschmidt@gmail.com>
parents:
1152
diff
changeset
|
93 |
1160 | 94 function this.send_redirect(location) |
95 this.status = STATUS.FOUND | |
96 this.headers["location"] = location | |
97 end | |
98 | |
1161 | 99 function this.send_error(status,msg) |
1163 | 100 this.reset() |
1161 | 101 this.status = status |
102 if msg ~= nil then | |
1165
668f29bc52ea
clean up content-type
Franklin Schmidt <fschmidt@gmail.com>
parents:
1164
diff
changeset
|
103 this.headers["content-type"] = "text/plain; charset=utf-8" |
1161 | 104 local writer = this.text_writer() |
105 writer.write(msg) | |
106 end | |
107 end | |
108 | |
1160 | 109 function this.set_cookie(name,value,attributes) |
1163 | 110 attributes = attributes or {} |
1213 | 111 attributes["Path"] = attributes["Path"] or "/" |
1163 | 112 local attrMap = HashMap.new() |
113 for attr_name, attr_value in pairs(attributes) do | |
1164 | 114 type(attr_name)=="string" or "cookie attribute name must be string" |
115 type(attr_value)=="string" or "cookie attribute value must be string" | |
1163 | 116 attrMap.put(attr_name,attr_value) |
117 end | |
118 this.java.setCookie(name,value,attrMap) | |
1160 | 119 end |
498
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
120 |
1160 | 121 function this.set_persistent_cookie(name,value,attributes) |
122 attributes = attributes or {} | |
123 attributes["Max-Age"] = "10000000" | |
124 this.set_cookie(name,value,attributes) | |
125 end | |
126 | |
127 function this.remove_cookie(name,attributes) | |
128 attributes = attributes or {} | |
129 attributes["Max-Age"] = "0" | |
130 this.set_cookie(name,"delete",attributes) | |
131 end | |
568
aa17ad66a370
fix http response headers
Franklin Schmidt <fschmidt@gmail.com>
parents:
509
diff
changeset
|
132 |
1160 | 133 function this.text_writer() |
1163 | 134 this.writer and "writer already set" |
1160 | 135 this.writer = ResponseOutputStream.new(this.java) |
136 this.writer = OutputStreamWriter.new(this.writer) | |
1280 | 137 return Boot.text_writer(this.writer) |
1160 | 138 end |
500
ab9c2afefb47
add response.binary_writer
Franklin Schmidt <fschmidt@gmail.com>
parents:
499
diff
changeset
|
139 |
1160 | 140 function this.binary_writer() |
1163 | 141 this.writer and "writer already set" |
1160 | 142 this.writer = ResponseOutputStream.new(this.java) |
1280 | 143 return Boot.binary_writer(this.writer) |
1160 | 144 end |
580 | 145 |
498
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
146 return this |
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
147 end |
ee55be414a34
Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
497
diff
changeset
|
148 |
1163 | 149 function Http.finish() -- called only from java |
150 local response = Http.response or error() | |
151 local java = response.java or error() | |
152 java.status = Status.getStatus(response.status) | |
153 for name, value in pairs(response.headers) do | |
1164 | 154 type(name)=="string" or "header name must be string" |
1166
7ef40e1923b7
add back Thread.global
Franklin Schmidt <fschmidt@gmail.com>
parents:
1165
diff
changeset
|
155 name = lower(name) |
1259
e8020216dee7
add Luan.to_luan and fix multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents:
1257
diff
changeset
|
156 value = LuanJava.toJava(value) |
1163 | 157 java.headers.put(name,value) |
158 end | |
159 response.writer and response.writer.close() | |
160 return java | |
161 end | |
162 | |
1263
382c444a6c77
add Http.eval_in_root and Http.handle_error
Franklin Schmidt <fschmidt@gmail.com>
parents:
1261
diff
changeset
|
163 function Http.error_priority(java_request,e) |
382c444a6c77
add Http.eval_in_root and Http.handle_error
Franklin Schmidt <fschmidt@gmail.com>
parents:
1261
diff
changeset
|
164 return "error" |
382c444a6c77
add Http.eval_in_root and Http.handle_error
Franklin Schmidt <fschmidt@gmail.com>
parents:
1261
diff
changeset
|
165 end |
382c444a6c77
add Http.eval_in_root and Http.handle_error
Franklin Schmidt <fschmidt@gmail.com>
parents:
1261
diff
changeset
|
166 |
382c444a6c77
add Http.eval_in_root and Http.handle_error
Franklin Schmidt <fschmidt@gmail.com>
parents:
1261
diff
changeset
|
167 function Http.handle_error(java_request,e) |
382c444a6c77
add Http.eval_in_root and Http.handle_error
Franklin Schmidt <fschmidt@gmail.com>
parents:
1261
diff
changeset
|
168 local call = Http.error_priority(java_request,e) |
382c444a6c77
add Http.eval_in_root and Http.handle_error
Franklin Schmidt <fschmidt@gmail.com>
parents:
1261
diff
changeset
|
169 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
|
170 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
|
171 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
|
172 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
|
173 end |
382c444a6c77
add Http.eval_in_root and Http.handle_error
Franklin Schmidt <fschmidt@gmail.com>
parents:
1261
diff
changeset
|
174 |
1315 | 175 Http.domain = nil -- set in domain specific cases |
176 | |
1088
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
1082
diff
changeset
|
177 return Http |