mirage / ocaml-uri

RFC3986 URI parsing library for OCaml
Other
98 stars 57 forks source link

Uri.query_of_encoded "" returns a non-empty list #60

Closed seliopou closed 9 years ago

seliopou commented 9 years ago

Uri.query_of_encoded "" returns a non-empty list:

utop # Uri.query_of_encoded "";;
- : (string * string list) list = [("", [])]   

It surprised me that it has this behavior. Is it somehow part of the spec, or is it a bug?

dsheets commented 9 years ago

This is not part of the spec as query string syntax is not standardized.

I don't believe this is a bug because:

# Uri.(query (of_string ""));;
- : (string * string list) list = []
# Uri.(query (of_string "?"));;
- : (string * string list) list = [("", [])]
# Uri.(query (of_string "?="));;
- : (string * string list) list = [("", [""])]
# Uri.(query (of_string "?=,"));;
- : (string * string list) list = [("", [""; ""])]
# Uri.(query (of_string "?=,&"));;
- : (string * string list) list = [("", [""; ""]); ("", [])]

That is, if you have an empty query, the query you have is the query with an empty key and no values. If you don't have a query, this is a different case. I recommend using the query accessor and with_query and with_query' functions when dealing with queries. These functions are aware of the whole URI and so Do The Right Thing with the query constructor ?.

# Uri.(to_string (with_query (of_string "") []));;
- : string = ""
# Uri.(to_string (with_query (of_string "") ["",[]]));;
- : string = "?"

I hope this answers your question. Please feel free to close this issue if you are satisfied.

Thanks for your report!

seliopou commented 9 years ago

This behavior makes a bit more sense to me now. I lightly disagree with it still, but I can understand why it's like that. Thanks!

Closing.