scoop:scoop.url:0.1.0
src/percent.scoop
Selected production source · src/percent.scoop
1
package scoop.url3
import scoop.bytes.ByteArrayBuilder4
import scoop.text.contains6
/**7
* URI percent encoding for arbitrary bytes and UTF-8 text. A plus sign is ordinary data here; form8
* encoding has a separate codec.9
*/10
public object PercentEncoding {11
/**12
* Percent-encodes arbitrary bytes, or the UTF-8 encoding of text, leaving only unreserved URI13
* characters literal.14
*/15
public fun encode(bytes: List<Byte>): String = encodeUrlBytes(bytes, false, "")16
/**17
* Percent-encodes arbitrary bytes, or the UTF-8 encoding of text, leaving only unreserved URI18
* characters literal.19
*/20
public fun encode(text: String): String = encode(text.toByteArray())21
/**22
* Decodes percent escapes into arbitrary bytes. Malformed escapes are rejected; plus is not23
* converted to space.24
*/25
public fun decode(text: String): Array<Byte> = decodeUrlBytes(text, false)26
/**27
* Decodes percent escapes and validates the result as UTF-8 text.28
*/29
public fun decodeUtf8(text: String): String = urlUtf8(decode(text), UrlComponent.PercentEncoding)30
}32
internal fun hexDigit(value: Int): Int =33
if (value >= 48 && value <= 57) value - 4834
else if (value >= 65 && value <= 70) value - 5535
else if (value >= 97 && value <= 102) value - 87 else -137
internal fun isUnreserved(value: Int): Boolean =38
(value >= 65 && value <= 90) || (value >= 97 && value <= 122) ||39
(value >= 48 && value <= 57) || value == 45 || value == 46 || value == 95 || value == 12641
internal fun appendPercent(output: ByteArrayBuilder, value: Byte) {42
val number = value.toInt() and 25543
val digits = "0123456789ABCDEF"44
output.append(37.toByte())45
output.append(digits.getCharAt((number shr 4).toLong()).code.toByte())46
output.append(digits.getCharAt((number and 15).toLong()).code.toByte())47
}49
internal fun encodeUrlBytes(bytes: List<Byte>, form: Boolean, allowed: String): String {50
val output = ByteArrayBuilder()51
for (byte in bytes) {52
val value = byte.toInt() and 25553
if (isUnreserved(value) || (value < 128 && allowed.contains(value.toChar()))) output.append(byte)54
else if (form && value == 32) output.append(43.toByte())55
else appendPercent(output, byte)56
}57
return String.fromUtf8(output.toByteArray())58
}60
internal fun decodeUrlBytes(text: String, form: Boolean): Array<Byte> {61
val input = text.toByteArray()62
val output = ByteArrayBuilder()63
var index = 0L64
while (index < input.size) {65
val value = input[index].toInt() and 25566
if (value == 37) {67
if (input.size - index < 3L) urlError(UrlComponent.PercentEncoding, "incomplete percent escape", Some(index))68
val high = hexDigit(input[index + 1L].toInt())69
val low = hexDigit(input[index + 2L].toInt())70
if (high < 0 || low < 0) urlError(UrlComponent.PercentEncoding, "invalid percent escape", Some(index))71
output.append((high * 16 + low).toByte())72
index += 3L73
} else {74
output.append(if (form && value == 43) 32.toByte() else input[index])75
index += 1L76
}77
}78
return output.toByteArray()79
}81
internal fun urlUtf8(bytes: List<Byte>, component: UrlComponent): String {82
val decoded = String.fromUtf8OrNone(bytes)83
if (decoded == None) urlError(component, "percent-decoded component is not UTF-8")84
return decoded!!85
}87
internal fun canonicalEncoded(text: String, component: UrlComponent): String {88
val input = text.toByteArray()89
val output = ByteArrayBuilder()90
var index = 0L91
while (index < input.size) {92
val byte = input[index]93
val value = byte.toInt() and 25594
if (value <= 32 || value >= 127 || value == 92) urlError(component, "encoded components require URI ASCII", Some(index))95
if (value == 37) {96
if (input.size - index < 3L) urlError(component, "incomplete percent escape", Some(index))97
val high = hexDigit(input[index + 1L].toInt())98
val low = hexDigit(input[index + 2L].toInt())99
if (high < 0 || low < 0) urlError(component, "invalid percent escape", Some(index))100
appendPercent(output, (high * 16 + low).toByte())101
index += 3L102
} else {103
output.append(byte)104
index += 1L105
}106
}107
return String.fromUtf8(output.toByteArray())108
}