scoop:scoop.http:0.1.0
src/message/field_operations.scoop
Selected production source · src/message/field_operations.scoop
1
package scoop.http3
/**4
* Returns whether all supplied bytes or scalars form a nonempty HTTP token.5
*/6
public fun isHttpToken(value: List<Byte>): Boolean = httpToken(value)7
/**8
* Returns whether all supplied bytes or scalars form a nonempty HTTP token.9
*/10
public fun isHttpToken(value: String): Boolean = httpToken(value.toByteArray())12
/**13
* Compares this field's name using ASCII case folding.14
*/15
public fun HttpField.hasName(name: List<Byte>): Boolean = httpAsciiEqual(this.name, name)17
/**18
* Validates this field's name token and permitted value bytes, throwing on invalid syntax.19
*/20
public fun HttpField.validate() {21
if (!httpToken(name)) throw IllegalArgumentException(Some("invalid ordinary HTTP field name"))22
requireHttpValue(value)23
}25
/**26
* Parses the selected fields as comma-separated HTTP tokens in field order.27
*/28
public fun parseHttpTokenFields(fields: List<HttpField>, name: String): Array<String> {29
if (!isHttpToken(name)) throw IllegalArgumentException(Some("invalid HTTP field name"))30
val result = ArrayList<String>()31
for (field in fields.findAll(name.toByteArray())) {32
for (value in HttpFieldReader(name, field.value).tokenList()) result.add(value)33
}34
return result.toArray()35
}37
/**38
* Parses Content-Length fields, rejecting malformed, overflowing, or conflicting values; absent39
* fields return None.40
*/41
public fun parseHttpContentLength(fields: List<HttpField>): Long? {42
var length: Long? = None43
for (field in fields.findAll("Content-Length".toByteArray())) {44
val reader = HttpFieldReader("Content-Length", field.value)45
reader.skipOws()46
while (true) {47
val offset = reader.position48
val value = reader.decimal()49
when (length) {50
case Some(val expected) -> if (value != expected) reader.failAt(offset, "conflicting Content-Length values")51
case None -> length = Some(value)52
}53
reader.skipOws()54
if (reader.ended) break55
reader.expect(44)56
reader.skipOws()57
}58
}59
return length60
}