scoop:scoop.http:0.1.0
src/message/location.scoop
Selected production source · src/message/location.scoop
1
package scoop.http3
import scoop.url.Url4
import scoop.url.UrlParseException6
/**7
* A parsed Location URL reference, preserving original bytes and requiring a base URL for relative8
* resolution.9
*/10
public class HttpLocation private constructor(11
/**12
* The parsed absolute or relative URL reference.13
*/14
public val reference: Url,15
/**16
* The original parsed field-value bytes, or None for a programmatically constructed value.17
*/18
public val originalValue: Array<Byte>?19
) {20
public constructor(reference: Url): this(reference, None) {}22
/**23
* Resolves against the base URL, inheriting its fragment when the Location reference omits24
* one.25
*/26
public fun resolve(base: Url): Url {27
val resolved = base.resolve(reference)28
return if (reference.encodedFragment == None && base.encodedFragment != None)29
resolved.copy { encodedFragment = base.encodedFragment }30
else31
resolved32
}34
/**35
* Serializes the structured value into HTTP field-value bytes.36
*/37
public fun toByteArray(): Array<Byte> = reference.toString().toByteArray()38
/**39
* Builds the corresponding named HTTP field from the structured value.40
*/41
public fun toHttpField(): HttpField = HttpField("Location".toByteArray(), toByteArray())43
/**44
* Construction and parsing of this structured HTTP field.45
*/46
public companion object {47
/**48
* Reads the Location field, returning None when absent and rejecting duplicates.49
*/50
public fun parseFields(fields: List<HttpField>): HttpLocation? {51
val values = fields.findAll("Location".toByteArray())52
if (values.size == 0L)53
return None54
if (values.size > 1L)55
throw HttpFieldParseException("Location", 0L, "Location must occur once")56
return Some(parse(values[0L].value))57
}59
/**60
* Parses the complete field value, throwing HttpFieldParseException for malformed syntax.61
*/62
public fun parse(value: List<Byte>): HttpLocation {63
val reader = HttpFieldReader("Location", value)64
reader.skipOws()65
val start = reader.position66
var end = reader.bytes.size67
while (68
end >69
start &&70
(reader.bytes[end - 1L] == 32.toByte() || reader.bytes[end - 1L] == 9.toByte())71
)72
end -= 1L73
for (index in start ..< end) {74
if (reader.bytes[index].toUByte().toInt() >= 128)75
reader.failAt(index, "Location requires URI ASCII")76
}77
val reference =78
try {79
Url.parse(String.fromUtf8(reader.slice(start, end)))80
}81
catch (error: UrlParseException) {82
throw HttpFieldParseException(83
"Location",84
start + (error.offset ?: 0L),85
error.detail,86
Some(error)87
)88
}89
return HttpLocation(reference, Some(reader.bytes))90
}91
/**92
* Parses the complete field value, throwing HttpFieldParseException for malformed syntax.93
*/94
public fun parse(value: String): HttpLocation = parse(value.toByteArray())95
/**96
* Parses the complete value, returning None for HttpFieldParseException while propagating97
* unrelated failures.98
*/99
public fun parseOrNone(value: List<Byte>): HttpLocation? {100
try {101
return Some(parse(value))102
}103
catch (_: HttpFieldParseException) {104
return None105
}106
}107
/**108
* Parses the complete value, returning None for HttpFieldParseException while propagating109
* unrelated failures.110
*/111
public fun parseOrNone(value: String): HttpLocation? = parseOrNone(value.toByteArray())112
}113
}