Debian Codegraph @

extensions/Scribunto/includes/Engines/LuaCommon/lualib/mw.uri.lua

raw symbols SCIP
1 local uri = {}
2 local urifuncs = {}
3 local urimt = {}
4 local php
5
6 local util = require 'libraryUtil'
7 local checkType = util.checkType
8 local checkTypeForIndex = util.checkTypeForIndex
9
10 function uri.setupInterface( options )
11 -- Boilerplate
12 uri.setupInterface = nil
13 php = mw_interface
14 mw_interface = nil
15
16 -- Store options
17 php.options = options
18
19 -- Register this library in the "mw" global
20 mw = mw or {}
21 mw.uri = uri
22
23 package.loaded['mw.uri'] = uri
24 end
25
26 local function rawencode( s, space )
27 space = space or '%20'
28 local ret = string.gsub( s, '([^a-zA-Z0-9_.~-])', function ( c )
29 if c == ' ' then
30 return space
31 else
32 return string.format( '%%%02X', string.byte( c, 1, 1 ) )
33 end
34 end );
35 return ret
36 end
37
38 local function wikiencode( s )
39 local ret = string.gsub( s, '([^a-zA-Z0-9!$()*,./:;@~_-])', function ( c )
40 if c == ' ' then
41 return '_'
42 else
43 return string.format( '%%%02X', string.byte( c, 1, 1 ) )
44 end
45 end );
46 return ret
47 end
48
49 local function rawdecode( s )
50 local ret = string.gsub( s, '%%(%x%x)', function ( hex )
51 return string.char( tonumber( hex, 16 ) )
52 end );
53 return ret
54 end
55
56 function uri.encode( s, enctype )
57 checkType( 'encode', 1, s, 'string' )
58
59 enctype = string.upper( enctype or 'QUERY' )
60 if enctype == 'QUERY' then
61 return rawencode( s, '+' )
62 elseif enctype == 'PATH' then
63 return rawencode( s, '%20' )
64 elseif enctype == 'WIKI' then
65 return wikiencode( s )
66 else
67 error( "bad argument #2 to 'encode' (expected QUERY, PATH, or WIKI)", 2 )
68 end
69 end
70
71 function uri.decode( s, enctype )
72 checkType( 'decode', 1, s, 'string' )
73
74 enctype = string.upper( enctype or 'QUERY' )
75 if enctype == 'QUERY' then
76 return rawdecode( string.gsub( s, '%+', ' ' ) )
77 elseif enctype == 'PATH' then
78 return rawdecode( s )
79 elseif enctype == 'WIKI' then
80 return rawdecode( string.gsub( s, '_', ' ' ) )
81 else
82 error( "bad argument #2 to 'decode' (expected QUERY, PATH, or WIKI)", 2 )
83 end
84 end
85
86 function uri.anchorEncode( s )
87 checkType( 'anchorEncode', 1, s, 'string' )
88 return php.anchorEncode( s )
89 end
90
91 function uri.localUrl( page, query )
92 checkType( 'localurl', 1, page, 'string' )
93 if query ~= nil and type( query ) ~= 'string' and type( query ) ~= 'table' then
94 checkType( 'localurl', 2, query, 'string or table' )
95 end
96 local url = php.localUrl( page, query )
97 if not url then
98 return nil
99 end
100 return uri.new( url )
101 end
102
103 function uri.fullUrl( page, query )
104 checkType( 'fullurl', 1, page, 'string' )
105 if query ~= nil and type( query ) ~= 'string' and type( query ) ~= 'table' then
106 checkType( 'fullurl', 2, query, 'string or table' )
107 end
108 local url = php.fullUrl( page, query )
109 if not url then
110 return nil
111 end
112 return uri.new( url )
113 end
114
115 function uri.canonicalUrl( page, query )
116 checkType( 'canonicalurl', 1, page, 'string' )
117 if query ~= nil and type( query ) ~= 'string' and type( query ) ~= 'table' then
118 checkType( 'canonicalurl', 2, query, 'string or table' )
119 end
120 local url = php.canonicalUrl( page, query )
121 if not url then
122 return nil
123 end
124 return uri.new( url )
125 end
126
127
128 function uri.new( s )
129 if s == nil or type( s ) == 'string' then
130 local obj = {
131 -- Yes, technically all this does nothing.
132 protocol = nil,
133 user = nil,
134 password = nil,
135 host = nil,
136 port = nil,
137 path = nil,
138 query = nil,
139 fragment = nil,
140 }
141 setmetatable( obj, urimt )
142 obj:parse( s or php.options.defaultUrl )
143 return obj
144 elseif type( s ) == 'table' then
145 local obj = {
146 protocol = s.protocol,
147 user = s.user,
148 password = s.password,
149 host = s.host,
150 port = s.port,
151 path = s.path,
152 query = mw.clone( s.query ),
153 fragment = s.fragment,
154 }
155 setmetatable( obj, urimt )
156 return obj
157 else
158 checkType( 'new', 1, s, 'string or table or nil' )
159 end
160 end
161
162 function uri.validate( obj )
163 checkType( 'validate', 1, obj, 'table' )
164
165 local err = {}
166
167 if obj.protocol then
168 if type( obj.protocol ) ~= 'string' then
169 err[#err+1] = '.protocol must be a string, not ' .. type( obj.protocol )
170 elseif not string.match( obj.protocol, '^[^:/?#]+$' ) then
171 err[#err+1] = 'invalid .protocol'
172 end
173 end
174
175 if obj.user then
176 if type( obj.user ) ~= 'string' then
177 err[#err+1] = '.user must be a string, not ' .. type( obj.user )
178 elseif not string.match( obj.user, '^[^:@/?#]*$' ) then
179 err[#err+1] = 'invalid .user'
180 end
181 end
182
183 if obj.password then
184 if type( obj.password ) ~= 'string' then
185 err[#err+1] = '.password must be a string, not ' .. type( obj.password )
186 elseif not string.match( obj.password, '^[^:@/?#]*$' ) then
187 err[#err+1] = 'invalid .password'
188 end
189 end
190
191 if obj.host then
192 if type( obj.host ) ~= 'string' then
193 err[#err+1] = '.host must be a string, not ' .. type( obj.host )
194 elseif not (
195 -- Normal syntax
196 string.match( obj.host, '^[^:/?#]*$' ) or
197 -- IP-literal syntax
198 string.match( obj.host, '^%[[^/?#%[%]@]+%]$' )
199 ) then
200 err[#err+1] = 'invalid .host'
201 end
202 end
203
204 if obj.port then
205 if type( obj.port ) ~= 'number' or math.floor( obj.port ) ~= obj.port then
206 err[#err+1] = '.port must be an integer, not ' .. type( obj.port )
207 elseif obj.port < 1 or obj.port > 65535 then
208 err[#err+1] = 'invalid .port'
209 end
210 end
211
212 local authority = obj.user or obj.password or obj.host or obj.port
213 if not obj.path then
214 err[#err+1] = 'missing .path'
215 elseif type( obj.path ) ~= 'string' then
216 err[#err+1] = '.path must be a string, not ' .. type( obj.path )
217 elseif authority and not ( obj.path == '' or string.match( obj.path, '^/[^?#]*$' ) ) then
218 err[#err+1] = 'invalid .path'
219 elseif not authority and not (
220 obj.path == '' or
221 obj.path == '/' or
222 string.match( obj.path, '^/[^?#/][^?#]*$' ) or
223 obj.protocol and string.match( obj.path, '^[^?#/][^?#]*$' ) or
224 not obj.protocol and string.match( obj.path, '^[^?#/:]+$' ) or
225 not obj.protocol and string.match( obj.path, '^[^?#/:]+/[^?#]*$' )
226 ) then
227 err[#err+1] = 'invalid .path'
228 end
229
230 if obj.query and type( obj.query ) ~= 'table' then
231 err[#err+1] = '.query must be a table, not ' .. type( obj.query )
232 end
233
234 if obj.fragment and type( obj.fragment ) ~= 'string' then
235 err[#err+1] = '.fragment must be a string, not ' .. type( obj.fragment )
236 end
237
238 return #err == 0, table.concat( err, '; ' )
239 end
240
241 -- Lua tables are unsorted, but we want to preserve the insertion order.
242 -- So, track the insertion order explicitly.
243 local function makeQueryTable()
244 local ret = {}
245 local keys = {}
246 local seenkeys = {}
247
248 setmetatable( ret, {
249 __newindex = function ( t, k, v )
250 if seenkeys[k] and not t[k] then
251 for i = 1, #keys do
252 if keys[i] == k then
253 table.remove( keys, i )
254 break
255 end
256 end
257 end
258 seenkeys[k] = 1
259 keys[#keys+1] = k
260 rawset( t, k, v )
261 end,
262 __pairs = function ( t )
263 local i, l = 0, #keys
264 return function ()
265 while i < l do
266 i = i + 1
267 local k = keys[i]
268 if t[k] ~= nil then
269 return k, t[k]
270 end
271 end
272 return nil
273 end
274 end
275 } )
276
277 return ret
278 end
279
280 function uri.parseQueryString( s, i, j )
281 checkType( 'parseQueryString', 1, s, 'string' )
282 checkType( 'parseQueryString', 2, i, 'number', true )
283 checkType( 'parseQueryString', 3, j, 'number', true )
284
285 s = string.gsub( string.sub( s, i or 1, j or -1 ), '%+', ' ' )
286 i = 1
287 j = string.len( s )
288
289 local qs = makeQueryTable()
290 if string.sub( s, i, 1 ) == '?' then
291 i = i + 1
292 end
293 while i <= j do
294 local amp = string.find( s, '&', i, true )
295 if not amp or amp > j then
296 amp = j + 1
297 end
298 local eq = string.find( s, '=', i, true )
299 local k, v
300 if not eq or eq > amp then
301 k = rawdecode( string.sub( s, i, amp - 1 ) )
302 v = false
303 else
304 k = rawdecode( string.sub( s, i, eq - 1 ) )
305 v = rawdecode( string.sub( s, eq + 1, amp - 1 ) )
306 end
307 if qs[k] then
308 if type( qs[k] ) ~= 'table' then
309 qs[k] = { qs[k], v }
310 else
311 table.insert( qs[k], v )
312 end
313 else
314 qs[k] = v
315 end
316 i = amp + 1
317 end
318 return qs
319 end
320
321 function uri.buildQueryString( qs )
322 checkType( 'buildQueryString', 1, qs, 'table' )
323
324 local t = {}
325 for k, v in pairs( qs ) do
326 if type( v ) ~= 'table' then
327 v = { v }
328 end
329 for i = 1, #v do
330 t[#t+1] = '&'
331 t[#t+1] = rawencode( k, '+' )
332 if v[i] then
333 t[#t+1] = '='
334 t[#t+1] = rawencode( v[i], '+' )
335 end
336 end
337 end
338 return table.concat( t, '', 2 )
339 end
340
341 -- Fields mapped to whether they're handled by __index
342 local knownFields = {
343 protocol = false,
344 user = false,
345 password = false,
346 host = false,
347 port = false,
348 path = false,
349 query = false,
350 fragment = false,
351 userInfo = true,
352 hostPort = true,
353 authority = true,
354 queryString = true,
355 relativePath = true,
356 }
357
358 local function pairsfunc( t, k )
359 local v, f
360 repeat
361 k, f = next( knownFields, k )
362 if k == nil then
363 return nil
364 end
365 if f then
366 v = t[k]
367 else
368 v = rawget( t, k )
369 end
370 until v ~= nil
371 return k, v
372 end
373 function urimt:__pairs()
374 return pairsfunc, self, nil
375 end
376
377 function urimt:__index( key )
378 if urifuncs[key] then
379 return urifuncs[key]
380 end
381
382 if key == 'userInfo' then
383 local user = rawget( self, 'user' )
384 local password = rawget( self, 'password' )
385 if user and password then
386 return user .. ':' .. password
387 else
388 return user
389 end
390 end
391
392 if key == 'hostPort' then
393 local host = rawget( self, 'host' )
394 local port = rawget( self, 'port' )
395 if port then
396 return ( host or '' ) .. ':' .. port
397 else
398 return host
399 end
400 end
401
402 if key == 'authority' then
403 local info = self.userInfo
404 local hostPort = self.hostPort
405 if info then
406 return info .. '@' .. ( hostPort or '' )
407 else
408 return hostPort
409 end
410 end
411
412 if key == 'queryString' then
413 local query = rawget( self, 'query' )
414 if not query then
415 return nil
416 end
417 return uri.buildQueryString( query )
418 end
419
420 if key == 'relativePath' then
421 local ret = rawget( self, 'path' ) or ''
422 local qs = self.queryString
423 if qs then
424 ret = ret .. '?' .. qs
425 end
426 local fragment = rawget( self, 'fragment' )
427 if fragment then
428 ret = ret .. '#' .. fragment
429 end
430 return ret
431 end
432
433 return nil
434 end
435
436 function urimt:__newindex( key, value )
437 if key == 'userInfo' then
438 local user, password = nil, nil
439 if value then
440 checkTypeForIndex( key, value, 'string' )
441 local i = string.find( value, ':', 1, true )
442 if i then
443 user = string.sub( value, 1, i - 1 )
444 password = string.sub( value, i + 1 )
445 else
446 user = value
447 end
448 end
449 rawset( self, 'user', user )
450 rawset( self, 'password', password )
451 return
452 end
453
454 if key == 'hostPort' then
455 local host, port = nil, nil
456 if value then
457 checkTypeForIndex( key, value, 'string' )
458
459 -- IP-literal syntax, with and without a port
460 host, port = string.match( value, '^(%[[^/?#%[%]@]+%]):(%d+)$' )
461 if port then
462 port = tonumber( port )
463 end
464 if not host then
465 host = string.match( value, '^(%[[^/?#%[%]@]+%])$' )
466 end
467 -- Normal syntax
468 if not host then
469 local i = string.find( value, ':', 1, true )
470 if i then
471 host = string.sub( value, 1, i - 1 )
472 port = tonumber( string.sub( value, i + 1 ) )
473 if not port then
474 error( string.format( "Invalid port in '%s'", value ), 2 )
475 end
476 else
477 host = value
478 end
479 end
480 end
481 rawset( self, 'host', host )
482 rawset( self, 'port', port )
483 return
484 end
485
486 if key == 'authority' then
487 if value then
488 checkTypeForIndex( key, value, 'string' )
489 local i = string.find( value, '@', 1, true )
490 if i then
491 self.userInfo = string.sub( value, 1, i - 1 )
492 self.hostPort = string.sub( value, i + 1 )
493 else
494 self.userInfo = nil
495 self.hostPort = value
496 end
497 else
498 self.userInfo = nil
499 self.hostPort = nil
500 end
501 return
502 end
503
504 if key == 'queryString' then
505 if value then
506 checkTypeForIndex( key, value, 'string' )
507 rawset( self, 'query', uri.parseQueryString( value ) )
508 else
509 rawset( self, 'query', nil )
510 end
511 return
512 end
513
514 if key == 'relativePath' then
515 local path, query, fragment = nil, nil, nil
516 if value then
517 checkTypeForIndex( key, value, 'string' )
518 local i, j = nil, string.len( value )
519 i = string.find( value, '#', 1, true )
520 if i and i <= j then
521 fragment = string.sub( value, i + 1, j )
522 j = i - 1
523 end
524 i = string.find( value, '?', 1, true )
525 if i and i <= j then
526 query = uri.parseQueryString( string.sub( value, i + 1, j ) )
527 j = i - 1
528 end
529 path = string.sub( value, 1, j )
530 end
531 rawset( self, 'path', path )
532 rawset( self, 'query', query )
533 rawset( self, 'fragment', fragment )
534 return
535 end
536
537 if knownFields[key] then
538 error( "index '" .. key .. "' is read only", 2 )
539 end
540
541 -- Default behavior
542 knownFields[key] = false
543 rawset( self, key, value )
544 end
545
546 function urimt:__tostring()
547 local ret = ''
548 local protocol = self.protocol
549 local authority = self.authority
550 if protocol then
551 ret = protocol .. ':'
552 end
553 if authority then
554 ret = ret .. '//' .. authority
555 end
556 return ret .. self.relativePath
557 end
558
559 urifuncs.validate = uri.validate
560
561 function urifuncs:parse( s )
562 checkType( 'uri:parse', 1, s, 'string' )
563
564 -- Since Lua's patterns can't do (...)?, we have to try with and without each part.
565 local protocol, authority, relativePath = string.match( s, '^([^:/?#]+)://([^/?#]*)(.*)$' )
566 if not ( protocol or authority or relativePath ) then
567 authority, relativePath = string.match( s, '^//([^/?#]*)(.*)$' )
568 end
569 if not ( protocol or authority or relativePath ) then
570 protocol, relativePath = string.match( s, '^([^:/?#]+):(.*)$' )
571 end
572 if not ( protocol or authority or relativePath ) then
573 relativePath = s
574 end
575
576 -- Parse it into a temporary object, so if there's an error we didn't break the real one
577 local tmp = { protocol = protocol }
578 setmetatable( tmp, urimt )
579 if not pcall( urimt.__newindex, tmp, 'authority', authority ) then
580 error( 'Invalid port number in string', 2 )
581 end
582 tmp.relativePath = relativePath
583
584 -- Check for validity
585 local ok, err = uri.validate( tmp )
586 if not ok then
587 error( err, 2 )
588 end
589
590 -- Merge in fields
591 if tmp.protocol then
592 self.protocol = tmp.protocol
593 end
594 if tmp.user or tmp.password then
595 self.user, self.password = tmp.user, tmp.password
596 end
597 if tmp.host then
598 self.host = tmp.host
599 end
600 if tmp.port then
601 self.port = tmp.port
602 end
603 if tmp.path then
604 self.path = tmp.path
605 end
606 if tmp.query then
607 self.query = tmp.query
608 end
609 if tmp.fragment then
610 self.fragment = tmp.fragment
611 end
612
613 return self
614 end
615
616 function urifuncs:clone()
617 return uri.new( self )
618 end
619
620 function urifuncs:extend( parameters )
621 checkType( 'uri:extend', 1, parameters, 'table' )
622
623 local query = self.query
624 if not query then
625 query = makeQueryTable()
626 self.query = query
627 end
628 for k, v in pairs( parameters ) do
629 query[k] = v
630 end
631
632 return self
633 end
634
635 -- Add all urifuncs as known fields
636 for k in pairs( urifuncs ) do
637 knownFields[k] = true
638 end
639
640 return uri