Scoop API

scoop:scoop.url:0.1.0

src/percent.scoop

Selected production source · src/percent.scoop

1package scoop.url
3import scoop.bytes.ByteArrayBuilder
4import scoop.text.contains
6/**
7 * URI percent encoding for arbitrary bytes and UTF-8 text. A plus sign is ordinary data here; form
8 * encoding has a separate codec.
9 */
10public object PercentEncoding {
11 /**
12 * Percent-encodes arbitrary bytes, or the UTF-8 encoding of text, leaving only unreserved URI
13 * 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 URI
18 * 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 not
23 * 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)
32internal fun hexDigit(value: Int): Int =
33 if (value >= 48 && value <= 57) value - 48
34 else if (value >= 65 && value <= 70) value - 55
35 else if (value >= 97 && value <= 102) value - 87 else -1
37internal 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 == 126
41internal fun appendPercent(output: ByteArrayBuilder, value: Byte) {
42 val number = value.toInt() and 255
43 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())
49internal 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 255
53 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())
60internal fun decodeUrlBytes(text: String, form: Boolean): Array<Byte> {
61 val input = text.toByteArray()
62 val output = ByteArrayBuilder()
63 var index = 0L
64 while (index < input.size) {
65 val value = input[index].toInt() and 255
66 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 += 3L
73 } else {
74 output.append(if (form && value == 43) 32.toByte() else input[index])
75 index += 1L
76 }
77 }
78 return output.toByteArray()
81internal 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!!
87internal fun canonicalEncoded(text: String, component: UrlComponent): String {
88 val input = text.toByteArray()
89 val output = ByteArrayBuilder()
90 var index = 0L
91 while (index < input.size) {
92 val byte = input[index]
93 val value = byte.toInt() and 255
94 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 += 3L
102 } else {
103 output.append(byte)
104 index += 1L
105 }
106 }
107 return String.fromUtf8(output.toByteArray())