Author: markt Date: Fri Sep 28 00:43:47 2018 New Revision: 1842203 URL: http://svn.apache.org/viewvc?rev=1842203&view=rev Log: Fix https://bz.apache.org/bugzilla/show_bug.cgi?id=62731 Make the URI returned by HandshakeRequest.getRequestURI() and Session.getRequestURI() absolute so that the scheme, host and port are accessible.
Modified: tomcat/trunk/java/org/apache/tomcat/websocket/LocalStrings.properties tomcat/trunk/java/org/apache/tomcat/websocket/server/WsHandshakeRequest.java tomcat/trunk/webapps/docs/changelog.xml Modified: tomcat/trunk/java/org/apache/tomcat/websocket/LocalStrings.properties URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/LocalStrings.properties?rev=1842203&r1=1842202&r2=1842203&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/websocket/LocalStrings.properties [UTF-8] (original) +++ tomcat/trunk/java/org/apache/tomcat/websocket/LocalStrings.properties [UTF-8] Fri Sep 28 00:43:47 2018 @@ -77,6 +77,9 @@ wsFrame.wrongRsv=The client frame set th wsFrameClient.ioe=Failure while reading data sent by server +wsHandshakeRequest.invalidUri=The string [{0}] cannot be used to construct a valid URI +wsHandshakeRequest.unknownScheme=The scheme [{0}] in the request is not recognised + wsRemoteEndpoint.acquireTimeout=The current message was not fully sent within the specified timeout wsRemoteEndpoint.closed=Message will not be sent because the WebSocket session has been closed wsRemoteEndpoint.closedDuringMessage=The remainder of the message will not be sent because the WebSocket session has been closed Modified: tomcat/trunk/java/org/apache/tomcat/websocket/server/WsHandshakeRequest.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/server/WsHandshakeRequest.java?rev=1842203&r1=1842202&r2=1842203&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/websocket/server/WsHandshakeRequest.java (original) +++ tomcat/trunk/java/org/apache/tomcat/websocket/server/WsHandshakeRequest.java Fri Sep 28 00:43:47 2018 @@ -31,12 +31,15 @@ import javax.servlet.http.HttpServletReq import javax.websocket.server.HandshakeRequest; import org.apache.tomcat.util.collections.CaseInsensitiveKeyMap; +import org.apache.tomcat.util.res.StringManager; /** * Represents the request that this session was opened under. */ public class WsHandshakeRequest implements HandshakeRequest { + private static final StringManager sm = StringManager.getManager(WsHandshakeRequest.class); + private final URI requestUri; private final Map<String,List<String>> parameterMap; private final String queryString; @@ -54,18 +57,7 @@ public class WsHandshakeRequest implemen queryString = request.getQueryString(); userPrincipal = request.getUserPrincipal(); httpSession = request.getSession(false); - - // URI - StringBuilder sb = new StringBuilder(request.getRequestURI()); - if (queryString != null) { - sb.append("?"); - sb.append(queryString); - } - try { - requestUri = new URI(sb.toString()); - } catch (URISyntaxException e) { - throw new IllegalArgumentException(e); - } + requestUri = buildRequestUri(request); // ParameterMap Map<String,String[]> originalParameters = request.getParameterMap(); @@ -147,4 +139,53 @@ public class WsHandshakeRequest implemen void finished() { request = null; } + + + /* + * See RequestUtil.getRequestURL() + */ + private static URI buildRequestUri(HttpServletRequest req) { + + StringBuffer uri = new StringBuffer(); + String scheme = req.getScheme(); + int port = req.getServerPort(); + if (port < 0) { + // Work around java.net.URL bug + port = 80; + } + + if ("http".equals(scheme)) { + uri.append("ws"); + } else if ("https".equals(scheme)) { + uri.append("wss"); + } else { + // Should never happen + throw new IllegalArgumentException( + sm.getString("wsHandshakeRequest.unknownScheme", scheme)); + } + + uri.append("://"); + uri.append(req.getServerName()); + + if ((scheme.equals("http") && (port != 80)) + || (scheme.equals("https") && (port != 443))) { + uri.append(':'); + uri.append(port); + } + + uri.append(req.getRequestURI()); + + if (req.getQueryString() != null) { + uri.append("?"); + uri.append(req.getQueryString()); + } + + try { + return new URI(uri.toString()); + } catch (URISyntaxException e) { + // Should never happen + throw new IllegalArgumentException( + sm.getString("wsHandshakeRequest.invalidUri", uri.toString()), e); + } + } } Modified: tomcat/trunk/webapps/docs/changelog.xml URL: http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/changelog.xml?rev=1842203&r1=1842202&r2=1842203&view=diff ============================================================================== --- tomcat/trunk/webapps/docs/changelog.xml (original) +++ tomcat/trunk/webapps/docs/changelog.xml Fri Sep 28 00:43:47 2018 @@ -115,6 +115,16 @@ </fix> </changelog> </subsection> + <subsection name="WebSocket"> + <changelog> + <fix> + <bug>62731</bug>: Make the URI returned by + <code>HandshakeRequest.getRequestURI()</code> and + <code>Session.getRequestURI()</code> absolute so that the scheme, host + and port are accessible. (markt) + </fix> + </changelog> + </subsection> <subsection name="Web applications"> <changelog> <fix> --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org