@@ -157,10 +157,7 @@ int aws_websocket_client_connect(const struct aws_websocket_client_connection_op
157157 }
158158
159159 /* Extensions are not currently supported */
160- struct aws_string * sec_websocket_extensions =
161- aws_http_headers_get_comma_separated (request_headers , aws_byte_cursor_from_c_str ("Sec-WebSocket-Extensions" ));
162- if (sec_websocket_extensions != NULL ) {
163- aws_string_destroy (sec_websocket_extensions );
160+ if (aws_http_headers_has (request_headers , aws_byte_cursor_from_c_str ("Sec-WebSocket-Extensions" ))) {
164161 AWS_LOGF_ERROR (
165162 AWS_LS_HTTP_WEBSOCKET_SETUP , "id=static: 'Sec-WebSocket-Extensions' are not currently supported" );
166163 return aws_raise_error (AWS_ERROR_INVALID_ARGUMENT );
@@ -189,8 +186,8 @@ int aws_websocket_client_connect(const struct aws_websocket_client_connection_op
189186 goto error ;
190187 }
191188
192- ws_bootstrap -> expected_sec_websocket_protocol = aws_http_headers_get_comma_separated (
193- options -> handshake_request , aws_byte_cursor_from_c_str ("Sec-WebSocket-Protocol" ));
189+ ws_bootstrap -> expected_sec_websocket_protocol =
190+ aws_http_headers_get_all ( request_headers , aws_byte_cursor_from_c_str ("Sec-WebSocket-Protocol" ));
194191
195192 /* Initiate HTTP connection */
196193 struct aws_http_client_connection_options http_options = AWS_HTTP_CLIENT_CONNECTION_OPTIONS_INIT ;
@@ -601,7 +598,6 @@ static int s_ws_bootstrap_validate_header(
601598}
602599
603600static int s_ws_bootstrap_validate_sec_websocket_protocol (const struct aws_websocket_client_bootstrap * ws_bootstrap ) {
604-
605601 /* First handle the easy case:
606602 * If client requested no protocols, then the response should not pick any */
607603 if (ws_bootstrap -> expected_sec_websocket_protocol == NULL ) {
@@ -618,9 +614,9 @@ static int s_ws_bootstrap_validate_sec_websocket_protocol(const struct aws_webso
618614 }
619615 }
620616
621- /* Check that server has picked one of the requested protocols */
617+ /* Check that server has picked one of the protocols listed in the request */
622618 struct aws_byte_cursor response_protocol ;
623- if (aws_http_headers_get_single (
619+ if (aws_http_headers_get (
624620 ws_bootstrap -> response_headers , aws_byte_cursor_from_c_str ("Sec-WebSocket-Protocol" ), & response_protocol )) {
625621 AWS_LOGF_ERROR (
626622 AWS_LS_HTTP_WEBSOCKET_SETUP ,
@@ -629,7 +625,32 @@ static int s_ws_bootstrap_validate_sec_websocket_protocol(const struct aws_webso
629625 return aws_raise_error (AWS_ERROR_HTTP_WEBSOCKET_UPGRADE_FAILURE );
630626 }
631627
632- YOU ARE HERE
628+ struct aws_byte_cursor expected_sec_websocket_protocol =
629+ aws_byte_cursor_from_string (ws_bootstrap -> expected_sec_websocket_protocol );
630+ struct aws_byte_cursor request_protocol_i ;
631+ AWS_ZERO_STRUCT (request_protocol_i );
632+ while (aws_byte_cursor_next_split (& expected_sec_websocket_protocol , ',' , & request_protocol_i )) {
633+ struct aws_byte_cursor request_protocol = aws_strutil_trim_http_whitespace (request_protocol_i );
634+ if (aws_byte_cursor_eq (& response_protocol , & request_protocol )) {
635+ /* Success! */
636+ AWS_LOGF_DEBUG (
637+ AWS_LS_HTTP_WEBSOCKET_SETUP ,
638+ "id=%p: Server selected '" PRInSTR ": " PRInSTR "'" ,
639+ (void * )ws_bootstrap ,
640+ AWS_BYTE_CURSOR_PRI (response_protocol ),
641+ AWS_BYTE_CURSOR_PRI (expected_sec_websocket_protocol ));
642+ return AWS_OP_SUCCESS ;
643+ }
644+ }
645+
646+ AWS_LOGF_ERROR (
647+ AWS_LS_HTTP_WEBSOCKET_SETUP ,
648+ "id=%p: Response 'Sec-WebSocket-Protocol' header has wrong value. Received '" PRInSTR
649+ "'. Expected one of '" PRInSTR "'" ,
650+ (void * )ws_bootstrap ,
651+ AWS_BYTE_CURSOR_PRI (response_protocol ),
652+ AWS_BYTE_CURSOR_PRI (expected_sec_websocket_protocol ));
653+ return aws_raise_error (AWS_ERROR_HTTP_WEBSOCKET_UPGRADE_FAILURE );
633654}
634655
635656/* OK, we've got all the headers for the 101 Switching Protocols response.
@@ -678,10 +699,7 @@ static int s_ws_bootstrap_validate_response_and_install_websocket_handler(
678699 }
679700
680701 /* (step 5 is about validating Sec-WebSocket-Extensions, but we don't support extensions) */
681- struct aws_string * sec_websocket_extensions = aws_http_headers_get_comma_separated (
682- ws_bootstrap -> response_headers , aws_byte_cursor_from_c_str ("Sec-WebSocket-Extensions" ));
683- if (sec_websocket_extensions != NULL ) {
684- aws_string_destroy (sec_websocket_extensions );
702+ if (aws_http_headers_has (ws_bootstrap -> response_headers , aws_byte_cursor_from_c_str ("Sec-WebSocket-Extensions" ))) {
685703 AWS_LOGF_ERROR (
686704 AWS_LS_HTTP_WEBSOCKET_SETUP ,
687705 "id=%p: Response has 'Sec-WebSocket-Extensions' header, but client does not support extensions." ,
@@ -695,45 +713,6 @@ static int s_ws_bootstrap_validate_response_and_install_websocket_handler(
695713 * not present in the client's handshake (the server has indicated a
696714 * subprotocol not requested by the client), the client MUST _Fail
697715 * the WebSocket Connection_. */
698- if (ws_bootstrap -> sec_websocket_protocol_values ) {
699- struct aws_byte_cursor response_protocol ;
700- if (aws_http_headers_get_single (
701- ws_bootstrap -> response_headers ,
702- aws_byte_cursor_from_c_str ("Sec-WebSocket-Protocol" ),
703- & response_protocol )) {
704-
705- AWS_LOGF_ERROR (
706- AWS_LS_HTTP_WEBSOCKET_SETUP ,
707- "id=%p: Response lacks required'Sec-WebSocket-Protocol' header" ,
708- (void * )ws_bootstrap );
709- aws_raise_error (AWS_ERROR_HTTP_WEBSOCKET_UPGRADE_FAILURE );
710- goto error ;
711- }
712-
713- } else {
714- if (aws_http_headers_has (ws_bootstrap -> response_headers , aws_byte_cursor_from_c_str ("Sec-WebSocket-Protocol" )) {
715- AWS_LOGF_ERROR (
716- AWS_LS_HTTP_WEBSOCKET_SETUP ,
717- "id=%p: Response has 'Sec-WebSocket-Protocol' header, no protocol was requested" ,
718- (void * )ws_bootstrap );
719- aws_raise_error (AWS_ERROR_HTTP_WEBSOCKET_UPGRADE_FAILURE );
720- goto error ;
721- }
722- }
723- struct aws_byte_cursor response_protocol ;
724- if (aws_http_headers_get_single (
725- ws_bootstrap -> response_headers , aws_byte_cursor_from_c_str ("Sec-WebSocket-Protocol" ), & response_protocol ) ==
726- AWS_OP_SUCCESS ) {
727-
728- if (ws_bootstrap -> sec_websocket_protocol_values == NULL ) {
729- AWS_LOGF_ERROR (
730- AWS_LS_HTTP_WEBSOCKET_SETUP ,
731- "id=%p: Response has 'Sec-WebSocket-Protocol' header, no protocol was requested" ,
732- (void * )ws_bootstrap );
733- aws_raise_error (AWS_ERROR_HTTP_WEBSOCKET_UPGRADE_FAILURE );
734- goto error ;
735- }
736- }
737716 if (s_ws_bootstrap_validate_sec_websocket_protocol (ws_bootstrap )) {
738717 goto error ;
739718 }
0 commit comments