Mercurial Hosting > luan
annotate src/luan/modules/Boot.luan @ 1754:d179810c6148
add url get_content_type
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sat, 04 Feb 2023 22:53:05 -0700 |
parents | 2f3a8f16f583 |
children | 78cedcd6e1a4 |
rev | line source |
---|---|
1353
8d95711f6615
replace java() with require "java"
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
1 require "java" |
1280 | 2 local System = require "java:java.lang.System" |
3 local URL = require "java:java.net.URL" | |
4 local BasicLuan = require "java:luan.modules.BasicLuan" | |
5 local new_error = BasicLuan.new_error | |
6 local ipairs = BasicLuan.ipairs | |
1283
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
7 local set_metatable = BasicLuan.set_metatable |
1424 | 8 local load = BasicLuan.load |
9 local type = BasicLuan.type | |
1280 | 10 local StringLuan = require "java:luan.modules.StringLuan" |
1716 | 11 local contains = StringLuan.contains -- String.contains |
12 local RegexLuan = require "java:luan.modules.RegexLuan" | |
1280 | 13 local IoLuan = require "java:luan.modules.IoLuan" |
14 local LuanUrl = require "java:luan.modules.url.LuanUrl" | |
1330 | 15 local LuanJava = require "java:luan.Luan" |
16 local LuanTable = require "java:luan.LuanTable" | |
1578 | 17 local get_local_cloned = BasicLuan.get_local_cloned |
18 local set_local_cloned = BasicLuan.set_local_cloned | |
19 local is_immutable = BasicLuan.is_immutable | |
20 local raw_set = BasicLuan.raw_set | |
1280 | 21 |
22 | |
23 local Boot = {} | |
24 | |
25 | |
26 local function error(message) | |
27 new_error(message).throw() | |
28 end | |
29 Boot.error = error | |
30 | |
1330 | 31 local function no_security(fn) |
32 LuanJava.checkCallerSecurity("no_security") | |
33 return function(...) | |
34 local security = LuanJava.setSecurity(nil) | |
1520 | 35 try |
36 return fn(...) | |
37 finally | |
38 security and LuanJava.setSecurity(security) | |
39 end | |
1330 | 40 end |
41 end | |
42 Boot.no_security = no_security | |
43 | |
1280 | 44 |
1578 | 45 local local_metatable = { |
46 __index = get_local_cloned | |
1685 | 47 __new_index = set_local_cloned |
1578 | 48 } |
49 Boot.local_metatable = local_metatable | |
50 | |
51 | |
1716 | 52 local function regex(pattern) |
53 local regex = RegexLuan.new(pattern) | |
54 return { | |
55 java = regex | |
56 find = regex.find | |
57 gmatch = regex.gmatch | |
58 gsub = regex.gsub | |
59 match = regex.match | |
60 matches = regex.matches | |
61 set = regex.set | |
1719 | 62 split = regex.split |
1716 | 63 } |
64 end | |
65 Boot.regex = regex | |
66 | |
67 | |
1280 | 68 local function new_LuanIn(io) |
69 local this = {} | |
70 this.java = io | |
71 this.to_string = io.to_string | |
72 this.read_text = io.read_text | |
73 this.read_binary = io.read_binary | |
74 this.read_lines = io.read_lines | |
75 this.read_blocks = io.read_blocks | |
76 this.exists = io.exists | |
77 this.checksum = io.checksum | |
78 this.charset = io.charset | |
79 this.set_charset = io.set_charset | |
1426 | 80 this.to_uri_string = io.to_uri_string |
1280 | 81 return this |
82 end | |
83 Boot.new_LuanIn = new_LuanIn | |
84 | |
85 local function new_writer(writer) | |
86 local this = {} | |
87 this.java = writer | |
88 this.write = writer.write | |
89 this.close = writer.close | |
90 return this | |
91 end | |
92 | |
93 function Boot.text_writer(out) | |
94 return new_writer( IoLuan.luanWriter(out) ) | |
95 end | |
96 | |
97 Boot.binary_writer = new_writer | |
98 | |
99 local function new_LuanIO(io) | |
100 local this = new_LuanIn(io) | |
101 this.write = io.write | |
102 this.write_text = io.write_text | |
1470 | 103 this.write_binary = io.write_binary |
1280 | 104 |
105 function this.text_writer() | |
106 return new_writer( io.text_writer() ) | |
107 end | |
108 | |
109 function this.binary_writer() | |
110 return new_writer( io.binary_writer() ) | |
111 end | |
112 | |
113 return this | |
114 end | |
115 | |
116 local schemes = {} | |
1562 | 117 --LuanTable.setSecurity(schemes,"schemes") |
1280 | 118 |
119 function schemes.null(path) | |
120 return new_LuanIO( IoLuan.nullIO ) | |
121 end | |
122 | |
123 function schemes.string(path) | |
124 return new_LuanIO( IoLuan.LuanString.new(path) ) | |
125 end | |
126 | |
127 function schemes.classpath(path) | |
128 local cp = IoLuan.classpath(path) | |
129 return cp and new_LuanIn(cp) | |
130 end | |
131 | |
132 function schemes.luan(path) | |
133 return schemes.classpath("luan/modules/"..path) | |
134 end | |
135 | |
136 function schemes.stdin(path) | |
137 local Io = require "luan:Io.luan" | |
138 return Io.stdin | |
139 end | |
140 | |
1364 | 141 |
142 local function new_LuanUrl(io) | |
143 local this = new_LuanIn(io) | |
144 | |
145 local function check() | |
146 io.httpCon or error "must open first" | |
147 end | |
148 | |
149 function this.get_header(name) | |
150 check() | |
151 return io.httpCon.getHeaderField(name) | |
152 end | |
153 | |
154 function this.get_response_code() | |
155 check() | |
156 return io.httpCon.getResponseCode() | |
157 end | |
158 | |
159 function this.get_response_message() | |
160 check() | |
161 return io.httpCon.getResponseMessage() | |
162 end | |
163 | |
1754
d179810c6148
add url get_content_type
Franklin Schmidt <fschmidt@gmail.com>
parents:
1719
diff
changeset
|
164 function this.get_content_type() |
d179810c6148
add url get_content_type
Franklin Schmidt <fschmidt@gmail.com>
parents:
1719
diff
changeset
|
165 check() |
d179810c6148
add url get_content_type
Franklin Schmidt <fschmidt@gmail.com>
parents:
1719
diff
changeset
|
166 return io.httpCon.getContentType() |
d179810c6148
add url get_content_type
Franklin Schmidt <fschmidt@gmail.com>
parents:
1719
diff
changeset
|
167 end |
d179810c6148
add url get_content_type
Franklin Schmidt <fschmidt@gmail.com>
parents:
1719
diff
changeset
|
168 |
1364 | 169 return this |
170 end | |
171 | |
1280 | 172 local function url(path,options) |
1364 | 173 return new_LuanUrl( LuanUrl.new(URL.new(path),options) ) |
1280 | 174 end |
175 | |
176 function schemes.http(path,options) | |
177 return url( "http:"..path, options ) | |
178 end | |
179 | |
180 function schemes.https(path,options) | |
181 return url( "https:"..path, options ) | |
182 end | |
183 | |
184 local function new_BaseOs(io) | |
185 local this = new_LuanIO(io) | |
186 this.wait_for = io.wait_for | |
187 return this | |
188 end | |
189 | |
190 function schemes.os(path,options) | |
191 return new_BaseOs( IoLuan.LuanOs.new(path,options) ) | |
192 end | |
193 | |
194 function schemes.bash(path,options) | |
195 return new_BaseOs( IoLuan.LuanBash.new(path,options) ) | |
196 end | |
197 | |
198 local function new_LuanFile(io) | |
199 local this = new_LuanIO(io) | |
200 this.name = io.file.getName | |
201 this.is_directory = io.file.isDirectory | |
202 this.is_file = io.file.isFile | |
203 this.delete = io.delete | |
204 this.delete_on_exit = io.file.deleteOnExit | |
205 this.mkdir = io.mkdir | |
206 this.last_modified = io.file.lastModified | |
207 this.set_last_modified = io.set_last_modified | |
208 this.length = io.file.length | |
1662 | 209 this.move_to = io.move_to |
1661 | 210 this.copy_to = io.copy_to |
1497 | 211 this.link_from = io.link_from |
212 this.symlink_from = io.symlink_from | |
1311
ca742d51b31f
don't delete into symbolic links
Franklin Schmidt <fschmidt@gmail.com>
parents:
1286
diff
changeset
|
213 this.is_symbolic_link = io.is_symbolic_link |
1280 | 214 |
215 function this.child(name) | |
216 return new_LuanFile( io.child(name) ) | |
217 end | |
218 | |
219 function this.children() | |
220 local raw = io.children() | |
221 if raw == nil then | |
222 return nil | |
223 end | |
224 local rtn = {} | |
225 for _, child in ipairs(raw) do | |
226 rtn[#rtn+1] = new_LuanFile(child) | |
227 end | |
228 return rtn | |
229 end | |
230 | |
231 function this.parent() | |
232 return new_LuanFile( io.parent() ) | |
233 end | |
234 | |
235 function this.canonical() | |
236 return new_LuanFile( io.canonical() ) | |
237 end | |
238 | |
239 function this.create_temp_file(prefix,suffix) | |
240 return new_LuanFile( io.create_temp_file(prefix,suffix) ) | |
241 end | |
242 | |
243 return this | |
244 end | |
245 | |
246 function schemes.file(path) | |
247 return new_LuanFile( IoLuan.LuanFile.new(path) ) | |
248 end | |
249 | |
1685 | 250 set_metatable(schemes,local_metatable) |
1280 | 251 Boot.schemes = schemes |
252 | |
253 | |
1716 | 254 local uri_regex = regex("(?s)^([^:]+):(.*)$") |
255 | |
1280 | 256 local function uri(name,options) |
1716 | 257 local scheme, location = uri_regex.match(name) |
1280 | 258 scheme or error( "invalid Io.uri name '"..name.."', missing scheme" ) |
1286 | 259 local opener = schemes[scheme] or error( "invalid scheme '"..scheme.."' in '"..name.."'" ) |
1280 | 260 return opener(location,options) |
261 end | |
262 Boot.uri = uri | |
263 | |
264 | |
1580 | 265 function Boot.read(uri_str,options) -- for PackageLuan.java |
266 local u = uri(uri_str,options) | |
1280 | 267 if u==nil or not u.exists() then |
268 return nil | |
269 end | |
270 return u.read_text() | |
271 end | |
272 | |
1424 | 273 function Boot.load_file(file) |
274 if type(file) == "string" then | |
1716 | 275 if not contains(file,":") then |
1424 | 276 file = "file:"..file |
277 end | |
278 local u = uri(file) | |
279 if u==nil or not u.exists() then | |
280 return nil | |
281 end | |
282 local src = u.read_text() | |
1434
56fb5cd8228d
cache compiled code in temp files
Franklin Schmidt <fschmidt@gmail.com>
parents:
1426
diff
changeset
|
283 return load(src,file,true) |
1424 | 284 elseif type(file) == "table" and file.read_text ~= nil then |
285 local src = file.read_text() | |
1426 | 286 local src_file = file.uri_string or file.to_uri_string() |
1434
56fb5cd8228d
cache compiled code in temp files
Franklin Schmidt <fschmidt@gmail.com>
parents:
1426
diff
changeset
|
287 return load(src,src_file,true) |
1424 | 288 else |
289 error("bad argument, expected string or uri table but got "..type(file)) | |
290 end | |
291 end | |
292 | |
1280 | 293 |
1283
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
294 local error_mt = {} |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
295 |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
296 function error_mt.__to_string(t) |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
297 return t.java.getLuanStackTraceString() |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
298 end |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
299 |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
300 function Boot.new_error_table(ex) -- for LuanException.java |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
301 local this = {} |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
302 set_metatable(this,error_mt) |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
303 this.java = ex |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
304 this.get_message = ex.getMessage |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
305 this.throw = ex.throwThis |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
306 this.get_stack_trace_string = ex.getLuanStackTraceString |
1347
643cf1c37723
move webserver to lib and bug fixes
Franklin Schmidt <fschmidt@gmail.com>
parents:
1330
diff
changeset
|
307 this.get_java_stack_trace_string = ex.getJavaStackTraceString |
1283
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
308 return this |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
309 end |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
310 |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
311 |
1280 | 312 return Boot |