Skip to content

Commit

Permalink
Async not supported without wrappers (#9061)
Browse files Browse the repository at this point in the history
  • Loading branch information
gregw authored Dec 19, 2022
1 parent dc49639 commit 9f5cb0c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ public class ServletApiRequest implements HttpServletRequest
private String _method;
private ServletMultiPartFormData.Parts _parts;
private ServletPathMapping _servletPathMapping;
private boolean _asyncSupported = true;

public static Session getSession(HttpSession httpSession)
{
Expand Down Expand Up @@ -1362,6 +1363,8 @@ public ServletContext getServletContext()
@Override
public AsyncContext startAsync() throws IllegalStateException
{
if (!isAsyncSupported())
throw new IllegalStateException("Async Not Supported");
ServletRequestState state = getState();
if (_async == null)
_async = new AsyncContextState(state);
Expand All @@ -1374,6 +1377,8 @@ public AsyncContext startAsync() throws IllegalStateException
@Override
public AsyncContext startAsync(ServletRequest servletRequest, ServletResponse servletResponse) throws IllegalStateException
{
if (!isAsyncSupported())
throw new IllegalStateException("Async Not Supported");
ServletRequestState state = getState();
if (_async == null)
_async = new AsyncContextState(state);
Expand All @@ -1398,7 +1403,12 @@ public boolean isAsyncStarted()
@Override
public boolean isAsyncSupported()
{
return true;
return _asyncSupported;
}

public void setAsyncSupported(boolean asyncSupported)
{
_asyncSupported = asyncSupported;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.util.Stack;
import java.util.concurrent.atomic.AtomicLong;

import jakarta.servlet.AsyncContext;
import jakarta.servlet.GenericServlet;
import jakarta.servlet.MultipartConfigElement;
import jakarta.servlet.Servlet;
Expand All @@ -39,12 +38,9 @@
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRegistration;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletRequestWrapper;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.ServletSecurityElement;
import jakarta.servlet.UnavailableException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletRequestWrapper;
import jakarta.servlet.http.HttpServletResponse;
import org.eclipse.jetty.ee10.servlet.security.IdentityService;
import org.eclipse.jetty.ee10.servlet.security.RunAsToken;
Expand Down Expand Up @@ -1376,49 +1372,24 @@ public NotAsync(Servlet servlet)
}

@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException
{
if (req.isAsyncSupported())
if (request.isAsyncSupported())
{
if (req instanceof HttpServletRequest httpServletRequest)
ServletContextRequest servletContextRequest = ServletContextRequest.getServletContextRequest(request);
servletContextRequest.getServletApiRequest().setAsyncSupported(false);
try
{
getWrapped().service(new HttpServletRequestWrapper(httpServletRequest)
{
@Override
public boolean isAsyncSupported()
{
return false;
}

@Override
public AsyncContext startAsync() throws IllegalStateException
{
throw new IllegalStateException("Async Not Supported");
}
}, res);
getWrapped().service(request, response);
}
else
finally
{
//TODO is this necessary to support?
getWrapped().service(new ServletRequestWrapper(req)
{
@Override
public boolean isAsyncSupported()
{
return false;
}

@Override
public AsyncContext startAsync() throws IllegalStateException
{
throw new IllegalStateException("Async Not Supported");
}
}, res);
servletContextRequest.getServletApiRequest().setAsyncSupported(true);
}
}
else
{
getWrapped().service(req, res);
getWrapped().service(request, response);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ public void testAsyncNotSupportedNoAsync() throws Exception
assertThat(response, Matchers.startsWith("HTTP/1.1 200 OK"));
assertThat(_history, contains(
"REQUEST /ctx/noasync/info",
"wrapped REQ",
"initial"
));

Expand All @@ -199,7 +198,6 @@ public void testAsyncNotSupportedAsync() throws Exception
assertThat(response, Matchers.startsWith("HTTP/1.1 500 "));
assertThat(_history, contains(
"REQUEST /ctx/noasync/info?start=200",
"wrapped REQ",
"initial",
"ERROR /ctx/error/custom?start=200",
"wrapped REQ",
Expand Down

0 comments on commit 9f5cb0c

Please sign in to comment.