Search code examples
ktor

Large HTTP header with Ktor


Our Ktor application clients are sending headers larger than 8KB. Those requests are being rejected by KTor server engines with a HTTP 400. I've tried Netty and Tomcat so far and they both fail with a HTTP 400 status. With Tomcat engine, the error is more obvious as the response from Tomcat contains the text: java.lang.IllegalArgumentException: Request header is too large . I have tried the configuration suggested in https://youtrack.jetbrains.com/issue/KTOR-27 . The config parameter maxHeaderLength doesn't work. This was suggested in the related PR https://github.com/ktorio/ktor/pull/2490 . I am using Ktor version 1.6.8. How can I get my Ktor apps working with large headers, like headers with almost 16000 characters?


Solution

  • You can configure the Netty engine to provide a HttpServerCodec object with the desired value for maximum header size. Here is an example:

    embeddedServer(Netty, applicationEngineEnvironment {
        connector {
            port = 3333
        }
    
        module {
            routing {
                get("/") {
                    call.respondText { "Hello" }
                }
            }
        }
    }) {
        httpServerCodec = {
            HttpServerCodec(
                HttpObjectDecoder.DEFAULT_MAX_INITIAL_LINE_LENGTH,
                32 * 1024, // max header size
                HttpObjectDecoder.DEFAULT_MAX_CHUNK_SIZE
            )
        }
    }.start(true)