added easier method name/usage.

updated comments
This commit is contained in:
Robinson 2024-01-17 12:30:21 +01:00
parent 3aa5a0e4e6
commit d5f856c7a1
No known key found for this signature in database
GPG Key ID: 8E7DB78588BD6F5C
2 changed files with 22 additions and 8 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2023 dorkbox, llc * Copyright 2024 dorkbox, llc
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -49,6 +49,8 @@
* ==================================================================== * ====================================================================
* *
* https://www.talisman.org/%7Eerlkonig/misc/lunatech%5Ewhat-every-webdev-must-know-about-url-encoding/ * https://www.talisman.org/%7Eerlkonig/misc/lunatech%5Ewhat-every-webdev-must-know-about-url-encoding/
* https://github.com/paultuckey/urlrewritefilter/
* https://github.com/paultuckey/urlrewritefilter/blob/main/src/main/java/org/tuckey/web/filters/urlrewrite/utils/URLDecoder.java
*/ */
package dorkbox.netUtil.web package dorkbox.netUtil.web

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2023 dorkbox, llc * Copyright 2024 dorkbox, llc
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -49,6 +49,8 @@
* ==================================================================== * ====================================================================
* *
* https://www.talisman.org/%7Eerlkonig/misc/lunatech%5Ewhat-every-webdev-must-know-about-url-encoding/ * https://www.talisman.org/%7Eerlkonig/misc/lunatech%5Ewhat-every-webdev-must-know-about-url-encoding/
* https://github.com/paultuckey/urlrewritefilter/
* https://github.com/paultuckey/urlrewritefilter/blob/main/src/main/java/org/tuckey/web/filters/urlrewrite/utils/URLEncoder.java
*/ */
package dorkbox.netUtil.web package dorkbox.netUtil.web
@ -220,24 +222,34 @@ object URLEncoder {
} }
/** /**
* Encodes a string to be a valid path parameter URL, which means it can contain PCHAR* only (do not put the leading * Encodes a string to be a valid path parameter URL, which means it can contain APLHA, NUMBERS, and [: @ & = + $ ,] only
* ";" or it will be escaped.
* *
* @throws UnsupportedEncodingException * @throws UnsupportedEncodingException
*/ */
@Throws(UnsupportedEncodingException::class) @Throws(UnsupportedEncodingException::class)
fun encodePathParam(pathParam: String, charset: Charset): String { fun encode(pathParam: String, charset: Charset = Charsets.UTF_8): String {
return encodePathSegment(pathParam, charset) return encodePathSegment(pathParam, charset)
} }
/** /**
* Encodes a string to be a valid path segment URL, which means it can contain PCHAR* only (do not put path * Encodes a string to be a valid path parameter URL, which means it can contain APLHA, NUMBERS, and [: @ & = + $ ,] only
* parameters or they will be escaped. * - do not put the leading ";" or it will be escaped.
* *
* @throws UnsupportedEncodingException * @throws UnsupportedEncodingException
*/ */
@Throws(UnsupportedEncodingException::class) @Throws(UnsupportedEncodingException::class)
fun encodePathSegment(pathSegment: String, charset: Charset): String { fun encodePathParam(pathParam: String, charset: Charset = Charsets.UTF_8): String {
return encodePathSegment(pathParam, charset)
}
/**
* Encodes a string to be a valid path segment URL, which means it can contain APLHA, NUMBERS, and [: @ & = + $ ,] only
* -do not put path parameters or they will be escaped
*
* @throws UnsupportedEncodingException
*/
@Throws(UnsupportedEncodingException::class)
fun encodePathSegment(pathSegment: String, charset: Charset = Charsets.UTF_8): String {
// start at *3 for the worst case when everything is %encoded on one byte // start at *3 for the worst case when everything is %encoded on one byte
val encoded = StringBuffer(pathSegment.length * 3) val encoded = StringBuffer(pathSegment.length * 3)
val toEncode = pathSegment.toCharArray() val toEncode = pathSegment.toCharArray()