Mercurial Hosting > luan
annotate src/luan/modules/Boot.luan @ 1330:f41919741100
fix security
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 11 Feb 2019 01:38:55 -0700 |
parents | 307e76ccd0d6 |
children | 643cf1c37723 |
rev | line source |
---|---|
1280 | 1 java() |
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 |
1330 | 8 local try = BasicLuan.try_ |
1280 | 9 local StringLuan = require "java:luan.modules.StringLuan" |
10 local match = StringLuan.match -- String.match | |
11 local IoLuan = require "java:luan.modules.IoLuan" | |
12 local LuanUrl = require "java:luan.modules.url.LuanUrl" | |
1330 | 13 local LuanJava = require "java:luan.Luan" |
14 local LuanTable = require "java:luan.LuanTable" | |
1280 | 15 |
16 | |
17 local Boot = {} | |
18 | |
19 | |
20 local function error(message) | |
21 new_error(message).throw() | |
22 end | |
23 Boot.error = error | |
24 | |
1330 | 25 local function no_security(fn) |
26 LuanJava.checkCallerSecurity("no_security") | |
27 return function(...) | |
28 local security = LuanJava.setSecurity(nil) | |
29 return try( { | |
30 fn | |
31 finally = function() | |
32 security and LuanJava.setSecurity(security) | |
33 end | |
34 }, ... ) | |
35 end | |
36 end | |
37 Boot.no_security = no_security | |
38 | |
1280 | 39 |
40 local function new_LuanIn(io) | |
41 local this = {} | |
42 this.java = io | |
43 this.to_string = io.to_string | |
44 this.to_uri_string = io.to_uri_string | |
45 this.read_text = io.read_text | |
46 this.read_binary = io.read_binary | |
47 this.read_lines = io.read_lines | |
48 this.read_blocks = io.read_blocks | |
49 this.exists = io.exists | |
50 this.checksum = io.checksum | |
51 this.charset = io.charset | |
52 this.set_charset = io.set_charset | |
53 return this | |
54 end | |
55 Boot.new_LuanIn = new_LuanIn | |
56 | |
57 local function new_writer(writer) | |
58 local this = {} | |
59 this.java = writer | |
60 this.write = writer.write | |
61 this.close = writer.close | |
62 return this | |
63 end | |
64 | |
65 function Boot.text_writer(out) | |
66 return new_writer( IoLuan.luanWriter(out) ) | |
67 end | |
68 | |
69 Boot.binary_writer = new_writer | |
70 | |
71 local function new_LuanIO(io) | |
72 local this = new_LuanIn(io) | |
73 this.write = io.write | |
74 this.write_text = io.write_text | |
75 | |
76 function this.text_writer() | |
77 return new_writer( io.text_writer() ) | |
78 end | |
79 | |
80 function this.binary_writer() | |
81 return new_writer( io.binary_writer() ) | |
82 end | |
83 | |
84 return this | |
85 end | |
86 | |
87 local schemes = {} | |
1330 | 88 LuanTable.setSecurity(schemes,"schemes") |
1280 | 89 |
90 function schemes.null(path) | |
91 return new_LuanIO( IoLuan.nullIO ) | |
92 end | |
93 | |
94 function schemes.string(path) | |
95 return new_LuanIO( IoLuan.LuanString.new(path) ) | |
96 end | |
97 | |
98 function schemes.classpath(path) | |
99 local cp = IoLuan.classpath(path) | |
100 return cp and new_LuanIn(cp) | |
101 end | |
102 | |
103 function schemes.luan(path) | |
104 return schemes.classpath("luan/modules/"..path) | |
105 end | |
106 | |
107 function schemes.stdin(path) | |
108 local Io = require "luan:Io.luan" | |
109 return Io.stdin | |
110 end | |
111 | |
112 local function url(path,options) | |
113 return new_LuanIn( LuanUrl.new(URL.new(path),options) ) | |
114 end | |
115 | |
116 function schemes.http(path,options) | |
117 return url( "http:"..path, options ) | |
118 end | |
119 | |
120 function schemes.https(path,options) | |
121 return url( "https:"..path, options ) | |
122 end | |
123 | |
124 local function new_BaseOs(io) | |
125 local this = new_LuanIO(io) | |
126 this.wait_for = io.wait_for | |
127 return this | |
128 end | |
129 | |
130 function schemes.os(path,options) | |
131 return new_BaseOs( IoLuan.LuanOs.new(path,options) ) | |
132 end | |
133 | |
134 function schemes.bash(path,options) | |
135 return new_BaseOs( IoLuan.LuanBash.new(path,options) ) | |
136 end | |
137 | |
138 local function new_LuanFile(io) | |
139 local this = new_LuanIO(io) | |
140 this.name = io.file.getName | |
141 this.is_directory = io.file.isDirectory | |
142 this.is_file = io.file.isFile | |
143 this.delete = io.delete | |
144 this.delete_on_exit = io.file.deleteOnExit | |
145 this.mkdir = io.mkdir | |
146 this.last_modified = io.file.lastModified | |
147 this.set_last_modified = io.set_last_modified | |
148 this.length = io.file.length | |
149 this.rename_to = io.rename_to | |
1311
ca742d51b31f
don't delete into symbolic links
Franklin Schmidt <fschmidt@gmail.com>
parents:
1286
diff
changeset
|
150 this.is_symbolic_link = io.is_symbolic_link |
1280 | 151 |
152 function this.child(name) | |
153 return new_LuanFile( io.child(name) ) | |
154 end | |
155 | |
156 function this.children() | |
157 local raw = io.children() | |
158 if raw == nil then | |
159 return nil | |
160 end | |
161 local rtn = {} | |
162 for _, child in ipairs(raw) do | |
163 rtn[#rtn+1] = new_LuanFile(child) | |
164 end | |
165 return rtn | |
166 end | |
167 | |
168 function this.parent() | |
169 return new_LuanFile( io.parent() ) | |
170 end | |
171 | |
172 function this.canonical() | |
173 return new_LuanFile( io.canonical() ) | |
174 end | |
175 | |
176 function this.create_temp_file(prefix,suffix) | |
177 return new_LuanFile( io.create_temp_file(prefix,suffix) ) | |
178 end | |
179 | |
180 return this | |
181 end | |
182 | |
183 function schemes.file(path) | |
184 return new_LuanFile( IoLuan.LuanFile.new(path) ) | |
185 end | |
186 | |
187 Boot.schemes = schemes | |
188 | |
189 | |
190 local function uri(name,options) | |
1286 | 191 local scheme, location = match( name, "(?s)^([^:]+):(.*)$" ) |
1280 | 192 scheme or error( "invalid Io.uri name '"..name.."', missing scheme" ) |
1286 | 193 local opener = schemes[scheme] or error( "invalid scheme '"..scheme.."' in '"..name.."'" ) |
1280 | 194 return opener(location,options) |
195 end | |
196 Boot.uri = uri | |
197 | |
198 | |
199 function Boot.read(uri_str) -- for PackageLuan.java | |
200 local u = uri(uri_str) | |
201 if u==nil or not u.exists() then | |
202 return nil | |
203 end | |
204 return u.read_text() | |
205 end | |
206 | |
207 | |
1283
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
208 local error_mt = {} |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
209 |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
210 function error_mt.__to_string(t) |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
211 return t.java.getLuanStackTraceString() |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
212 end |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
213 |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
214 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
|
215 local this = {} |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
216 set_metatable(this,error_mt) |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
217 this.java = ex |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
218 this.get_message = ex.getMessage |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
219 this.throw = ex.throwThis |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
220 this.get_stack_trace_string = ex.getLuanStackTraceString |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
221 this.get_java_stack_trace_string = ex.getLuanStackTraceString |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
222 return this |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
223 end |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
224 |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
225 |
1280 | 226 return Boot |