Grizzly:非法尝试超过配置的最大标头数

时间:2017-04-25 05:54:46

标签: jersey-2.0 grizzly

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

阅读Grizzy Http Server Framework Overview,似乎可以以某种方式配置 maxResponseHeaders 响应可以发送给客户端的最大标头数),但目前还不清楚当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);
}

当我超过响应头的最大数量时,这是堆栈:

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$8.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$2.run(ServerRuntime.java:316)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
    at org.glassfish.jersey.internal.Errors$1.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$1.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)

1 个答案:

答案 0 :(得分:1)

您需要做的第一件事就是在致电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()