gpt4 book ai didi

java - 100 继续使用 Jersey2+ 和 Jetty9+

转载 作者:行者123 更新时间:2023-11-30 03:03:48 25 4
gpt4 key购买 nike

我看到我的 jersey+jetty 服务器在请求传递到我的处理程序之前响应 100 continue header 。我不想发送 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 header 。来自 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 header

 /**
* 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 发回 100 continue 直到请求到达 jersey 标记的 URI 路径方法?

最佳答案

使用期望:100-Continue处理..

如果您想响应请求的该部分,则:

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

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

关于java - 100 继续使用 Jersey2+ 和 Jetty9+,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35305084/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com