Issue #5868 - changes from review

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2021-02-09 10:56:31 +11:00
parent 2ac50130d6
commit 1dc98b611e
3 changed files with 108 additions and 38 deletions

View File

@ -39,6 +39,10 @@ import org.eclipse.jetty.websocket.core.ExtensionConfig;
import org.eclipse.jetty.websocket.core.WebSocketConstants; import org.eclipse.jetty.websocket.core.WebSocketConstants;
import org.eclipse.jetty.websocket.core.server.internal.UpgradeHttpServletRequest; import org.eclipse.jetty.websocket.core.server.internal.UpgradeHttpServletRequest;
/**
* Upgrade request used for websocket negotiation.
* Provides getters for things like the requested extensions and subprotocols so that the headers don't have to be parsed manually.
*/
public class ServerUpgradeRequest public class ServerUpgradeRequest
{ {
private final URI requestURI; private final URI requestURI;

View File

@ -26,6 +26,10 @@ import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.HttpHeader; import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.websocket.core.ExtensionConfig; import org.eclipse.jetty.websocket.core.ExtensionConfig;
/**
* Upgrade response used for websocket negotiation.
* Allows setting of extensions and subprotocol without using headers directly.
*/
public class ServerUpgradeResponse public class ServerUpgradeResponse
{ {
private final HttpServletResponse response; private final HttpServletResponse response;

View File

@ -14,6 +14,8 @@
package org.eclipse.jetty.websocket.core.server.internal; package org.eclipse.jetty.websocket.core.server.internal;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.security.Principal; import java.security.Principal;
import java.util.ArrayList; import java.util.ArrayList;
@ -30,6 +32,7 @@ import javax.servlet.AsyncContext;
import javax.servlet.DispatcherType; import javax.servlet.DispatcherType;
import javax.servlet.RequestDispatcher; import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream; import javax.servlet.ServletInputStream;
import javax.servlet.ServletRequest; import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse; import javax.servlet.ServletResponse;
@ -85,6 +88,9 @@ public class UpgradeHttpServletRequest implements HttpServletRequest
private final String remoteName; private final String remoteName;
private final InetSocketAddress serverAddress; private final InetSocketAddress serverAddress;
private boolean isAsyncStarted;
private boolean isAsyncSupported;
public UpgradeHttpServletRequest(HttpServletRequest httpRequest) public UpgradeHttpServletRequest(HttpServletRequest httpRequest)
{ {
// The original request object must be held temporarily for the duration of the handshake // The original request object must be held temporarily for the duration of the handshake
@ -145,6 +151,8 @@ public class UpgradeHttpServletRequest implements HttpServletRequest
attributes.put(name, request.getAttribute(name)); attributes.put(name, request.getAttribute(name));
} }
this.isAsyncStarted = request.isAsyncStarted();
this.isAsyncSupported = request.isAsyncSupported();
request = null; request = null;
} }
@ -197,13 +205,17 @@ public class UpgradeHttpServletRequest implements HttpServletRequest
@Override @Override
public long getDateHeader(String name) public long getDateHeader(String name)
{ {
if (request == null)
throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE); throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE);
return request.getDateHeader(name);
} }
@Override @Override
public int getIntHeader(String name) public int getIntHeader(String name)
{ {
if (request == null)
throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE); throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE);
return request.getIntHeader(name);
} }
@Override @Override
@ -291,31 +303,41 @@ public class UpgradeHttpServletRequest implements HttpServletRequest
@Override @Override
public String getRequestedSessionId() public String getRequestedSessionId()
{ {
if (request == null)
throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE); throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE);
return request.getRequestedSessionId();
} }
@Override @Override
public boolean isRequestedSessionIdValid() public boolean isRequestedSessionIdValid()
{ {
if (request == null)
throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE); throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE);
return request.isRequestedSessionIdValid();
} }
@Override @Override
public boolean isRequestedSessionIdFromCookie() public boolean isRequestedSessionIdFromCookie()
{ {
if (request == null)
throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE); throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE);
return request.isRequestedSessionIdFromCookie();
} }
@Override @Override
public boolean isRequestedSessionIdFromURL() public boolean isRequestedSessionIdFromURL()
{ {
if (request == null)
throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE); throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE);
return request.isRequestedSessionIdFromURL();
} }
@Override @Override
public boolean isRequestedSessionIdFromUrl() public boolean isRequestedSessionIdFromUrl()
{ {
if (request == null)
throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE); throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE);
return request.isRequestedSessionIdFromUrl();
} }
@Override @Override
@ -407,7 +429,7 @@ public class UpgradeHttpServletRequest implements HttpServletRequest
public void setAttribute(String name, Object value) public void setAttribute(String name, Object value)
{ {
if (request == null) if (request == null)
throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE); attributes.put(name, value);
request.setAttribute(name, value); request.setAttribute(name, value);
} }
@ -415,7 +437,7 @@ public class UpgradeHttpServletRequest implements HttpServletRequest
public void removeAttribute(String name) public void removeAttribute(String name)
{ {
if (request == null) if (request == null)
throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE); attributes.remove(name);
request.removeAttribute(name); request.removeAttribute(name);
} }
@ -476,122 +498,162 @@ public class UpgradeHttpServletRequest implements HttpServletRequest
} }
@Override @Override
public boolean authenticate(HttpServletResponse response) public boolean authenticate(HttpServletResponse response) throws IOException, ServletException
{ {
if (request == null)
throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE); throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE);
return request.authenticate(response);
} }
@Override @Override
public String changeSessionId() public String changeSessionId()
{ {
if (request == null)
throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE); throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE);
return request.changeSessionId();
} }
@Override @Override
public AsyncContext getAsyncContext() public AsyncContext getAsyncContext()
{ {
if (request == null)
throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE); throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE);
return request.getAsyncContext();
} }
@Override @Override
public String getCharacterEncoding() public String getCharacterEncoding()
{ {
if (request == null)
throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE); throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE);
return request.getCharacterEncoding();
} }
@Override @Override
public int getContentLength() public int getContentLength()
{ {
if (request == null)
throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE); throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE);
return request.getContentLength();
} }
@Override @Override
public long getContentLengthLong() public long getContentLengthLong()
{ {
if (request == null)
throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE); throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE);
return request.getContentLengthLong();
} }
@Override @Override
public String getContentType() public String getContentType()
{ {
if (request == null)
throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE); throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE);
return request.getContentType();
} }
@Override @Override
public ServletInputStream getInputStream() public ServletInputStream getInputStream() throws IOException
{ {
if (request == null)
throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE); throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE);
return request.getInputStream();
} }
@Override @Override
public Part getPart(String name) public Part getPart(String name) throws IOException, ServletException
{ {
if (request == null)
throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE); throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE);
return request.getPart(name);
} }
@Override @Override
public Collection<Part> getParts() public Collection<Part> getParts() throws IOException, ServletException
{ {
if (request == null)
throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE); throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE);
return request.getParts();
} }
@Override @Override
public BufferedReader getReader() public BufferedReader getReader() throws IOException
{ {
if (request == null)
throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE); throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE);
return request.getReader();
} }
@Override @Override
public RequestDispatcher getRequestDispatcher(String path) public RequestDispatcher getRequestDispatcher(String path)
{ {
if (request == null)
throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE); throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE);
return request.getRequestDispatcher(path);
} }
@Override @Override
public boolean isAsyncStarted() public boolean isAsyncStarted()
{ {
return false; if (request == null)
return isAsyncStarted;
return request.isAsyncStarted();
} }
@Override @Override
public boolean isAsyncSupported() public boolean isAsyncSupported()
{ {
return false; if (request == null)
return isAsyncSupported;
return request.isAsyncSupported();
} }
@Override @Override
public void login(String username, String password) public void login(String username, String password) throws ServletException
{ {
if (request == null)
throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE); throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE);
request.login(username, password);
} }
@Override @Override
public void logout() public void logout() throws ServletException
{ {
if (request == null)
throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE); throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE);
request.logout();
} }
@Override @Override
public void setCharacterEncoding(String enc) public void setCharacterEncoding(String enc) throws UnsupportedEncodingException
{ {
if (request == null)
throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE); throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE);
request.setCharacterEncoding(enc);
} }
@Override @Override
public AsyncContext startAsync() throws IllegalStateException public AsyncContext startAsync() throws IllegalStateException
{ {
if (request == null)
throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE); throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE);
return request.startAsync();
} }
@Override @Override
public AsyncContext startAsync(ServletRequest servletRequest, ServletResponse servletResponse) throws IllegalStateException public AsyncContext startAsync(ServletRequest servletRequest, ServletResponse servletResponse) throws IllegalStateException
{ {
if (request == null)
throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE); throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE);
return request.startAsync(servletRequest, servletResponse);
} }
@Override @Override
public <T extends HttpUpgradeHandler> T upgrade(Class<T> handlerClass) public <T extends HttpUpgradeHandler> T upgrade(Class<T> handlerClass) throws IOException, ServletException
{ {
if (request == null)
throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE); throw new UnsupportedOperationException(UNSUPPORTED_AFTER_WEBSOCKET_UPGRADE);
return request.upgrade(handlerClass);
} }
} }