2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								/*
  
						 
					
						
							
								
									
										
										
										
											2022-04-30 11:26:21 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								 *  Copyright  ( c )  2021 - 2022 ,  Dex ♪  < dexes . ttp @ gmail . com > 
							 
						 
					
						
							
								
									
										
										
										
											2023-06-24 23:00:09 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								 *  Copyright  ( c )  2023 ,  Kenneth  Myhra  < kennethmyhra @ serenityos . org > 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								 * 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								 *  SPDX - License - Identifier :  BSD - 2 - Clause 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								 */ 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-12-31 10:37:10 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								# include  <AK/QuickSort.h> 
  
						 
					
						
							
								
									
										
										
										
											2022-02-11 03:43:03 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								# include  <LibJS/Runtime/ArrayBuffer.h> 
  
						 
					
						
							
								
									
										
										
										
											2021-06-27 21:48:34 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								# include  <LibJS/Runtime/FunctionObject.h> 
  
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								# include  <LibWeb/DOM/Document.h> 
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								# include  <LibWeb/DOM/Event.h> 
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								# include  <LibWeb/DOM/EventDispatcher.h> 
  
						 
					
						
							
								
									
										
										
										
											2022-02-16 20:43:24 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								# include  <LibWeb/DOM/IDLEventListener.h> 
  
						 
					
						
							
								
									
										
										
										
											2023-04-20 19:15:21 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								# include  <LibWeb/FileAPI/Blob.h> 
  
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								# include  <LibWeb/HTML/CloseEvent.h> 
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								# include  <LibWeb/HTML/EventHandler.h> 
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								# include  <LibWeb/HTML/EventNames.h> 
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								# include  <LibWeb/HTML/MessageEvent.h> 
  
						 
					
						
							
								
									
										
										
										
											2022-07-12 20:37:43 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								# include  <LibWeb/HTML/Origin.h> 
  
						 
					
						
							
								
									
										
										
										
											2022-03-07 23:08:26 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								# include  <LibWeb/HTML/Window.h> 
  
						 
					
						
							
								
									
										
										
										
											2023-04-20 19:15:21 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								# include  <LibWeb/WebIDL/AbstractOperations.h> 
  
						 
					
						
							
								
									
										
										
										
											2022-09-25 17:28:46 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								# include  <LibWeb/WebIDL/DOMException.h> 
  
						 
					
						
							
								
									
										
										
										
											2022-09-25 17:03:42 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								# include  <LibWeb/WebIDL/ExceptionOr.h> 
  
						 
					
						
							
								
									
										
										
										
											2022-02-18 17:35:45 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								# include  <LibWeb/WebSockets/WebSocket.h> 
  
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-02-18 17:35:45 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								namespace  Web : : WebSockets  {  
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-04-30 11:26:21 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								static  RefPtr < WebSocketClientManager >  s_websocket_client_manager ;  
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-04-30 11:26:21 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								void  WebSocketClientManager : : initialize ( RefPtr < WebSocketClientManager >  websocket_client_manager )  
						 
					
						
							
								
									
										
										
										
											2022-01-14 13:12:49 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								{  
						 
					
						
							
								
									
										
										
										
											2022-04-30 11:26:21 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    s_websocket_client_manager  =  websocket_client_manager ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-01-14 13:12:49 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-04-30 11:26:21 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								WebSocketClientManager &  WebSocketClientManager : : the ( )  
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								{  
						 
					
						
							
								
									
										
										
										
											2022-04-30 11:26:21 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    if  ( ! s_websocket_client_manager )  [[unlikely]]  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								        dbgln ( " Web::WebSockets::WebSocketClientManager was not initialized! " ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								        VERIFY_NOT_REACHED ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    return  * s_websocket_client_manager ; 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-04-30 11:26:21 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								WebSocketClientSocket : : WebSocketClientSocket ( )  =  default ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								WebSocketClientSocket : : ~ WebSocketClientSocket ( )  =  default ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								WebSocketClientManager : : WebSocketClientManager ( )  =  default ;  
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-02-18 18:14:22 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								// https://websockets.spec.whatwg.org/#dom-websocket-websocket
  
						 
					
						
							
								
									
										
										
										
											2023-06-17 09:18:55 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								WebIDL : : ExceptionOr < JS : : NonnullGCPtr < WebSocket > >  WebSocket : : construct_impl ( JS : : Realm &  realm ,  String  const &  url ,  Optional < Variant < String ,  Vector < String > > >  const &  protocols )  
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								{  
						 
					
						
							
								
									
										
										
										
											2023-06-24 23:00:09 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    auto &  vm  =  realm . vm ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    auto  web_socket  =  MUST_OR_THROW_OOM ( realm . heap ( ) . allocate < WebSocket > ( realm ,  realm ) ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    auto &  relevant_settings_object  =  HTML : : relevant_settings_object ( * web_socket ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    // 1. Let baseURL be this's relevant settings object's API base URL.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    auto  base_url  =  relevant_settings_object . api_base_url ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    // 2. Let urlRecord be the result of applying the URL parser to url with baseURL.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    // FIXME: This should call an implementation of https://url.spec.whatwg.org/#concept-url-parser, currently it calls https://url.spec.whatwg.org/#concept-basic-url-parser
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    auto  url_record  =  base_url . complete_url ( url ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    // 3. If urlRecord is failure, then throw a "SyntaxError" DOMException.
 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    if  ( ! url_record . is_valid ( ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2022-09-25 18:12:50 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								        return  WebIDL : : SyntaxError : : create ( realm ,  " Invalid URL " ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2023-06-24 23:00:09 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
										 
							
							
								    // 4. If urlRecord’ ’  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    if  ( url_record . scheme ( )  = =  " http " sv ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								        url_record . set_scheme ( " ws " sv ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
										 
							
							
								    // 5. Otherwise, if urlRecord’ ’  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    else  if  ( url_record . scheme ( )  = =  " https " sv ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								        url_record . set_scheme ( " wss " sv ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
										 
							
							
								    // 6. If urlRecord’  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    if  ( ! url_record . scheme ( ) . is_one_of ( " ws " sv ,  " wss " sv ) ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								        return  WebIDL : : SyntaxError : : create ( realm ,  " Invalid protocol " sv ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
										 
							
							
								    // 7. If urlRecord’  
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    if  ( ! url_record . fragment ( ) . is_empty ( ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2023-06-24 23:00:09 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								        return  WebIDL : : SyntaxError : : create ( realm ,  " Presence of URL fragment is invalid " sv ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-17 09:18:55 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    Vector < String >  protocols_sequence ; 
							 
						 
					
						
							
								
									
										
										
										
											2023-06-24 23:00:09 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    // 8. If protocols is a string, set protocols to a sequence consisting of just that string.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    if  ( protocols . has_value ( )  & &  protocols - > has < String > ( ) ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								        protocols_sequence  =  {  protocols . value ( ) . get < String > ( )  } ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    else  if  ( protocols . has_value ( )  & &  protocols - > has < Vector < String > > ( ) ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								        protocols_sequence  =  protocols . value ( ) . get < Vector < String > > ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    else 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								        protocols_sequence  =  { } ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    // 9. If any of the values in protocols occur more than once or otherwise fail to match the requirements for elements that comprise
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    //    the value of `Sec-WebSocket-Protocol` fields as defined by The WebSocket protocol, then throw a "SyntaxError" DOMException. [WSP]
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    auto  sorted_protocols  =  protocols_sequence ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    quick_sort ( sorted_protocols ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    for  ( size_t  i  =  0 ;  i  <  sorted_protocols . size ( ) ;  i + + )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								        // https://datatracker.ietf.org/doc/html/rfc6455
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								        // The elements that comprise this value MUST be non-empty strings with characters in the range U+0021 to U+007E not including
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								        // separator characters as defined in [RFC2616] and MUST all be unique strings.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								        auto  protocol  =  sorted_protocols [ i ] ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								        if  ( i  <  sorted_protocols . size ( )  -  1  & &  protocol  = =  sorted_protocols [ i  +  1 ] ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								            return  WebIDL : : SyntaxError : : create ( realm ,  " Found a duplicate protocol name in the specified list " sv ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								        for  ( auto  code_point  :  protocol . code_points ( ) )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								            if  ( code_point  <  ' \x21 '  | |  code_point  >  ' \x7E ' ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								                return  WebIDL : : SyntaxError : : create ( realm ,  " Found invalid character in subprotocol name " sv ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-12-31 10:37:10 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								        } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    } 
							 
						 
					
						
							
								
									
										
										
										
											2023-06-24 23:00:09 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    // 10. Set this's url to urlRecord.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    web_socket - > set_url ( url_record ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
										 
							
							
								    // 11. Let client be this’  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    auto &  client  =  relevant_settings_object ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    // FIXME: 12. Run this step in parallel:
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    //     1. Establish a WebSocket connection given urlRecord, protocols, and client. [FETCH]
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    TRY_OR_THROW_OOM ( vm ,  web_socket - > establish_web_socket_connection ( url_record ,  protocols_sequence ,  client ) ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    return  web_socket ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								WebSocket : : WebSocket ( JS : : Realm &  realm )  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    :  EventTarget ( realm ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								{  
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-24 23:00:09 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								WebSocket : : ~ WebSocket ( )  =  default ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-08-07 08:41:28 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								void  WebSocket : : initialize ( JS : : Realm &  realm )  
						 
					
						
							
								
									
										
										
										
											2023-06-24 23:00:09 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								{  
						 
					
						
							
								
									
										
										
										
											2023-08-07 08:41:28 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    Base : : initialize ( realm ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2023-06-24 23:00:09 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    set_prototype ( & Bindings : : ensure_web_prototype < Bindings : : WebSocketPrototype > ( realm ,  " WebSocket " ) ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								ErrorOr < void >  WebSocket : : establish_web_socket_connection ( AK : : URL &  url_record ,  Vector < String > &  protocols ,  HTML : : EnvironmentSettingsObject &  client )  
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								{  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    // FIXME: Integrate properly with FETCH as per https://fetch.spec.whatwg.org/#websocket-opening-handshake
 
							 
						 
					
						
							
								
									
										
										
										
											2023-06-24 23:00:09 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    auto &  window  =  verify_cast < HTML : : Window > ( client . global_object ( ) ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    auto  origin_string  =  window . associated_document ( ) . origin ( ) . serialize ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-17 09:18:55 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    Vector < DeprecatedString >  protcol_deprecated_strings ; 
							 
						 
					
						
							
								
									
										
										
										
											2023-06-24 23:00:09 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    for  ( auto  const &  protocol  :  protocols ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								        TRY ( protcol_deprecated_strings . try_append ( protocol . to_deprecated_string ( ) ) ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    m_websocket  =  WebSocketClientManager : : the ( ) . connect ( url_record ,  origin_string ,  protcol_deprecated_strings ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-08-28 13:42:07 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    m_websocket - > on_open  =  [ weak_this  =  make_weak_ptr < WebSocket > ( ) ]  { 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								        if  ( ! weak_this ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								            return ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								        auto &  websocket  =  const_cast < WebSocket & > ( * weak_this ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								        websocket . on_open ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    } ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-08-28 13:42:07 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    m_websocket - > on_message  =  [ weak_this  =  make_weak_ptr < WebSocket > ( ) ] ( auto  message )  { 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								        if  ( ! weak_this ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								            return ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								        auto &  websocket  =  const_cast < WebSocket & > ( * weak_this ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								        websocket . on_message ( move ( message . data ) ,  message . is_text ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    } ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-08-28 13:42:07 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    m_websocket - > on_close  =  [ weak_this  =  make_weak_ptr < WebSocket > ( ) ] ( auto  code ,  auto  reason ,  bool  was_clean )  { 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								        if  ( ! weak_this ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								            return ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								        auto &  websocket  =  const_cast < WebSocket & > ( * weak_this ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2023-06-17 09:18:55 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								        websocket . on_close ( code ,  String : : from_deprecated_string ( reason ) . release_value_but_fixme_should_propagate_errors ( ) ,  was_clean ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    } ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-08-28 13:42:07 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    m_websocket - > on_error  =  [ weak_this  =  make_weak_ptr < WebSocket > ( ) ] ( auto )  { 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								        if  ( ! weak_this ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								            return ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								        auto &  websocket  =  const_cast < WebSocket & > ( * weak_this ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								        websocket . on_error ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    } ; 
							 
						 
					
						
							
								
									
										
										
										
											2023-01-28 12:33:35 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    return  { } ; 
							 
						 
					
						
							
								
									
										
										
										
											2023-01-10 06:28:20 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-02-18 18:14:22 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								// https://websockets.spec.whatwg.org/#dom-websocket-readystate
  
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								WebSocket : : ReadyState  WebSocket : : ready_state ( )  const  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								{  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    if  ( ! m_websocket ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								        return  WebSocket : : ReadyState : : Closed ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-04-30 11:26:21 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    return  const_cast < WebSocketClientSocket & > ( * m_websocket ) . ready_state ( ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-02-18 18:14:22 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								// https://websockets.spec.whatwg.org/#dom-websocket-extensions
  
						 
					
						
							
								
									
										
										
										
											2023-06-17 09:18:55 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								String  WebSocket : : extensions ( )  const  
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								{  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    if  ( ! m_websocket ) 
							 
						 
					
						
							
								
									
										
										
										
											2023-06-17 09:18:55 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								        return  String  { } ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-02-18 17:35:45 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    // https://websockets.spec.whatwg.org/#feedback-from-the-protocol
 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    // FIXME: Change the extensions attribute's value to the extensions in use, if it is not the null value.
 
							 
						 
					
						
							
								
									
										
										
										
											2023-06-17 09:18:55 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    return  String  { } ; 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-02-18 18:14:22 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								// https://websockets.spec.whatwg.org/#dom-websocket-protocol
  
						 
					
						
							
								
									
										
										
										
											2023-06-17 09:18:55 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								WebIDL : : ExceptionOr < String >  WebSocket : : protocol ( )  const  
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								{  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    if  ( ! m_websocket ) 
							 
						 
					
						
							
								
									
										
										
										
											2023-06-17 09:18:55 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								        return  String  { } ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    return  TRY_OR_THROW_OOM ( vm ( ) ,  String : : from_deprecated_string ( m_websocket - > subprotocol_in_use ( ) ) ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-02-18 18:14:22 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								// https://websockets.spec.whatwg.org/#dom-websocket-close
  
						 
					
						
							
								
									
										
										
										
											2023-06-17 09:18:55 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								WebIDL : : ExceptionOr < void >  WebSocket : : close ( Optional < u16 >  code ,  Optional < String >  reason )  
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								{  
						 
					
						
							
								
									
										
										
										
											2022-03-29 22:35:54 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    // 1. If code is present, but is neither an integer equal to 1000 nor an integer in the range 3000 to 4999, inclusive, throw an "InvalidAccessError" DOMException.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    if  ( code . has_value ( )  & &  * code  ! =  1000  & &  ( * code  <  3000  | |  * code  >  4099 ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2022-09-25 18:12:50 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								        return  WebIDL : : InvalidAccessError : : create ( realm ( ) ,  " The close error code is invalid " ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-03-29 22:35:54 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    // 2. If reason is present, then run these substeps:
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    if  ( reason . has_value ( ) )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								        // 1. Let reasonBytes be the result of encoding reason.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								        // 2. If reasonBytes is longer than 123 bytes, then throw a "SyntaxError" DOMException.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								        if  ( reason - > bytes ( ) . size ( )  >  123 ) 
							 
						 
					
						
							
								
									
										
										
										
											2022-09-25 18:12:50 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								            return  WebIDL : : SyntaxError : : create ( realm ( ) ,  " The close reason is longer than 123 bytes " ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-03-29 22:35:54 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    // 3. Run the first matching steps from the following list:
 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    auto  state  =  ready_state ( ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-03-29 22:35:54 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    // -> If this's ready state is CLOSING (2) or CLOSED (3)
 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    if  ( state  = =  WebSocket : : ReadyState : : Closing  | |  state  = =  WebSocket : : ReadyState : : Closed ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								        return  { } ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-03-29 22:35:54 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    // -> If the WebSocket connection is not yet established [WSP]
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    // -> If the WebSocket closing handshake has not yet been started [WSP]
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    // -> Otherwise
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    // NOTE: All of these are handled by the WebSocket Protocol when calling close()
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    // FIXME: LibProtocol does not yet support sending empty Close messages, so we use default values for now
 
							 
						 
					
						
							
								
									
										
										
										
											2023-06-17 09:18:55 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    m_websocket - > close ( code . value_or ( 1000 ) ,  reason . value_or ( String  { } ) . to_deprecated_string ( ) ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    return  { } ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-02-18 18:14:22 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								// https://websockets.spec.whatwg.org/#dom-websocket-send
  
						 
					
						
							
								
									
										
										
										
											2023-06-17 09:18:55 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								WebIDL : : ExceptionOr < void >  WebSocket : : send ( Variant < JS : : Handle < JS : : Object > ,  JS : : Handle < FileAPI : : Blob > ,  String >  const &  data )  
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								{  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    auto  state  =  ready_state ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    if  ( state  = =  WebSocket : : ReadyState : : Connecting ) 
							 
						 
					
						
							
								
									
										
										
										
											2022-09-25 18:12:50 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								        return  WebIDL : : InvalidStateError : : create ( realm ( ) ,  " Websocket is still CONNECTING " ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    if  ( state  = =  WebSocket : : ReadyState : : Open )  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-04-20 19:15:21 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								        TRY_OR_THROW_OOM ( vm ( ) , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								            data . visit ( 
							 
						 
					
						
							
								
									
										
										
										
											2023-06-17 09:18:55 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								                [ this ] ( String  const &  string )  - >  ErrorOr < void >  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-04-20 19:15:21 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								                    m_websocket - > send ( string ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								                    return  { } ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								                } , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								                [ this ] ( JS : : Handle < JS : : Object >  const &  buffer_source )  - >  ErrorOr < void >  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								                    // FIXME: While the spec doesn't say to do this, it's not observable except from potentially throwing OOM.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								                    //        Can we avoid this copy?
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								                    auto  data_buffer  =  TRY ( WebIDL : : get_buffer_source_copy ( * buffer_source . cell ( ) ) ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								                    m_websocket - > send ( data_buffer ,  false ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								                    return  { } ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								                } , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								                [ this ] ( JS : : Handle < FileAPI : : Blob >  const &  blob )  - >  ErrorOr < void >  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								                    auto  byte_buffer  =  TRY ( ByteBuffer : : copy ( blob - > bytes ( ) ) ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								                    m_websocket - > send ( byte_buffer ,  false ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								                    return  { } ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								                } ) ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								        // TODO : If the data cannot be sent, e.g. because it would need to be buffered but the buffer is full, the user agent must flag the WebSocket as full and then close the WebSocket connection.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								        // TODO : Any invocation of this method with a string argument that does not throw an exception must increase the bufferedAmount attribute by the number of bytes needed to express the argument as UTF-8.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    return  { } ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-02-18 17:35:45 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								// https://websockets.spec.whatwg.org/#feedback-from-the-protocol
  
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								void  WebSocket : : on_open ( )  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								{  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    // 1. Change the readyState attribute's value to OPEN (1).
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    // 2. Change the extensions attribute's value to the extensions in use, if it is not the null value. [WSP]
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    // 3. Change the protocol attribute's value to the subprotocol in use, if it is not the null value. [WSP]
 
							 
						 
					
						
							
								
									
										
										
										
											2023-04-06 16:12:33 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    dispatch_event ( DOM : : Event : : create ( realm ( ) ,  HTML : : EventNames : : open ) . release_value_but_fixme_should_propagate_errors ( ) ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-02-18 17:35:45 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								// https://websockets.spec.whatwg.org/#feedback-from-the-protocol
  
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								void  WebSocket : : on_error ( )  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								{  
						 
					
						
							
								
									
										
										
										
											2023-04-06 16:12:33 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    dispatch_event ( DOM : : Event : : create ( realm ( ) ,  HTML : : EventNames : : error ) . release_value_but_fixme_should_propagate_errors ( ) ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-02-18 17:35:45 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								// https://websockets.spec.whatwg.org/#feedback-from-the-protocol
  
						 
					
						
							
								
									
										
										
										
											2023-06-17 09:18:55 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								void  WebSocket : : on_close ( u16  code ,  String  reason ,  bool  was_clean )  
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								{  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    // 1. Change the readyState attribute's value to CLOSED. This is handled by the Protocol's WebSocket
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    // 2. If [needed], fire an event named error at the WebSocket object. This is handled by the Protocol's WebSocket
 
							 
						 
					
						
							
								
									
										
										
										
											2022-02-18 17:35:45 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    HTML : : CloseEventInit  event_init  { } ; 
							 
						 
					
						
							
								
									
										
										
										
											2021-10-01 18:16:12 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    event_init . was_clean  =  was_clean ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    event_init . code  =  code ; 
							 
						 
					
						
							
								
									
										
										
										
											2023-06-17 09:18:55 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    event_init . reason  =  reason ; 
							 
						 
					
						
							
								
									
										
										
										
											2023-04-06 07:25:18 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    dispatch_event ( HTML : : CloseEvent : : create ( realm ( ) ,  HTML : : EventNames : : close ,  event_init ) . release_value_but_fixme_should_propagate_errors ( ) ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-02-18 17:35:45 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								// https://websockets.spec.whatwg.org/#feedback-from-the-protocol
  
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								void  WebSocket : : on_message ( ByteBuffer  message ,  bool  is_text )  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								{  
						 
					
						
							
								
									
										
										
										
											2022-04-30 11:26:21 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    if  ( m_websocket - > ready_state ( )  ! =  WebSocket : : ReadyState : : Open ) 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								        return ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    if  ( is_text )  { 
							 
						 
					
						
							
								
									
										
										
										
											2022-12-04 18:02:33 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								        auto  text_message  =  DeprecatedString ( ReadonlyBytes ( message ) ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-02-18 17:35:45 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								        HTML : : MessageEventInit  event_init ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-12-06 22:17:27 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								        event_init . data  =  JS : : PrimitiveString : : create ( vm ( ) ,  text_message ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2023-06-17 09:18:55 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								        event_init . origin  =  url ( ) . release_value_but_fixme_should_propagate_errors ( ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2023-04-06 07:25:18 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								        dispatch_event ( HTML : : MessageEvent : : create ( realm ( ) ,  HTML : : EventNames : : message ,  event_init ) . release_value_but_fixme_should_propagate_errors ( ) ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								        return ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    } 
							 
						 
					
						
							
								
									
										
										
										
											2022-02-11 03:43:03 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    if  ( m_binary_type  = =  " blob " )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								        // type indicates that the data is Binary and binaryType is "blob"
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								        TODO ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    }  else  if  ( m_binary_type  = =  " arraybuffer " )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								        // type indicates that the data is Binary and binaryType is "arraybuffer"
 
							 
						 
					
						
							
								
									
										
										
										
											2022-02-18 17:35:45 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								        HTML : : MessageEventInit  event_init ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-08-28 13:42:07 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								        event_init . data  =  JS : : ArrayBuffer : : create ( realm ( ) ,  message ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2023-06-17 09:18:55 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								        event_init . origin  =  url ( ) . release_value_but_fixme_should_propagate_errors ( ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2023-04-06 07:25:18 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								        dispatch_event ( HTML : : MessageEvent : : create ( realm ( ) ,  HTML : : EventNames : : message ,  event_init ) . release_value_but_fixme_should_propagate_errors ( ) ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-02-11 03:43:03 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								        return ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    dbgln ( " Unsupported WebSocket message type {} " ,  m_binary_type ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    TODO ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								# undef __ENUMERATE 
  
						 
					
						
							
								
									
										
										
										
											2022-09-24 16:02:41 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								# define __ENUMERATE(attribute_name, event_name)                       \ 
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    void  WebSocket : : set_ # # attribute_name ( WebIDL : : CallbackType *  value )  \
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    {                                                                  \
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								        set_event_handler_attribute ( event_name ,  value ) ;                \
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    }                                                                  \
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    WebIDL : : CallbackType *  WebSocket : : attribute_name ( )                  \
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    {                                                                  \
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								        return  event_handler_attribute ( event_name ) ;                    \
							 
						 
					
						
							
								
									
										
										
										
											2021-04-24 13:54:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								ENUMERATE_WEBSOCKET_EVENT_HANDLERS ( __ENUMERATE )  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								# undef __ENUMERATE 
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								}