https://kotlinlang.org logo
Title
d

detouched

07/15/2019, 2:10 AM
Hello there, I'm trying to create a custom feature for Ktor Server that validates request signature and needs to fully read request content. I'm wondering what's the right pipeline and phase to intercept here. Currently intercepting
ApplicationReceivePipeline.Before
– this kinda works, but in case of invalid signature after I send 401 back and call
finish()
I still get
CannotTransformContentToTypeException
in the related target route. I understand that it's what triggers receive pipeline, so this setup is obviously wrong. Seeking for the right solution, appreciate any help.
a

avolkmann

07/15/2019, 5:22 PM
Could you place this logic in an auth route?
d

detouched

07/16/2019, 4:07 AM
I actually tried this first but I didn’t figure the proper way of reading the content and then putting it “back” so that the actual endpoint handler can consume it again. I found another solution, not sure if it’s 100% correct though. I’m doing something similar to what ContentNegotiation feature is doing: intercepting receive pipeline, reading content and and throwing an exception if it is not valid, otherwise
proceedWith
a recreated
ByteReadChannel
so that it is not considered transformed and JSON deserializers can kick in. Another interceptor of
ApplicationCallPipeline
is catching that exception and responding with a proper 401