scoop:scoop.http:0.1.0
src/message/content_disposition.scoop
Selected production source · src/message/content_disposition.scoop
1
package scoop.http3
import scoop.charset.Charset4
import scoop.collections.emptyList5
import scoop.text.lowercaseAscii7
/**8
* The selected filename text/language and any failure decoding filename*, retained when falling9
* back to filename.10
*/11
public class HttpFilename internal constructor(12
/**13
* The selected decoded filename, or None when unavailable.14
*/15
public val text: String?,16
/**17
* The language tag from a valid extended filename.18
*/19
public val language: String?,20
/**21
* The filename* decoding error retained when fallback was needed.22
*/23
public val extendedFailure: HttpFieldParseException?24
)26
/**27
* A disposition token with unique parameters. Filename interpretation is explicit and does not28
* sanitize a filesystem path.29
*/30
public class HttpContentDisposition private constructor(31
/**32
* The normalized disposition token.33
*/34
public val disposition: String,35
/**36
* The parsed named parameters in their stored order.37
*/38
public val parameters: Array<HttpParameter>,39
/**40
* The original parsed field-value bytes, or None for a programmatically constructed value.41
*/42
public val originalValue: Array<Byte>?43
) {44
public constructor(disposition: String, parameters: List<HttpParameter> = emptyList<HttpParameter>())45
: this(httpNormalizedToken(disposition), uniqueHttpParameters(parameters), None) {}47
/**48
* Looks up a named parameter using ASCII case val folding, returning None when absent.49
*/50
public fun parameter(name: String): HttpParameter? = findHttpParameter(parameters, name)51
/**52
* Serializes the structured value into HTTP field-value bytes.53
*/54
public fun toByteArray(): Array<Byte> = encodeHttpParameters(disposition, parameters)55
/**56
* Builds the corresponding named HTTP field from the structured value.57
*/58
public fun toHttpField(): HttpField = HttpField("Content-Disposition".toByteArray(), toByteArray())60
/**61
* Prefers decoded filename*, falling back to ISO-8859-1 filename and retaining any62
* extended-value error. The result is not a sanitized filesystem path.63
*/64
public fun filename(allowIso88591: Boolean = false): HttpFilename {65
var failure: HttpFieldParseException? = None66
when (parameter("filename*")) {67
case Some(val value) -> {68
try {69
val decoded = value.decodeExtended(allowIso88591)70
return HttpFilename(Some(decoded.text), Some(decoded.language), None)71
} catch (error: HttpFieldParseException) { failure = Some(error) }72
}73
case None -> {}74
}75
val text = when (parameter("filename")) {76
case Some(val value) -> Some(Charset.Iso8859_1.decode(value.value))77
case None -> None78
}79
return HttpFilename(text, None, failure)80
}82
/**83
* Construction and parsing of this structured HTTP field.84
*/85
public companion object {86
/**87
* Parses the complete field value, throwing HttpFieldParseException for malformed syntax.88
*/89
public fun parse(value: List<Byte>): HttpContentDisposition {90
val reader = HttpFieldReader("Content-Disposition", value)91
reader.skipOws()92
val disposition = reader.token().lowercaseAscii()93
val parameters = reader.parameters(allowBws = true)94
return HttpContentDisposition(disposition, parameters, Some(reader.bytes))95
}96
/**97
* Parses the complete field value, throwing HttpFieldParseException for malformed syntax.98
*/99
public fun parse(value: String): HttpContentDisposition = parse(value.toByteArray())100
/**101
* Parses the complete value, returning None for HttpFieldParseException while propagating102
* unrelated failures.103
*/104
public fun parseOrNone(value: List<Byte>): HttpContentDisposition? {105
try { return Some(parse(value)) }106
catch (_: HttpFieldParseException) { return None }107
}108
/**109
* Parses the complete value, returning None for HttpFieldParseException while propagating110
* unrelated failures.111
*/112
public fun parseOrNone(value: String): HttpContentDisposition? = parseOrNone(value.toByteArray())113
}114
}