Grizzly:非法尝试超过配置的最大数量 headers

Grizzly: Illegal attempt to exceed the configured maximum number of headers

我在 RESTful 网络服务服务器应用程序中使用 Grizzly/Jersey/Jackson。对于某些交互,可能会在响应中返回大量 HTTP headers。默认情况下,Grizzy 将最大响应数 headers 设置为 100。

阅读 Grizzy Http Server Framework Overview,似乎 maxResponseHeaders最多 headers 个响应可以发送到客户端) 可以以某种方式进行配置,但不清楚当 Grizzly 与 Jersey 叠加时如何完成。

关于尝试设置此配置的任何建议?

这就是我目前配置 Grizzly 和 Jackson 的方式:

packages(true, Config.CONFIG_RESOURCE_BASE_PACKAGE);
register(JacksonFeature.class);
register(GrizzlyHttpContainerProvider.class);
register(CustomInjectables.class);
register(RolesAllowedDynamicFeature.class);
register(AccessSecurityFilter.class);

String host = Config.get("webserver.address");
int port = Config.getInteger("webserver.port");
boolean secure = Config.getBoolean("webserver.secure");

if (secure) {
    SSLContextConfigurator sslContextConfigurator = new SSLContextConfigurator();
    sslContextConfigurator.setKeyStoreFile(Config
            .get("webserver.keystore.location"));
    sslContextConfigurator.setKeyStorePass(Config
            .get("webserver.keystore.password"));

    boolean clientMode = false;
    boolean needClientAuth = false;
    boolean wantClientAuth = false;
    SSLEngineConfigurator sslEngineConfigurator = new SSLEngineConfigurator(
            sslContextConfigurator, clientMode, needClientAuth,
            wantClientAuth);

    URI uri = URI.create("https://" + host + ":" + port);
    log.info("Starting web server (secure): " + uri + " ...");
    server = GrizzlyHttpServerFactory.createHttpServer(uri, this, true,
            sslEngineConfigurator, true);
} else {
    URI uri = URI.create("http://" + host + ":" + port);
    log.info("Starting web server: " + uri + " ...");
    server = GrizzlyHttpServerFactory.createHttpServer(uri, this, true);
}

这是我超过最大响应数时的堆栈headers:

Apr 24, 2017 10:28:46 PM org.glassfish.grizzly.filterchain.DefaultFilterChain execute
WARNING: GRIZZLY0013: Exception during FilterChain execution
org.glassfish.grizzly.http.util.MimeHeaders$MaxHeaderCountExceededException: Illegal attempt to exceed the configured maximum number of headers: 100
    at org.glassfish.grizzly.http.util.MimeHeaders.createHeader(MimeHeaders.java:396)
    at org.glassfish.grizzly.http.util.MimeHeaders.setValue(MimeHeaders.java:498)
    at org.glassfish.grizzly.http.HttpServerFilter.prepareResponse(HttpServerFilter.java:944)
    at org.glassfish.grizzly.http.HttpServerFilter.encodeHttpPacket(HttpServerFilter.java:834)
    at org.glassfish.grizzly.http.HttpCodecFilter.handleWrite(HttpCodecFilter.java:1407)
    at org.glassfish.grizzly.filterchain.ExecutorResolver.execute(ExecutorResolver.java:111)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.filterchain.FilterChainContext.write(FilterChainContext.java:890)
    at org.glassfish.grizzly.filterchain.FilterChainContext.write(FilterChainContext.java:858)
    at org.glassfish.grizzly.http.io.OutputBuffer.flushBuffer(OutputBuffer.java:1029)
    at org.glassfish.grizzly.http.io.OutputBuffer.flushBinaryBuffers(OutputBuffer.java:1016)
    at org.glassfish.grizzly.http.io.OutputBuffer.flushAllBuffers(OutputBuffer.java:987)
    at org.glassfish.grizzly.http.io.OutputBuffer.close(OutputBuffer.java:716)
    at org.glassfish.grizzly.http.server.NIOWriterImpl.close(NIOWriterImpl.java:111)
    at org.glassfish.grizzly.http.server.util.HtmlHelper.sendErrorPage(HtmlHelper.java:103)
    at org.glassfish.grizzly.http.server.Response.sendError(Response.java:1358)
    at org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer$ResponseWriter.failure(GrizzlyHttpContainer.java:287)
    at org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:486)
    at org.glassfish.jersey.server.ServerRuntime.run(ServerRuntime.java:316)
    at org.glassfish.jersey.internal.Errors.call(Errors.java:271)
    at org.glassfish.jersey.internal.Errors.call(Errors.java:267)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:291)
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1140)
    at org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer.service(GrizzlyHttpContainer.java:375)
    at org.glassfish.grizzly.http.server.HttpHandler.run(HttpHandler.java:224)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
    at java.lang.Thread.run(Unknown Source)

您需要做的第一件事是让服务器 而不是 在您调用 createHttpServer 时自动启动。目前,您将 true 作为最后一个参数传递,也就是说 应该 自动启动。此配置已经是默认配置。所以主要用来设置值为false,意思是自启动。因此将该值设置为 false.

现在服务器没有自动启动,我们可以配置它了。 setMaxResponseHeader的具体配置是NetworkListener上的配置。您可以从 HttpServer.

final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(...);
final NetworkListener listener = server.getListener("grizzly");
listener.setMaxResponseHeaders(300);
server.start();

现在我们在配置后手动启动服务器。我不确定是否有更好的方法来吸引听众。我只是对 "grizzly" 进行了硬编码,因为我之前所做的只是遍历 server.getListeners() 并打印出所有名称,然后发现 "grizzly" 是唯一可用的。这就是我用来测试的。

除了NetworkListener配置之外,您还可以通过ServerConfiguration进行其他与服务器相关的配置。您可以使用 server.getServerConfiguration()