restructure project to Kotlin Multiplatform
- custom POM location removed (there are now two POMs, one for the 'Kotlin Multiplatform' publication, and another for Kotlin/JVM, and more are on the way, which would lead to a cluttered build dir) - renamed the directories (the directory name is how Kotlin Multiplatform chooses the published artifact ID, and there's no an easier way to change it.) - updated README examples, and link to `-jvm` variant guide
This commit is contained in:
parent
4df6d3f599
commit
dce203845e
16 changed files with 59 additions and 243 deletions
|
@ -0,0 +1,106 @@
|
|||
/*
|
||||
* Copyright 2001-2023 Geert Bevin (gbevin[remove] at uwyn dot com)
|
||||
* Copyright 2022-2023 Erik C. Thauvin (erik@thauvin.net)
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package net.thauvin.erik.urlencoder
|
||||
|
||||
import kotlin.system.exitProcess
|
||||
|
||||
/**
|
||||
* Most defensive approach to URL encoding and decoding.
|
||||
*
|
||||
* - Rules 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).
|
||||
*
|
||||
* - 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 `+`, which has the
|
||||
* potential to be misunderstood.
|
||||
*
|
||||
* - This library encodes with rules that will be decoded correctly in either case.
|
||||
*
|
||||
* @author Geert Bevin (gbevin(remove) at uwyn dot com)
|
||||
* @author Erik C. Thauvin (erik@thauvin.net)
|
||||
**/
|
||||
object UrlEncoder {
|
||||
|
||||
internal val usage =
|
||||
"Usage : java -jar urlencoder-*all.jar [-ed] text" + System.lineSeparator() +
|
||||
"Encode and decode URL components defensively." + System.lineSeparator() +
|
||||
" -e encode (default) " + System.lineSeparator() +
|
||||
" -d decode"
|
||||
|
||||
/**
|
||||
* Encodes and decodes URLs from the command line.
|
||||
*
|
||||
* - `java -jar urlencoder-*all.jar [-ed] text`
|
||||
*/
|
||||
@JvmStatic
|
||||
fun main(args: Array<String>) {
|
||||
try {
|
||||
val result = processMain(args)
|
||||
if (result.status == 1) {
|
||||
System.err.println(result.output)
|
||||
} else {
|
||||
println(result.output)
|
||||
}
|
||||
exitProcess(result.status)
|
||||
} catch (e: IllegalArgumentException) {
|
||||
System.err.println("${UrlEncoder::class.java.simpleName}: ${e.message}")
|
||||
exitProcess(1)
|
||||
}
|
||||
}
|
||||
|
||||
internal data class MainResult(var output: String = usage, var status: Int = 1)
|
||||
|
||||
internal fun processMain(args: Array<String>): MainResult {
|
||||
val result = MainResult()
|
||||
if (args.isNotEmpty() && args[0].isNotEmpty()) {
|
||||
val hasDecode = (args[0] == "-d")
|
||||
val hasOption = (hasDecode || args[0] == "-e")
|
||||
if (hasOption && args.size == 2 || !hasOption && args.size == 1) {
|
||||
val arg = if (hasOption) args[1] else args[0]
|
||||
if (hasDecode) {
|
||||
result.output = decode(arg)
|
||||
} else {
|
||||
result.output = UrlEncoderUtil.encode(arg)
|
||||
}
|
||||
result.status = 0
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms a provided [String] into a new string, containing decoded URL characters in the UTF-8
|
||||
* encoding.
|
||||
*/
|
||||
@JvmStatic
|
||||
@JvmOverloads
|
||||
fun decode(source: String, plusToSpace: Boolean = false): String =
|
||||
// delegate to UrlEncoderFunctions for backwards compatibility
|
||||
UrlEncoderUtil.decode(source, plusToSpace)
|
||||
|
||||
/**
|
||||
* Transforms a provided [String] object into a new string, containing only valid URL characters in the UTF-8
|
||||
* encoding.
|
||||
*
|
||||
* - Letters, numbers, unreserved (`_-!.'()*`) and allowed characters are left intact.
|
||||
*/
|
||||
@JvmStatic
|
||||
@JvmOverloads
|
||||
fun encode(source: String, allow: String = "", spaceToPlus: Boolean = false): String =
|
||||
UrlEncoderUtil.encode(source, allow, spaceToPlus)
|
||||
}
|
|
@ -0,0 +1,122 @@
|
|||
/*
|
||||
* Copyright 2001-2023 Geert Bevin (gbevin[remove] at uwyn dot com)
|
||||
* Copyright 2022-2023 Erik C. Thauvin (erik@thauvin.net)
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package net.thauvin.erik.urlencoder
|
||||
|
||||
import net.thauvin.erik.urlencoder.UrlEncoder.processMain
|
||||
import net.thauvin.erik.urlencoder.UrlEncoder.usage
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFailsWith
|
||||
|
||||
class UrlEncoderTest {
|
||||
companion object {
|
||||
@JvmStatic
|
||||
var invalid = arrayOf("sdkjfh%", "sdkjfh%6", "sdkjfh%xx", "sdfjfh%-1")
|
||||
|
||||
@JvmStatic
|
||||
var validMap = arrayOf(
|
||||
Pair("a test &", "a%20test%20%26"),
|
||||
Pair(
|
||||
"!abcdefghijklmnopqrstuvwxyz%%ABCDEFGHIJKLMNOPQRSTUVQXYZ0123456789-_.~=",
|
||||
"%21abcdefghijklmnopqrstuvwxyz%25%25ABCDEFGHIJKLMNOPQRSTUVQXYZ0123456789-_.%7E%3D"
|
||||
),
|
||||
Pair("%#okékÉȢ smile!😁", "%25%23ok%C3%A9k%C3%89%C8%A2%20smile%21%F0%9F%98%81"),
|
||||
Pair(
|
||||
"\uD808\uDC00\uD809\uDD00\uD808\uDF00\uD808\uDD00", "%F0%92%80%80%F0%92%94%80%F0%92%8C%80%F0%92%84%80"
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Encode with SpaceToPlus`() {
|
||||
assertEquals("this+is+a+test", UrlEncoder.encode("this is a test", spaceToPlus = true))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Encode with Allow`() {
|
||||
assertEquals("this is a test", UrlEncoder.encode("this is a test", allow = " "))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Encode without Parameters`() {
|
||||
for (m in validMap) {
|
||||
assertEquals(m.second, UrlEncoder.encode(m.first), "encode(${m.first})")
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Main Decode`() {
|
||||
for (m in validMap) {
|
||||
val result: UrlEncoder.MainResult = processMain(arrayOf("-d", m.second))
|
||||
assertEquals(m.first, result.output)
|
||||
assertEquals(0, result.status, "processMain(-d ${m.second}).status")
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Main Decode with Exception`() {
|
||||
for (source in invalid) {
|
||||
assertFailsWith<IllegalArgumentException>(
|
||||
message = source,
|
||||
block = { processMain(arrayOf("-d", source)) }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Main Encode`() {
|
||||
for (m in validMap) {
|
||||
val result = processMain(arrayOf(m.first))
|
||||
assertEquals(m.second, result.output)
|
||||
assertEquals(0, result.status, "processMain(-e ${m.first}).status")
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Main Encode with Option`() {
|
||||
for (m in validMap) {
|
||||
val result = processMain(arrayOf("-e", m.first))
|
||||
assertEquals(m.second, result.output)
|
||||
assertEquals(0, result.status, "processMain(-e ${m.first}).status")
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Main Usage with Empty Args`() {
|
||||
assertEquals(usage, processMain(arrayOf(" ", " ")).output, "processMain(' ', ' ')")
|
||||
assertEquals(usage, processMain(arrayOf("foo", " ")).output, "processMain('foo', ' ')")
|
||||
assertEquals(usage, processMain(arrayOf(" ", "foo")).output, "processMain(' ', 'foo')")
|
||||
assertEquals(usage, processMain(arrayOf("-d ", "")).output, "processMain('-d', '')")
|
||||
assertEquals("%20", processMain(arrayOf("-e", " ")).output, "processMain('-e', ' ')")
|
||||
assertEquals(" ", processMain(arrayOf("-d", " ")).output, "processMain('-d', ' ')")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Main Usage with Invalid arg`() {
|
||||
for (arg in arrayOf("", "-d", "-e")) {
|
||||
val result = processMain(arrayOf(arg))
|
||||
assertEquals(usage, result.output, "processMain('$arg')")
|
||||
assertEquals(1, result.status, "processMain('$arg').status")
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Main Usage with too Many Args`() {
|
||||
assertEquals(usage, processMain(arrayOf("foo", "bar", "test")).output, "too many args")
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue