Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #235 from Kami/url_parse_parse_query_string
Url parse parse query string
  • Loading branch information
rphillips committed Jun 5, 2012
2 parents 89faeba + fd29c5a commit e2c9fb9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/luvit/url.lua
Expand Up @@ -16,9 +16,11 @@ limitations under the License.
--]]
local querystring = require('querystring')
local url = {}
function url.parse(url)
function url.parse(url, parseQueryString)
local href = url
local chunk, protocol = url:match("^(([a-z0-9+]+)://)")
url = url:sub((chunk and #chunk or 0) + 1)
Expand All @@ -33,7 +35,11 @@ function url.parse(url)
local pathname = url:match("^[^?]*")
local search = url:sub((pathname and #pathname or 0) + 1)
local query = search:sub(2)
if parseQueryString then
query = querystring.parse(query)
end
return {
href = href,
protocol = protocol,
Expand Down
13 changes: 13 additions & 0 deletions tests/test-url.lua
@@ -0,0 +1,13 @@
require('helper')

local url = require('url')

local path = '/somepath?test=bar&ponies=foo'

local parsed = url.parse(path)
assert(parsed.pathname == '/somepath')
assert(parsed.query == 'test=bar&ponies=foo')

local parsed = url.parse(path, true)
assert(parsed.pathname == '/somepath')
assert(deep_equal(parsed.query, {test = 'bar', ponies = 'foo'}))

0 comments on commit e2c9fb9

Please sign in to comment.