首页 > 解决方案 > 如何增加 ktor 可接受 url 的最大长度?

问题描述

现在我的ktor服务器是基于netty的。当我做长 GET 请求(大约 9300 个字符(主要在查询参数中))时,ktor 回答Unhandled: GET - /bad-request。如果我减少 url 的长度,它工作正常。

标签: kotlinnettyktor

解决方案


在您的嵌入式服务器配置中,您可以提供一个函数“httpServerCodec”来创建 HttpServerCodec ( https://netty.io/4.1/api/io/netty/handler/codec/http/HttpServerCodec.html ),您可以在其中设置 maxInitialLineLength财产。

embeddedServer(Netty, configure = {
    // Size of the queue to store [ApplicationCall] instances that cannot be immediately processed
    requestQueueLimit = 16 
    // Do not create separate call event group and reuse worker group for processing calls
    shareWorkGroup = false 
    // User-provided function to configure Netty's [ServerBootstrap]
    configureBootstrap = {
        // ...
    } 

    httpServerCodec = {
       HttpServerCodec(.......)
    }
    // Timeout in seconds for sending responses to client
    responseWriteTimeoutSeconds = 10 
}) {
    // ...
}.start(true)

推荐阅读