Skip to content

Commit

Permalink
Merge pull request #43 from JuliaWeb/jq/queryparampairs
Browse files Browse the repository at this point in the history
Add queryparampairs to support duplicate query param key case
  • Loading branch information
quinnj authored Jul 17, 2022
2 parents 85e5eaf + c6d2e84 commit 0ce3d89
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ name: CI
on:
- push
- pull_request
concurrency:
group: ${{ github.head_ref }}
cancel-in-progress: true
jobs:
test:
name: Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} - ${{ github.event_name }}
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "URIs"
uuid = "5c2747f8-b7ea-4ff2-ba2e-563bfd36b1d4"
authors = ["Jacob Quinn", "Sam O'Connor", "contributors: https://github.com/JuliaWeb/URIs.jl/graphs/contributors"]
version = "1.3.1"
version = "1.4.0"

[compat]
julia = "1.6"
Expand Down
1 change: 1 addition & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Dict{String,String} with 2 entries:
```@docs
URI
queryparams
queryparampairs
absuri
escapeuri
unescapeuri
Expand Down
22 changes: 21 additions & 1 deletion src/URIs.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module URIs

export URI,
queryparams, absuri,
queryparams, queryparampairs, absuri,
escapeuri, unescapeuri, escapepath,
resolvereference

Expand Down Expand Up @@ -267,6 +267,8 @@ uristring(u::URI) = uristring(u.scheme, u.userinfo, u.host, u.port,
Returns a `Dict` containing the `query` parameter string parsed according to
the key=value pair formatting convention.
Note that duplicate query param values are not supported; if needed, use `queryparampairs`.
Note that this is not part of the formal URI grammar, merely a common parsing
convention — see [RFC 3986](https://tools.ietf.org/html/rfc3986#section-3.4).
"""
Expand All @@ -278,6 +280,24 @@ function queryparams(q::AbstractString)
for e in split(q, "&", keepempty=false)))
end

"""
queryparampairs(::URI) -> Vector{Pair{String, String}}
queryparampairs(query_str::AbstractString) -> Vector{Pair{String, String}}
Identical to `queryparams`, but returns a `Vector{Pair{String, String}}` containing the `query` parameter string parsed according to
the key=value pair formatting convention.
Note that this is not part of the formal URI grammar, merely a common parsing
convention — see [RFC 3986](https://tools.ietf.org/html/rfc3986#section-3.4).
"""
queryparampairs(uri::URI) = queryparampairs(uri.query)

function queryparampairs(q::AbstractString)
[unescapeuri(decodeplus(k)) => unescapeuri(decodeplus(v))
for (k,v) in ([split(e, "=")..., ""][1:2]
for e in split(q, "&", keepempty=false))]
end

# Validate known URI formats
const uses_authority = ["https", "http", "ws", "wss", "hdfs", "ftp", "gopher", "nntp", "telnet", "imap", "wais", "file", "mms", "shttp", "snews", "prospero", "rtsp", "rtspu", "rsync", "svn", "svn+ssh", "sftp" ,"nfs", "git", "git+ssh", "ldap", "s3", "ssh"]
const non_hierarchical = ["gopher", "hdl", "mailto", "news", "telnet", "wais", "imap", "snews", "sip", "sips"]
Expand Down
1 change: 1 addition & 0 deletions test/uri.jl
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ urltests = URLTest[
@test queryparams(URI("http://example.com"))::Dict{String,String} == Dict()
@test queryparams(URI("https://httphost/path1/path2;paramstring?q=a&p=r#frag")) == Dict("q"=>"a","p"=>"r")
@test queryparams(URI("https://foo.net/?q=a&malformed")) == Dict("q"=>"a","malformed"=>"")
@test queryparampairs(URI("http://example.com?a=b&a=c&a=d"))::Vector{Pair{String, String}} == ["a" => "b", "a" => "c", "a" => "d"]
end

@testset "Parse Errors" begin
Expand Down

2 comments on commit 0ce3d89

@quinnj
Copy link
Member Author

@quinnj quinnj commented on 0ce3d89 Jul 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/64388

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.4.0 -m "<description of version>" 0ce3d89b81dadaae52b104353843ef67ceecd176
git push origin v1.4.0

Please sign in to comment.