annotate src/luan/modules/http/Http.luan @ 1515:78d937870762

Thread.synchronized
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 30 May 2020 17:58:35 -0600
parents a47021c3da1e
children 2b4471d310b3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
9218f9cf45d3 various fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 504
diff changeset
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
1e69d9c21461 add Table.clear();
Franklin Schmidt <fschmidt@gmail.com>
parents: 572
diff changeset
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
781ec0a92bb5 add Boot.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
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"
1402
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1383
diff changeset
19 local Request = require "java:goodjava.webserver.Request"
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1383
diff changeset
20 local Response = require "java:goodjava.webserver.Response"
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1383
diff changeset
21 local ResponseOutputStream = require "java:goodjava.webserver.ResponseOutputStream"
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1383
diff changeset
22 local Status = require "java:goodjava.webserver.Status"
1160
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1159
diff changeset
23 local OutputStreamWriter = require "java:java.io.OutputStreamWriter"
1163
Franklin Schmidt <fschmidt@gmail.com>
parents: 1162
diff changeset
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
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1159
diff changeset
44 function Http.new_request(java)
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1159
diff changeset
45 local this = {}
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1159
diff changeset
46 Http.request = this
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1159
diff changeset
47 if java == nil then
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1159
diff changeset
48 this.method = "GET"
1237
275d1b52dbce add Request.scheme
Franklin Schmidt <fschmidt@gmail.com>
parents: 1226
diff changeset
49 this.scheme = "http"
1160
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1159
diff changeset
50 this.headers = {}
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1159
diff changeset
51 this.parameters = {}
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1159
diff changeset
52 this.cookies = {}
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1159
diff changeset
53 else
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1159
diff changeset
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()
1437
3e185b27f982 Http.request.body
Franklin Schmidt <fschmidt@gmail.com>
parents: 1418
diff changeset
56 this.body = java.body
1160
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1159
diff changeset
57 this.method = java.method or error()
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1159
diff changeset
58 this.raw_path = java.rawPath or error()
1371
4c5548a61d4f add Request.originalPath
Franklin Schmidt <fschmidt@gmail.com>
parents: 1353
diff changeset
59 this.original_path = java.originalPath or error()
1160
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1159
diff changeset
60 this.path = java.path or error()
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1159
diff changeset
61 this.protocol = java.protocol or error()
1237
275d1b52dbce add Request.scheme
Franklin Schmidt <fschmidt@gmail.com>
parents: 1226
diff changeset
62 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
63 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
64 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
65 this.cookies = java_to_table_deep(java.cookies)
1160
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1159
diff changeset
66 end
497
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
67
499
fa4af530697f add http/dump
Franklin Schmidt <fschmidt@gmail.com>
parents: 498
diff changeset
68 function this.url()
1160
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1159
diff changeset
69 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
70 end
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
71
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
72 return this
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
73 end
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 495
diff changeset
74
503
92c3d22745b8 make _ENV optional
Franklin Schmidt <fschmidt@gmail.com>
parents: 500
diff changeset
75 local STATUS = {
1109
Franklin Schmidt <fschmidt@gmail.com>
parents: 1102
diff changeset
76 OK = 200
Franklin Schmidt <fschmidt@gmail.com>
parents: 1102
diff changeset
77 MOVED_PERMANENTLY = 301
1160
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1159
diff changeset
78 FOUND = 302
1383
a3d0d1c2ce89 add NotFound to luanhost
Franklin Schmidt <fschmidt@gmail.com>
parents: 1371
diff changeset
79 NOT_FOUND = 404
498
ee55be414a34 Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 497
diff changeset
80 -- add more as needed
ee55be414a34 Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 497
diff changeset
81 }
1088
bae2d0c2576c change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents: 1082
diff changeset
82 Http.STATUS = STATUS
498
ee55be414a34 Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 497
diff changeset
83
1163
Franklin Schmidt <fschmidt@gmail.com>
parents: 1162
diff changeset
84 function Http.new_response()
1160
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1159
diff changeset
85 local this = {}
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1159
diff changeset
86 Http.response = this
498
ee55be414a34 Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 497
diff changeset
87
1163
Franklin Schmidt <fschmidt@gmail.com>
parents: 1162
diff changeset
88 function this.reset()
Franklin Schmidt <fschmidt@gmail.com>
parents: 1162
diff changeset
89 this.java = Response.new()
Franklin Schmidt <fschmidt@gmail.com>
parents: 1162
diff changeset
90 this.headers = {}
Franklin Schmidt <fschmidt@gmail.com>
parents: 1162
diff changeset
91 this.status = STATUS.OK
Franklin Schmidt <fschmidt@gmail.com>
parents: 1162
diff changeset
92 this.writer = nil
Franklin Schmidt <fschmidt@gmail.com>
parents: 1162
diff changeset
93 end
498
ee55be414a34 Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 497
diff changeset
94
1163
Franklin Schmidt <fschmidt@gmail.com>
parents: 1162
diff changeset
95 this.reset()
1153
1f4da56abd4f change http cookies interface
Franklin Schmidt <fschmidt@gmail.com>
parents: 1152
diff changeset
96
1160
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1159
diff changeset
97 function this.send_redirect(location)
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1159
diff changeset
98 this.status = STATUS.FOUND
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1159
diff changeset
99 this.headers["location"] = location
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1159
diff changeset
100 end
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1159
diff changeset
101
1161
Franklin Schmidt <fschmidt@gmail.com>
parents: 1160
diff changeset
102 function this.send_error(status,msg)
1163
Franklin Schmidt <fschmidt@gmail.com>
parents: 1162
diff changeset
103 this.reset()
1161
Franklin Schmidt <fschmidt@gmail.com>
parents: 1160
diff changeset
104 this.status = status
Franklin Schmidt <fschmidt@gmail.com>
parents: 1160
diff changeset
105 if msg ~= nil then
1165
668f29bc52ea clean up content-type
Franklin Schmidt <fschmidt@gmail.com>
parents: 1164
diff changeset
106 this.headers["content-type"] = "text/plain; charset=utf-8"
1161
Franklin Schmidt <fschmidt@gmail.com>
parents: 1160
diff changeset
107 local writer = this.text_writer()
Franklin Schmidt <fschmidt@gmail.com>
parents: 1160
diff changeset
108 writer.write(msg)
Franklin Schmidt <fschmidt@gmail.com>
parents: 1160
diff changeset
109 end
Franklin Schmidt <fschmidt@gmail.com>
parents: 1160
diff changeset
110 end
Franklin Schmidt <fschmidt@gmail.com>
parents: 1160
diff changeset
111
1160
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1159
diff changeset
112 function this.set_cookie(name,value,attributes)
1163
Franklin Schmidt <fschmidt@gmail.com>
parents: 1162
diff changeset
113 attributes = attributes or {}
1213
d5d5d29d7592 fix cookie bug
Franklin Schmidt <fschmidt@gmail.com>
parents: 1179
diff changeset
114 attributes["Path"] = attributes["Path"] or "/"
1163
Franklin Schmidt <fschmidt@gmail.com>
parents: 1162
diff changeset
115 local attrMap = HashMap.new()
Franklin Schmidt <fschmidt@gmail.com>
parents: 1162
diff changeset
116 for attr_name, attr_value in pairs(attributes) do
1164
1f9d34a6f308 remove assertions
Franklin Schmidt <fschmidt@gmail.com>
parents: 1163
diff changeset
117 type(attr_name)=="string" or "cookie attribute name must be string"
1f9d34a6f308 remove assertions
Franklin Schmidt <fschmidt@gmail.com>
parents: 1163
diff changeset
118 type(attr_value)=="string" or "cookie attribute value must be string"
1163
Franklin Schmidt <fschmidt@gmail.com>
parents: 1162
diff changeset
119 attrMap.put(attr_name,attr_value)
Franklin Schmidt <fschmidt@gmail.com>
parents: 1162
diff changeset
120 end
Franklin Schmidt <fschmidt@gmail.com>
parents: 1162
diff changeset
121 this.java.setCookie(name,value,attrMap)
1160
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1159
diff changeset
122 end
498
ee55be414a34 Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 497
diff changeset
123
1160
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1159
diff changeset
124 function this.set_persistent_cookie(name,value,attributes)
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1159
diff changeset
125 attributes = attributes or {}
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1159
diff changeset
126 attributes["Max-Age"] = "10000000"
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1159
diff changeset
127 this.set_cookie(name,value,attributes)
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1159
diff changeset
128 end
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1159
diff changeset
129
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1159
diff changeset
130 function this.remove_cookie(name,attributes)
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1159
diff changeset
131 attributes = attributes or {}
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1159
diff changeset
132 attributes["Max-Age"] = "0"
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1159
diff changeset
133 this.set_cookie(name,"delete",attributes)
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1159
diff changeset
134 end
568
aa17ad66a370 fix http response headers
Franklin Schmidt <fschmidt@gmail.com>
parents: 509
diff changeset
135
1160
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1159
diff changeset
136 function this.text_writer()
1163
Franklin Schmidt <fschmidt@gmail.com>
parents: 1162
diff changeset
137 this.writer and "writer already set"
1160
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1159
diff changeset
138 this.writer = ResponseOutputStream.new(this.java)
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1159
diff changeset
139 this.writer = OutputStreamWriter.new(this.writer)
1280
781ec0a92bb5 add Boot.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
140 return Boot.text_writer(this.writer)
1160
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1159
diff changeset
141 end
500
ab9c2afefb47 add response.binary_writer
Franklin Schmidt <fschmidt@gmail.com>
parents: 499
diff changeset
142
1160
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1159
diff changeset
143 function this.binary_writer()
1163
Franklin Schmidt <fschmidt@gmail.com>
parents: 1162
diff changeset
144 this.writer and "writer already set"
1160
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1159
diff changeset
145 this.writer = ResponseOutputStream.new(this.java)
1280
781ec0a92bb5 add Boot.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 1263
diff changeset
146 return Boot.binary_writer(this.writer)
1160
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1159
diff changeset
147 end
580
1e69d9c21461 add Table.clear();
Franklin Schmidt <fschmidt@gmail.com>
parents: 572
diff changeset
148
498
ee55be414a34 Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 497
diff changeset
149 return this
ee55be414a34 Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 497
diff changeset
150 end
ee55be414a34 Http.response is now mostly luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 497
diff changeset
151
1163
Franklin Schmidt <fschmidt@gmail.com>
parents: 1162
diff changeset
152 function Http.finish() -- called only from java
Franklin Schmidt <fschmidt@gmail.com>
parents: 1162
diff changeset
153 local response = Http.response or error()
Franklin Schmidt <fschmidt@gmail.com>
parents: 1162
diff changeset
154 local java = response.java or error()
Franklin Schmidt <fschmidt@gmail.com>
parents: 1162
diff changeset
155 java.status = Status.getStatus(response.status)
Franklin Schmidt <fschmidt@gmail.com>
parents: 1162
diff changeset
156 for name, value in pairs(response.headers) do
1164
1f9d34a6f308 remove assertions
Franklin Schmidt <fschmidt@gmail.com>
parents: 1163
diff changeset
157 type(name)=="string" or "header name must be string"
1166
7ef40e1923b7 add back Thread.global
Franklin Schmidt <fschmidt@gmail.com>
parents: 1165
diff changeset
158 name = lower(name)
1259
e8020216dee7 add Luan.to_luan and fix multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1257
diff changeset
159 value = LuanJava.toJava(value)
1163
Franklin Schmidt <fschmidt@gmail.com>
parents: 1162
diff changeset
160 java.headers.put(name,value)
Franklin Schmidt <fschmidt@gmail.com>
parents: 1162
diff changeset
161 end
Franklin Schmidt <fschmidt@gmail.com>
parents: 1162
diff changeset
162 response.writer and response.writer.close()
Franklin Schmidt <fschmidt@gmail.com>
parents: 1162
diff changeset
163 return java
Franklin Schmidt <fschmidt@gmail.com>
parents: 1162
diff changeset
164 end
Franklin Schmidt <fschmidt@gmail.com>
parents: 1162
diff changeset
165
1438
Franklin Schmidt <fschmidt@gmail.com>
parents: 1437
diff changeset
166 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
167 return "error"
382c444a6c77 add Http.eval_in_root and Http.handle_error
Franklin Schmidt <fschmidt@gmail.com>
parents: 1261
diff changeset
168 end
382c444a6c77 add Http.eval_in_root and Http.handle_error
Franklin Schmidt <fschmidt@gmail.com>
parents: 1261
diff changeset
169
382c444a6c77 add Http.eval_in_root and Http.handle_error
Franklin Schmidt <fschmidt@gmail.com>
parents: 1261
diff changeset
170 function Http.handle_error(java_request,e)
1438
Franklin Schmidt <fschmidt@gmail.com>
parents: 1437
diff changeset
171 Http.new_request(java_request)
Franklin Schmidt <fschmidt@gmail.com>
parents: 1437
diff changeset
172 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
173 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
174 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
175 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
176 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
177 end
382c444a6c77 add Http.eval_in_root and Http.handle_error
Franklin Schmidt <fschmidt@gmail.com>
parents: 1261
diff changeset
178
1315
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1280
diff changeset
179 Http.domain = nil -- set in domain specific cases
5763597ca5c0 add DomainHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1280
diff changeset
180
1414
4abf88ec66de add Http.is_serving
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
181 Http.is_serving = false
4abf88ec66de add Http.is_serving
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
182
1088
bae2d0c2576c change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents: 1082
diff changeset
183 return Http