Skip to content

Commit

Permalink
avoid potential mistakes by caching rawBody value and allowing to cal…
Browse files Browse the repository at this point in the history
…l it more than once
  • Loading branch information
angryziber committed Jan 15, 2025
1 parent 4b7f6ae commit 0886386
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* server: expose Server.listen and bound Server.address as separate properties #93
* server: Server.use() can register extensions that implement multiple supported interfaces at the same time
* server: useOnly() will now add parser/renderer if it wasn't yet registered, to avoid confusion
* server: HttpExchange.rawBody can now be accessed more than once (is a lazy property now)
* jdbc: `distinct` and `notDistinct` operators introduced for where expressions

# 1.6.10
Expand Down
5 changes: 2 additions & 3 deletions server/src/klite/HttpExchange.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,14 @@ open class HttpExchange(

@Suppress("UNCHECKED_CAST")
fun <T: Any> body(type: KType): T {
if (type.classifier == String::class) return rawBody as T
if (type.classifier == String::class) return requestStream.reader().use { it.readText() } as T
if (type.classifier == ByteArray::class) return requestStream.readBytes() as T
val contentType = requestType ?: MimeTypes.text
val bodyParser = config.parsers.find { contentType.startsWith(it.contentType) } ?: throw UnsupportedMediaTypeException(requestType)
return bodyParser.parse(this, type)
}

/** Note: this can be called only once */
val rawBody: String get() = requestStream.reader().use { it.readText() }
val rawBody by lazy { body<String>() }

val bodyParams: Map<String, Any?> by lazy { body() }
/** e.g. form param, passed in body */ @Suppress("UNCHECKED_CAST")
Expand Down

0 comments on commit 0886386

Please sign in to comment.