Fork me on GitHub
#off-topic
<
2024-01-15
>
roklenarcic18:01:24

I always get entangled into the rules of URL parameter encoding and cannot make sense of this. If I have URL parameter my param and value & & &%%%% I have to form encode the key and value of the param, so I get this URL: /?my+param=%26+%26+%26%25%25%25%25 but then if I put it into URI only part of it gets decoded:

(bean (URI. "/?my+param=%26+%26+%26%25%25%25%25"))
=>
{:path "/",
 :rawQuery "my+param=%26+%26+%26%25%25%25%25",
 :fragment nil,
 :authority nil,
 :rawAuthority nil,
 :port -1,
 :absolute false,
 :host nil,
 :rawPath "/",
 :opaque false,
 :rawSchemeSpecificPart "/?my+param=%26+%26+%26%25%25%25%25",
 :class java.net.URI,
 :rawUserInfo nil,
 :query "my+param=&+&+&%%%%",
 :rawFragment nil,
 :scheme nil,
 :userInfo nil,
 :schemeSpecificPart "/?my+param=&+&+&%%%%"}
Why is that? I always get tripped up by this encoding stuff.

hiredman19:01:18

URLs and URIs are not the same

user=> (codec/form-decode (.getQuery (java.net.URL. "")))
{"my param" "=& & &%%%%"}
user=> (codec/form-decode (.getQuery (java.net.URI. "")))
{"my param" "=", " " ["" ""]}
user=>

👀 2
roklenarcic21:01:44

Interesting… really haven’t considered that

Vincent22:01:43

can base64 encode before and base64 decode after

Vincent22:01:30

#?(:clj (:import [java.util Base64])) is what i have been using lately, and one on the clojurescript side from the goog.crypto

roklenarcic12:01:08

The issue I am running into now is that URL doesn’t support relative paths

Vincent13:01:04

what are you fixin' to do

roklenarcic13:01:41

I want to parse parameters out of an URL correctly

roklenarcic18:01:34

It gets worse:

(codec/form-decode (.getQuery (URI. "/?my+param=%26+%26+%26%25%25%25%25")))
=> {"my param" "", " " ["" ""]}

Juλian (he/him)18:01:02

Shouldn't space be encoded %20 and not plus?

roklenarcic18:01:05

That’s what ring.util.codec/form-encode produces

roklenarcic18:01:15

And + is valid as space in URLs

roklenarcic18:01:50

Another problem is if you have = in there:

(bean (URI. "/?my+param=%3D%26+%26+%26%25%25%25%25"))
=>
{:path "/",
 :rawQuery "my+param=%3D%26+%26+%26%25%25%25%25",
 :fragment nil,
 :authority nil,
 :rawAuthority nil,
 :port -1,
 :absolute false,
 :host nil,
 :rawPath "/",
 :opaque false,
 :rawSchemeSpecificPart "/?my+param=%3D%26+%26+%26%25%25%25%25",
 :class java.net.URI,
 :rawUserInfo nil,
 :query "my+param==&+&+&%%%%",
 :rawFragment nil,
 :scheme nil,
 :userInfo nil,
 :schemeSpecificPart "/?my+param==&+&+&%%%%"}

seancorfield18:01:59

(just a friendly admin reminder to use threads for answers please @julian608 @roklenarcic)

👍 1
Vincent22:01:44

when the string contains 31337

🔥 1