100使用Jetty9 +继续使用Jersey2 +

时间:2016-02-10 00:59:12

标签: java jetty jersey-2.0 jetty-9

我看到我的jersey + jetty服务器在请求传递给我的处理程序之前响应100个继续标头。我不想发送100-Continue,而是根据请求的URI / Header发送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");
        }
    }
}

如何防止球衣+码头发送回100继续,直到请求到达球衣标记的URI路径方法?

1 个答案:

答案 0 :(得分:2)

使用Expect: 100-Continue处理..

如果您想回复请求的那一部分,那么:

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

这是所有标准的Servlet / Jetty行为。