feat: StructuredFieldを扱えるように

This commit is contained in:
usbharu 2024-09-24 11:27:23 +09:00
parent e438411685
commit c15cb74b73
Signed by: usbharu
GPG Key ID: 95CBCF7046307B77
2 changed files with 30 additions and 2 deletions

View File

@ -1,5 +1,6 @@
package dev.usbharu.httpsignature.v2
import org.greenbytes.http.sfv.Parser
import java.net.http.HttpRequest
import kotlin.jvm.optionals.getOrNull
@ -56,15 +57,30 @@ class SignatureBaseBuilder {
return this
}
fun header(headerName:String,headerValues:List<String>): SignatureBaseBuilder {
fun header(headerName: String, headerValues: List<String>): SignatureBaseBuilder {
signatureBase.addComponent(HttpMessageComponent(headerName, headerValues))
return this
}
fun header(headerName: String,headerValue:String): SignatureBaseBuilder {
fun header(headerName: String, headerValue: String): SignatureBaseBuilder {
return header(headerName, listOf(headerValue))
}
fun structuredFieldItem(fieldName: String, fieldValue: String): SignatureBaseBuilder {
signatureBase.addComponent(StructuredFieldComponent(fieldName, Parser(fieldValue).parseItem()))
return this
}
fun structuredFieldList(fieldName: String, fieldValue: String): SignatureBaseBuilder {
signatureBase.addComponent(StructuredFieldComponent(fieldName, Parser(fieldValue).parseList()))
return this
}
fun structuredFieldDictionary(fieldName: String, fieldValue: String): SignatureBaseBuilder {
signatureBase.addComponent(StructuredFieldComponent(fieldName, Parser(fieldValue).parseDictionary()))
return this
}
companion object {
fun fromHttpRequest(httpRequest: HttpRequest): SignatureBaseBuilder {
val signatureBaseBuilder = SignatureBaseBuilder()

View File

@ -0,0 +1,12 @@
package dev.usbharu.httpsignature.v2
import org.greenbytes.http.sfv.Type
class StructuredFieldComponent(val name: String, val structuredField: Type<*>) : Component {
override val componentName: String
get() = name
override val componentParameter: String
get() = "sf"
override val componentValue: String
get() = structuredField.serialize()
}