mirror of
https://github.com/gbevin/urlencoder.git
synced 2025-04-24 23:07:12 -07:00
Made the encoding even more defensive.
This commit is contained in:
parent
320be3b3a1
commit
b12f3eafd7
3 changed files with 32 additions and 17 deletions
26
README.md
26
README.md
|
@ -8,21 +8,27 @@
|
|||
|
||||
# URL Encoder for Java
|
||||
|
||||
A simple library to encode/decode URL parameters.
|
||||
A simple defensive library to encode/decode URL components.
|
||||
|
||||
This library was extracted from the [RIFE2 Web Application Framework](https://rife2.com).
|
||||
A Kotlin version can also be found at [https://github.com/ethauvin/urlencoder](https://github.com/ethauvin/urlencoder).
|
||||
|
||||
For decades, we've been using [java.net.URLEncoder](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/net/URLEncoder.html)
|
||||
because of its improper naming. It is actually intended to encode HTML form
|
||||
parameters, not URLs, causing the wrong escape sequences to be used.
|
||||
The rules are determined by combining the unreserved character set from
|
||||
[RFC 3986](https://www.rfc-editor.org/rfc/rfc3986#page-13) with the
|
||||
percent-encode set from
|
||||
[application/x-www-form-urlencoded](https://url.spec.whatwg.org/#application-x-www-form-urlencoded-percent-encode-set).
|
||||
|
||||
Additionally, `java.net.URLEncoder` allocates memory even when no encoding is
|
||||
necessary, significantly impacting performance. This library has a negligible
|
||||
performance impact when a specified string doesn't need to be encoded.
|
||||
Both specs above support percent decoding of two hexadecimal digits to a
|
||||
binary octet, however their unreserved set of characters differs and
|
||||
`application/x-www-form-urlencoded` adds conversion of space to `+`,
|
||||
that has the potential to be misunderstood.
|
||||
|
||||
Android's [Uri.encode](https://developer.android.com/reference/android/net/Uri#encode(java.lang.String,%20java.lang.String))
|
||||
also addresses the same issues.
|
||||
This class encodes with rules that will be decoded correctly in either case.
|
||||
|
||||
Additionally, this library allocates no memory when encoding isn't needed and
|
||||
does the work in a single pass without multiple loops, both of these
|
||||
optimizations have a significantly beneficial impact on performance of encoding
|
||||
compared to other solutions like the standard URLEncoder in the JDK.
|
||||
|
||||
## Examples (TL;DR)
|
||||
|
||||
|
@ -57,7 +63,7 @@ You have two options:
|
|||
The usage is as follows:
|
||||
|
||||
```
|
||||
Encode and decode URL parameters.
|
||||
Encode and decode URL components defensively.
|
||||
-e encode (default)
|
||||
-d decode
|
||||
```
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue