Skip to content

Commit bbeaedc

Browse files
Merge pull request #5 from GabrielNicolasAvellaneda/master
Better url handling.
2 parents 7d376eb + 90f3a02 commit bbeaedc

File tree

1 file changed

+94
-81
lines changed

1 file changed

+94
-81
lines changed

modules/framework.lua

Lines changed: 94 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,18 @@ local boundary = require('boundary')
4242
local io = require('io')
4343
local hrtime = require('uv').Process.hrtime
4444

45-
framework.version = '0.9.6'
45+
local callable = function (class, func)
46+
class.meta.__call = func
47+
end
48+
49+
local factory = function (class)
50+
local mt = getmetatable(class)
51+
mt.__call = function (t, ...)
52+
return t:new(...)
53+
end
54+
end
55+
56+
framework.version = '0.9.9'
4657
framework.boundary = boundary
4758
framework.params = boundary.param or json.parse(fs.readFileSync('param.json')) or {}
4859
framework.plugin_params = boundary.plugin or json.parse(fs.readFileSync('plugin.json')) or {}
@@ -55,80 +66,6 @@ framework.table = {}
5566
framework.util = {}
5667
framework.http = {}
5768

58-
-- Remove this when we migrate to luvit 2.0.x
59-
function framework.util.parseUrl(url, parseQueryString)
60-
assert(url, 'parse expect a non-nil value')
61-
local href = url
62-
local chunk, protocol = url:match("^(([a-z0-9+]+)://)")
63-
url = url:sub((chunk and #chunk or 0) + 1)
64-
65-
local auth
66-
chunk, auth = url:match('(([0-9a-zA-Z]+:?[0-9a-zA-Z]+)@)')
67-
url = url:sub((chunk and #chunk or 0) + 1)
68-
69-
local host
70-
local hostname
71-
local port
72-
if protocol then
73-
host = url:match("^([%a%.%d-]+:?%d*)")
74-
if host then
75-
hostname = host:match("^([^:/]+)")
76-
port = host:match(":(%d+)$")
77-
end
78-
url = url:sub((host and #host or 0) + 1)
79-
end
80-
81-
host = hostname -- Just to be compatible with our code base. Discuss this.
82-
83-
local path
84-
local pathname
85-
local search
86-
local query
87-
local hash
88-
hash = url:match("(#.*)$")
89-
url = url:sub(1, (#url - (hash and #hash or 0)))
90-
91-
if url ~= '' then
92-
path = url
93-
local temp
94-
temp = url:match("^[^?]*")
95-
if temp ~= '' then
96-
pathname = temp
97-
end
98-
temp = url:sub((pathname and #pathname or 0) + 1)
99-
if temp ~= '' then
100-
search = temp
101-
end
102-
if search then
103-
temp = search:sub(2)
104-
if temp ~= '' then
105-
query = temp
106-
end
107-
end
108-
end
109-
110-
if parseQueryString then
111-
query = querystring.parse(query)
112-
end
113-
114-
return {
115-
href = href,
116-
protocol = protocol,
117-
host = host,
118-
hostname = hostname,
119-
port = port,
120-
path = path or '/',
121-
pathname = pathname or '/',
122-
search = search,
123-
query = query,
124-
auth = auth,
125-
hash = hash
126-
}
127-
end
128-
129-
_url.parse = framework.util.parseUrl
130-
131-
13269
do
13370
local encode_alphabet = {
13471
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
@@ -323,6 +260,79 @@ function framework.string.trim(self)
323260
end
324261
local trim = framework.string.trim
325262

263+
function framework.util.parseUrl(url, parseQueryString)
264+
assert(url, 'parse expect a non-nil value')
265+
url = trim(url)
266+
local href = url
267+
local chunk, protocol = url:match("^(([a-zA-Z0-9+]+)://)")
268+
url = url:sub((chunk and #chunk or 0) + 1)
269+
270+
local auth
271+
chunk, auth = url:match('(([0-9a-zA-Z]+:?[0-9a-zA-Z]+)@)')
272+
url = url:sub((chunk and #chunk or 0) + 1)
273+
274+
local host
275+
local hostname
276+
local port
277+
if protocol then
278+
host = url:match("^([%a%.%d-]+:?%d*)")
279+
if host then
280+
hostname = host:match("^([^:/]+)")
281+
port = host:match(":(%d+)$")
282+
end
283+
url = url:sub((host and #host or 0) + 1)
284+
end
285+
286+
host = hostname -- Just to be compatible with our code base. Discuss this.
287+
288+
local path
289+
local pathname
290+
local search
291+
local query
292+
local hash
293+
hash = url:match("(#.*)$")
294+
url = url:sub(1, (#url - (hash and #hash or 0)))
295+
296+
if url ~= '' then
297+
path = url
298+
local temp
299+
temp = url:match("^[^?]*")
300+
if temp ~= '' then
301+
pathname = temp
302+
end
303+
temp = url:sub((pathname and #pathname or 0) + 1)
304+
if temp ~= '' then
305+
search = temp
306+
end
307+
if search then
308+
temp = search:sub(2)
309+
if temp ~= '' then
310+
query = temp
311+
end
312+
end
313+
end
314+
315+
if parseQueryString then
316+
query = querystring.parse(query)
317+
end
318+
319+
return {
320+
href = href,
321+
protocol = protocol,
322+
host = host,
323+
hostname = hostname,
324+
port = port,
325+
path = path or '/',
326+
pathname = pathname or '/',
327+
search = search,
328+
query = query,
329+
auth = auth,
330+
hash = hash
331+
}
332+
end
333+
334+
_url.parse = framework.util.parseUrl
335+
326336
--- Returns the char from a string at the specified position.
327337
-- @param str the string from were a char will be extracted.
328338
-- @param pos the position in the string. Should be a numeric value greater or equal than 1.
@@ -512,7 +522,7 @@ end
512522
-- @param link the link to check
513523
-- @return true if the link is relative. false otherwise.
514524
function framework.util.isRelativeLink(link)
515-
return not string.match(link, '^https?')
525+
return not string.match(string.lower(link), '^https?')
516526
end
517527

518528
--- Wraps a function to calculate the time passed between the wrap and the function execution.
@@ -873,6 +883,7 @@ exportable(framework.http)
873883
-- Work as a cache of values
874884
-- @type Cache
875885
local Cache = Object:extend()
886+
factory(Cache, factory)
876887

877888
--- Cache constructor.
878889
-- @name Cache:new
@@ -1014,7 +1025,7 @@ function NetDataSource:fetch(context, callback)
10141025
self:onFetch(self.socket)
10151026
if callback then
10161027
self.socket:once('data', function (data)
1017-
callback(data)
1028+
callback(data, {context = self})
10181029
if self.close_connection then
10191030
self:disconnect()
10201031
end
@@ -1051,6 +1062,7 @@ framework.NetDataSource = NetDataSource
10511062
--- DataSourcePoller class
10521063
-- @type DataSourcePoller
10531064
local DataSourcePoller = Emitter:extend()
1065+
factory(DataSourcePoller)
10541066

10551067
--- DataSourcePoller constructor.
10561068
-- DataSourcePoller Polls a DataSource at the specified interval and calls a callback when there is some data available.
@@ -1088,6 +1100,7 @@ end
10881100
-- @type Plugin
10891101
local Plugin = Emitter:extend()
10901102
framework.Plugin = Plugin
1103+
factory(Plugin)
10911104

10921105
--- Plugin constructor.
10931106
-- A base plugin implementation that accept a dataSource and polls periodically for new data and format the output so the boundary meter can collect the metrics.
@@ -1272,6 +1285,7 @@ end
12721285
--- Acumulator Class
12731286
-- @type Accumulator
12741287
local Accumulator = Emitter:extend()
1288+
factory(Accumulator)
12751289

12761290
--- Accumulator constructor.
12771291
-- Track values and return the delta for accumulated metrics.
@@ -1316,15 +1330,14 @@ function Accumulator:resetAll()
13161330
end
13171331

13181332
-- The object instance can be used as a function call that calls accumulate.
1319-
Accumulator.meta.__call = function (t, ...)
1320-
return t:accumulate(...)
1321-
end
1333+
callable(Accumulator, function (t, ...) return t:accumulate(...) end)
13221334

13231335
framework.Accumulator = Accumulator
13241336

13251337
--- A Collection of DataSourcePoller
13261338
-- @type PollerCollection
13271339
local PollerCollection = Emitter:extend()
1340+
factory(PollerCollection)
13281341

13291342
--- PollerCollection constructor
13301343
-- @param[opt] pollers a list of poller to initially add to this collection.
@@ -1439,7 +1452,7 @@ function WebRequestDataSource:fetch(context, callback, params)
14391452
end
14401453

14411454
local req
1442-
if options.protocol == 'https' then
1455+
if string.lower(options.protocol) == 'https' then
14431456
req = https.request(options, success)
14441457
else
14451458
req = http.request(options, success)

0 commit comments

Comments
 (0)