【问题标题】:100 Continue with Jersey2+ with Jetty9+100 继续使用 Jersey2+ 和 Jetty9+
【发布时间】:2016-02-10 00:59:11
【问题描述】:

我看到我的 jersey+jetty 服务器在将请求传递给我的处理程序之前响应 100 continue 标头。我不想发送 100-Continue 而是根据请求的 URI/Headers 发送 3xx 响应。

这是调用 request.getInputStream() 的类 org.glassfish.jersey.servlet.WebComponent

 public Value<Integer> service(
        final URI baseUri,
        final URI requestUri,
        final HttpServletRequest servletRequest,
        final HttpServletResponse servletResponse) throws ServletException, IOException {

    ContainerRequest requestContext = new ContainerRequest(baseUri, requestUri,
            servletRequest.getMethod(), getSecurityContext(servletRequest), new ServletPropertiesDelegate(servletRequest));
    requestContext.setEntityStream(servletRequest.getInputStream());
    addRequestHeaders(servletRequest, requestContext);

当请求中的输入流被请求时,Jetty 会发送 100 Continue 标头。来自 org.eclipse.jetty.server.Request

@Override
public ServletInputStream getInputStream() throws IOException
{
    if (_inputState != __NONE && _inputState != _STREAM)
        throw new IllegalStateException("READER");
    _inputState = _STREAM;

    if (_channel.isExpecting100Continue())
        _channel.continue100(_input.available());

    return _input;
}

它调用 HttpChannel 以发送 100 Continue 标头

 /**
 * If the associated response has the Expect header set to 100 Continue,
 * then accessing the input stream indicates that the handler/servlet
 * is ready for the request body and thus a 100 Continue response is sent.
 *
 * @throws IOException if the InputStream cannot be created
 */
public void continue100(int available) throws IOException
{
    // If the client is expecting 100 CONTINUE, then send it now.
    // TODO: consider using an AtomicBoolean ?
    if (isExpecting100Continue())
    {
        _expect100Continue = false;

        // is content missing?
        if (available == 0)
        {
            if (_response.isCommitted())
                throw new IOException("Committed before 100 Continues");

            // TODO: break this dependency with HttpGenerator
            boolean committed = sendResponse(HttpGenerator.CONTINUE_100_INFO, null, false);
            if (!committed)
                throw new IOException("Concurrent commit while trying to send 100-Continue");
        }
    }
}

如何防止 jersey+jetty 在请求到达 jersey 标记的 URI 路径方法之前发回 100 继续?

【问题讨论】:

    标签: java jetty jersey-2.0 jetty-9


    【解决方案1】:

    Expect: 100-Continue处理..

    如果你想响应那部分请求,那么:

    • 不要尝试访问请求参数(这需要读取输入流来处理所有可能的参数源)
    • 不要尝试访问请求输入流(这意味着您正在接受 100-Continue 期望并准备好接收实际的请求正文内容)
    • 不要尝试访问请求阅读器(这也会打开请求输入流)
    • 不要尝试使用 servlet 异步 I/O 层(这会打开请求输入流和响应输出流)
    • 请务必使用 HttpServletResponse 发送您的备用状态代码和/或响应正文内容。

    这都是标准的 Servlet / Jetty 行为。

    【讨论】:

    • 同意。但这并不能回答我的问题。我知道这些是 Jetty 层的要求。但是由于Jersey在代码到达我的任何代码之前调用了getInputStream()方法,这是否意味着Jersey+Jetty组合没有正确实现100 continue?
    • 重读问题。我想我只回答了一半的问题,码头方面。问题不在于 Jetty 方面,而是泽西岛假设它不必处理 100-Continue 期望的替代处理。
    • 问题是一起使用Jetty+Jersey。我明白你的意思,这就是 Jetty 本身的工作方式,是的,100 Continue 支持已正确实施。但是,当使用 Jetty 作为带有 Jersey 的容器时,它就坏了。问题是让 jersey+jetty 组合使用 100 成功继续,这里没有回答
    • 我遇到了这个确切的问题。你有什么办法绕过它吗?
    猜你喜欢
    • 2011-01-16
    • 2015-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-22
    • 1970-01-01
    • 2012-01-03
    相关资源
    最近更新 更多