@@ -64,6 +64,12 @@ static const char *TAG = "websocket_client";
64
64
#define WS_OVER_TCP_SCHEME "ws"
65
65
#define WS_OVER_TLS_SCHEME "wss"
66
66
#define WS_HTTP_BASIC_AUTH "Basic "
67
+ #define WS_HTTP_REDIRECT (code ) ((code >= 300) && (code < 400))
68
+
69
+ #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL (5 , 5 , 0 )
70
+ // Features supported in 5.5.0
71
+ #define WS_TRANSPORT_REDIRECT_HEADER_SUPPORT 1
72
+ #endif
67
73
68
74
const static int STOPPED_BIT = BIT0 ;
69
75
const static int CLOSE_FRAME_SENT_BIT = BIT1 ; // Indicates that a close frame was sent by the client
@@ -598,6 +604,23 @@ static esp_err_t esp_websocket_client_create_transport(esp_websocket_client_hand
598
604
return ESP_OK ;
599
605
}
600
606
607
+ static void esp_websocket_client_prepare_transport (esp_websocket_client_handle_t client )
608
+ {
609
+ //get transport by scheme
610
+ if (client -> transport == NULL && client -> config -> ext_transport == NULL ) {
611
+ client -> transport = esp_transport_list_get_transport (client -> transport_list , client -> config -> scheme );
612
+ }
613
+
614
+ if (client -> transport == NULL ) {
615
+ ESP_LOGE (TAG , "There are no transports valid, stop websocket client" );
616
+ client -> run = false;
617
+ }
618
+ //default port
619
+ if (client -> config -> port == 0 ) {
620
+ client -> config -> port = esp_transport_get_default_port (client -> transport );
621
+ }
622
+ }
623
+
601
624
static int esp_websocket_client_send_with_exact_opcode (esp_websocket_client_handle_t client , ws_transport_opcodes_t opcode , const uint8_t * data , int len , TickType_t timeout )
602
625
{
603
626
int ret = -1 ;
@@ -1022,19 +1045,7 @@ static void esp_websocket_client_task(void *pv)
1022
1045
esp_websocket_client_handle_t client = (esp_websocket_client_handle_t ) pv ;
1023
1046
client -> run = true;
1024
1047
1025
- //get transport by scheme
1026
- if (client -> transport == NULL && client -> config -> ext_transport == NULL ) {
1027
- client -> transport = esp_transport_list_get_transport (client -> transport_list , client -> config -> scheme );
1028
- }
1029
-
1030
- if (client -> transport == NULL ) {
1031
- ESP_LOGE (TAG , "There are no transports valid, stop websocket client" );
1032
- client -> run = false;
1033
- }
1034
- //default port
1035
- if (client -> config -> port == 0 ) {
1036
- client -> config -> port = esp_transport_get_default_port (client -> transport );
1037
- }
1048
+ esp_websocket_client_prepare_transport (client );
1038
1049
1039
1050
client -> state = WEBSOCKET_STATE_INIT ;
1040
1051
xEventGroupClearBits (client -> status_bits , STOPPED_BIT | CLOSE_FRAME_SENT_BIT );
@@ -1072,6 +1083,26 @@ static void esp_websocket_client_task(void *pv)
1072
1083
esp_websocket_client_abort_connection (client , WEBSOCKET_ERROR_TYPE_TCP_TRANSPORT );
1073
1084
break ;
1074
1085
}
1086
+ #if WS_TRANSPORT_REDIRECT_HEADER_SUPPORT
1087
+ else if (WS_HTTP_REDIRECT (result )) {
1088
+ const char * redir = esp_transport_ws_get_redir_uri (client -> transport );
1089
+ if (redir ) {
1090
+ // Redirecting to a new URI
1091
+ free (client -> config -> uri );
1092
+
1093
+ client -> config -> uri = strdup (redir );
1094
+ client -> config -> port = 0 ;
1095
+
1096
+ esp_websocket_client_set_uri (client , client -> config -> uri );
1097
+ esp_websocket_client_prepare_transport (client );
1098
+
1099
+ // Rerun the connection with the redir uri.
1100
+ client -> state = WEBSOCKET_STATE_INIT ;
1101
+ ESP_LOGI (TAG , "Redirecting to %s" , client -> config -> uri );
1102
+ break ;
1103
+ }
1104
+ }
1105
+ #endif
1075
1106
ESP_LOGD (TAG , "Transport connected to %s://%s:%d" , client -> config -> scheme , client -> config -> host , client -> config -> port );
1076
1107
1077
1108
client -> state = WEBSOCKET_STATE_CONNECTED ;
0 commit comments