简体   繁体   English

配置 Javalin (Jetty) 最大请求大小(414 URI 太长)

[英]Configure Javalin (Jetty) max request size (414 URI too long)

How do you configure Javalin to change the max request size, specifically the config to increase the max size of the query parameters on the request (avoiding 414 URI too long )?如何配置 Javalin 以更改最大请求大小,特别是配置以增加请求中查询参数的最大大小(避免414 URI too long )?

I get 414 URI is too long when I exceed what looks like a default size of 8KB so would like to configure my Javalin server to increase that slightly.当我超过 8KB 的默认大小时,我得到414 URI is too long因此我想配置我的 Javalin 服务器以稍微增加它。

I think it uses Jetty under the hood which has a HttpConfiguration.requestHeaderSize variable that may control it.我认为它在引擎盖下使用 Jetty,它有一个HttpConfiguration.requestHeaderSize变量可以控制它。 Or there's a HttpParser._maxHeaderBytes which is checked before throwing the URI_TOO_LONG_414 exception.或者在抛出HttpParser._maxHeaderBytes异常之前检查URI_TOO_LONG_414

I can't see how it can all be wired up...我看不出它是如何连接起来的……

Only change the HttpConfiguration , it will inform the HttpParser .只改变HttpConfiguration ,它会通知HttpParser

You should be leery of doing this because...你应该谨慎地这样做,因为......

  • many browsers do not support that large of a query.许多浏览器不支持那么大的查询。
  • 3rd party inte.net security software on laptops will reject that exchange.笔记本电脑上的第 3 方互联网安全软件将拒绝该交换。
  • you open yourself to various old school DoS (Denial of Service) attacks related to hashmap/hashcode abuse.您向与散列图/散列码滥用相关的各种老式 DoS(拒绝服务)攻击敞开心扉。 (to minimize this issue, use Java 17 or newer) (要尽量减少此问题,请使用 Java 17 或更新版本)
  • If you move to HTTP/2 (or HTTP/3) many servers will reject the extension of the maximum request headers (at the HTTP level) that would be needed to support this massive request path.如果您迁移到 HTTP/2(或 HTTP/3),许多服务器将拒绝支持这种大规模请求路径所需的最大请求标头扩展(在 HTTP 级别)。

Also, depending on what technology Javalin is using, you might need to also increase the ContextHandler maxFormContentSize and/or maxFormKeys .此外,根据 Javalin 使用的技术,您可能还需要增加ContextHandler maxFormContentSize和/或maxFormKeys

If you have reached this need, it screams of abuse of the HTTP spec, you should investigate moving to a traditional HTTP POST with Content-Type: application/x-www-form-urlencoded instead.如果你已经达到了这个需求,它尖叫着滥用 HTTP 规范,你应该调查转向传统的 HTTP POST, Content-Type: application/x-www-form-urlencoded

Bearing in mind all the advice and warnings listed here ... Assuming you have Javalin defined as follows in your main method:请牢记此处列出的所有建议和警告...假设您在main方法中按如下方式定义了 Javalin:

Javalin app = Javalin.create(config -> {
    config.jetty.server(MyJetty::create);
}).start();

Then you can create MyJetty to customize this and any other settings you may want to use.然后您可以创建MyJetty来自定义此设置以及您可能想要使用的任何其他设置。

A very basic example:一个非常基本的例子:

import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;

public class MyJetty {

    public static Server create() {
        Server server = new Server();
        HttpConfiguration httpConfiguration = new HttpConfiguration();

        httpConfiguration.setRequestHeaderSize(8192); // use your value here

        HttpConnectionFactory httpCF = new HttpConnectionFactory(httpConfiguration);
        ServerConnector httpConnector = new ServerConnector(server, httpCF);
        httpConnector.setPort(8080); // use your port here
        server.addConnector(httpConnector);
        return server;
    }

}

This only sets up a simple insecure HTTP connection - but shows one way to change the HttpConfiguration value for Javalin.这只设置了一个简单的不安全的 HTTP 连接 - 但显示了一种更改 Javalin 的HttpConfiguration值的方法。 You can use the same approach for other connectors you may want to configure, including ones using SSL/TLS.您可以对可能想要配置的其他连接器使用相同的方法,包括使用 SSL/TLS 的连接器。


I am assuming the latest version of Javalin (version 5) since there were some syntax changes from Javalin 4 to 5 - and also the version of Jetty changed from 9 to 11.我假设是最新版本的 Javalin(版本 5),因为从 Javalin 4 到 5 有一些语法变化——而且 Jetty 的版本也从 9 到 11。

If you are using Javalin 4, the config syntax is a bit different:如果您使用的是 Javalin 4,则config语法有点不同:

config.server(MyJetty::create);

But I don't think the Jetty code changes (for this specific setting, at least).但我认为 Jetty 代码不会改变(至少对于这个特定设置)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM