@@ -427,7 +427,7 @@ public class WebSocketModule(context: ReactApplicationContext) :
427427 */
428428 private fun getCookie (uri : String ): String? {
429429 try {
430- val origin = URI (getDefaultOrigin( uri) )
430+ val origin = getCookieLookupUri( uri)
431431 val cookieMap = cookieHandler.get(origin, HashMap <String , List <String >>())
432432 val cookieList = cookieMap[" Cookie" ]
433433 if (cookieList.isNullOrEmpty()) {
@@ -459,6 +459,16 @@ public class WebSocketModule(context: ReactApplicationContext) :
459459 customClientBuilder?.apply (builder)
460460 }
461461
462+ /* * Map a WebSocket URI's scheme to its HTTP(S) equivalent, e.g. "wss" -> "https". */
463+ private fun httpSchemeFor (requestURI : URI ): String =
464+ when (requestURI.scheme) {
465+ " wss" -> " https"
466+ " ws" -> " http"
467+ " http" ,
468+ " https" -> requestURI.scheme
469+ else -> " "
470+ }
471+
462472 /* *
463473 * Get the default HTTP(S) origin for a specific WebSocket URI
464474 *
@@ -468,14 +478,7 @@ public class WebSocketModule(context: ReactApplicationContext) :
468478 private fun getDefaultOrigin (uri : String ): String {
469479 try {
470480 val requestURI = URI (uri)
471- val scheme =
472- when (requestURI.scheme) {
473- " wss" -> " https"
474- " ws" -> " http"
475- " http" ,
476- " https" -> requestURI.scheme
477- else -> " "
478- }
481+ val scheme = httpSchemeFor(requestURI)
479482
480483 val defaultOrigin =
481484 if (requestURI.port != - 1 ) {
@@ -489,5 +492,29 @@ public class WebSocketModule(context: ReactApplicationContext) :
489492 throw IllegalArgumentException (" Unable to set $uri as default origin header" )
490493 }
491494 }
495+
496+ /* *
497+ * Get the URI used to look up cookies for a specific WebSocket URI, keeping its path so that
498+ * path-scoped cookies are matched correctly
499+ *
500+ * @param uri
501+ * @return A URI with the endpoint converted to HTTP protocol (http[s]://host[:port]/path)
502+ */
503+ private fun getCookieLookupUri (uri : String ): URI {
504+ try {
505+ val requestURI = URI (uri)
506+ return URI (
507+ httpSchemeFor(requestURI),
508+ null ,
509+ requestURI.host,
510+ requestURI.port,
511+ requestURI.path,
512+ requestURI.query,
513+ requestURI.fragment,
514+ )
515+ } catch (e: URISyntaxException ) {
516+ throw IllegalArgumentException (" Unable to get cookie lookup URI from $uri " )
517+ }
518+ }
492519 }
493520}
0 commit comments